2014-07-06 23:56:54 +02:00
|
|
|
// Copyright 2014 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"
|
2014-07-16 01:35:18 +02:00
|
|
|
"reflect"
|
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
|
|
|
|
2014-07-16 01:35:18 +02:00
|
|
|
"github.com/chihaya/bencode"
|
2014-07-02 03:40:29 +02:00
|
|
|
"github.com/chihaya/chihaya/config"
|
2014-07-17 07:26:34 +02:00
|
|
|
"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-16 02:38:17 +02:00
|
|
|
// Add one seeder.
|
2014-07-16 02:50:33 +02:00
|
|
|
peer := makePeerParams("peer1", true)
|
|
|
|
expected := makeResponse(1, 0, bencode.List{})
|
2014-07-16 03:07:33 +02:00
|
|
|
checkAnnounce(peer, expected, srv, t)
|
2014-07-16 02:38:17 +02:00
|
|
|
|
|
|
|
// Add another seeder.
|
2014-07-16 02:50:33 +02:00
|
|
|
peer = makePeerParams("peer2", true)
|
|
|
|
expected = makeResponse(2, 0, bencode.List{})
|
2014-07-16 03:07:33 +02:00
|
|
|
checkAnnounce(peer, expected, srv, t)
|
2014-07-16 02:38:17 +02:00
|
|
|
|
|
|
|
// Add a leecher.
|
2014-07-16 02:50:33 +02:00
|
|
|
peer = makePeerParams("peer3", false)
|
|
|
|
expected = makeResponse(2, 1, bencode.List{
|
|
|
|
makePeerResponse("peer1"),
|
|
|
|
makePeerResponse("peer2"),
|
2014-07-16 02:38:17 +02:00
|
|
|
})
|
2014-07-16 03:07:33 +02:00
|
|
|
checkAnnounce(peer, expected, srv, t)
|
2014-07-16 02:38:17 +02:00
|
|
|
|
|
|
|
// Remove seeder.
|
2014-07-16 02:50:33 +02:00
|
|
|
peer = makePeerParams("peer1", true)
|
2014-07-16 02:38:17 +02:00
|
|
|
peer["event"] = "stopped"
|
2014-07-16 02:50:33 +02:00
|
|
|
expected = makeResponse(1, 1, nil)
|
2014-07-16 03:07:33 +02:00
|
|
|
checkAnnounce(peer, expected, srv, t)
|
2014-07-16 02:38:17 +02:00
|
|
|
|
|
|
|
// Check seeders.
|
2014-07-16 02:50:33 +02:00
|
|
|
peer = makePeerParams("peer3", false)
|
|
|
|
expected = makeResponse(1, 1, bencode.List{
|
|
|
|
makePeerResponse("peer2"),
|
2014-07-16 02:38:17 +02:00
|
|
|
})
|
2014-07-16 03:07:33 +02:00
|
|
|
checkAnnounce(peer, 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) {
|
2014-07-17 02:08:03 +02:00
|
|
|
cfg := config.DefaultConfig
|
|
|
|
srv, err := setupTracker(&cfg)
|
2014-07-16 19:53:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
torrentApiPath := srv.URL + "/torrents/" + url.QueryEscape(infoHash)
|
|
|
|
|
|
|
|
// Add one seeder.
|
|
|
|
peer := makePeerParams("peer1", true)
|
|
|
|
announce(peer, srv)
|
|
|
|
|
|
|
|
_, status, err := fetchPath(torrentApiPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-07-17 02:17:10 +02:00
|
|
|
} else if status != http.StatusOK {
|
2014-07-16 20:11:01 +02:00
|
|
|
t.Fatalf("expected torrent to exist (got %s)", http.StatusText(status))
|
|
|
|
}
|
2014-07-16 19:53:42 +02:00
|
|
|
|
2014-07-17 02:08:03 +02:00
|
|
|
// Remove seeder.
|
|
|
|
peer = makePeerParams("peer1", true)
|
|
|
|
peer["event"] = "stopped"
|
|
|
|
announce(peer, srv)
|
|
|
|
|
2014-07-16 20:11:01 +02:00
|
|
|
_, status, err = fetchPath(torrentApiPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-07-17 02:17:10 +02:00
|
|
|
} else if status != http.StatusNotFound {
|
2014-07-17 02:08:03 +02:00
|
|
|
t.Fatalf("expected torrent to have been purged (got %s)", http.StatusText(status))
|
2014-07-16 19:53:42 +02:00
|
|
|
}
|
2014-07-17 02:08:03 +02:00
|
|
|
}
|
2014-07-16 19:53:42 +02:00
|
|
|
|
2014-07-17 02:08:03 +02:00
|
|
|
func TestStalePeerPurging(t *testing.T) {
|
|
|
|
cfg := config.DefaultConfig
|
|
|
|
cfg.Announce = config.Duration{10 * time.Millisecond}
|
|
|
|
|
|
|
|
srv, err := setupTracker(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
torrentApiPath := srv.URL + "/torrents/" + url.QueryEscape(infoHash)
|
|
|
|
|
|
|
|
// Add one seeder.
|
|
|
|
peer := makePeerParams("peer1", true)
|
2014-07-16 19:53:42 +02:00
|
|
|
announce(peer, srv)
|
|
|
|
|
2014-07-17 02:08:03 +02:00
|
|
|
_, status, err := fetchPath(torrentApiPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-07-17 02:17:10 +02:00
|
|
|
} else if status != http.StatusOK {
|
2014-07-17 02:08:03 +02:00
|
|
|
t.Fatalf("expected torrent to exist (got %s)", http.StatusText(status))
|
|
|
|
}
|
|
|
|
|
2014-07-17 02:17:10 +02:00
|
|
|
// Add a leecher.
|
|
|
|
peer = makePeerParams("peer2", false)
|
|
|
|
expected := makeResponse(1, 1, bencode.List{
|
|
|
|
makePeerResponse("peer1"),
|
|
|
|
})
|
|
|
|
expected["interval"] = int64(0)
|
|
|
|
checkAnnounce(peer, expected, srv, t)
|
|
|
|
|
|
|
|
// Let them both expire.
|
|
|
|
time.Sleep(30 * time.Millisecond)
|
2014-07-16 19:53:42 +02:00
|
|
|
|
|
|
|
_, status, err = fetchPath(torrentApiPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2014-07-17 02:17:10 +02:00
|
|
|
} 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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 07:26:34 +02:00
|
|
|
func TestPrivateAnnounce(t *testing.T) {
|
|
|
|
cfg := config.DefaultConfig
|
|
|
|
cfg.Private = true
|
|
|
|
|
|
|
|
tkr, err := tracker.New(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = loadPrivateTestData(tkr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
srv, err := createServer(tkr, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer srv.Close()
|
|
|
|
baseURL := srv.URL
|
|
|
|
|
|
|
|
peer := makePeerParams("-TR2820-peer1", false)
|
|
|
|
expected := makeResponse(0, 1, bencode.List{})
|
|
|
|
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
|
|
|
|
checkAnnounce(peer, expected, srv, t)
|
|
|
|
|
|
|
|
peer = makePeerParams("-TR2820-peer2", false)
|
|
|
|
expected = makeResponse(0, 2, bencode.List{
|
|
|
|
makePeerResponse("-TR2820-peer1"),
|
|
|
|
})
|
|
|
|
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2"
|
|
|
|
checkAnnounce(peer, expected, srv, t)
|
|
|
|
|
|
|
|
peer = makePeerParams("-TR2820-peer3", true)
|
|
|
|
expected = makeResponse(1, 2, bencode.List{
|
|
|
|
makePeerResponse("-TR2820-peer1"),
|
|
|
|
makePeerResponse("-TR2820-peer2"),
|
|
|
|
})
|
|
|
|
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3"
|
|
|
|
checkAnnounce(peer, expected, srv, t)
|
|
|
|
|
|
|
|
peer = makePeerParams("-TR2820-peer1", false)
|
|
|
|
expected = makeResponse(1, 2, bencode.List{
|
|
|
|
makePeerResponse("-TR2820-peer2"),
|
|
|
|
makePeerResponse("-TR2820-peer3"),
|
|
|
|
})
|
|
|
|
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
|
|
|
|
checkAnnounce(peer, expected, srv, t)
|
|
|
|
}
|
|
|
|
|
2014-07-16 02:50:33 +02:00
|
|
|
func makePeerParams(id string, seed bool) 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-16 02:38:17 +02:00
|
|
|
return params{
|
|
|
|
"info_hash": infoHash,
|
|
|
|
"peer_id": id,
|
|
|
|
"port": "1234",
|
|
|
|
"uploaded": "0",
|
|
|
|
"downloaded": "0",
|
|
|
|
"left": left,
|
|
|
|
"compact": "0",
|
|
|
|
"numwant": "50",
|
2014-07-02 03:40:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 02:50:33 +02:00
|
|
|
func makePeerResponse(id string) bencode.Dict {
|
|
|
|
return bencode.Dict{
|
|
|
|
"ip": "127.0.0.1",
|
|
|
|
"peer id": id,
|
|
|
|
"port": int64(1234),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeResponse(seeders, leechers int64, peers bencode.List) bencode.Dict {
|
2014-07-16 02:38:17 +02:00
|
|
|
dict := bencode.Dict{
|
|
|
|
"complete": seeders,
|
|
|
|
"incomplete": leechers,
|
2014-07-16 01:35:18 +02:00
|
|
|
"interval": int64(1800),
|
|
|
|
"min interval": int64(900),
|
|
|
|
}
|
2014-07-02 03:40:29 +02:00
|
|
|
|
2014-07-16 02:38:17 +02:00
|
|
|
if peers != nil {
|
|
|
|
dict["peers"] = peers
|
2014-07-16 01:35:18 +02:00
|
|
|
}
|
2014-07-16 02:38:17 +02:00
|
|
|
return dict
|
|
|
|
}
|
2014-07-16 01:35:18 +02:00
|
|
|
|
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)
|
2014-07-16 01:35:18 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2014-07-16 02:38:17 +02:00
|
|
|
return false
|
2014-07-16 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 04:44:20 +02:00
|
|
|
if e, ok := expected.(bencode.Dict); ok {
|
|
|
|
sortPeersInResponse(e)
|
|
|
|
}
|
|
|
|
|
2014-07-16 02:38:17 +02:00
|
|
|
got, err := bencode.Unmarshal(body)
|
2014-07-16 04:44:20 +02:00
|
|
|
if e, ok := got.(bencode.Dict); ok {
|
|
|
|
sortPeersInResponse(e)
|
|
|
|
}
|
|
|
|
|
2014-07-16 01:35:18 +02:00
|
|
|
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 01:35:18 +02:00
|
|
|
}
|
2014-07-16 02:38:17 +02:00
|
|
|
return true
|
|
|
|
}
|
2014-07-17 07:26:34 +02:00
|
|
|
|
|
|
|
func loadPrivateTestData(tkr *tracker.Tracker) error {
|
|
|
|
conn, err := tkr.Pool.Get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
users := []string{
|
|
|
|
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1",
|
|
|
|
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2",
|
|
|
|
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3",
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, passkey := range users {
|
|
|
|
err = conn.PutUser(&models.User{
|
|
|
|
ID: uint64(i + 1),
|
|
|
|
Passkey: passkey,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.PutClient("TR2820")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
torrent := &models.Torrent{
|
|
|
|
ID: 1,
|
|
|
|
Infohash: infoHash,
|
|
|
|
Seeders: models.PeerMap{},
|
|
|
|
Leechers: models.PeerMap{},
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn.PutTorrent(torrent)
|
|
|
|
}
|