9 "github.com/stretchr/testify/assert"
12 type random_encode_test struct {
17 type random_struct struct {
18 ABC int `bencode:"abc"`
19 SkipThisOne string `bencode:"-"`
27 func (d *dummy) MarshalBencode() ([]byte, error) {
29 _, err := fmt.Fprintf(&b, "i%dei%dei%de", d.a+1, d.b+1, d.c+1)
36 var random_encode_tests = []random_encode_test{
39 {"hello, world", "12:hello, world"},
43 {int16(-16), "i-16e"},
45 {int64(-64), "i-64e"},
50 {random_struct{123, "nono", "hello"}, "d3:CDE5:hello3:abci123ee"},
51 {map[string]string{"a": "b", "c": "d"}, "d1:a1:b1:c1:de"},
52 {[]byte{1, 2, 3, 4}, "4:\x01\x02\x03\x04"},
53 {&[4]byte{1, 2, 3, 4}, "4:\x01\x02\x03\x04"},
59 {map[string]int{}, "de"},
60 {&dummy{1, 2, 3}, "i2ei3ei4e"},
66 }{new(string)}, "d1:A0:e"},
68 A *string `bencode:",omitempty"`
71 A *string `bencode:",omitempty"`
72 }{new(string)}, "d1:A0:e"},
73 {bigIntFromString("62208002200000000000"), "i62208002200000000000e"},
74 {*bigIntFromString("62208002200000000000"), "i62208002200000000000e"},
77 func bigIntFromString(s string) *big.Int {
78 bi, ok := new(big.Int).SetString(s, 10)
85 func TestRandomEncode(t *testing.T) {
86 for _, test := range random_encode_tests {
87 data, err := Marshal(test.value)
88 assert.NoError(t, err, "%s", test)
89 assert.EqualValues(t, test.expected, string(data))