aboutsummaryrefslogtreecommitdiff
path: root/bencode/bencode_test.go
blob: 212065495c8f5ab6b430b9daab642cdeb67c0e6c (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
package bencode

import "testing"

/*
func TestMarshal(t *testing.T) {
	v := meta.Torrent{
		Announce: "test",
		AnnounceList: [][]string{
			[]string{"test1", "test2"},
			[]string{"test3"},
		},
		CreationDate: time.Now(),
		Info: meta.Info{
			Length: 1000,
			Files: []meta.File{
				{Path: []string{"A"}},
				{Path: []string{"B"}},
			},
			Private: true,
		},
	}
	out, err := Marshal(v)
	if err != nil {
		t.Error(err)
	}
	t.Logf("%q\n", string(out))
}
*/

func TestParseString(t *testing.T) {
	in := "4:testZZZ"
	s, l := parseString([]byte(in))
	if s != "test" || l != 6 {
		t.Error("want test, got", s)
	}
}

func TestParseInt(t *testing.T) {
	in := "i12345e999"
	i, l := parseInt([]byte(in))
	if i != 12345 || l != 7 {
		t.Error("want 12345, got", i)
	}
}

/*
var testCase = []struct {
	Torrent  string
	InfoHash string
}{
	{
		Torrent:  "../testdata/OpenBSD_5.9_amd64_install59.iso-2016-03-29-0449.torrent",
		InfoHash: "e840038dea1998c39614dcd28594501df02bd32d",
	},
	{
		Torrent:  "../testdata/OpenBSD_songs_ogg-2016-03-25-0127.torrent",
		InfoHash: "1aa5af9f6f533961a65169bdeeb801e611724d32",
	},
	{
		Torrent:  "../testdata/debian-8.5.0-amd64-netinst.iso.torrent",
		InfoHash: "47b9ad52c009f3bd562ffc6da40e5c55d3fb47f3",
	},
}

func TestUnmarshal(t *testing.T) {
	for _, tc := range testCase {
		t.Log("Testing", tc.Torrent)
		var tor meta.Torrent
		body, err := ioutil.ReadFile(tc.Torrent)
		if err != nil {
			t.Error(err)
		}
		err = Unmarshal(body, &tor)
		if err != nil {
			t.Error(err)
		}
		h := hex.EncodeToString(tor.InfoHash)
		if h != tc.InfoHash {
			t.Error("got", h, "want", tc.InfoHash)
		}
	}
}

func TestUnmarshalPartial(t *testing.T) {
	for _, tc := range testCase {
		t.Log("Testing", tc.Torrent)
		var tor = struct {
			Announce string // `bencode:"announce"`
		}{}
		body, err := ioutil.ReadFile(tc.Torrent)
		if err != nil {
			t.Error(err)
		}
		err = Unmarshal(body, &tor)
		if err != nil {
			t.Error(err)
		}
		t.Log(tor.Announce)
	}
}
*/

func TestUnmarshalBool(t *testing.T) {
	var b struct{ A bool }
	in := `d1:Ai1ee`
	_, err := Unmarshal([]byte(in), &b)
	if err != nil {
		t.Error(err)
	}
	if b.A != true {
		t.Error("want true, got", b.A)
	}
}

func TestUnmarshalInt64(t *testing.T) {
	var b struct{ A int64 }
	in := `d1:Ai42ee`
	_, err := Unmarshal([]byte(in), &b)
	if err != nil {
		t.Error(err)
	}
	if b.A != 42 {
		t.Error("want true, got", b.A)
	}
}

func TestUnmarshalSlice(t *testing.T) {
	var b []byte
	in := `4:test`
	_, err := Unmarshal([]byte(in), &b)
	if err != nil {
		t.Error(err)
	}
	if string(b) != "test" {
		t.Error("want test, got", string(b))
	}
}

func TestUnmarshalArray(t *testing.T) {
	var b [4]byte
	in := `4:test`
	_, err := Unmarshal([]byte(in), &b)
	if err != nil {
		t.Error(err)
	}
	if string(b[:]) != "test" {
		t.Error("want test, got", string(b[:]))
	}
}