Commit cabc0101 authored by David Fifield's avatar David Fifield
Browse files

Omit USERADDR and TRANSPORT if the corresponding argument is empty.

parent b0a3458e
Loading
Loading
Loading
Loading
+11 −2
Changes for pt.go: 11 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -738,19 +738,24 @@ 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

	if addr != "" {
		err = extOrPortSendUserAddr(s, addr)
		if err != nil {
			return err
		}
	}
	if methodName != "" {
		err = extOrPortSendTransport(s, methodName)
		if err != nil {
			return err
		}
	}
	err = extOrPortSendDone(s)
	if err != nil {
		return err
@@ -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)
+56 −0
Changes for pt_test.go: 56 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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)
}