-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_test.go
More file actions
114 lines (106 loc) · 3.37 KB
/
encoder_test.go
File metadata and controls
114 lines (106 loc) · 3.37 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
package bencode
import (
"reflect"
"testing"
)
func TestEcodeInt(t *testing.T) {
type test struct {
Test uint64 `bencode:"test"`
}
have, err := Encode(&test{1234567890})
want := []byte("d4:testi1234567890ee")
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 TestEncodeString(t *testing.T) {
type test struct {
Test string `bencode:"test"`
}
have, err := Encode(&test{"test"})
want := []byte("d4:test4:teste")
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 TestEncodeStringInt(t *testing.T) {
type test struct {
TestString string `bencode:"teststring"`
TestInt uint64 `bencode:"testint"`
}
have, err := Encode(&test{"test", 1234567890})
want := []byte("d10:teststring4:test7:testinti1234567890ee")
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 TestEncodeMockTorrentFile(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"`
AnnounceList [][]string `bencode:"announce-list"`
Comment string `bencode:"comment"`
CreatedBy string `bencode:"created by"`
CreationDate uint64 `bencode:"creation date"`
Info info `bencode:"info"`
}
have, err := Encode(&test{"https://torrent.ubuntu.com/announce", [][]string{{"https://torrent.ubuntu.com/announce"}, {"https://ipv6.torrent.ubuntu.com/announce"}}, "Ubuntu CD releases.ubuntu.com", "mktorrent 1.1", 1681992794, info{4932407296, "ubuntu-23.04-desktop-amd64.iso", 262144}})
want := []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")
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 TestEncodeEmbeddedStruct(t *testing.T) {
type base struct {
Basefield string `bencode:"base"`
}
type extended struct {
base
Extendedfield string `bencode:"extended"`
}
have, err := Encode(&extended{base{"base"}, "extended"})
want := []byte("d4:base4:base8:extended8:extendede")
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 TestEncodeDoubleEmbeddedStruct(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, err := Encode(&doubleExtended{extended{base{"base"}, "extended"}, "doubleExtended"})
want := []byte("d4:base4:base8:extended8:extended14:doubleExtended14:doubleExtendede")
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(have, want) {
t.Errorf("Struct not properly encoded: wanted %s but have %s", want, have)
}
}