10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
14 type random_decode_test struct {
19 var random_decode_tests = []random_decode_test{
21 {"i-9223372036854775808e", int64(-9223372036854775808)},
23 {"29:unicode test проверка", "unicode test проверка"},
24 {"d1:ai5e1:b5:helloe", map[string]interface{}{"a": int64(5), "b": "hello"}},
25 {"li5ei10ei15ei20e7:bencodee",
26 []interface{}{int64(5), int64(10), int64(15), int64(20), "bencode"}},
27 {"ldedee", []interface{}{map[string]interface{}{}, map[string]interface{}{}}},
28 {"le", []interface{}{}},
29 {"i604919719469385652980544193299329427705624352086e", func() *big.Int {
30 ret, _ := big.NewInt(-1).SetString("604919719469385652980544193299329427705624352086", 10)
33 {"d1:rd6:\xd4/\xe2F\x00\x01e1:t3:\x9a\x87\x011:v4:TR%=1:y1:re", map[string]interface{}{
34 "r": map[string]interface{}{},
41 func TestRandomDecode(t *testing.T) {
42 for _, test := range random_decode_tests {
44 err := Unmarshal([]byte(test.data), &value)
46 t.Error(err, test.data)
49 assert.EqualValues(t, test.expected, value)
53 func TestLoneE(t *testing.T) {
55 err := Unmarshal([]byte("e"), &v)
56 se := err.(*SyntaxError)
57 require.EqualValues(t, 0, se.Offset)
60 func TestDecoderConsecutive(t *testing.T) {
61 d := NewDecoder(bytes.NewReader([]byte("i1ei2e")))
64 require.NoError(t, err)
65 require.EqualValues(t, 1, i)
67 require.NoError(t, err)
68 require.EqualValues(t, 2, i)
70 require.Equal(t, io.EOF, err)
73 func TestDecoderConsecutiveDicts(t *testing.T) {
74 bb := bytes.NewBufferString("d4:herp4:derped3:wat1:ke17:oh baby a triple!")
76 var m map[string]interface{}
77 require.NoError(t, d.Decode(&m))
79 assert.Equal(t, "derp", m["herp"])
80 require.NoError(t, d.Decode(&m))
81 assert.Equal(t, "k", m["wat"])
83 require.NoError(t, d.Decode(&s))
84 assert.Equal(t, "oh baby a triple!", s)
87 func check_error(t *testing.T, err error) {
93 func assert_equal(t *testing.T, x, y interface{}) {
94 if !reflect.DeepEqual(x, y) {
95 t.Errorf("got: %v (%T), expected: %v (%T)\n", x, x, y, y)
99 type unmarshaler_int struct {
103 func (this *unmarshaler_int) UnmarshalBencode(data []byte) error {
104 return Unmarshal(data, &this.x)
107 type unmarshaler_string struct {
111 func (this *unmarshaler_string) UnmarshalBencode(data []byte) error {
112 this.x = string(data)
116 func TestUnmarshalerBencode(t *testing.T) {
117 var i unmarshaler_int
118 var ss []unmarshaler_string
119 check_error(t, Unmarshal([]byte("i71e"), &i))
120 assert_equal(t, i.x, 71)
121 check_error(t, Unmarshal([]byte("l5:hello5:fruit3:waye"), &ss))
122 assert_equal(t, ss[0].x, "5:hello")
123 assert_equal(t, ss[1].x, "5:fruit")
124 assert_equal(t, ss[2].x, "3:way")