commit 1ef6211c6c00101e6f0355f83168681b5fd65bfc Author: David Fifield david@bamsoftware.com Date: Tue Oct 17 22:09:58 2017 -0700
Rough tests for the clientAddr function. --- server/server_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
diff --git a/server/server_test.go b/server/server_test.go new file mode 100644 index 0000000..84ac7ba --- /dev/null +++ b/server/server_test.go @@ -0,0 +1,49 @@ +package main + +import ( + "net" + "strconv" + "testing" +) + +func TestClientAddr(t *testing.T) { + // good tests + for _, test := range []struct { + input string + expected net.IP + }{ + {"1.2.3.4", net.ParseIP("1.2.3.4")}, + {"1:2::3:4", net.ParseIP("1:2::3:4")}, + } { + useraddr := clientAddr(test.input) + host, port, err := net.SplitHostPort(useraddr) + if err != nil { + t.Errorf("clientAddr(%q) → SplitHostPort error %v", test.input, err) + continue + } + if !test.expected.Equal(net.ParseIP(host)) { + t.Errorf("clientAddr(%q) → host %q, not %v", test.input, host, test.expected) + } + portNo, err := strconv.Atoi(port) + if err != nil { + t.Errorf("clientAddr(%q) → port %q", test.input, port) + continue + } + if portNo == 0 { + t.Errorf("clientAddr(%q) → port %d", test.input, portNo) + } + } + + // bad tests + for _, input := range []string{ + "", + "abc", + "1.2.3.4.5", + "[12::34]", + } { + useraddr := clientAddr(input) + if useraddr != "" { + t.Errorf("clientAddr(%q) → %q, not %q", input, useraddr, "") + } + } +}
tor-commits@lists.torproject.org