Make private announce test more complete and fix brittle peer order

This commit is contained in:
Justin Li 2014-07-15 22:44:20 -04:00
parent 9adf9a114c
commit 5ccb42a0c7
3 changed files with 60 additions and 29 deletions

View file

@ -99,7 +99,15 @@ func checkAnnounce(p params, expected interface{}, srv *httptest.Server, t *test
return false return false
} }
if e, ok := expected.(bencode.Dict); ok {
sortPeersInResponse(e)
}
got, err := bencode.Unmarshal(body) got, err := bencode.Unmarshal(body)
if e, ok := got.(bencode.Dict); ok {
sortPeersInResponse(e)
}
if !reflect.DeepEqual(got, expected) { if !reflect.DeepEqual(got, expected) {
t.Errorf("\ngot: %#v\nwanted: %#v", got, expected) t.Errorf("\ngot: %#v\nwanted: %#v", got, expected)
return false return false

View file

@ -9,7 +9,9 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"sort"
"github.com/chihaya/bencode"
"github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/config"
_ "github.com/chihaya/chihaya/drivers/backend/noop" _ "github.com/chihaya/chihaya/drivers/backend/noop"
@ -47,3 +49,28 @@ func announce(p params, srv *httptest.Server) ([]byte, error) {
response.Body.Close() response.Body.Close()
return body, err return body, err
} }
type peerList bencode.List
func (p peerList) Len() int {
return len(p)
}
func (p peerList) Less(i, j int) bool {
if peer1, ok := p[i].(bencode.Dict); ok {
if peer2, ok := p[j].(bencode.Dict); ok {
return peer1["peer id"].(string) < peer2["peer id"].(string)
}
}
return false
}
func (p peerList) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func sortPeersInResponse(dict bencode.Dict) {
if peers, ok := dict["peers"].(bencode.List); ok {
sort.Stable(peerList(peers))
}
}

View file

@ -5,7 +5,6 @@
package http package http
import ( import (
"net"
"testing" "testing"
"github.com/chihaya/bencode" "github.com/chihaya/bencode"
@ -51,30 +50,7 @@ func loadTestData(tkr *Tracker) error {
Leechers: make(map[string]models.Peer), Leechers: make(map[string]models.Peer),
} }
err = conn.PutTorrent(torrent) return conn.PutTorrent(torrent)
if err != nil {
return err
}
peer := &models.Peer{
ID: "-TR2820-peer1",
UserID: 1,
TorrentID: torrent.ID,
IP: net.ParseIP("127.0.0.1"),
Port: 1234,
Left: 0,
}
err = conn.PutLeecher(torrent.Infohash, peer)
if err != nil {
return err
}
peer = &*peer
peer.ID = "-TR2820-peer3"
peer.UserID = 3
return conn.PutLeecher(torrent.Infohash, peer)
} }
func TestPrivateAnnounce(t *testing.T) { func TestPrivateAnnounce(t *testing.T) {
@ -95,14 +71,34 @@ func TestPrivateAnnounce(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
srv.URL = srv.URL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2"
defer srv.Close() defer srv.Close()
baseURL := srv.URL
peer := makePeerParams("-TR2820-peer2", true) peer := makePeerParams("-TR2820-peer1", false)
expected := makeResponse(1, 2, bencode.List{ 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"), makePeerResponse("-TR2820-peer1"),
})
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2"
checkAnnounce(peer, expected, srv, t)
peer = makePeerParams("-TR2820-peer3", true)
expected = makeResponse(1, 2, bencode.List{
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"),
makePeerResponse("-TR2820-peer3"), makePeerResponse("-TR2820-peer3"),
}) })
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
checkAnnounce(peer, expected, srv, t) checkAnnounce(peer, expected, srv, t)
} }