Refactor announce test helpers

This commit is contained in:
Justin Li 2014-07-17 14:28:55 -04:00
parent 6b089342d1
commit 664b365061

View file

@ -26,36 +26,30 @@ func TestPublicAnnounce(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
peer1 := makePeerParams("peer1", true)
peer2 := makePeerParams("peer2", true)
peer3 := makePeerParams("peer3", false)
// Add one seeder. // Add one seeder.
peer := makePeerParams("peer1", true) expected := makeResponse(1, 0)
expected := makeResponse(1, 0, bencode.List{}) checkAnnounce(peer1, expected, srv, t)
checkAnnounce(peer, expected, srv, t)
// Add another seeder. // Add another seeder.
peer = makePeerParams("peer2", true) expected = makeResponse(2, 0)
expected = makeResponse(2, 0, bencode.List{}) checkAnnounce(peer2, expected, srv, t)
checkAnnounce(peer, expected, srv, t)
// Add a leecher. // Add a leecher.
peer = makePeerParams("peer3", false) expected = makeResponse(2, 1, peer1, peer2)
expected = makeResponse(2, 1, bencode.List{ checkAnnounce(peer3, expected, srv, t)
makePeerResponse("peer1"),
makePeerResponse("peer2"),
})
checkAnnounce(peer, expected, srv, t)
// Remove seeder. // Remove seeder.
peer = makePeerParams("peer1", true) peer1["event"] = "stopped"
peer["event"] = "stopped"
expected = makeResponse(1, 1, nil) expected = makeResponse(1, 1, nil)
checkAnnounce(peer, expected, srv, t) checkAnnounce(peer1, expected, srv, t)
// Check seeders. // Check seeders.
peer = makePeerParams("peer3", false) expected = makeResponse(1, 1, peer2)
expected = makeResponse(1, 1, bencode.List{ checkAnnounce(peer3, expected, srv, t)
makePeerResponse("peer2"),
})
checkAnnounce(peer, expected, srv, t)
} }
func TestTorrentPurging(t *testing.T) { func TestTorrentPurging(t *testing.T) {
@ -105,8 +99,8 @@ func TestStalePeerPurging(t *testing.T) {
torrentApiPath := srv.URL + "/torrents/" + url.QueryEscape(infoHash) torrentApiPath := srv.URL + "/torrents/" + url.QueryEscape(infoHash)
// Add one seeder. // Add one seeder.
peer := makePeerParams("peer1", true) peer1 := makePeerParams("peer1", true)
announce(peer, srv) announce(peer1, srv)
_, status, err := fetchPath(torrentApiPath) _, status, err := fetchPath(torrentApiPath)
if err != nil { if err != nil {
@ -116,12 +110,10 @@ func TestStalePeerPurging(t *testing.T) {
} }
// Add a leecher. // Add a leecher.
peer = makePeerParams("peer2", false) peer2 := makePeerParams("peer2", false)
expected := makeResponse(1, 1, bencode.List{ expected := makeResponse(1, 1, peer1)
makePeerResponse("peer1"),
})
expected["interval"] = int64(0) expected["interval"] = int64(0)
checkAnnounce(peer, expected, srv, t) checkAnnounce(peer2, expected, srv, t)
// Let them both expire. // Let them both expire.
time.Sleep(30 * time.Millisecond) time.Sleep(30 * time.Millisecond)
@ -156,40 +148,32 @@ func TestPrivateAnnounce(t *testing.T) {
defer srv.Close() defer srv.Close()
baseURL := srv.URL baseURL := srv.URL
peer := makePeerParams("-TR2820-peer1", false) peer1 := makePeerParams("-TR2820-peer1", false)
expected := makeResponse(0, 1, bencode.List{}) peer2 := makePeerParams("-TR2820-peer2", false)
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1" peer3 := makePeerParams("-TR2820-peer3", true)
checkAnnounce(peer, expected, srv, t)
peer = makePeerParams("-TR2820-peer2", false) expected := makeResponse(0, 1)
expected = makeResponse(0, 2, bencode.List{ srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
makePeerResponse("-TR2820-peer1"), checkAnnounce(peer1, expected, srv, t)
})
expected = makeResponse(0, 2, peer1)
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2" srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2"
checkAnnounce(peer, expected, srv, t) checkAnnounce(peer2, expected, srv, t)
peer = makePeerParams("-TR2820-peer3", true) expected = makeResponse(1, 2, peer1, peer2)
expected = makeResponse(1, 2, bencode.List{
makePeerResponse("-TR2820-peer1"),
makePeerResponse("-TR2820-peer2"),
})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3" srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3"
checkAnnounce(peer, expected, srv, t) checkAnnounce(peer3, expected, srv, t)
peer = makePeerParams("-TR2820-peer1", false) expected = makeResponse(1, 2, peer2, peer3)
expected = makeResponse(1, 2, bencode.List{
makePeerResponse("-TR2820-peer2"),
makePeerResponse("-TR2820-peer3"),
})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1" srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
checkAnnounce(peer, expected, srv, t) checkAnnounce(peer1, expected, srv, t)
} }
func TestPreferredSubnet(t *testing.T) { func TestPreferredSubnet(t *testing.T) {
cfg := config.DefaultConfig cfg := config.DefaultConfig
cfg.PreferredSubnet = true cfg.PreferredSubnet = true
cfg.PreferredIPv4Subnet = 8 cfg.PreferredIPv4Subnet = 8
cfg.PreferredIPv6Subnet = 8 cfg.PreferredIPv6Subnet = 16
srv, err := setupTracker(&cfg) srv, err := setupTracker(&cfg)
if err != nil { if err != nil {
@ -197,7 +181,6 @@ func TestPreferredSubnet(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
// Make a bunch of peers in two subnets.
peerA1 := makePeerParams("peerA1", false) peerA1 := makePeerParams("peerA1", false)
peerA1["ip"] = "44.0.0.1" peerA1["ip"] = "44.0.0.1"
@ -216,42 +199,27 @@ func TestPreferredSubnet(t *testing.T) {
peerB2 := makePeerParams("peerB2", false) peerB2 := makePeerParams("peerB2", false)
peerB2["ip"] = "45.0.0.2" peerB2["ip"] = "45.0.0.2"
// Check what peers their announces return. expected := makeResponse(0, 1)
expected := makeResponse(0, 1, bencode.List{})
checkAnnounce(peerA1, expected, srv, t) checkAnnounce(peerA1, expected, srv, t)
expected = makeResponse(0, 2, bencode.List{ expected = makeResponse(0, 2, peerA1)
peerFromParams(peerA1),
})
checkAnnounce(peerA2, expected, srv, t) checkAnnounce(peerA2, expected, srv, t)
expected = makeResponse(0, 3, bencode.List{ expected = makeResponse(0, 3, peerA1, peerA2)
peerFromParams(peerA1),
peerFromParams(peerA2),
})
checkAnnounce(peerB1, expected, srv, t) checkAnnounce(peerB1, expected, srv, t)
peerB2["numwant"] = "1" peerB2["numwant"] = "1"
expected = makeResponse(0, 4, bencode.List{ expected = makeResponse(0, 4, peerB1)
peerFromParams(peerB1),
})
checkAnnounce(peerB2, expected, srv, t) checkAnnounce(peerB2, expected, srv, t)
checkAnnounce(peerB2, expected, srv, t) checkAnnounce(peerB2, expected, srv, t)
checkAnnounce(peerB2, expected, srv, t) checkAnnounce(peerB2, expected, srv, t)
peerA3["numwant"] = "2" peerA3["numwant"] = "2"
expected = makeResponse(0, 5, bencode.List{ expected = makeResponse(0, 5, peerA1, peerA2)
peerFromParams(peerA1),
peerFromParams(peerA2),
})
checkAnnounce(peerA3, expected, srv, t) checkAnnounce(peerA3, expected, srv, t)
peerA4["numwant"] = "3" peerA4["numwant"] = "3"
expected = makeResponse(0, 6, bencode.List{ expected = makeResponse(0, 6, peerA1, peerA2, peerA3)
peerFromParams(peerA1),
peerFromParams(peerA2),
peerFromParams(peerA3),
})
checkAnnounce(peerA4, expected, srv, t) checkAnnounce(peerA4, expected, srv, t)
} }
@ -264,6 +232,7 @@ func makePeerParams(id string, seed bool) params {
return params{ return params{
"info_hash": infoHash, "info_hash": infoHash,
"peer_id": id, "peer_id": id,
"ip": "10.0.0.1",
"port": "1234", "port": "1234",
"uploaded": "0", "uploaded": "0",
"downloaded": "0", "downloaded": "0",
@ -273,30 +242,17 @@ func makePeerParams(id string, seed bool) params {
} }
} }
func makePeerResponse(id string) bencode.Dict {
return bencode.Dict{
"peer id": id,
"ip": "127.0.0.1",
"port": int64(1234),
}
}
func peerFromParams(peer params) bencode.Dict { func peerFromParams(peer params) bencode.Dict {
ip := peer["ip"]
if ip == "" {
ip = "127.0.0.1"
}
port, _ := strconv.ParseInt(peer["port"], 10, 64) port, _ := strconv.ParseInt(peer["port"], 10, 64)
return bencode.Dict{ return bencode.Dict{
"peer id": peer["peer_id"], "peer id": peer["peer_id"],
"ip": ip, "ip": peer["ip"],
"port": port, "port": port,
} }
} }
func makeResponse(seeders, leechers int64, peers bencode.List) bencode.Dict { func makeResponse(seeders, leechers int64, peers ...params) bencode.Dict {
dict := bencode.Dict{ dict := bencode.Dict{
"complete": seeders, "complete": seeders,
"incomplete": leechers, "incomplete": leechers,
@ -304,8 +260,12 @@ func makeResponse(seeders, leechers int64, peers bencode.List) bencode.Dict {
"min interval": int64(900), "min interval": int64(900),
} }
if peers != nil { if !(len(peers) == 1 && peers[0] == nil) {
dict["peers"] = peers peerList := bencode.List{}
for _, peer := range peers {
peerList = append(peerList, peerFromParams(peer))
}
dict["peers"] = peerList
} }
return dict return dict
} }