commit 8821313485f1da2281b7763991a8042303630cbd Author: David Fifield david@bamsoftware.com Date: Sat Dec 7 21:25:34 2013 -0800
Add tests for sendSocks4aResponse. --- socks_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
diff --git a/socks_test.go b/socks_test.go index bb638b5..2fb8a8c 100644 --- a/socks_test.go +++ b/socks_test.go @@ -107,3 +107,40 @@ func TestReadSocks4aConnect(t *testing.T) { } } } + +func TestSendSocks4aResponse(t *testing.T) { + tests := [...]struct { + code byte + addr net.TCPAddr + expected []byte + }{ + { + socksRequestGranted, + net.TCPAddr{IP: net.ParseIP("1.2.3.4"), Port: 0x1234}, + []byte("\x00\x5a\x12\x34\x01\x02\x03\x04"), + }, + { + socksRequestRejected, + net.TCPAddr{IP: net.ParseIP("1:2::3:4"), Port: 0x1234}, + []byte("\x00\x5b\x12\x34\x00\x00\x00\x00"), + }, + } + + for _, test := range tests { + var buf bytes.Buffer + err := sendSocks4aResponse(&buf, test.code, &test.addr) + if err != nil { + t.Errorf("0x%02x %s unexpectedly returned an error: %s", test.code, test.addr, err) + } + p := make([]byte, 1024) + n, err := buf.Read(p) + if err != nil { + t.Fatal(err) + } + output := p[:n] + if !bytes.Equal(output, test.expected) { + t.Errorf("0x%02x %s → %v (expected %v)", + test.code, test.addr, output, test.expected) + } + } +}