tracker/http/private_announce_test.go

105 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/models"
_ "github.com/chihaya/chihaya/drivers/backend/noop"
_ "github.com/chihaya/chihaya/drivers/tracker/memory"
)
2014-07-16 04:19:44 +02:00
func loadTestData(tkr *Tracker) error {
2014-07-16 03:14:20 +02:00
conn, err := tkr.tp.Get()
if err != nil {
2014-07-16 04:19:44 +02:00
return err
2014-07-16 03:14:20 +02:00
}
2014-07-16 02:38:17 +02:00
2014-07-16 03:14:20 +02:00
users := []string{
2014-07-16 03:21:42 +02:00
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1",
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2",
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3",
2014-07-16 03:14:20 +02:00
}
for i, passkey := range users {
2014-07-16 02:38:17 +02:00
err = conn.PutUser(&models.User{
2014-07-16 03:14:20 +02:00
ID: uint64(i + 1),
Passkey: passkey,
2014-07-16 02:38:17 +02:00
})
if err != nil {
2014-07-16 04:19:44 +02:00
return err
2014-07-16 02:38:17 +02:00
}
2014-07-16 03:14:20 +02:00
}
2014-07-16 02:38:17 +02:00
2014-07-16 03:14:20 +02:00
err = conn.PutClient("TR2820")
if err != nil {
2014-07-16 04:19:44 +02:00
return err
2014-07-16 03:35:33 +02:00
}
2014-07-16 03:14:20 +02:00
torrent := &models.Torrent{
ID: 1,
2014-07-16 04:19:44 +02:00
Infohash: infoHash,
2014-07-16 03:14:20 +02:00
Seeders: make(map[string]models.Peer),
Leechers: make(map[string]models.Peer),
}
2014-07-16 02:38:17 +02:00
return conn.PutTorrent(torrent)
2014-07-16 02:38:17 +02:00
}
func TestPrivateAnnounce(t *testing.T) {
cfg := config.DefaultConfig
cfg.Private = true
2014-07-16 04:19:44 +02:00
tkr, err := NewTracker(&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
}