-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker_response_test.go
54 lines (43 loc) · 1.25 KB
/
tracker_response_test.go
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
package main
import (
"github.com/alexmarchant/bencoding"
"io/ioutil"
"testing"
)
func TestNewTrackerResponse(t *testing.T) {
peerResponse := getSamplePeerResponse()
tr, e := NewTrackerResponse(peerResponse)
if e != nil {
t.Errorf("Error parsing binary peer list: %v", e)
}
if len(tr.Peers) != 8 {
t.Errorf("Peer list length doesn't match declared length")
}
if tr.Interval != 1800 {
t.Errorf("Error generating peer list interval")
}
if tr.State != TrackerResponseStateSuccess {
t.Errorf("Error generating tracker response state")
}
}
func TestParseBinaryPeerList(t *testing.T) {
peerList := getSampleBinaryPeerList()
peers, e := parseBinaryPeerList(peerList)
if e != nil {
t.Errorf("Error parsing binary peer list: %v", e)
}
if len(peers) != 8 {
t.Errorf("Peer list length doesn't match declared length")
}
}
func getSamplePeerResponse() (fileBytes []byte) {
sampleTrackerResponseFilepath := "./test_resources/sample_binary_peer_response.txt"
fileBytes, _ = ioutil.ReadFile(sampleTrackerResponseFilepath)
return
}
func getSampleBinaryPeerList() (peerList bencoding.Element) {
fileBytes := getSamplePeerResponse()
benResponse, _ := bencoding.ParseString(string(fileBytes))
peerList = benResponse[0].DictValue["peers"]
return
}