-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuncode_test.go
More file actions
81 lines (75 loc) · 2.7 KB
/
Copy pathbuncode_test.go
File metadata and controls
81 lines (75 loc) · 2.7 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 (
"fmt"
"math/big"
"testing"
)
func TestBuncode(t *testing.T) {
// Integers
s := "i3e"
expected := big.NewInt(3)
r, ok := Buncode([]byte(s)).(*big.Int)
if !ok || r.Cmp(expected) != 0 {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
s = "i123456e"
expected = big.NewInt(123456)
r, ok = Buncode([]byte(s)).(*big.Int)
if !ok || r.Cmp(expected) != 0 {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
s = "i3306489856e"
expected = new(big.Int)
expected.SetString("3306489856", 10)
r, ok = Buncode([]byte(s)).(*big.Int)
if !ok || r.Cmp(expected) != 0 {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
// Strings
s = "1:a"
if r := Buncode([]byte(s)); !sameSlice(r, []byte("a")) {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
s = "4:spam"
if r := Buncode([]byte(s)); !sameSlice(r, []byte("spam")) {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
s = "13:" + string([]byte{0, 5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100})
if r := Buncode([]byte(s)); !sameSlice(r, []byte{0, 5, 10, 15, 20, 25, 30, 35, 40, 50, 60, 80, 100}) {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
// Lists (slices)
s = "l4:spam4:eggse"
if r := Buncode([]byte(s)); !sameSlice(r, [][]byte{[]byte("spam"), []byte("eggs")}) {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
s = "l1:al3:wayi2e2:goe1:ce"
if r := Buncode([]byte(s)); !sameSlice(r, []interface{}{[]byte("a"), []interface{}{[]byte("way"), big.NewInt(2), []byte("go")}, []byte("c")}) {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
s = "l4:pathl4:fileee"
if r := Buncode([]byte(s)); !sameSlice(r, []interface{}{[]byte("path"), []interface{}{[]byte("file")}}) {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
s = "li1ee"
if r := Buncode([]byte(s)); !sameSlice(r, []interface{}{1}) {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
s = "lli2eee"
if r := Buncode([]byte(s)); !sameSlice(r, []interface{}{[]interface{}{2}}) {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
s = "l4:look4:it's5:emptylee"
if r := Buncode([]byte(s)); !sameSlice(r, []interface{}{[]byte("look"), []byte("it's"), []byte("empty"), []interface{}{}}) {
t.Errorf("Doesn't decode %s correctly: %v", s, r)
}
// Dictionaries (maps)
s = "d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee"
d, ok := Buncode([]byte(s)).(map[string]interface{})
if !ok && (!sameSlice(d["publisher"], []byte("bob")) || !sameSlice(d["publisher-webpage"], []byte("www.example.com")) || !sameSlice(d["publisher.location"], []byte("home"))) {
t.Errorf("Doesn't decode %s correctly: %v", s, d)
}
}
func sameSlice(a interface{}, b interface{}) bool {
return (fmt.Sprintf("%v", a) == fmt.Sprintf("%v", b))
}