tracker/http/announce_test.go

370 lines
8.9 KiB
Go
Raw Normal View History

2015-01-01 18:02:25 +01:00
// Copyright 2015 The Chihaya Authors. All rights reserved.
2014-07-02 03:40:29 +02:00
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package http
import (
2014-07-16 19:53:42 +02:00
"net/http"
2014-07-02 03:40:29 +02:00
"net/http/httptest"
2014-07-16 19:53:42 +02:00
"net/url"
"reflect"
2014-07-17 20:14:29 +02:00
"strconv"
2014-07-02 03:40:29 +02:00
"testing"
2014-07-16 19:53:42 +02:00
"time"
2014-07-02 03:40:29 +02:00
"github.com/chihaya/bencode"
2014-07-02 03:40:29 +02:00
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/tracker"
"github.com/chihaya/chihaya/tracker/models"
2014-07-02 03:40:29 +02:00
)
2014-07-16 02:38:17 +02:00
func TestPublicAnnounce(t *testing.T) {
2014-07-16 04:19:44 +02:00
srv, err := setupTracker(&config.DefaultConfig)
if err != nil {
t.Fatal(err)
}
2014-07-16 02:38:17 +02:00
defer srv.Close()
2014-07-02 03:40:29 +02:00
2014-07-17 20:28:55 +02:00
peer1 := makePeerParams("peer1", true)
peer2 := makePeerParams("peer2", true)
peer3 := makePeerParams("peer3", false)
peer1["event"] = "started"
2014-07-17 20:28:55 +02:00
expected := makeResponse(1, 0)
checkAnnounce(peer1, expected, srv, t)
2014-07-16 02:38:17 +02:00
2014-07-17 20:28:55 +02:00
expected = makeResponse(2, 0)
checkAnnounce(peer2, expected, srv, t)
2014-07-16 02:38:17 +02:00
2014-07-17 20:28:55 +02:00
expected = makeResponse(2, 1, peer1, peer2)
checkAnnounce(peer3, expected, srv, t)
2014-07-16 02:38:17 +02:00
2014-07-17 20:28:55 +02:00
peer1["event"] = "stopped"
expected = makeResponse(1, 1, nil)
2014-07-17 20:28:55 +02:00
checkAnnounce(peer1, expected, srv, t)
2014-07-16 02:38:17 +02:00
2014-07-17 20:28:55 +02:00
expected = makeResponse(1, 1, peer2)
checkAnnounce(peer3, expected, srv, t)
2014-07-16 02:38:17 +02:00
}
2014-07-02 03:40:29 +02:00
2014-07-16 19:53:42 +02:00
func TestTorrentPurging(t *testing.T) {
cfg := config.DefaultConfig
srv, err := setupTracker(&cfg)
2014-07-16 19:53:42 +02:00
if err != nil {
t.Fatal(err)
}
defer srv.Close()
2014-08-01 22:16:38 +02:00
torrentAPIPath := srv.URL + "/torrents/" + url.QueryEscape(infoHash)
2014-07-16 19:53:42 +02:00
// Add one seeder.
peer := makePeerParams("peer1", true)
announce(peer, srv)
2014-08-01 22:16:38 +02:00
_, status, err := fetchPath(torrentAPIPath)
2014-07-16 19:53:42 +02:00
if err != nil {
t.Fatal(err)
} else if status != http.StatusOK {
t.Fatalf("expected torrent to exist (got %s)", http.StatusText(status))
}
2014-07-16 19:53:42 +02:00
// Remove seeder.
peer = makePeerParams("peer1", true)
peer["event"] = "stopped"
announce(peer, srv)
2014-08-01 22:16:38 +02:00
_, status, err = fetchPath(torrentAPIPath)
if err != nil {
t.Fatal(err)
} else if status != http.StatusNotFound {
t.Fatalf("expected torrent to have been purged (got %s)", http.StatusText(status))
2014-07-16 19:53:42 +02:00
}
}
2014-07-16 19:53:42 +02:00
func TestStalePeerPurging(t *testing.T) {
cfg := config.DefaultConfig
cfg.Announce = config.Duration{
Duration: 10 * time.Millisecond,
}
srv, err := setupTracker(&cfg)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
2014-08-01 22:16:38 +02:00
torrentAPIPath := srv.URL + "/torrents/" + url.QueryEscape(infoHash)
// Add one seeder.
2014-07-17 20:28:55 +02:00
peer1 := makePeerParams("peer1", true)
announce(peer1, srv)
2014-07-16 19:53:42 +02:00
2014-08-01 22:16:38 +02:00
_, status, err := fetchPath(torrentAPIPath)
if err != nil {
t.Fatal(err)
} else if status != http.StatusOK {
t.Fatalf("expected torrent to exist (got %s)", http.StatusText(status))
}
// Add a leecher.
2014-07-17 20:28:55 +02:00
peer2 := makePeerParams("peer2", false)
expected := makeResponse(1, 1, peer1)
expected["interval"] = int64(0)
2014-07-17 20:28:55 +02:00
checkAnnounce(peer2, expected, srv, t)
// Let them both expire.
time.Sleep(30 * time.Millisecond)
2014-07-16 19:53:42 +02:00
2014-08-01 22:16:38 +02:00
_, status, err = fetchPath(torrentAPIPath)
2014-07-16 19:53:42 +02:00
if err != nil {
t.Fatal(err)
} else if status != http.StatusNotFound {
2014-07-16 19:53:42 +02:00
t.Fatalf("expected torrent to have been purged (got %s)", http.StatusText(status))
}
}
func TestPrivateAnnounce(t *testing.T) {
cfg := config.DefaultConfig
2014-07-25 22:58:26 +02:00
cfg.PrivateEnabled = true
tkr, err := tracker.New(&cfg)
if err != nil {
t.Fatal(err)
}
loadPrivateTestData(tkr)
srv, err := createServer(tkr, &cfg)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
baseURL := srv.URL
2014-07-17 20:28:55 +02:00
peer1 := makePeerParams("-TR2820-peer1", false)
peer2 := makePeerParams("-TR2820-peer2", false)
peer3 := makePeerParams("-TR2820-peer3", true)
expected := makeResponse(0, 1)
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
2014-07-17 20:28:55 +02:00
checkAnnounce(peer1, expected, srv, t)
2014-07-17 20:28:55 +02:00
expected = makeResponse(0, 2, peer1)
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2"
2014-07-17 20:28:55 +02:00
checkAnnounce(peer2, expected, srv, t)
2014-07-17 20:28:55 +02:00
expected = makeResponse(1, 2, peer1, peer2)
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3"
2014-07-17 20:28:55 +02:00
checkAnnounce(peer3, expected, srv, t)
2014-07-17 20:28:55 +02:00
expected = makeResponse(1, 2, peer2, peer3)
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
2014-07-17 20:28:55 +02:00
checkAnnounce(peer1, expected, srv, t)
}
2014-07-17 20:14:29 +02:00
func TestPreferredSubnet(t *testing.T) {
cfg := config.DefaultConfig
cfg.PreferredSubnet = true
cfg.PreferredIPv4Subnet = 8
2014-07-17 20:28:55 +02:00
cfg.PreferredIPv6Subnet = 16
cfg.DualStackedPeers = false
2014-07-17 20:14:29 +02:00
srv, err := setupTracker(&cfg)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
2014-07-17 20:38:01 +02:00
peerA1 := makePeerParams("peerA1", false, "44.0.0.1")
peerA2 := makePeerParams("peerA2", false, "44.0.0.2")
peerA3 := makePeerParams("peerA3", false, "44.0.0.3")
peerA4 := makePeerParams("peerA4", false, "44.0.0.4")
peerB1 := makePeerParams("peerB1", false, "45.0.0.1")
peerB2 := makePeerParams("peerB2", false, "45.0.0.2")
peerC1 := makePeerParams("peerC1", false, "fc01::1")
peerC2 := makePeerParams("peerC2", false, "fc01::2")
peerC3 := makePeerParams("peerC3", false, "fc01::3")
peerD1 := makePeerParams("peerD1", false, "fc02::1")
peerD2 := makePeerParams("peerD2", false, "fc02::2")
2014-07-17 20:14:29 +02:00
2014-07-17 20:28:55 +02:00
expected := makeResponse(0, 1)
2014-07-17 20:14:29 +02:00
checkAnnounce(peerA1, expected, srv, t)
2014-07-17 20:28:55 +02:00
expected = makeResponse(0, 2, peerA1)
2014-07-17 20:14:29 +02:00
checkAnnounce(peerA2, expected, srv, t)
2014-07-17 20:28:55 +02:00
expected = makeResponse(0, 3, peerA1, peerA2)
2014-07-17 20:14:29 +02:00
checkAnnounce(peerB1, expected, srv, t)
peerB2["numwant"] = "1"
2014-07-17 20:28:55 +02:00
expected = makeResponse(0, 4, peerB1)
2014-07-17 20:14:29 +02:00
checkAnnounce(peerB2, expected, srv, t)
checkAnnounce(peerB2, expected, srv, t)
peerA3["numwant"] = "2"
2014-07-17 20:28:55 +02:00
expected = makeResponse(0, 5, peerA1, peerA2)
2014-07-17 20:14:29 +02:00
checkAnnounce(peerA3, expected, srv, t)
2014-07-17 20:38:01 +02:00
checkAnnounce(peerA3, expected, srv, t)
2014-07-17 20:14:29 +02:00
peerA4["numwant"] = "3"
2014-07-17 20:28:55 +02:00
expected = makeResponse(0, 6, peerA1, peerA2, peerA3)
2014-07-17 20:14:29 +02:00
checkAnnounce(peerA4, expected, srv, t)
2014-07-17 20:38:01 +02:00
checkAnnounce(peerA4, expected, srv, t)
expected = makeResponse(0, 7, peerA1, peerA2, peerA3, peerA4, peerB1, peerB2)
checkAnnounce(peerC1, expected, srv, t)
peerC2["numwant"] = "1"
expected = makeResponse(0, 8, peerC1)
checkAnnounce(peerC2, expected, srv, t)
checkAnnounce(peerC2, expected, srv, t)
peerC3["numwant"] = "2"
expected = makeResponse(0, 9, peerC1, peerC2)
checkAnnounce(peerC3, expected, srv, t)
checkAnnounce(peerC3, expected, srv, t)
expected = makeResponse(0, 10, peerA1, peerA2, peerA3, peerA4, peerB1, peerB2, peerC1, peerC2, peerC3)
checkAnnounce(peerD1, expected, srv, t)
peerD2["numwant"] = "1"
expected = makeResponse(0, 11, peerD1)
checkAnnounce(peerD2, expected, srv, t)
checkAnnounce(peerD2, expected, srv, t)
2014-07-17 20:14:29 +02:00
}
2014-07-23 06:55:57 +02:00
func TestCompactAnnounce(t *testing.T) {
srv, err := setupTracker(&config.DefaultConfig)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
compact := "\xff\x09\x7f\x05\x04\xd2"
ip := "255.9.127.5" // Use the same IP for all of them so we don't have to worry about order.
2014-07-23 06:55:57 +02:00
peer1 := makePeerParams("peer1", false, ip)
2014-07-23 06:55:57 +02:00
peer1["compact"] = "1"
peer2 := makePeerParams("peer2", false, ip)
2014-07-23 06:55:57 +02:00
peer2["compact"] = "1"
peer3 := makePeerParams("peer3", false, ip)
2014-07-23 06:55:57 +02:00
peer3["compact"] = "1"
expected := makeResponse(0, 1)
expected["peers"] = ""
checkAnnounce(peer1, expected, srv, t)
expected = makeResponse(0, 2)
expected["peers"] = compact
checkAnnounce(peer2, expected, srv, t)
expected = makeResponse(0, 3)
expected["peers"] = compact + compact
checkAnnounce(peer3, expected, srv, t)
}
2014-07-17 20:38:01 +02:00
func makePeerParams(id string, seed bool, extra ...string) params {
2014-07-16 02:38:17 +02:00
left := "1"
if seed {
left = "0"
2014-07-02 03:40:29 +02:00
}
2014-07-17 20:38:01 +02:00
ip := "10.0.0.1"
if len(extra) >= 1 {
ip = extra[0]
}
2014-07-16 02:38:17 +02:00
return params{
"info_hash": infoHash,
"peer_id": id,
2014-07-17 20:38:01 +02:00
"ip": ip,
2014-07-16 02:38:17 +02:00
"port": "1234",
"uploaded": "0",
"downloaded": "0",
"left": left,
"compact": "0",
"numwant": "50",
2014-07-02 03:40:29 +02:00
}
}
2014-07-17 20:14:29 +02:00
func peerFromParams(peer params) bencode.Dict {
port, _ := strconv.ParseInt(peer["port"], 10, 64)
return bencode.Dict{
"peer id": peer["peer_id"],
2014-07-17 20:28:55 +02:00
"ip": peer["ip"],
2014-07-17 20:14:29 +02:00
"port": port,
}
}
2014-07-17 20:28:55 +02:00
func makeResponse(seeders, leechers int64, peers ...params) bencode.Dict {
2014-07-16 02:38:17 +02:00
dict := bencode.Dict{
"complete": seeders,
"incomplete": leechers,
"interval": int64(1800),
"min interval": int64(900),
}
2014-07-02 03:40:29 +02:00
2014-07-17 20:28:55 +02:00
if !(len(peers) == 1 && peers[0] == nil) {
peerList := bencode.List{}
for _, peer := range peers {
peerList = append(peerList, peerFromParams(peer))
}
dict["peers"] = peerList
}
2014-07-16 02:38:17 +02:00
return dict
}
2014-07-16 03:07:33 +02:00
func checkAnnounce(p params, expected interface{}, srv *httptest.Server, t *testing.T) bool {
body, err := announce(p, srv)
if err != nil {
t.Error(err)
2014-07-16 02:38:17 +02:00
return false
}
if e, ok := expected.(bencode.Dict); ok {
sortPeersInResponse(e)
}
2014-07-16 02:38:17 +02:00
got, err := bencode.Unmarshal(body)
if e, ok := got.(bencode.Dict); ok {
sortPeersInResponse(e)
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("\ngot: %#v\nwanted: %#v", got, expected)
2014-07-16 02:38:17 +02:00
return false
}
2014-07-16 02:38:17 +02:00
return true
}
func loadPrivateTestData(tkr *tracker.Tracker) {
users := []string{
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1",
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2",
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3",
}
for i, passkey := range users {
tkr.PutUser(&models.User{
ID: uint64(i + 1),
Passkey: passkey,
})
}
tkr.PutClient("TR2820")
torrent := &models.Torrent{
ID: 1,
Infohash: infoHash,
Seeders: models.NewPeerMap(true, tkr.Config),
Leechers: models.NewPeerMap(false, tkr.Config),
}
tkr.PutTorrent(torrent)
}