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

dcf at torproject.org dcf at torproject.org
Sun Nov 10 03:11:28 UTC 2013


commit eb036ed1a7fc9925d6734fcf2f7a72f0cb1c1f77
Author: David Fifield <david at bamsoftware.com>
Date:   Sat Nov 9 18:03:43 2013 -0800

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

diff --git a/pt_test.go b/pt_test.go
index 21cb5cb..793635b 100644
--- a/pt_test.go
+++ b/pt_test.go
@@ -3,6 +3,7 @@ package pt
 import (
 	"bytes"
 	"os"
+	"sort"
 	"testing"
 )
 
@@ -83,6 +84,92 @@ func TestGetManagedTransportVer(t *testing.T) {
 	}
 }
 
+// return true iff the two slices contain the same elements, possibly in a
+// different order.
+func stringSetsEqual(a, b []string) bool {
+	ac := make([]string, len(a))
+	bc := make([]string, len(b))
+	sort.Strings(ac)
+	sort.Strings(bc)
+	if len(ac) != len(bc) {
+		return false
+	}
+	for i := 0; i < len(ac); i++ {
+		if ac[i] != bc[i] {
+			return false
+		}
+	}
+	return true
+}
+
+func TestGetClientTransports(t *testing.T) {
+	tests := [...]struct {
+		envvar      string
+		methodNames []string
+		expected    []string
+	}{
+		{
+			"*",
+			[]string{},
+			[]string{},
+		},
+		{
+			"*",
+			[]string{"alpha", "beta", "gamma"},
+			[]string{"alpha", "beta", "gamma"},
+		},
+		{
+			"alpha,beta,gamma",
+			[]string{"alpha", "beta", "gamma"},
+			[]string{"alpha", "beta", "gamma"},
+		},
+		{
+			"alpha,beta",
+			[]string{"alpha", "beta", "gamma"},
+			[]string{"alpha", "beta"},
+		},
+		{
+			"alpha",
+			[]string{"alpha", "beta", "gamma"},
+			[]string{"alpha"},
+		},
+		{
+			"alpha,beta",
+			[]string{"alpha", "beta", "alpha"},
+			[]string{"alpha", "beta"},
+		},
+		// my reading of pt-spec.txt says that "*" has special meaning
+		// only when it is the entirety of the environment variable.
+		{
+			"alpha,*,gamma",
+			[]string{"alpha", "beta", "gamma"},
+			[]string{"alpha", "gamma"},
+		},
+		{
+			"alpha",
+			[]string{"beta"},
+			[]string{},
+		},
+	}
+
+	os.Clearenv()
+	_, err := getClientTransports([]string{"alpha", "beta", "gamma"})
+	if err == nil {
+		t.Errorf("empty environment unexpectedly succeeded")
+	}
+
+	for _, test := range tests {
+		os.Setenv("TOR_PT_CLIENT_TRANSPORTS", test.envvar)
+		output, err := getClientTransports(test.methodNames)
+		if err != nil {
+			t.Errorf("%q unexpectedly returned an error: %s", test.envvar, err)
+		}
+		if !stringSetsEqual(output, test.expected) {
+			t.Errorf("%q %q → %q (expected %q)", test.envvar, test.methodNames, output, test.expected)
+		}
+	}
+}
+
 func TestReadAuthCookie(t *testing.T) {
 	badTests := [...][]byte{
 		[]byte(""),





More information about the tor-commits mailing list