2014-07-06 23:56:54 +02:00
|
|
|
// 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 (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2014-07-16 02:38:17 +02:00
|
|
|
"net/url"
|
2014-07-16 01:35:18 +02:00
|
|
|
"reflect"
|
2014-07-02 03:40:29 +02:00
|
|
|
"testing"
|
|
|
|
|
2014-07-16 01:35:18 +02:00
|
|
|
"github.com/chihaya/bencode"
|
2014-07-02 03:40:29 +02:00
|
|
|
"github.com/chihaya/chihaya/config"
|
2014-07-15 06:22:04 +02:00
|
|
|
|
|
|
|
_ "github.com/chihaya/chihaya/drivers/backend/noop"
|
|
|
|
_ "github.com/chihaya/chihaya/drivers/tracker/memory"
|
2014-07-02 03:40:29 +02:00
|
|
|
)
|
|
|
|
|
2014-07-16 02:38:17 +02:00
|
|
|
type params map[string]string
|
2014-07-02 03:40:29 +02:00
|
|
|
|
2014-07-16 02:38:17 +02:00
|
|
|
const infoHash = "%89%d4%bcR%11%16%ca%1dB%a2%f3%0d%1f%27M%94%e4h%1d%af"
|
2014-07-02 03:40:29 +02:00
|
|
|
|
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 := basePeer("peer1", true)
|
2014-07-16 02:45:18 +02:00
|
|
|
expected := baseResponse(1, 0, bencode.List{})
|
2014-07-16 02:38:17 +02:00
|
|
|
checkResponse(peer, expected, srv, t)
|
|
|
|
|
|
|
|
// Add another seeder.
|
|
|
|
peer = basePeer("peer2", true)
|
2014-07-16 02:45:18 +02:00
|
|
|
expected = baseResponse(2, 0, bencode.List{})
|
2014-07-16 02:38:17 +02:00
|
|
|
checkResponse(peer, expected, srv, t)
|
|
|
|
|
|
|
|
// Add a leecher.
|
|
|
|
peer = basePeer("peer3", false)
|
2014-07-16 02:45:18 +02:00
|
|
|
expected = baseResponse(2, 1, bencode.List{
|
2014-07-16 02:38:17 +02:00
|
|
|
bencode.Dict{
|
|
|
|
"ip": "127.0.0.1",
|
|
|
|
"peer id": "peer1",
|
|
|
|
"port": int64(1234),
|
|
|
|
},
|
|
|
|
bencode.Dict{
|
|
|
|
"ip": "127.0.0.1",
|
|
|
|
"peer id": "peer2",
|
|
|
|
"port": int64(1234),
|
|
|
|
},
|
|
|
|
})
|
2014-07-02 03:40:29 +02:00
|
|
|
|
2014-07-16 02:38:17 +02:00
|
|
|
checkResponse(peer, expected, srv, t)
|
|
|
|
|
|
|
|
// Remove seeder.
|
|
|
|
peer = basePeer("peer1", true)
|
|
|
|
peer["event"] = "stopped"
|
|
|
|
expected = baseResponse(1, 1, nil)
|
|
|
|
checkResponse(peer, expected, srv, t)
|
|
|
|
|
|
|
|
// Check seeders.
|
|
|
|
peer = basePeer("peer3", false)
|
2014-07-16 02:45:18 +02:00
|
|
|
expected = baseResponse(1, 1, bencode.List{
|
2014-07-16 02:38:17 +02:00
|
|
|
bencode.Dict{
|
|
|
|
"ip": "127.0.0.1",
|
|
|
|
"peer id": "peer2",
|
|
|
|
"port": int64(1234),
|
|
|
|
},
|
|
|
|
})
|
2014-07-02 03:40:29 +02:00
|
|
|
|
2014-07-16 02:38:17 +02:00
|
|
|
checkResponse(peer, expected, srv, t)
|
|
|
|
}
|
2014-07-02 03:40:29 +02:00
|
|
|
|
2014-07-16 02:38:17 +02:00
|
|
|
func basePeer(id string, seed bool) params {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 02:45:18 +02:00
|
|
|
func baseResponse(seeders, leechers int64, peers bencode.List) bencode.Dict {
|
2014-07-16 02:38:17 +02:00
|
|
|
dict := bencode.Dict{
|
|
|
|
"complete": seeders,
|
|
|
|
"incomplete": leechers,
|
2014-07-16 01:35:18 +02:00
|
|
|
"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 01:35:18 +02:00
|
|
|
}
|
2014-07-16 02:38:17 +02:00
|
|
|
return dict
|
|
|
|
}
|
2014-07-16 01:35:18 +02:00
|
|
|
|
2014-07-16 02:38:17 +02:00
|
|
|
func checkResponse(p params, expected interface{}, srv *httptest.Server, t *testing.T) bool {
|
|
|
|
values := &url.Values{}
|
|
|
|
for k, v := range p {
|
|
|
|
values.Add(k, v)
|
2014-07-16 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 02:38:17 +02:00
|
|
|
response, err := http.Get(srv.URL + "/announce?" + values.Encode())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return false
|
2014-07-16 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 02:38:17 +02:00
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
response.Body.Close()
|
|
|
|
|
2014-07-16 01:35:18 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2014-07-16 02:38:17 +02:00
|
|
|
return false
|
2014-07-16 01:35:18 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 02:38:17 +02:00
|
|
|
got, err := bencode.Unmarshal(body)
|
2014-07-16 01:35:18 +02:00
|
|
|
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 01:35:18 +02:00
|
|
|
}
|
2014-07-16 02:38:17 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupTracker(cfg *config.Config) (*httptest.Server, error) {
|
|
|
|
tkr, err := NewTracker(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return httptest.NewServer(setupRoutes(tkr, cfg)), nil
|
2014-07-02 03:40:29 +02:00
|
|
|
}
|