commit 602b719604c0a9935482bcfca5549384d1e57277 Author: David Fifield david@bamsoftware.com Date: Sat Nov 9 17:02:27 2013 -0800
Add tests for readAuthCookie. --- pt_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
diff --git a/pt_test.go b/pt_test.go index ada9ed5..44cfa54 100644 --- a/pt_test.go +++ b/pt_test.go @@ -1,5 +1,6 @@ package pt
+import "bytes" import "os" import "testing"
@@ -79,3 +80,39 @@ func TestGetManagedTransportVer(t *testing.T) { } } } + +func TestReadAuthCookie(t *testing.T) { + badTests := [...][]byte{ + []byte(""), + // bad header + []byte("! Impostor ORPort Auth Cookie !\x0a0123456789ABCDEF0123456789ABCDEF"), + // too short + []byte("! Extended ORPort Auth Cookie !\x0a0123456789ABCDEF0123456789ABCDE"), + // too long + []byte("! Extended ORPort Auth Cookie !\x0a0123456789ABCDEF0123456789ABCDEFX"), + } + goodTests := [...][]byte{ + []byte("! Extended ORPort Auth Cookie !\x0a0123456789ABCDEF0123456789ABCDEF"), + } + + for _, input := range badTests { + var buf bytes.Buffer + buf.Write(input) + _, err := readAuthCookie(&buf) + if err == nil { + t.Errorf("%q unexpectedly succeeded", input) + } + } + + for _, input := range goodTests { + var buf bytes.Buffer + buf.Write(input) + cookie, err := readAuthCookie(&buf) + if err != nil { + t.Errorf("%q unexpectedly returned an error: %s", input, err) + } + if !bytes.Equal(cookie, input[32:64]) { + t.Errorf("%q → %q (expected %q)", input, cookie, input[:32]) + } + } +}