commit 0f8fbff83c68e6b7c7db96a39b994ae288309bae Author: David Fifield david@bamsoftware.com Date: Sat Nov 9 18:33:42 2013 -0800
Add tests for resolveAddr. --- pt_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
diff --git a/pt_test.go b/pt_test.go index 793635b..01dd0ad 100644 --- a/pt_test.go +++ b/pt_test.go @@ -2,6 +2,7 @@ package pt
import ( "bytes" + "net" "os" "sort" "testing" @@ -170,6 +171,48 @@ func TestGetClientTransports(t *testing.T) { } }
+func TestResolveAddr(t *testing.T) { + badTests := [...]string{ + "", + "1.2.3.4", + "1.2.3.4:", + "9999", + ":9999", + "[1:2::3:4]", + "[1:2::3:4]:", + "[1::2::3:4]", + "1:2::3:4::9999", + "1:2:3:4::9999", + "localhost:9999", + "[localhost]:9999", + } + goodTests := [...]struct { + input string + expected net.TCPAddr + }{ + {"1.2.3.4:9999", net.TCPAddr{IP: net.ParseIP("1.2.3.4"), Port: 9999}}, + {"[1:2::3:4]:9999", net.TCPAddr{IP: net.ParseIP("1:2::3:4"), Port: 9999}}, + {"1:2::3:4:9999", net.TCPAddr{IP: net.ParseIP("1:2::3:4"), Port: 9999}}, + } + + for _, input := range badTests { + output, err := resolveAddr(input) + if err == nil { + t.Errorf("%q unexpectedly succeeded: %q", input, output) + } + } + + for _, test := range goodTests { + output, err := resolveAddr(test.input) + if err != nil { + t.Errorf("%q unexpectedly returned an error: %s", test.input, err) + } + if !output.IP.Equal(test.expected.IP) || output.Port != test.expected.Port { + t.Errorf("%q → %q (expected %q)", test.input, output, test.expected) + } + } +} + func TestReadAuthCookie(t *testing.T) { badTests := [...][]byte{ []byte(""),
tor-commits@lists.torproject.org