More test cleanup

This commit is contained in:
Justin Li 2014-07-15 22:19:44 -04:00
parent 71cd410116
commit 9adf9a114c
4 changed files with 45 additions and 107 deletions

View file

@ -14,7 +14,10 @@ import (
)
func TestPublicAnnounce(t *testing.T) {
srv, _ := setupTracker(&config.DefaultConfig)
srv, err := setupTracker(&config.DefaultConfig)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
// Add one seeder.

View file

@ -18,13 +18,17 @@ import (
type params map[string]string
const infoHash = "%89%d4%bcR%11%16%ca%1dB%a2%f3%0d%1f%27M%94%e4h%1d%af"
var infoHash = string([]byte{0x89, 0xd4, 0xbc, 0x52, 0x11, 0x16, 0xca, 0x1d, 0x42, 0xa2, 0xf3, 0x0d, 0x1f, 0x27, 0x4d, 0x94, 0xe4, 0x68, 0x1d, 0xaf})
func setupTracker(cfg *config.Config) (*httptest.Server, error) {
tkr, err := NewTracker(cfg)
if err != nil {
return nil, err
}
return createServer(tkr, cfg)
}
func createServer(tkr *Tracker, cfg *config.Config) (*httptest.Server, error) {
return httptest.NewServer(setupRoutes(tkr, cfg)), nil
}

View file

@ -5,12 +5,7 @@
package http
import (
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"github.com/chihaya/bencode"
@ -21,10 +16,10 @@ import (
_ "github.com/chihaya/chihaya/drivers/tracker/memory"
)
func loadTestData(tkr *Tracker) (err error) {
func loadTestData(tkr *Tracker) error {
conn, err := tkr.tp.Get()
if err != nil {
return
return err
}
users := []string{
@ -40,141 +35,74 @@ func loadTestData(tkr *Tracker) (err error) {
})
if err != nil {
return
return err
}
}
err = conn.PutClient("TR2820")
if err != nil {
return
}
hash, err := url.QueryUnescape(infoHash)
if err != nil {
return
return err
}
torrent := &models.Torrent{
ID: 1,
Infohash: hash,
Infohash: infoHash,
Seeders: make(map[string]models.Peer),
Leechers: make(map[string]models.Peer),
}
err = conn.PutTorrent(torrent)
if err != nil {
return
return err
}
err = conn.PutLeecher(torrent.Infohash, &models.Peer{
ID: "-TR2820-vvvvvvvvvvv1",
peer := &models.Peer{
ID: "-TR2820-peer1",
UserID: 1,
TorrentID: torrent.ID,
IP: net.ParseIP("127.0.0.1"),
Port: 34000,
Port: 1234,
Left: 0,
})
if err != nil {
return
}
err = conn.PutLeecher(torrent.Infohash, &models.Peer{
ID: "-TR2820-vvvvvvvvvvv3",
UserID: 3,
TorrentID: torrent.ID,
IP: net.ParseIP("::1"),
Port: 34000,
Left: 0,
})
return
}
func testRoute(cfg *config.Config, path string) ([]byte, error) {
tkr, err := NewTracker(cfg)
err = conn.PutLeecher(torrent.Infohash, peer)
if err != nil {
return nil, err
return err
}
err = loadTestData(tkr)
if err != nil {
return nil, err
}
peer = &*peer
peer.ID = "-TR2820-peer3"
peer.UserID = 3
srv := httptest.NewServer(setupRoutes(tkr, cfg))
defer srv.Close()
resp, err := http.Get(srv.URL + path)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
return body, nil
return conn.PutLeecher(torrent.Infohash, peer)
}
func TestPrivateAnnounce(t *testing.T) {
cfg := config.DefaultConfig
cfg.Private = true
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"
expected := bencode.Dict{
"complete": int64(1),
"incomplete": int64(2),
"interval": int64(1800),
"min interval": int64(900),
"peers": bencode.List{
bencode.Dict{
"ip": "127.0.0.1",
"peer id": "-TR2820-vvvvvvvvvvv1",
"port": int64(34000),
},
},
}
response, err := testRoute(&cfg, path)
tkr, err := NewTracker(&cfg)
if err != nil {
t.Error(err)
}
got, err := bencode.Unmarshal(response)
if !reflect.DeepEqual(got, expected) {
t.Errorf("\ngot: %#v\nwanted: %#v", got, expected)
t.Fatal(err)
}
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"
expected = bencode.Dict{
"complete": int64(1),
"incomplete": int64(2),
"interval": int64(1800),
"min interval": int64(900),
"peers": bencode.List{
bencode.Dict{
"ip": "127.0.0.1",
"peer id": "-TR2820-vvvvvvvvvvv1",
"port": int64(34000),
},
bencode.Dict{
"ip": "::1",
"peer id": "-TR2820-vvvvvvvvvvv3",
"port": int64(34000),
},
},
}
response, err = testRoute(&cfg, path)
err = loadTestData(tkr)
if err != nil {
t.Error(err)
t.Fatal(err)
}
got, err = bencode.Unmarshal(response)
if !reflect.DeepEqual(got, expected) {
t.Errorf("\ngot: %#v\nwanted: %#v", got, expected)
srv, err := createServer(tkr, &cfg)
if err != nil {
t.Fatal(err)
}
srv.URL = srv.URL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2"
defer srv.Close()
peer := makePeerParams("-TR2820-peer2", true)
expected := makeResponse(1, 2, bencode.List{
makePeerResponse("-TR2820-peer1"),
makePeerResponse("-TR2820-peer3"),
})
checkAnnounce(peer, expected, srv, t)
}

View file

@ -17,7 +17,10 @@ import (
)
func TestPublicScrape(t *testing.T) {
srv, _ := setupTracker(&config.DefaultConfig)
srv, err := setupTracker(&config.DefaultConfig)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
scrapeParams := params{