2016-09-02 21:53:28 +02:00
|
|
|
package bittorrent
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-09-02 21:53:28 +02:00
|
|
|
testPeerID = "-TEST01-6wfG2wk6wWLc"
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
ValidAnnounceArguments = []url.Values{
|
2016-09-02 21:53:28 +02:00
|
|
|
{},
|
2016-08-03 09:11:52 +02:00
|
|
|
{"peer_id": {testPeerID}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}},
|
|
|
|
{"peer_id": {testPeerID}, "ip": {"192.168.0.1"}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}},
|
|
|
|
{"peer_id": {testPeerID}, "ip": {"192.168.0.1"}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "numwant": {"28"}},
|
|
|
|
{"peer_id": {testPeerID}, "ip": {"192.168.0.1"}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "event": {"stopped"}},
|
|
|
|
{"peer_id": {testPeerID}, "ip": {"192.168.0.1"}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "event": {"started"}, "numwant": {"13"}},
|
|
|
|
{"peer_id": {testPeerID}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "no_peer_id": {"1"}},
|
|
|
|
{"peer_id": {testPeerID}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "compact": {"0"}, "no_peer_id": {"1"}},
|
|
|
|
{"peer_id": {testPeerID}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "compact": {"0"}, "no_peer_id": {"1"}, "key": {"peerKey"}},
|
|
|
|
{"peer_id": {testPeerID}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "compact": {"0"}, "no_peer_id": {"1"}, "key": {"peerKey"}, "trackerid": {"trackerId"}},
|
|
|
|
{"peer_id": {"%3Ckey%3A+0x90%3E"}, "port": {"6881"}, "downloaded": {"1234"}, "left": {"4321"}, "compact": {"0"}, "no_peer_id": {"1"}, "key": {"peerKey"}, "trackerid": {"trackerId"}},
|
|
|
|
{"peer_id": {"%3Ckey%3A+0x90%3E"}, "compact": {"1"}},
|
|
|
|
{"peer_id": {""}, "compact": {""}},
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidQueries = []string{
|
2016-09-02 21:53:28 +02:00
|
|
|
"/announce?" + "info_hash=%0%a",
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func mapArrayEqual(boxed map[string][]string, unboxed map[string]string) bool {
|
|
|
|
if len(boxed) != len(unboxed) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for mapKey, mapVal := range boxed {
|
|
|
|
// Always expect box to hold only one element
|
|
|
|
if len(mapVal) != 1 || mapVal[0] != unboxed[mapKey] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:53:28 +02:00
|
|
|
func TestParseEmptyURLData(t *testing.T) {
|
|
|
|
parsedQuery, err := ParseURLData("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if parsedQuery == nil {
|
|
|
|
t.Fatal("Parsed query must not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseValidURLData(t *testing.T) {
|
2016-08-03 09:11:52 +02:00
|
|
|
for parseIndex, parseVal := range ValidAnnounceArguments {
|
2016-09-02 21:53:28 +02:00
|
|
|
parsedQueryObj, err := ParseURLData("/announce?" + parseVal.Encode())
|
2016-08-03 09:11:52 +02:00
|
|
|
if err != nil {
|
2016-09-02 21:53:28 +02:00
|
|
|
t.Fatal(err)
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !mapArrayEqual(parseVal, parsedQueryObj.params) {
|
2016-09-06 06:44:13 +02:00
|
|
|
t.Fatalf("Incorrect parse at item %d.\n Expected=%v\n Received=%v\n", parseIndex, parseVal, parsedQueryObj.params)
|
2016-09-02 21:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if parsedQueryObj.path != "/announce" {
|
|
|
|
t.Fatalf("Incorrect path, expected %q, got %q", "/announce", parsedQueryObj.path)
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 21:53:28 +02:00
|
|
|
func TestParseInvalidURLData(t *testing.T) {
|
2016-08-03 09:11:52 +02:00
|
|
|
for parseIndex, parseStr := range InvalidQueries {
|
2016-09-02 21:53:28 +02:00
|
|
|
parsedQueryObj, err := ParseURLData(parseStr)
|
2016-08-03 09:11:52 +02:00
|
|
|
if err == nil {
|
2016-09-02 21:53:28 +02:00
|
|
|
t.Fatal("Should have produced error", parseIndex)
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if parsedQueryObj != nil {
|
2016-09-02 21:53:28 +02:00
|
|
|
t.Fatal("Should be nil after error", parsedQueryObj, parseIndex)
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkParseQuery(b *testing.B) {
|
|
|
|
for bCount := 0; bCount < b.N; bCount++ {
|
|
|
|
for parseIndex, parseStr := range ValidAnnounceArguments {
|
2016-09-02 21:53:28 +02:00
|
|
|
parsedQueryObj, err := parseQuery(parseStr.Encode())
|
2016-08-03 09:11:52 +02: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 {
|
2016-09-02 21:53:28 +02:00
|
|
|
parsedQueryObj, err := url.ParseQuery(parseStr.Encode())
|
2016-08-03 09:11:52 +02:00
|
|
|
if err != nil {
|
|
|
|
b.Error(err, parseIndex)
|
|
|
|
b.Log(parsedQueryObj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|