-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder_test.go
More file actions
123 lines (116 loc) · 3.44 KB
/
decoder_test.go
File metadata and controls
123 lines (116 loc) · 3.44 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
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
package bencode
import (
"reflect"
"testing"
)
func TestDecodeInt(t *testing.T) {
type test struct {
Test uint64 `bencode:"test"`
}
have := &test{}
want := &test{1234567890}
input := []byte("d4:testi1234567890ee")
err := Decode(input, have)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(have, want) {
t.Errorf("Struct not properly hidrated: wanted %v but have %v", want, have)
}
}
func TestDecodeString(t *testing.T) {
type test struct {
Test string `bencode:"test"`
}
have := &test{}
want := &test{"test"}
input := []byte("d4:test4:teste")
err := Decode(input, have)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(have, want) {
t.Errorf("Struct not properly hidrated: wanted %v but have %v", want, have)
}
}
func TestDecodeStringInt(t *testing.T) {
type test struct {
Foo string `bencode:"foo"`
Bar uint64 `bencode:"bar"`
}
have := &test{}
want := &test{"test", 1234567890}
input := []byte("d3:bari1234567890e3:foo4:teste")
err := Decode(input, have)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(have, want) {
t.Errorf("Struct not properly hidrated: wanted %v but have %v", want, have)
}
}
func TestDecodeMockTorrentFile(t *testing.T) {
type info struct {
Length uint64 `bencode:"length"`
Name string `bencode:"name"`
PieceLength uint64 `bencode:"piece length"`
}
type test struct {
Announce string `bencode:"announce"`
Comment string `bencode:"comment"`
AnnounceList [][]string `bencode:"announce-list"`
Info info `bencode:"info"`
}
have := &test{}
want := &test{"https://torrent.ubuntu.com/announce", "Ubuntu CD releases.ubuntu.com", [][]string{{"https://torrent.ubuntu.com/announce"}, {"https://ipv6.torrent.ubuntu.com/announce"}}, info{4932407296, "ubuntu-23.04-desktop-amd64.iso", 262144}}
input := []byte("d8:announce35:https://torrent.ubuntu.com/announce13:announce-listll35:https://torrent.ubuntu.com/announceel40:https://ipv6.torrent.ubuntu.com/announceee7:comment29:Ubuntu CD releases.ubuntu.com10:created by13:mktorrent 1.113:creation datei1681992794e4:infod6:lengthi4932407296e4:name30:ubuntu-23.04-desktop-amd64.iso12:piece lengthi262144eee")
err := Decode(input, have)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(have, want) {
t.Errorf("Struct not properly hidrated: wanted %+v but have %+v", want, have)
}
}
func TestDecodeEmbeddedStruct(t *testing.T) {
type base struct {
Basefield string `bencode:"base"`
}
type extended struct {
base
Extendedfield string `bencode:"extended"`
}
have := &extended{}
want := &extended{base{"base"}, "extended"}
input := []byte("d4:base4:base8:extended8:extendede")
err := Decode(input, have)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(have, want) {
t.Errorf("Struct not properly encoded: wanted %s but have %s", want, have)
}
}
func TestDecodeDoubleEmbeddedStruct(t *testing.T) {
type base struct {
Basefield string `bencode:"base"`
}
type extended struct {
base
Extendedfield string `bencode:"extended"`
}
type doubleExtended struct {
extended
DoubleExtendedField string `bencode:"doubleExtended"`
}
have := &doubleExtended{}
want := &doubleExtended{extended{base{"base"}, "extended"}, "doubleExtended"}
input := []byte("d4:base4:base14:doubleExtended14:doubleExtended8:extended8:extendede")
err := Decode(input, have)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(have, want) {
t.Errorf("Struct not properly encoded: wanted %s but have %s", want, have)
}
}