]> Sergey Matveev's repositories - btrtrc.git/blob - bencode/decode_test.go
bencode: Remove trailing bytes checks, this broke the peer protocol and should be...
[btrtrc.git] / bencode / decode_test.go
1 package bencode
2
3 import (
4         "bytes"
5         "io"
6         "reflect"
7         "testing"
8
9         "github.com/stretchr/testify/require"
10 )
11
12 type random_decode_test struct {
13         data     string
14         expected interface{}
15 }
16
17 var random_decode_tests = []random_decode_test{
18         {"i57e", int64(57)},
19         {"i-9223372036854775808e", int64(-9223372036854775808)},
20         {"5:hello", "hello"},
21         {"29:unicode test проверка", "unicode test проверка"},
22         {"d1:ai5e1:b5:helloe", map[string]interface{}{"a": int64(5), "b": "hello"}},
23         {"li5ei10ei15ei20e7:bencodee",
24                 []interface{}{int64(5), int64(10), int64(15), int64(20), "bencode"}},
25         {"ldedee", []interface{}{map[string]interface{}{}, map[string]interface{}{}}},
26         {"le", []interface{}{}},
27 }
28
29 func TestRandomDecode(t *testing.T) {
30         for _, test := range random_decode_tests {
31                 var value interface{}
32                 err := Unmarshal([]byte(test.data), &value)
33                 if err != nil {
34                         t.Error(err, test.data)
35                         continue
36                 }
37                 if !reflect.DeepEqual(test.expected, value) {
38                         t.Errorf("got: %v (%T), expected: %v (%T)\n",
39                                 value, value, test.expected, test.expected)
40                 }
41         }
42 }
43
44 func TestLoneE(t *testing.T) {
45         var v int
46         err := Unmarshal([]byte("e"), &v)
47         se := err.(*SyntaxError)
48         require.EqualValues(t, 0, se.Offset)
49 }
50
51 func TestDecoderConsecutive(t *testing.T) {
52         d := NewDecoder(bytes.NewReader([]byte("i1ei2e")))
53         var i int
54         err := d.Decode(&i)
55         require.NoError(t, err)
56         require.EqualValues(t, 1, i)
57         err = d.Decode(&i)
58         require.NoError(t, err)
59         require.EqualValues(t, 2, i)
60         err = d.Decode(&i)
61         require.Equal(t, io.EOF, err)
62 }
63
64 func check_error(t *testing.T, err error) {
65         if err != nil {
66                 t.Error(err)
67         }
68 }
69
70 func assert_equal(t *testing.T, x, y interface{}) {
71         if !reflect.DeepEqual(x, y) {
72                 t.Errorf("got: %v (%T), expected: %v (%T)\n", x, x, y, y)
73         }
74 }
75
76 type unmarshaler_int struct {
77         x int
78 }
79
80 func (this *unmarshaler_int) UnmarshalBencode(data []byte) error {
81         return Unmarshal(data, &this.x)
82 }
83
84 type unmarshaler_string struct {
85         x string
86 }
87
88 func (this *unmarshaler_string) UnmarshalBencode(data []byte) error {
89         this.x = string(data)
90         return nil
91 }
92
93 func TestUnmarshalerBencode(t *testing.T) {
94         var i unmarshaler_int
95         var ss []unmarshaler_string
96         check_error(t, Unmarshal([]byte("i71e"), &i))
97         assert_equal(t, i.x, 71)
98         check_error(t, Unmarshal([]byte("l5:hello5:fruit3:waye"), &ss))
99         assert_equal(t, ss[0].x, "5:hello")
100         assert_equal(t, ss[1].x, "5:fruit")
101         assert_equal(t, ss[2].x, "3:way")
102
103 }