tracker/http/announce_test.go

106 lines
2.3 KiB
Go
Raw Normal View History

// Copyright 2014 The Chihaya Authors. All rights reserved.
2014-07-02 03:40:29 +02:00
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package http
import (
"net/http/httptest"
"reflect"
2014-07-02 03:40:29 +02:00
"testing"
"github.com/chihaya/bencode"
2014-07-02 03:40:29 +02:00
"github.com/chihaya/chihaya/config"
)
2014-07-16 02:38:17 +02:00
func TestPublicAnnounce(t *testing.T) {
srv, _ := setupTracker(&config.DefaultConfig)
defer srv.Close()
2014-07-02 03:40:29 +02:00
2014-07-16 02:38:17 +02:00
// Add one seeder.
peer := makePeerParams("peer1", true)
expected := makeResponse(1, 0, bencode.List{})
2014-07-16 03:07:33 +02:00
checkAnnounce(peer, expected, srv, t)
2014-07-16 02:38:17 +02:00
// Add another seeder.
peer = makePeerParams("peer2", true)
expected = makeResponse(2, 0, bencode.List{})
2014-07-16 03:07:33 +02:00
checkAnnounce(peer, expected, srv, t)
2014-07-16 02:38:17 +02:00
// Add a leecher.
peer = makePeerParams("peer3", false)
expected = makeResponse(2, 1, bencode.List{
makePeerResponse("peer1"),
makePeerResponse("peer2"),
2014-07-16 02:38:17 +02:00
})
2014-07-16 03:07:33 +02:00
checkAnnounce(peer, expected, srv, t)
2014-07-16 02:38:17 +02:00
// Remove seeder.
peer = makePeerParams("peer1", true)
2014-07-16 02:38:17 +02:00
peer["event"] = "stopped"
expected = makeResponse(1, 1, nil)
2014-07-16 03:07:33 +02:00
checkAnnounce(peer, expected, srv, t)
2014-07-16 02:38:17 +02:00
// Check seeders.
peer = makePeerParams("peer3", false)
expected = makeResponse(1, 1, bencode.List{
makePeerResponse("peer2"),
2014-07-16 02:38:17 +02:00
})
2014-07-16 03:07:33 +02:00
checkAnnounce(peer, expected, srv, t)
2014-07-16 02:38:17 +02:00
}
2014-07-02 03:40:29 +02:00
func makePeerParams(id string, seed bool) params {
2014-07-16 02:38:17 +02:00
left := "1"
if seed {
left = "0"
2014-07-02 03:40:29 +02:00
}
2014-07-16 02:38:17 +02:00
return params{
"info_hash": infoHash,
"peer_id": id,
"port": "1234",
"uploaded": "0",
"downloaded": "0",
"left": left,
"compact": "0",
"numwant": "50",
2014-07-02 03:40:29 +02:00
}
}
func makePeerResponse(id string) bencode.Dict {
return bencode.Dict{
"ip": "127.0.0.1",
"peer id": id,
"port": int64(1234),
}
}
func makeResponse(seeders, leechers int64, peers bencode.List) bencode.Dict {
2014-07-16 02:38:17 +02:00
dict := bencode.Dict{
"complete": seeders,
"incomplete": leechers,
"interval": int64(1800),
"min interval": int64(900),
}
2014-07-02 03:40:29 +02:00
2014-07-16 02:38:17 +02:00
if peers != nil {
dict["peers"] = peers
}
2014-07-16 02:38:17 +02:00
return dict
}
2014-07-16 03:07:33 +02:00
func checkAnnounce(p params, expected interface{}, srv *httptest.Server, t *testing.T) bool {
body, err := announce(p, srv)
if err != nil {
t.Error(err)
2014-07-16 02:38:17 +02:00
return false
}
2014-07-16 02:38:17 +02:00
got, err := bencode.Unmarshal(body)
if !reflect.DeepEqual(got, expected) {
t.Errorf("\ngot: %#v\nwanted: %#v", got, expected)
2014-07-16 02:38:17 +02:00
return false
}
2014-07-16 02:38:17 +02:00
return true
}