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

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


commit 25b94879100ca5416a10c7285e3195d2691e4faf
Author: David Fifield <david at bamsoftware.com>
Date:   Tue Nov 26 22:46:12 2013 -0800

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

diff --git a/pt_test.go b/pt_test.go
index 1201e8a..cd9c280 100644
--- a/pt_test.go
+++ b/pt_test.go
@@ -2,6 +2,7 @@ package pt
 
 import (
 	"bytes"
+	"encoding/binary"
 	"fmt"
 	"io/ioutil"
 	"net"
@@ -431,3 +432,41 @@ func TestExtOrSendCommand(t *testing.T) {
 		}
 	}
 }
+
+func TestExtOrSendUserAddr(t *testing.T) {
+	addrs := [...]net.TCPAddr{
+		net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 0},
+		net.TCPAddr{IP: net.ParseIP("1.2.3.4"), Port: 9999},
+		net.TCPAddr{IP: net.ParseIP("255.255.255.255"), Port: 65535},
+		net.TCPAddr{IP: net.ParseIP("::"), Port: 0},
+		net.TCPAddr{IP: net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"), Port: 65535},
+	}
+
+	for _, addr := range addrs {
+		var buf bytes.Buffer
+		err := extOrPortSendUserAddr(&buf, &addr)
+		if err != nil {
+			t.Errorf("%s unexpectedly returned an error: %s", addr, err)
+		}
+		var cmd, length uint16
+		binary.Read(&buf, binary.BigEndian, &cmd)
+		if cmd != extOrCmdUserAddr {
+			t.Errorf("%s → cmd 0x%04x (expected 0x%04x)", addr, cmd, extOrCmdUserAddr)
+		}
+		binary.Read(&buf, binary.BigEndian, &length)
+		p := make([]byte, length+1)
+		n, err := buf.Read(p)
+		if n != int(length) {
+			t.Errorf("%s said length %d but had at least %d", addr, length, n)
+		}
+		// test that parsing the address gives something equivalent to
+		// the original.
+		outputAddr, err := resolveAddr(string(p))
+		if err != nil {
+			t.Fatal(err)
+		}
+		if !tcpAddrsEqual(&addr, outputAddr) {
+			t.Errorf("%s → %s", addr, outputAddr)
+		}
+	}
+}





More information about the tor-commits mailing list