From 493d40da3d6086287bf04ed1a3c5805ed599549f Mon Sep 17 00:00:00 2001 From: cpb8010 Date: Sat, 9 Nov 2013 03:43:37 -0500 Subject: [PATCH] Added ParseQuery tests and benchmarks --- server/query_test.go | 94 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 server/query_test.go diff --git a/server/query_test.go b/server/query_test.go new file mode 100644 index 0000000..c9a2045 --- /dev/null +++ b/server/query_test.go @@ -0,0 +1,94 @@ +package server + +import ( + "net/url" + "testing" +) + +var ( + baseAddr = "https://www.subdomain.tracker.com:80/" + testInfoHash = "01234567890123456789" + testPeerId = "-TEST01-6wfG2wk6wWLc" + + ValidAnnounceArguments = []url.Values{ + 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"}}, + 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"}}, + } + + 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 + } + for mapKey, mapVal := range boxed { + // Always expect box to hold only one element + if len(mapVal) != 1 { + return false + } + if ub_mapVal, eleExists := unboxed[mapKey]; !eleExists || mapVal[0] != ub_mapVal { + return false + } + } + return true +} + +func TestValidQueries(t *testing.T) { + for parseIndex, parseVal := range ValidAnnounceArguments { + parsedQueryObj, err := parseQuery(baseAddr + "announce/?" + parseVal.Encode()) + if err != nil { + t.Error(err) + } + 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 { + parsedQueryObj, err := parseQuery(parseStr) + if err == nil { + t.Error("Should have produced error ", parseIndex) + } + if parsedQueryObj != nil { + t.Error("Should be nil after error ", parsedQueryObj, parseIndex) + } + } +} + +func BenchmarkParseQuery(b *testing.B) { + for bCount := 0; bCount < b.N; bCount++ { + for parseIndex, parseStr := range ValidAnnounceArguments { + parsedQueryObj, err := parseQuery(baseAddr + "announce/?" + parseStr.Encode()) + 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) + } + } + } +}