Make private announce test more complete and fix brittle peer order
This commit is contained in:
parent
9adf9a114c
commit
5ccb42a0c7
3 changed files with 60 additions and 29 deletions
|
@ -99,7 +99,15 @@ func checkAnnounce(p params, expected interface{}, srv *httptest.Server, t *test
|
|||
return false
|
||||
}
|
||||
|
||||
if e, ok := expected.(bencode.Dict); ok {
|
||||
sortPeersInResponse(e)
|
||||
}
|
||||
|
||||
got, err := bencode.Unmarshal(body)
|
||||
if e, ok := got.(bencode.Dict); ok {
|
||||
sortPeersInResponse(e)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(got, expected) {
|
||||
t.Errorf("\ngot: %#v\nwanted: %#v", got, expected)
|
||||
return false
|
||||
|
|
|
@ -9,7 +9,9 @@ import (
|
|||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"sort"
|
||||
|
||||
"github.com/chihaya/bencode"
|
||||
"github.com/chihaya/chihaya/config"
|
||||
|
||||
_ "github.com/chihaya/chihaya/drivers/backend/noop"
|
||||
|
@ -47,3 +49,28 @@ func announce(p params, srv *httptest.Server) ([]byte, error) {
|
|||
response.Body.Close()
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/chihaya/bencode"
|
||||
|
@ -51,30 +50,7 @@ func loadTestData(tkr *Tracker) error {
|
|||
Leechers: make(map[string]models.Peer),
|
||||
}
|
||||
|
||||
err = 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)
|
||||
return conn.PutTorrent(torrent)
|
||||
}
|
||||
|
||||
func TestPrivateAnnounce(t *testing.T) {
|
||||
|
@ -95,14 +71,34 @@ func TestPrivateAnnounce(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
srv.URL = srv.URL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv2"
|
||||
defer srv.Close()
|
||||
baseURL := srv.URL
|
||||
|
||||
peer := makePeerParams("-TR2820-peer2", true)
|
||||
expected := makeResponse(1, 2, bencode.List{
|
||||
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)
|
||||
|
||||
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"),
|
||||
})
|
||||
srv.URL = baseURL + "/users/vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv1"
|
||||
checkAnnounce(peer, expected, srv, t)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue