1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 09:29:16 +02:00

Test tracker

This commit is contained in:
Jesse Li
2020-01-02 13:07:59 -05:00
parent 0050772d42
commit f46d43985a
2 changed files with 42 additions and 8 deletions

View File

@@ -13,7 +13,7 @@ import (
type bencodeTrackerResp struct { type bencodeTrackerResp struct {
Interval int `bencode:"interval"` Interval int `bencode:"interval"`
Peers string `bencode:"port"` Peers string `bencode:"peers"`
} }
func (t *TorrentFile) buildTrackerURL(peerID [20]byte, port uint16) (string, error) { func (t *TorrentFile) buildTrackerURL(peerID [20]byte, port uint16) (string, error) {
@@ -53,10 +53,5 @@ func (t *TorrentFile) requestPeers(peerID [20]byte, port uint16) ([]peers.Peer,
return nil, err return nil, err
} }
peers, err := peers.Unmarshal([]byte(trackerResp.Peers)) return peers.Unmarshal([]byte(trackerResp.Peers))
if err != nil {
return nil, err
}
return peers, nil
} }

View File

@@ -1,9 +1,13 @@
package torrentfile package torrentfile
import ( import (
"net"
"net/http"
"net/http/httptest"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/veggiedefender/torrent-client/peers"
) )
func TestBuildTrackerURL(t *testing.T) { func TestBuildTrackerURL(t *testing.T) {
@@ -19,9 +23,44 @@ func TestBuildTrackerURL(t *testing.T) {
Name: "debian-10.2.0-amd64-netinst.iso", Name: "debian-10.2.0-amd64-netinst.iso",
} }
peerID := [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} peerID := [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
var port uint16 = 6882 const port uint16 = 6882
url, err := to.buildTrackerURL(peerID, port) url, err := to.buildTrackerURL(peerID, port)
expected := "http://bttracker.debian.org:6969/announce?compact=1&downloaded=0&info_hash=%D8%F79%CE%C3%28%95l%CC%5B%BF%1F%86%D9%FD%CF%DB%A8%CE%B6&left=351272960&peer_id=%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14&port=6881&uploaded=0" expected := "http://bttracker.debian.org:6969/announce?compact=1&downloaded=0&info_hash=%D8%F79%CE%C3%28%95l%CC%5B%BF%1F%86%D9%FD%CF%DB%A8%CE%B6&left=351272960&peer_id=%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14&port=6881&uploaded=0"
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, url, expected) assert.Equal(t, url, expected)
} }
func TestRequestPeers(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := []byte(
"d" +
"8:interval" + "i900e" +
"5:peers" + "12:" +
string([]byte{
192, 0, 2, 123, 0x1A, 0xE1, // 0x1AE1 = 6881
127, 0, 0, 1, 0x1A, 0xE9, // 0x1AE9 = 6889
}) + "e")
w.Write(response)
}))
defer ts.Close()
tf := TorrentFile{
Announce: ts.URL,
InfoHash: [20]byte{216, 247, 57, 206, 195, 40, 149, 108, 204, 91, 191, 31, 134, 217, 253, 207, 219, 168, 206, 182},
PieceHashes: [][20]byte{
{49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106},
{97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 49, 50, 51, 52, 53, 54, 55, 56, 57, 48},
},
PieceLength: 262144,
Length: 351272960,
Name: "debian-10.2.0-amd64-netinst.iso",
}
peerID := [20]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
const port uint16 = 6882
expected := []peers.Peer{
{IP: net.IP{192, 0, 2, 123}, Port: 6881},
{IP: net.IP{127, 0, 0, 1}, Port: 6889},
}
p, err := tf.requestPeers(peerID, port)
assert.Nil(t, err)
assert.Equal(t, expected, p)
}