-
Notifications
You must be signed in to change notification settings - Fork 25
/
encode_test.go
354 lines (310 loc) · 8.26 KB
/
encode_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package bencode
import (
"errors"
"fmt"
"testing"
"time"
)
func TestEncode(t *testing.T) {
type encodeTestCase struct {
in interface{}
out string
err bool
}
type eT struct {
A string
X string `bencode:"D"`
Y string `bencode:"B"`
Z string `bencode:"C"`
}
type sortProblem struct {
A string
B string `bencode:","`
}
type issue18Sub struct {
Name string
}
type issue18 struct {
T *issue18Sub
}
type Embedded struct {
B string
}
type issue22 struct {
Time myTimeType `bencode:"t"`
Foo myBoolType `bencode:"f"`
}
type issue22WithErrorChild struct {
Name string `bencode:"n"`
Error errorMarshalType `bencode:"e"`
}
type issue26 struct {
Answer int64 `bencode:"a"`
Foo myBoolTextType `bencode:"f"`
Name string `bencode:"n"`
}
type issue26WithErrorChild struct {
Name string `bencode:"n"`
Error errorTextMarshalType `bencode:"e"`
}
type issue28 struct {
X string `bencode:"x"`
Time *myTimeType `bencode:"t"`
Foo myBoolPtrType `bencode:"f"`
Y string `bencode:"y"`
}
now := time.Now()
var encodeCases = []encodeTestCase{
// integers
{10, `i10e`, false},
{-10, `i-10e`, false},
{0, `i0e`, false},
{int(10), `i10e`, false},
{int8(10), `i10e`, false},
{int16(10), `i10e`, false},
{int32(10), `i10e`, false},
{int64(10), `i10e`, false},
{uint(10), `i10e`, false},
{uint8(10), `i10e`, false},
{uint16(10), `i10e`, false},
{uint32(10), `i10e`, false},
{uint64(10), `i10e`, false},
{(*int)(nil), ``, false},
// ptr-to-integer
{func() *int {
i := 42
return &i
}(), `i42e`, false},
// strings
{"foo", `3:foo`, false},
{"barbb", `5:barbb`, false},
{"", `0:`, false},
{(*string)(nil), ``, false},
// ptr-to-string
{func() *string {
str := "foo"
return &str
}(), `3:foo`, false},
// lists
{[]interface{}{"foo", 20}, `l3:fooi20ee`, false},
{[]interface{}{90, 20}, `li90ei20ee`, false},
{[]interface{}{[]interface{}{"foo", "bar"}, 20}, `ll3:foo3:barei20ee`, false},
{[]map[string]int{
{"a": 0, "b": 1},
{"c": 2, "d": 3},
}, `ld1:ai0e1:bi1eed1:ci2e1:di3eee`, false},
{[][]byte{
[]byte{'0', '2', '4', '6', '8'},
[]byte{'a', 'c', 'e'},
}, `l5:024683:acee`, false},
{(*[]interface{})(nil), ``, false},
// boolean
{true, "i1e", false},
{false, "i0e", false},
{(*bool)(nil), ``, false},
// dicts
{map[string]interface{}{
"a": "foo",
"c": "bar",
"b": "tes",
}, `d1:a3:foo1:b3:tes1:c3:bare`, false},
{eT{"foo", "bar", "far", "boo"}, `d1:A3:foo1:B3:far1:C3:boo1:D3:bare`, false},
{map[string][]int{
"a": {0, 1},
"b": {2, 3},
}, `d1:ali0ei1ee1:bli2ei3eee`, false},
{struct{ A, b int }{1, 2}, "d1:Ai1ee", false},
{(*struct{ A int })(nil), ``, false},
// raw
{RawMessage(`i5e`), `i5e`, false},
{[]RawMessage{
RawMessage(`i5e`),
RawMessage(`5:hello`),
RawMessage(`ldededee`),
}, `li5e5:helloldededeee`, false},
{map[string]RawMessage{
"a": RawMessage(`i5e`),
"b": RawMessage(`5:hello`),
"c": RawMessage(`ldededee`),
}, `d1:ai5e1:b5:hello1:cldededeee`, false},
// problem sorting
{sortProblem{A: "foo", B: "bar"}, `d1:A3:foo1:B3:bare`, false},
// nil values dropped from maps and structs
{map[string]*int{"a": nil}, `de`, false},
{struct{ A *int }{nil}, `de`, false},
{issue18{}, `de`, false},
{map[string]interface{}{"a": nil}, `de`, false},
{struct{ A interface{} }{nil}, `de`, false},
// embedded structs
{struct {
A string
Embedded
}{"foo", Embedded{"bar"}}, `d1:A3:foo1:B3:bare`, false},
{struct {
A string
Embedded `bencode:"C"`
}{"foo", Embedded{"bar"}}, `d1:A3:foo1:Cd1:B3:baree`, false},
// embedded structs order issue #20
{struct {
Embedded
A string
}{Embedded{"bar"}, "foo"}, `d1:A3:foo1:B3:bare`, false},
// types which implement the Marshal interface will
// be marshalled using this interface
{myBoolType(true), `1:y`, false},
{myBoolType(false), `1:n`, false},
{myTimeType{now}, fmt.Sprintf("i%de", now.Unix()), false},
{errorMarshalType{}, "", true},
// pointers to types which implement the Marshal interface will
// be marshalled using this interface
{func() *myBoolType {
b := myBoolType(true)
return &b
}(), `1:y`, false},
{func() *myTimeType {
t := myTimeType{now}
return &t
}(), fmt.Sprintf("i%de", now.Unix()), false},
{func() *errorMarshalType {
e := errorMarshalType{}
return &e
}(), "", true},
// nil-pointers to types which implement the Marshal interface will be ignored
{(*myBoolType)(nil), "", false},
{(*myTimeType)(nil), "", false},
{(*errorMarshalType)(nil), "", false},
// ptr-types which implements the Marshal interface will
// be marshalled using this interface
{func() *myBoolPtrType {
b := myBoolPtrType(true)
return &b
}(), `1:y`, false},
{func() *myBoolPtrType {
b := myBoolPtrType(false)
return &b
}(), `1:n`, false},
{(*myBoolPtrType)(nil), ``, false},
// structures can also have children which support
// the Marshal interface
{
issue22{Time: myTimeType{now}, Foo: myBoolType(true)},
fmt.Sprintf("d1:f1:y1:ti%dee", now.Unix()),
false,
},
{ // an error will be returned if a child can't be marshalled
issue22WithErrorChild{Name: "Foo", Error: errorMarshalType{}},
"", true,
},
// structures passed by reference which have children that support
// the (Text)Marshal interface (by value or by reference),
// will be marshaled using that interface
{
&issue22{Time: myTimeType{now}, Foo: myBoolType(true)},
fmt.Sprintf("d1:f1:y1:ti%dee", now.Unix()),
false,
},
{ // an error will be returned if a child can't be marshalled
&issue22WithErrorChild{Name: "Foo", Error: errorMarshalType{}},
"", true,
},
// types which implement the TextMarshal interface will
// be marshalled into a bencode string value using this interface
{myBoolTextType(true), `1:y`, false},
{myBoolTextType(false), `1:n`, false},
{errorTextMarshalType{}, "", true},
// structures can also have children which support
// the TextMarshal interface
{
issue26{Answer: 42, Foo: myBoolTextType(true), Name: "Nova"},
`d1:ai42e1:f1:y1:n4:Novae`,
false,
},
{ // an error will be returned if a child TextMarshaler returns an error
issue26WithErrorChild{Name: "Foo", Error: errorTextMarshalType{}},
"", true,
},
// ptr types which are used as value types,
// but which ptr version implement the Marshaler/TextMarshaler interface,
// will still get marshalling using this interface, when possible
{
&issue28{X: "x", Time: &myTimeType{now}, Foo: myBoolPtrType(true), Y: "y"},
fmt.Sprintf(`d1:f1:y1:ti%de1:x1:x1:y1:ye`, now.Unix()),
false,
},
}
for i, tt := range encodeCases {
t.Logf("%d: %#v", i, tt.in)
data, err := EncodeString(tt.in)
if !tt.err && err != nil {
t.Errorf("#%d: Unexpected err: %v", i, err)
continue
}
if tt.err && err == nil {
t.Errorf("#%d: Expected err is nil", i)
continue
}
if tt.out != data {
t.Errorf("#%d: Val: %q != %q", i, data, tt.out)
}
}
}
type myBoolPtrType bool
// MarshalBencode implements Marshaler.MarshalBencode
func (mbt *myBoolPtrType) MarshalBencode() ([]byte, error) {
var c string
if *mbt {
c = "y"
} else {
c = "n"
}
return EncodeBytes(c)
}
// UnmarshalBencode implements Unmarshaler.UnmarshalBencode
func (mbt *myBoolPtrType) UnmarshalBencode(b []byte) error {
var str string
err := DecodeBytes(b, &str)
if err != nil {
return err
}
switch str {
case "y":
*mbt = true
case "n":
*mbt = false
default:
err = errors.New("invalid myBoolType")
}
return err
}
func TestEncodeOmit(t *testing.T) {
type encodeTestCase struct {
in interface{}
out string
err bool
}
type eT struct {
A string `bencode:",omitempty"`
B int `bencode:",omitempty"`
C *int `bencode:",omitempty"`
}
var encodeCases = []encodeTestCase{
{eT{}, `de`, false},
{eT{A: "a"}, `d1:A1:ae`, false},
{eT{B: 5}, `d1:Bi5ee`, false},
{eT{C: new(int)}, `d1:Ci0ee`, false},
}
for i, tt := range encodeCases {
data, err := EncodeString(tt.in)
if !tt.err && err != nil {
t.Errorf("#%d: Unexpected err: %v", i, err)
continue
}
if tt.err && err == nil {
t.Errorf("#%d: Expected err is nil", i)
continue
}
if tt.out != data {
t.Errorf("#%d: Val: %q != %q", i, data, tt.out)
}
}
}