1
2
3
4 package tracker
5
6 import (
7 "bufio"
8 "bytes"
9 "encoding/binary"
10 "fmt"
11 "github.com/stretchr/testify/assert"
12 "io"
13 "math/rand"
14 "testing"
15 )
16
17 func getErrorMsg(varName, functionName string) string {
18 return varName + ": not set properly in " + functionName + ". Tip: You might want to check if your network allows torrenting!"
19 }
20
21 func TestBuildConnReq(t *testing.T) {
22 fmt.Print("Testing tracker/utils.go : buildConnReq(): ")
23 req := buildConnReq()
24 errorMessage := "Invalid Connection Request for tracker"
25 assert.Equal(t, req[:12], []byte{0x00, 0x00, 0x04, 0x17, 0x27, 0x10, 0x19, 0x80, 0x00, 0x00, 0x00, 0x00}, errorMessage)
26 assert.NotEqual(t, req[:12], []byte{0x01, 0x00, 0x04, 0x17, 0x27, 0x10, 0x19, 0x80, 0x00, 0x00, 0x00, 0x00}, errorMessage)
27
28 fmt.Println("PASS")
29 }
30
31 func TestRespType(t *testing.T) {
32
33 fmt.Print("Testing tracker/utils.go : respType(): ")
34
35 var mockResponseBuf bytes.Buffer
36 writer := bufio.NewWriter(&mockResponseBuf)
37
38
39 binary.Write(writer, binary.BigEndian, uint32(1))
40 writer.Flush()
41 assert.Equal(t, respType(mockResponseBuf), "announce", "Unable to detect \"announce\" response when action=1")
42
43
44 mockResponseBuf = GetMockConnectResponseBuf(rand.Uint32(), rand.Uint64())
45 assert.Equal(t, respType(mockResponseBuf), "connect", "Unable to detect \"connect\" response when action=0")
46
47 mockResponseBuf.Reset()
48
49
50 binary.Write(writer, binary.BigEndian, uint32(1))
51 for i := 0; i < 3; i++ {
52 binary.Write(writer, binary.BigEndian, uint32(rand.Uint32()))
53 }
54 writer.Flush()
55
56 assert.Equal(t, respType(mockResponseBuf), "announce", "Unable to detect \"announce\" response when action=1")
57
58 fmt.Println("PASS")
59 }
60
61 func TestParseConnResp(t *testing.T) {
62
63 fmt.Print("Testing tracker/utils.go : parseConnResp(): ")
64 trID := rand.Uint32()
65 connID := rand.Uint64()
66 mockConnRespBuf := GetMockConnectResponseBuf(trID, connID)
67
68 mockConnResp := parseConnResp(mockConnRespBuf)
69
70 assert.Equal(t, mockConnResp.Action, uint32(0), "Action for connect response must be uint32(0)")
71 assert.Equal(t, mockConnResp.TransactionID, trID, "Unable to detect transactionID in connection response")
72 assert.Equal(t, mockConnResp.ConnectionID, connID, "Unable to detect connectionID in connection response")
73 fmt.Println("PASS")
74 }
75
76
77
78
79
80 func TestBuildAnnounceReq(t *testing.T) {
81 fmt.Print("Testing tracker/utils.go : buildAnnounceReq(): ")
82
83 connID := rand.Uint64()
84 report := GetRandomClientReport()
85
86 announceReqBuf, _ := buildAnnounceReq(connID, report)
87
88 var announceReqReader io.Reader = bytes.NewReader(announceReqBuf.Bytes())
89
90
91 var tempUint64 uint64
92 var tempUint32 uint32
93 var temp20ByteArr [20]byte
94 var tempInt32 int32
95 var tempUint16 uint16
96
97 errorMsg := func(varName string) string {
98 return varName + ": not set properly in Announce Request"
99 }
100
101
102 binary.Read(announceReqReader, binary.BigEndian, &tempUint64)
103 assert.Equal(t, connID, tempUint64, errorMsg("connectionID"))
104
105
106 binary.Read(announceReqReader, binary.BigEndian, &tempUint32)
107 assert.Equal(t, uint32(1), tempUint32, errorMsg("action"))
108
109
110 binary.Read(announceReqReader, binary.BigEndian, &tempUint32)
111
112
113 binary.Read(announceReqReader, binary.BigEndian, &temp20ByteArr)
114 var infoHash [20]byte
115
116 copy(infoHash[:], report.TorrentFile.InfoHash)
117 assert.Equal(t, infoHash, temp20ByteArr, errorMsg("torrent.InfoHash"))
118
119
120 binary.Read(announceReqReader, binary.BigEndian, &temp20ByteArr)
121
122
123 binary.Read(announceReqReader, binary.BigEndian, &tempUint64)
124 assert.Equal(t, uint64(0), tempUint64, errorMsg("downloaded"))
125
126
127 binary.Read(announceReqReader, binary.BigEndian, &tempUint64)
128
129 assert.Equal(t, report.Left, tempUint64, errorMsg("left"))
130
131 binary.Read(announceReqReader, binary.BigEndian, &tempUint64)
132 assert.Equal(t, uint64(0), tempUint64, errorMsg("uploaded"))
133
134
135 binary.Read(announceReqReader, binary.BigEndian, &tempUint32)
136 assert.Equal(t, uint32(0), tempUint32, errorMsg("event"))
137
138
139 binary.Read(announceReqReader, binary.BigEndian, &tempUint32)
140 assert.Equal(t, uint32(0), tempUint32, errorMsg("Ip address"))
141
142
143 binary.Read(announceReqReader, binary.BigEndian, &tempUint32)
144
145
146 binary.Read(announceReqReader, binary.BigEndian, &tempInt32)
147 assert.Equal(t, int32(-1), tempInt32, errorMsg("num_want"))
148
149
150 binary.Read(announceReqReader, binary.BigEndian, &tempUint16)
151 assert.Equal(t, report.Port, tempUint16, errorMsg("port"))
152
153 fmt.Println("PASS")
154 }
155
156 func TestParseAnnounceResp(t *testing.T) {
157 fmt.Print("Testing tracker/utils.go : parseAnnounceResp(): ")
158 transactionID, interval, leechers, seeders := rand.Uint32(), rand.Uint32(), rand.Uint32(), rand.Uint32()
159 length := rand.Intn(5)
160 peers := make([]Peer, length)
161 for i := 0; i < length; i++ {
162 peers[i].IPAdress = rand.Uint32()
163 peers[i].Port = uint16(rand.Intn(9000) + 1000)
164 }
165
166 mockAnnounceResponseBuf := GetMockAnnounceResponseBuf(transactionID, interval, leechers, seeders, peers)
167
168 announceResponse := parseAnnounceResp(mockAnnounceResponseBuf)
169
170
171 assert.Equal(t, transactionID, announceResponse.TransactionID, getErrorMsg("transactionID", "TestParseAnnounceResp"))
172 assert.Equal(t, interval, announceResponse.Interval, getErrorMsg("interval", "TestParseAnnounceResp"))
173 assert.Equal(t, leechers, announceResponse.Leechers, getErrorMsg("leechers", "TestParseAnnounceResponse"))
174 assert.Equal(t, seeders, announceResponse.Seeders, getErrorMsg("seeders", "TestParseAnnounceResponse"))
175
176
177 assert.Equal(t, len(peers), len(announceResponse.Peers), getErrorMsg("LengthNotEqual", "TestParseAnnounceResponse"))
178
179
180 for i := 0; i < length; i++ {
181 assert.Equal(t, peers[i].IPAdress, announceResponse.Peers[i].IPAdress, getErrorMsg("IPMismatchInPeers", "TestParseAnnounceResponse"))
182 assert.Equal(t, peers[i].Port, announceResponse.Peers[i].Port, getErrorMsg("PortMismatchInPeers", "TestParseAnnounceResponse"))
183
184 }
185
186 fmt.Println("PASS")
187 }
188
189
220
View as plain text