aboutsummaryrefslogtreecommitdiff
path: root/bencode/encode_test.go
blob: efdfcf3cf1593daa5150c56e9866bea78b3e8855 (plain)
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
package bencode

import (
	"testing"
)

func TestEncodeString(t *testing.T) {
	tests := []struct {
		in  string
		out string
		end int
	}{
		{in: "", out: "0:"},
		{in: "hello", out: "5:hello"},
		{in: "goodbye", out: "7:goodbye"},
		{in: "hello world", out: "11:hello world"},
		{in: "1-5%3~]+=\\| []>.,`??", out: "20:1-5%3~]+=\\| []>.,`??"},
	}

	for _, tt := range tests {
		r1b, err := EncodeString(tt.in)
		if err != nil {
			t.Errorf("EncodeString(%q) failed with error %q", tt.in, err)
		}

		r1 := string(r1b)

		if r1 != tt.out {
			t.Errorf("EncodeString(%q) => %q, expected %q", tt.in, r1, tt.out)
		}

		r2b, err := Encode(tt.in)
		if err != nil {
			t.Errorf("EncodeString(%q) failed with error %q", tt.in, err)
		}

		r2 := string(r2b)
		if r2 != tt.out {
			t.Errorf("EncodeString(%q) => %q, expected %q", tt.in, r2, tt.out)
		}
	}
}

func TestEncodeInt(t *testing.T) {
	tests := []struct {
		in  interface{}
		out string
	}{
		{out: "i0e", in: int64(0)},
		{out: "i0e", in: int32(0)},
		{out: "i0e", in: int16(0)},
		{out: "i0e", in: 0},
		{out: "i5e", in: int64(5)},
		{out: "i5e", in: int32(5)},
		{out: "i5e", in: 5},
		{out: "i-5e", in: int64(-5)},
		{out: "i-5e", in: int32(-5)},
		{out: "i-5e", in: -5},
		{out: "i1234567890e", in: int64(1234567890)},
		{out: "i-1234567890e", in: int64(-1234567890)},
	}

	var r1b []byte
	var err error

	for _, tt := range tests {
		switch v := tt.in.(type) {
		case int:
			r1b, err = EncodeInt(int64(v))
		case int16:
			r1b, err = EncodeInt(int64(v))
		case int32:
			r1b, err = EncodeInt(int64(v))
		case int64:
			r1b, err = EncodeInt(v)
		}
		if err != nil {
			t.Errorf("EncodeInt(%d) failed with error %q", tt.in, err)
		}
		r1 := string(r1b)

		if r1 != tt.out {
			t.Errorf("EncodeInt(%d) => %s, expected %s", tt.in, r1, tt.out)
		}

		r2b, err := Encode(tt.in)
		if err != nil {
			t.Errorf("EncodeInt(%d) failed with error %q", tt.in, err)
		}
		r2 := string(r2b)

		if r2 != tt.out {
			t.Errorf("EncodeInt(%d) => %s, expected %s", tt.in, r2, tt.out)
		}
	}
}

func TestEncodeList(t *testing.T) {
	tests := []struct {
		in  []interface{}
		out string
	}{
		{out: "l4:spam4:eggse", in: []interface{}{"spam", "eggs"}},
		{out: "le", in: []interface{}{}},
		{out: "li-1ei0ee", in: []interface{}{int64(-1), int64(0)}},
		{out: "l4:testi-1ei0ee", in: []interface{}{"test", int64(-1), int64(0)}},
	}

	for _, tt := range tests {
		r1b, err := EncodeList(tt.in)
		if err != nil {
			t.Errorf("EncodeList(%v) failed with error %q", tt.in, err)
		}
		r1 := string(r1b)

		if r1 != tt.out {
			t.Errorf("EncodeList(%v) => %s, expected %s", tt.in, r1, tt.out)
		}

		r2b, err := Encode(tt.in)
		if err != nil {
			t.Errorf("Encode(%v) failed with error %q", tt.in, err)
		}
		r2 := string(r2b)

		if r2 != tt.out {
			t.Errorf("Encode(%v) => %s, expected %s", tt.in, r2, tt.out)
		}
	}
}

func TestEncodeDict(t *testing.T) {
	tests := []struct {
		in  map[string]interface{}
		out string
	}{
		{out: "d4:spam4:eggse", in: map[string]interface{}{"spam": "eggs"}},
		{out: "de", in: map[string]interface{}{}},
		{out: "d4:testi-1e3:twoi0ee", in: map[string]interface{}{"test": int64(-1), "two": int64(0)}},
		{out: "d4:testi0ee", in: map[string]interface{}{"test": int64(0)}},
	}

	for _, tt := range tests {
		r1b, err := EncodeDict(tt.in)
		if err != nil {
			t.Errorf("EncodeDict(%v) failed with error %q", tt.in, err)
		}
		r1 := string(r1b)

		if r1 != tt.out {
			t.Errorf("EncodeDict(%v) => %s, expected %s", tt.in, r1, tt.out)
		}

		r2b, err := Encode(tt.in)
		if err != nil {
			t.Errorf("Encode(%v) failed with error %q", tt.in, err)
		}
		r2 := string(r2b)

		if r2 != tt.out {
			t.Errorf("Encode(%v) => %s, expected %s", tt.in, r2, tt.out)
		}
	}
}