-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbencode_test.go
More file actions
81 lines (72 loc) · 2.54 KB
/
Copy pathbencode_test.go
File metadata and controls
81 lines (72 loc) · 2.54 KB
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
package btgo
import (
"math/big"
"testing"
)
func TestBencode(t *testing.T) {
// Strings
if s := Bencode("spam"); s != "4:spam" {
t.Error("Doesn't encode spam right:", s)
}
if s := Bencode("eggs and such"); s != "13:eggs and such" {
t.Error("Doesn't encode 'eggs and such' right:", s)
}
// Byte array strings
if s := Bencode([]byte("spam")); s != "4:spam" {
t.Error("Doesn't encode spam right:", s)
}
if s := Bencode([]byte("eggs and such")); s != "13:eggs and such" {
t.Error("Doesn't encode 'eggs and such' right:", s)
}
// Integers
if s := Bencode(3); s != "i3e" {
t.Error("Doesn't encode 3 correctly:", s)
}
if s := Bencode(100); s != "i100e" {
t.Error("Doesn't encode 100 correctly:", s)
}
// Big Integers
expected := new(big.Int)
expected.SetString("123456789123456789", 10)
if s := Bencode(expected); s != "i123456789123456789e" {
t.Error("Doesn't encode 123456789123456789 correctly:", s)
}
// Lists (slices)
stringSlice := []string{"spam", "eggs"}
if s := Bencode(stringSlice); s != "l4:spam4:eggse" {
t.Errorf("Doesn't encode %v correctly: %s", stringSlice, s)
}
singleElementSlice := genSlice("path", genSlice("file"))
if s := Bencode(singleElementSlice); s != "l4:pathl4:fileee" {
t.Errorf("Doesn't encode %v correctly: %s", stringSlice, s)
}
multiSlice := genSlice("way", 2, "go")
if s := Bencode(multiSlice); s != "l3:wayi2e2:goe" {
t.Errorf("Doesn't encode %v correctly: %s", multiSlice, s)
}
twoLayerSlice := genSlice("a", multiSlice, "c")
if s := Bencode(twoLayerSlice); s != "l1:al3:wayi2e2:goe1:ce" {
t.Errorf("Doesn't encode %v correctly: %s", twoLayerSlice, s)
}
// Dictionaries (maps)
stringMap := map[string]string{"publisher": "bob", "publisher-webpage": "www.example.com", "publisher.location": "home"}
if s := Bencode(stringMap); s != "d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee" {
t.Errorf("Doesn't encode %v correctly: %s", stringMap, s)
}
listMap := map[string]interface{}{"spam": genSlice("a", "b")}
if s := Bencode(listMap); s != "d4:spaml1:a1:bee" {
t.Errorf("Doesn't encode %v correctly: %s", listMap, s)
}
// Mixup
complexSlice := genSlice(genSlice("a", "b", "c"), listMap, genSlice(multiSlice, twoLayerSlice), "done")
if s := Bencode(complexSlice); s != "ll1:a1:b1:ced4:spaml1:a1:beell3:wayi2e2:goel1:al3:wayi2e2:goe1:cee4:donee" {
t.Errorf("Doesn't encode %v correctly: %s", listMap, s)
}
}
func genSlice(args ...interface{}) (s []interface{}) {
s = make([]interface{}, len(args))
for i, e := range args {
s[i] = interface{}(e)
}
return
}