tracker/http/query/query_test.go

101 lines
4 KiB
Go
Raw Normal View History

2015-01-01 18:02:25 +01:00
// Copyright 2015 The Chihaya Authors. All rights reserved.
2014-06-24 04:47:43 +02:00
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
2014-06-24 04:55:27 +02:00
2014-06-24 04:47:43 +02:00
package query
2013-11-09 09:43:37 +01:00
import (
"net/url"
"testing"
)
var (
baseAddr = "https://www.subdomain.tracker.com:80/"
testInfoHash = "01234567890123456789"
2014-05-01 06:30:46 +02:00
testPeerID = "-TEST01-6wfG2wk6wWLc"
2013-11-09 09:43:37 +01:00
ValidAnnounceArguments = []url.Values{
2014-05-01 06:30:46 +02:00
url.Values{"info_hash": {testInfoHash}, "peer_id": {testPeerID}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}},
url.Values{"info_hash": {testInfoHash}, "peer_id": {testPeerID}, "ip": {"192.168.0.1"}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}},
url.Values{"info_hash": {testInfoHash}, "peer_id": {testPeerID}, "ip": {"192.168.0.1"}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "numwant": {"28"}},
url.Values{"info_hash": {testInfoHash}, "peer_id": {testPeerID}, "ip": {"192.168.0.1"}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "event": {"stopped"}},
url.Values{"info_hash": {testInfoHash}, "peer_id": {testPeerID}, "ip": {"192.168.0.1"}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "event": {"started"}, "numwant": {"13"}},
url.Values{"info_hash": {testInfoHash}, "peer_id": {testPeerID}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "no_peer_id": {"1"}},
url.Values{"info_hash": {testInfoHash}, "peer_id": {testPeerID}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "compact": {"0"}, "no_peer_id": {"1"}},
url.Values{"info_hash": {testInfoHash}, "peer_id": {testPeerID}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "compact": {"0"}, "no_peer_id": {"1"}, "key": {"peerKey"}},
url.Values{"info_hash": {testInfoHash}, "peer_id": {testPeerID}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "compact": {"0"}, "no_peer_id": {"1"}, "key": {"peerKey"}, "trackerid": {"trackerId"}},
2013-11-09 09:43:37 +01:00
url.Values{"info_hash": {testInfoHash}, "peer_id": {"%3Ckey%3A+0x90%3E"}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "compact": {"0"}, "no_peer_id": {"1"}, "key": {"peerKey"}, "trackerid": {"trackerId"}},
url.Values{"info_hash": {testInfoHash}, "peer_id": {"%3Ckey%3A+0x90%3E"}, "compact": {"1"}},
2015-06-18 16:15:48 +02:00
url.Values{"info_hash": {""}, "peer_id": {""}, "compact": {""}},
2013-11-09 09:43:37 +01:00
}
InvalidQueries = []string{
baseAddr + "announce/?" + "info_hash=%0%a",
}
)
func mapArrayEqual(boxed map[string][]string, unboxed map[string]string) bool {
if len(boxed) != len(unboxed) {
return false
}
2014-05-08 07:09:02 +02:00
2013-11-09 09:43:37 +01:00
for mapKey, mapVal := range boxed {
// Always expect box to hold only one element
2013-12-13 17:48:47 +01:00
if len(mapVal) != 1 || mapVal[0] != unboxed[mapKey] {
2013-11-09 09:43:37 +01:00
return false
}
}
2014-05-08 07:09:02 +02:00
2013-11-09 09:43:37 +01:00
return true
}
func TestValidQueries(t *testing.T) {
for parseIndex, parseVal := range ValidAnnounceArguments {
2014-06-24 04:47:43 +02:00
parsedQueryObj, err := New(baseAddr + "announce/?" + parseVal.Encode())
2013-11-09 09:43:37 +01:00
if err != nil {
t.Error(err)
}
2014-05-08 07:09:02 +02:00
2013-11-09 09:43:37 +01:00
if !mapArrayEqual(parseVal, parsedQueryObj.Params) {
t.Errorf("Incorrect parse at item %d.\n Expected=%v\n Recieved=%v\n", parseIndex, parseVal, parsedQueryObj.Params)
}
}
}
func TestInvalidQueries(t *testing.T) {
for parseIndex, parseStr := range InvalidQueries {
2014-06-24 04:47:43 +02:00
parsedQueryObj, err := New(parseStr)
2013-11-09 09:43:37 +01:00
if err == nil {
2013-12-13 17:48:47 +01:00
t.Error("Should have produced error", parseIndex)
2013-11-09 09:43:37 +01:00
}
2014-05-08 07:09:02 +02:00
2013-11-09 09:43:37 +01:00
if parsedQueryObj != nil {
2013-12-13 17:48:47 +01:00
t.Error("Should be nil after error", parsedQueryObj, parseIndex)
2013-11-09 09:43:37 +01:00
}
}
}
func BenchmarkParseQuery(b *testing.B) {
for bCount := 0; bCount < b.N; bCount++ {
for parseIndex, parseStr := range ValidAnnounceArguments {
2014-06-24 04:47:43 +02:00
parsedQueryObj, err := New(baseAddr + "announce/?" + parseStr.Encode())
2013-11-09 09:43:37 +01:00
if err != nil {
b.Error(err, parseIndex)
b.Log(parsedQueryObj)
}
}
}
}
func BenchmarkURLParseQuery(b *testing.B) {
for bCount := 0; bCount < b.N; bCount++ {
for parseIndex, parseStr := range ValidAnnounceArguments {
parsedQueryObj, err := url.ParseQuery(baseAddr + "announce/?" + parseStr.Encode())
if err != nil {
b.Error(err, parseIndex)
b.Log(parsedQueryObj)
}
}
}
}