tracker/http/announce_private_test.go

104 lines
2.3 KiB
Go
Raw Normal View History

2014-07-16 02:38:17 +02:00
// Copyright 2014 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package http
import (
"testing"
"github.com/chihaya/bencode"
"github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/tracker"
2014-07-17 06:41:59 +02:00
"github.com/chihaya/chihaya/tracker/models"
2014-07-16 02:38:17 +02:00
)
func TestPrivateAnnounce(t *testing.T) {
cfg := config.DefaultConfig
cfg.Private = true
2014-07-17 06:41:59 +02:00
tkr, err := tracker.New(&cfg)
2014-07-16 02:38:17 +02:00
if err != nil {
2014-07-16 04:19:44 +02:00
t.Fatal(err)
2014-07-16 02:38:17 +02:00
}
2014-07-16 04:19:44 +02:00
err = loadTestData(tkr)
if err != nil {
t.Fatal(err)
2014-07-16 02:38:17 +02:00
}
2014-07-16 04:19:44 +02:00
srv, err := createServer(tkr, &cfg)
2014-07-16 02:38:17 +02:00
if err != nil {
2014-07-16 04:19:44 +02:00
t.Fatal(err)
2014-07-16 02:38:17 +02:00
}
2014-07-16 04:19:44 +02:00
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)
2014-07-16 04:19:44 +02:00
peer = makePeerParams("-TR2820-peer3", true)
expected = makeResponse(1, 2, bencode.List{
2014-07-16 04:19:44 +02:00
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"),
2014-07-16 04:19:44 +02:00
makePeerResponse("-TR2820-peer3"),
})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
2014-07-16 04:19:44 +02:00
checkAnnounce(peer, expected, srv, t)
2014-07-16 02:38:17 +02:00
}
2014-07-17 06:41:59 +02:00
func loadTestData(tkr *tracker.Tracker) error {
conn, err := tkr.Pool.Get()
if err != nil {
return err
}
users := []string{
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1",
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2",
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3",
}
for i, passkey := range users {
2014-07-17 06:41:59 +02:00
err = conn.PutUser(&models.User{
ID: uint64(i + 1),
Passkey: passkey,
})
if err != nil {
return err
}
}
2014-07-16 19:03:59 +02:00
err = conn.PutClient("TR2820")
if err != nil {
return err
}
2014-07-17 06:41:59 +02:00
torrent := &models.Torrent{
ID: 1,
Infohash: infoHash,
2014-07-17 06:41:59 +02:00
Seeders: make(map[string]models.Peer),
Leechers: make(map[string]models.Peer),
}
return conn.PutTorrent(torrent)
}