[tor-commits] [goptlib/master] Omit USERADDR and TRANSPORT if the corresponding argument is empty.

dcf at torproject.org dcf at torproject.org
Sun Dec 15 01:39:01 UTC 2013


commit cabc0101ab6e99decf5da82864c114308200f796
Author: David Fifield <david at bamsoftware.com>
Date:   Sat Dec 14 17:36:13 2013 -0800

    Omit USERADDR and TRANSPORT if the corresponding argument is empty.
---
 pt.go      |   25 +++++++++++++++++--------
 pt_test.go |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 8 deletions(-)

diff --git a/pt.go b/pt.go
index 1c3d77e..4b2220a 100644
--- a/pt.go
+++ b/pt.go
@@ -738,18 +738,23 @@ func extOrPortRecvCommand(s io.Reader) (cmd uint16, body []byte, err error) {
 }
 
 // Send USERADDR and TRANSPORT commands followed by a DONE command. Wait for an
-// OKAY or DENY response command from the server. Returns nil if and only if
-// OKAY is received.
+// OKAY or DENY response command from the server. If addr or methodName is "",
+// the corresponding command is not sent. Returns nil if and only if OKAY is
+// received.
 func extOrPortSetup(s io.ReadWriter, addr, methodName string) error {
 	var err error
 
-	err = extOrPortSendUserAddr(s, addr)
-	if err != nil {
-		return err
+	if addr != "" {
+		err = extOrPortSendUserAddr(s, addr)
+		if err != nil {
+			return err
+		}
 	}
-	err = extOrPortSendTransport(s, methodName)
-	if err != nil {
-		return err
+	if methodName != "" {
+		err = extOrPortSendTransport(s, methodName)
+		if err != nil {
+			return err
+		}
 	}
 	err = extOrPortSendDone(s)
 	if err != nil {
@@ -772,6 +777,10 @@ func extOrPortSetup(s io.ReadWriter, addr, methodName string) error {
 // *net.TCPConn. If connecting to the extended OR port, extended OR port
 // authentication à la 217-ext-orport-auth.txt is done before returning; an
 // error is returned if authentication fails.
+//
+// The addr and methodName arguments are put in USERADDR and TRANSPORT ExtOrPort
+// commands, respectively. If either is "", the corresponding command is not
+// sent.
 func DialOr(info *ServerInfo, addr, methodName string) (*net.TCPConn, error) {
 	if info.ExtendedOrAddr == nil || info.AuthCookie == nil {
 		return net.DialTCP("tcp", nil, info.OrAddr)
diff --git a/pt_test.go b/pt_test.go
index aa629c9..a6b7a02 100644
--- a/pt_test.go
+++ b/pt_test.go
@@ -625,3 +625,59 @@ func TestExtOrPortRecvCommand(t *testing.T) {
 		}
 	}
 }
+
+// set up so that extOrPortSetup can write to one buffer and read from another.
+type MockSetupBuf struct {
+	bytes.Buffer
+	ReadBuf bytes.Buffer
+}
+
+func (buf *MockSetupBuf) Read(p []byte) (int, error) {
+	n, err := buf.ReadBuf.Read(p)
+	return n, err
+}
+
+func testExtOrPortSetupIndividual(t *testing.T, addr, methodName string) {
+	var err error
+	var buf MockSetupBuf
+	// fake an OKAY response.
+	err = extOrPortSendCommand(&buf.ReadBuf, extOrCmdOkay, []byte{})
+	if err != nil {
+		t.Fatal()
+	}
+	err = extOrPortSetup(&buf, addr, methodName)
+	if err != nil {
+		t.Fatalf("error in extOrPortSetup: %s", err)
+	}
+	for {
+		cmd, body, err := extOrPortRecvCommand(&buf.Buffer)
+		if err != nil {
+			t.Fatalf("error in extOrPortRecvCommand: %s", err)
+		}
+		if cmd == extOrCmdDone {
+			break
+		}
+		if addr != "" && cmd == extOrCmdUserAddr {
+			if string(body) != addr {
+				t.Errorf("addr=%q methodName=%q got USERADDR with body %q (expected %q)", addr, methodName, body, addr)
+			}
+			continue
+		}
+		if methodName != "" && cmd == extOrCmdTransport {
+			if string(body) != methodName {
+				t.Errorf("addr=%q methodName=%q got TRANSPORT with body %q (expected %q)", addr, methodName, body, methodName)
+			}
+			continue
+		}
+		t.Errorf("addr=%q methodName=%q got unknown cmd %d body %q", addr, methodName, cmd, body)
+	}
+}
+
+func TestExtOrPortSetup(t *testing.T) {
+	const addr = "127.0.0.1:40000"
+	const methodName = "alpha"
+	testExtOrPortSetupIndividual(t, "", "")
+	testExtOrPortSetupIndividual(t, addr, "")
+	testExtOrPortSetupIndividual(t, "", methodName)
+	testExtOrPortSetupIndividual(t, addr, methodName)
+}



More information about the tor-commits mailing list