[tor-commits] [goptlib/master] Refactor extOrPortSetup in DialOr.

dcf at torproject.org dcf at torproject.org
Tue Mar 5 23:22:53 UTC 2019


commit ff3defd597a05fa1cf123ed518a224bc049d84be
Author: David Fifield <david at bamsoftware.com>
Date:   Tue Mar 5 14:14:19 2019 -0700

    Refactor extOrPortSetup in DialOr.
---
 pt.go      | 40 +++++++++++++++++++++++-----------------
 pt_test.go | 24 ++++++++++++------------
 2 files changed, 35 insertions(+), 29 deletions(-)

diff --git a/pt.go b/pt.go
index c2895b8..95695ac 100644
--- a/pt.go
+++ b/pt.go
@@ -883,7 +883,7 @@ func extOrPortRecvCommand(s io.Reader) (cmd uint16, body []byte, err error) {
 // 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 {
+func extOrPortSetMetadata(s io.ReadWriter, addr, methodName string) error {
 	var err error
 
 	if addr != "" {
@@ -915,6 +915,27 @@ func extOrPortSetup(s io.ReadWriter, addr, methodName string) error {
 	return nil
 }
 
+func extOrPortSetup(s net.Conn, timeout time.Duration,
+	info *ServerInfo, addr, methodName string) error {
+	err := s.SetDeadline(time.Now().Add(5 * time.Second))
+	if err != nil {
+		return err
+	}
+	err = extOrPortAuthenticate(s, info)
+	if err != nil {
+		return err
+	}
+	err = extOrPortSetMetadata(s, addr, methodName)
+	if err != nil {
+		return err
+	}
+	err = s.SetDeadline(time.Time{})
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
 // Dial info.ExtendedOrAddr if defined, or else info.OrAddr, and return an open
 // *net.TCPConn. If connecting to the extended OR port, extended OR port
 // authentication à la 217-ext-orport-auth.txt is done before returning; an
@@ -932,22 +953,7 @@ func DialOr(info *ServerInfo, addr, methodName string) (*net.TCPConn, error) {
 	if err != nil {
 		return nil, err
 	}
-	err = s.SetDeadline(time.Now().Add(5 * time.Second))
-	if err != nil {
-		s.Close()
-		return nil, err
-	}
-	err = extOrPortAuthenticate(s, info)
-	if err != nil {
-		s.Close()
-		return nil, err
-	}
-	err = extOrPortSetup(s, addr, methodName)
-	if err != nil {
-		s.Close()
-		return nil, err
-	}
-	err = s.SetDeadline(time.Time{})
+	err = extOrPortSetup(s, 5*time.Second, info, addr, methodName)
 	if err != nil {
 		s.Close()
 		return nil, err
diff --git a/pt_test.go b/pt_test.go
index f9f4790..ce26b16 100644
--- a/pt_test.go
+++ b/pt_test.go
@@ -704,28 +704,28 @@ func TestExtOrPortRecvCommand(t *testing.T) {
 	}
 }
 
-// set up so that extOrPortSetup can write to one buffer and read from another.
-type mockSetupBuf struct {
+// set up so that extOrPortSetMetadata can write to one buffer and read from another.
+type mockSetMetadataBuf struct {
 	bytes.Buffer
 	ReadBuf bytes.Buffer
 }
 
-func (buf *mockSetupBuf) Read(p []byte) (int, error) {
+func (buf *mockSetMetadataBuf) Read(p []byte) (int, error) {
 	n, err := buf.ReadBuf.Read(p)
 	return n, err
 }
 
-func testExtOrPortSetupIndividual(t *testing.T, addr, methodName string) {
+func testExtOrPortSetMetadataIndividual(t *testing.T, addr, methodName string) {
 	var err error
-	var buf mockSetupBuf
+	var buf mockSetMetadataBuf
 	// fake an OKAY response.
 	err = extOrPortSendCommand(&buf.ReadBuf, extOrCmdOkay, []byte{})
 	if err != nil {
 		t.Fatal(err)
 	}
-	err = extOrPortSetup(&buf, addr, methodName)
+	err = extOrPortSetMetadata(&buf, addr, methodName)
 	if err != nil {
-		t.Fatalf("error in extOrPortSetup: %s", err)
+		t.Fatalf("error in extOrPortSetMetadata: %s", err)
 	}
 	for {
 		cmd, body, err := extOrPortRecvCommand(&buf.Buffer)
@@ -751,13 +751,13 @@ func testExtOrPortSetupIndividual(t *testing.T, addr, methodName string) {
 	}
 }
 
-func TestExtOrPortSetup(t *testing.T) {
+func TestExtOrPortSetMetadata(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)
+	testExtOrPortSetMetadataIndividual(t, "", "")
+	testExtOrPortSetMetadataIndividual(t, addr, "")
+	testExtOrPortSetMetadataIndividual(t, "", methodName)
+	testExtOrPortSetMetadataIndividual(t, addr, methodName)
 }
 
 func TestMakeStateDir(t *testing.T) {





More information about the tor-commits mailing list