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"
|
2014-07-17 06:09:56 +02:00
|
|
|
"github.com/chihaya/chihaya/tracker"
|
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-17 02:17:10 +02:00
|
|
|
|
2014-07-16 04:19:44 +02:00
|
|
|
defer srv.Close()
|
2014-07-16 04:44:20 +02:00
|
|
|
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
|
|
|
|
2014-07-16 04:44:20 +02:00
|
|
|
peer = makePeerParams("-TR2820-peer3", true)
|
|
|
|
expected = makeResponse(1, 2, bencode.List{
|
2014-07-16 04:19:44 +02:00
|
|
|
makePeerResponse("-TR2820-peer1"),
|
2014-07-16 04:44:20 +02:00
|
|
|
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"),
|
|
|
|
})
|
2014-07-16 04:44:20 +02:00
|
|
|
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-16 04:54:59 +02:00
|
|
|
|
|
|
|
func loadTestData(tkr *Tracker) error {
|
2014-07-17 06:09:56 +02:00
|
|
|
conn, err := tkr.Pool.Get()
|
2014-07-16 04:54:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
users := []string{
|
|
|
|
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1",
|
|
|
|
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2",
|
|
|
|
"vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3",
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, passkey := range users {
|
2014-07-17 06:09:56 +02:00
|
|
|
err = conn.PutUser(&tracker.User{
|
2014-07-16 04:54:59 +02:00
|
|
|
ID: uint64(i + 1),
|
|
|
|
Passkey: passkey,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 19:03:59 +02:00
|
|
|
err = conn.PutClient("TR2820")
|
2014-07-16 04:54:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-17 06:09:56 +02:00
|
|
|
torrent := &tracker.Torrent{
|
2014-07-16 04:54:59 +02:00
|
|
|
ID: 1,
|
|
|
|
Infohash: infoHash,
|
2014-07-17 06:09:56 +02:00
|
|
|
Seeders: make(map[string]tracker.Peer),
|
|
|
|
Leechers: make(map[string]tracker.Peer),
|
2014-07-16 04:54:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return conn.PutTorrent(torrent)
|
|
|
|
}
|