[tor-commits] [goptlib/master] Add tests for extOrPortRecvCommand.

dcf at torproject.org dcf at torproject.org
Wed Nov 27 07:51:00 UTC 2013


commit 8a12de97587e4a828381c30d8b80526b8a3a31fa
Author: David Fifield <david at bamsoftware.com>
Date:   Tue Nov 26 23:46:53 2013 -0800

    Add tests for extOrPortRecvCommand.
---
 pt_test.go |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)

diff --git a/pt_test.go b/pt_test.go
index 4579fbd..6dc8845 100644
--- a/pt_test.go
+++ b/pt_test.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"encoding/binary"
 	"fmt"
+	"io"
 	"io/ioutil"
 	"net"
 	"os"
@@ -518,3 +519,71 @@ func TestExtOrPortSendDone(t *testing.T) {
 		t.Errorf("→ %s (expected %s)", fmtBytes(output), fmtBytes(expected))
 	}
 }
+
+func TestExtOrPortRecvCommand(t *testing.T) {
+	badTests := [...][]byte{
+		[]byte(""),
+		[]byte("\x12"),
+		[]byte("\x12\x34"),
+		[]byte("\x12\x34\x00"),
+		[]byte("\x12\x34\x00\x01"),
+	}
+	goodTests := [...]struct {
+		input []byte
+		cmd uint16
+		body []byte
+		leftover []byte
+	}{
+		{
+			[]byte("\x12\x34\x00\x00"),
+			0x1234, []byte(""), []byte(""),
+		},
+		{
+			[]byte("\x12\x34\x00\x00more"),
+			0x1234, []byte(""), []byte("more"),
+		},
+		{
+			[]byte("\x12\x34\x00\x04body"),
+			0x1234, []byte("body"), []byte(""),
+		},
+		{
+			[]byte("\x12\x34\x00\x04bodymore"),
+			0x1234, []byte("body"), []byte("more"),
+		},
+	}
+
+	for _, input := range badTests {
+		var buf bytes.Buffer
+		buf.Write(input)
+		_, _, err := extOrPortRecvCommand(&buf)
+		if err == nil {
+			t.Errorf("%q unexpectedly succeeded", fmtBytes(input))
+		}
+	}
+
+	for _, test := range goodTests {
+		var buf bytes.Buffer
+		buf.Write(test.input)
+		cmd, body, err := extOrPortRecvCommand(&buf)
+		if err != nil {
+			t.Errorf("%s unexpectedly returned an error: %s", fmtBytes(test.input), err)
+		}
+		if cmd != test.cmd {
+			t.Errorf("%s → cmd 0x%04x (expected 0x%04x)", fmtBytes(test.input), cmd, test.cmd)
+		}
+		if !bytes.Equal(body, test.body) {
+			t.Errorf("%s → body %s (expected %s)", fmtBytes(test.input),
+				fmtBytes(body), fmtBytes(test.body))
+		}
+		p := make([]byte, 1024)
+		n, err := buf.Read(p)
+		if err != nil && err != io.EOF {
+			t.Fatal(err)
+		}
+		leftover := p[:n]
+		if !bytes.Equal(leftover, test.leftover) {
+			t.Errorf("%s → leftover %s (expected %s)", fmtBytes(test.input),
+				fmtBytes(leftover), fmtBytes(test.leftover))
+		}
+	}
+}





More information about the tor-commits mailing list