-
Notifications
You must be signed in to change notification settings - Fork 5
/
encoder_test.go
98 lines (85 loc) · 3.07 KB
/
encoder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package bencode
import (
"runtime"
"testing"
"github.com/stretchr/testify/assert"
)
var marshalTestData = map[string]interface{}{
"announce": []byte("udp://tracker.publicbt.com:80/announce"),
"announce-list": []interface{}{
[]interface{}{[]byte("udp://tracker.publicbt.com:80/announce")},
[]interface{}{[]byte("udp://tracker.openbittorrent.com:80/announce")},
},
"comment": []byte("Debian CD from cdimage.debian.org"),
"info": map[string]interface{}{
"name": []byte("debian-8.8.0-arm64-netinst.iso"),
"length": 170917888,
"piece length": 262144,
},
}
func TestMarshal(t *testing.T) {
assert := assert.New(t)
result, err := Marshal(int64(38))
if !assert.NoError(err) || !assert.Equal([]byte("i38e"), result) {
return
}
result, err = Marshal([]byte("udp://tracker.publicbt.com:80/announce"))
if !assert.NoError(err) || !assert.Equal([]byte("38:udp://tracker.publicbt.com:80/announce"), result) {
return
}
result, err = Marshal([]interface{}{
[]interface{}{[]byte("udp://tracker.publicbt.com:80/announce")},
[]interface{}{[]byte("udp://tracker.openbittorrent.com:80/announce")},
})
if !assert.NoError(err) || !assert.Equal([]byte("ll38:udp://tracker.publicbt.com:80/announceel44:udp://tracker.openbittorrent.com:80/announceee"), result) {
return
}
result, err = Marshal(map[string]interface{}{
"announce": []byte("udp://tracker.publicbt.com:80/announce"),
"announce-list": []interface{}{
[]interface{}{[]byte("udp://tracker.publicbt.com:80/announce")},
[]interface{}{[]byte("udp://tracker.openbittorrent.com:80/announce")},
},
})
if !assert.NoError(err) || !assert.Equal([]byte("d8:announce38:udp://tracker.publicbt.com:80/announce13:announce-listll38:udp://tracker.publicbt.com:80/announceel44:udp://tracker.openbittorrent.com:80/announceeee"), result) {
return
}
result, err = Marshal(marshalTestData)
if !assert.NoError(err) || !assert.Equal(unmarshalTestData, result) {
return
}
result, err = MarshalTo(make([]byte, 1), marshalTestData)
if !assert.NoError(err) || !assert.Equal(unmarshalTestData, result) {
return
}
result, err = Marshal(nil)
if !assert.Error(err) || !assert.Nil(result) || !assert.Equal("bencode: unsupported type: <nil>", err.Error()) {
return
}
}
func TestMarshalUnOrderedDict(t *testing.T) {
assert := assert.New(t)
unorderedEncodedData := []byte(`d7:ttg_tag32:8032a74ec22927a5bd6367537eafd87e7:privatei1e6:source20:[ttys3.bencode.test]e`)
expectedReencodedData := []byte(`d7:privatei1e6:source20:[ttys3.bencode.test]7:ttg_tag32:8032a74ec22927a5bd6367537eafd87ee`)
data, err := Unmarshal(unorderedEncodedData)
assert.NoError(err)
//t.Logf("decoded data: %#v", data)
reencoded, err := Marshal(data)
assert.NoError(err)
assert.Equal(expectedReencodedData, reencoded)
}
func BenchmarkMarshal(b *testing.B) {
b.ReportAllocs()
var result []byte
b.Run("Marshal", func(b *testing.B) {
for n := 0; n < b.N; n++ {
result, _ = Marshal(marshalTestData)
}
})
b.Run("MarshalTo", func(b *testing.B) {
for n := 0; n < b.N; n++ {
result, _ = MarshalTo(result, marshalTestData)
}
})
runtime.KeepAlive(result)
}