tracker/http/private_announce_test.go

181 lines
3.8 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 (
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
2014-07-16 03:35:33 +02:00
"net/url"
2014-07-16 02:38:17 +02:00
"reflect"
"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"
)
func loadTestData(tkr *Tracker) (err error) {
2014-07-16 03:14:20 +02:00
conn, err := tkr.tp.Get()
if err != nil {
return
}
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 {
return
}
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 {
return
}
2014-07-16 02:38:17 +02:00
2014-07-16 03:35:33 +02:00
hash, err := url.QueryUnescape(infoHash)
if err != nil {
return
}
2014-07-16 03:14:20 +02:00
torrent := &models.Torrent{
ID: 1,
2014-07-16 03:35:33 +02:00
Infohash: hash,
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
2014-07-16 03:14:20 +02:00
err = conn.PutTorrent(torrent)
if err != nil {
return
}
2014-07-16 02:38:17 +02:00
2014-07-16 03:14:20 +02:00
err = conn.PutLeecher(torrent.Infohash, &models.Peer{
2014-07-16 03:21:42 +02:00
ID: "-TR2820-vvvvvvvvvvv1",
2014-07-16 03:14:20 +02:00
UserID: 1,
TorrentID: torrent.ID,
IP: net.ParseIP("127.0.0.1"),
Port: 34000,
Left: 0,
})
if err != nil {
2014-07-16 02:38:17 +02:00
return
2014-07-16 03:14:20 +02:00
}
err = conn.PutLeecher(torrent.Infohash, &models.Peer{
2014-07-16 03:21:42 +02:00
ID: "-TR2820-vvvvvvvvvvv3",
2014-07-16 03:14:20 +02:00
UserID: 3,
TorrentID: torrent.ID,
2014-07-16 03:21:42 +02:00
IP: net.ParseIP("::1"),
2014-07-16 03:14:20 +02:00
Port: 34000,
Left: 0,
2014-07-16 02:38:17 +02:00
})
2014-07-16 03:14:20 +02:00
return
2014-07-16 02:38:17 +02:00
}
2014-07-16 03:35:33 +02:00
func testRoute(cfg *config.Config, path string) ([]byte, error) {
2014-07-16 02:38:17 +02:00
tkr, err := NewTracker(cfg)
if err != nil {
return nil, err
}
err = loadTestData(tkr)
if err != nil {
return nil, err
}
srv := httptest.NewServer(setupRoutes(tkr, cfg))
defer srv.Close()
2014-07-16 03:35:33 +02:00
resp, err := http.Get(srv.URL + path)
2014-07-16 02:38:17 +02:00
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
return body, nil
}
func TestPrivateAnnounce(t *testing.T) {
cfg := config.DefaultConfig
cfg.Private = true
2014-07-16 03:35:33 +02:00
path := "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2/announce?info_hash=%89%d4%bcR%11%16%ca%1dB%a2%f3%0d%1f%27M%94%e4h%1d%af&peer_id=-TR2820-vvvvvvvvvvv2&port=51413&uploaded=0&downloaded=0&left=0&numwant=1&key=3c8e3319&compact=0"
2014-07-16 02:38:17 +02:00
expected := bencode.Dict{
"complete": int64(1),
"incomplete": int64(2),
"interval": int64(1800),
"min interval": int64(900),
2014-07-16 02:45:18 +02:00
"peers": bencode.List{
2014-07-16 02:38:17 +02:00
bencode.Dict{
"ip": "127.0.0.1",
2014-07-16 03:21:42 +02:00
"peer id": "-TR2820-vvvvvvvvvvv1",
2014-07-16 02:38:17 +02:00
"port": int64(34000),
},
},
}
2014-07-16 03:35:33 +02:00
response, err := testRoute(&cfg, path)
2014-07-16 02:38:17 +02:00
if err != nil {
t.Error(err)
}
got, err := bencode.Unmarshal(response)
if !reflect.DeepEqual(got, expected) {
t.Errorf("\ngot: %#v\nwanted: %#v", got, expected)
}
2014-07-16 03:35:33 +02:00
path = "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2/announce?info_hash=%89%d4%bcR%11%16%ca%1dB%a2%f3%0d%1f%27M%94%e4h%1d%af&peer_id=-TR2820-vvvvvvvvvvv2&port=51413&uploaded=0&downloaded=0&left=0&numwant=2&key=3c8e3319&compact=0"
2014-07-16 02:38:17 +02:00
expected = bencode.Dict{
"complete": int64(1),
"incomplete": int64(2),
"interval": int64(1800),
"min interval": int64(900),
2014-07-16 02:45:18 +02:00
"peers": bencode.List{
2014-07-16 02:38:17 +02:00
bencode.Dict{
"ip": "127.0.0.1",
2014-07-16 03:21:42 +02:00
"peer id": "-TR2820-vvvvvvvvvvv1",
2014-07-16 02:38:17 +02:00
"port": int64(34000),
},
bencode.Dict{
2014-07-16 03:21:42 +02:00
"ip": "::1",
"peer id": "-TR2820-vvvvvvvvvvv3",
2014-07-16 02:38:17 +02:00
"port": int64(34000),
},
},
}
2014-07-16 03:35:33 +02:00
response, err = testRoute(&cfg, path)
2014-07-16 02:38:17 +02:00
if err != nil {
t.Error(err)
}
got, err = bencode.Unmarshal(response)
if !reflect.DeepEqual(got, expected) {
t.Errorf("\ngot: %#v\nwanted: %#v", got, expected)
}
}