-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_test.go
70 lines (63 loc) · 2.06 KB
/
decode_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
package bencode
import (
"encoding/hex"
"reflect"
"testing"
)
func TestDecodeDictionary(t *testing.T) {
dict := make(map[string]interface{})
dict["str"] = "string"
dict["int"] = int64(1234)
dict["list"] = []interface{}{
"string",
int64(1234),
[]interface{}{"string", int64(1234)},
}
dict["dict"] = map[string]interface{}{
"str": "string",
"int": int64(1234),
"list": []interface{}{
"string",
int64(1234),
[]interface{}{"string", int64(1234)},
},
}
res, err := Decode([]byte("d4:dictd3:inti1234e4:listl6:stringi1234el6:stringi1234eee3:str6:stringe3:inti1234e4:listl6:stringi1234el6:stringi1234eee3:str6:stringe"))
switch {
case err != nil:
t.Errorf("while decoding dictionary: %v", err)
case !reflect.DeepEqual(res, dict):
t.Errorf("unexpected dictionary decoding: %v", res)
}
}
func TestDecodeExampleTorrent(t *testing.T) {
hexTorrent := "64373a636f6d6d656e7431353a6578616d706c6520636f6d6d656e743130" +
"3a6372656174656420627931383a71426974746f7272656e742076342e30" +
"2e3331333a6372656174696f6e2064617465693135383638303432383365" +
"343a696e666f64363a6c656e6774686937383565343a6e616d65363a7465" +
"73742e7631323a7069656365206c656e67746869313633383465363a7069" +
"6563657332303a5fdf83383fbb7aad048ae7ae23c71f9c15f7802165383a" +
"75726c2d6c697374303a65"
data, err := hex.DecodeString(hexTorrent)
if err != nil {
t.Errorf("unable to hex decode torrent file: %v", err)
}
dict := make(map[string]interface{})
dict["comment"] = "example comment"
dict["created by"] = "qBittorrent v4.0.3"
dict["creation date"] = int64(1586804283)
dict["info"] = map[string]interface{}{
"length": int64(785),
"name": "test.v",
"piece length": int64(16384),
"pieces": string([]byte{0x5f, 0xdf, 0x83, 0x38, 0x3f, 0xbb, 0x7a, 0xad, 0x04, 0x8a, 0xe7, 0xae, 0x23, 0xc7, 0x1f, 0x9c, 0x15, 0xf7, 0x80, 0x21}),
}
dict["url-list"] = ""
res, err := Decode(data)
switch {
case err != nil:
t.Errorf("while decoding torrent: %v", err)
case !reflect.DeepEqual(res, dict):
t.Errorf("unexpected torrent decoding: %v\n%v", res, dict)
}
}