[tor-commits] [snowflake/master] more complete and improved client rendezvous tests

serene at torproject.org serene at torproject.org
Wed May 18 20:21:09 UTC 2016


commit 00196bbd7499fb0a2a909dcf8d3f81deaeecd3f6
Author: Serene Han <keroserene+git at gmail.com>
Date:   Wed May 18 13:08:10 2016 -0700

    more complete and improved client rendezvous tests
---
 client/client_test.go | 43 +++++++++++++++++++++++++++++++++++--------
 client/rendezvous.go  | 32 +++++++++++++++++++++-----------
 client/snowflake.go   |  3 ++-
 3 files changed, 58 insertions(+), 20 deletions(-)

diff --git a/client/client_test.go b/client/client_test.go
index 733242b..41dc5e6 100644
--- a/client/client_test.go
+++ b/client/client_test.go
@@ -35,13 +35,14 @@ func (m *MockResponse) Close() error {
 }
 
 type MockTransport struct {
+	statusOverride int
 }
 
 // Just returns a response with fake SDP answer.
 func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
 	s := ioutil.NopCloser(strings.NewReader(`{"type":"answer","sdp":"fake"}`))
 	r := &http.Response{
-		StatusCode: http.StatusOK,
+		StatusCode: m.statusOverride,
 		Body:       s,
 	}
 	return r, nil
@@ -119,16 +120,19 @@ func TestConnect(t *testing.T) {
 	})
 
 	Convey("Rendezvous", t, func() {
+		webrtc.SetLoggingVerbosity(0)
+		transport := &MockTransport{http.StatusOK}
+		fakeOffer := webrtc.DeserializeSessionDescription("test")
 
 		Convey("BrokerChannel with no front domain", func() {
-			b := NewBrokerChannel("test.broker", "")
+			b := NewBrokerChannel("test.broker", "", transport)
 			So(b.url, ShouldNotBeNil)
 			So(b.url.Path, ShouldResemble, "test.broker")
 			So(b.transport, ShouldNotBeNil)
 		})
 
 		Convey("BrokerChannel with front domain", func() {
-			b := NewBrokerChannel("test.broker", "front")
+			b := NewBrokerChannel("test.broker", "front", transport)
 			So(b.url, ShouldNotBeNil)
 			So(b.url.Path, ShouldResemble, "test.broker")
 			So(b.url.Host, ShouldResemble, "front")
@@ -136,15 +140,38 @@ func TestConnect(t *testing.T) {
 		})
 
 		Convey("BrokerChannel Negotiate responds with answer", func() {
-			b := NewBrokerChannel("test.broker", "")
-			sdp := webrtc.DeserializeSessionDescription("test")
-			// Replace transport with a mock.
-			b.transport = &MockTransport{}
-			answer, err := b.Negotiate(sdp)
+			b := NewBrokerChannel("test.broker", "", transport)
+			answer, err := b.Negotiate(fakeOffer)
 			So(err, ShouldBeNil)
 			So(answer, ShouldNotBeNil)
 			So(answer.Sdp, ShouldResemble, "fake")
 		})
 
+		Convey("BrokerChannel Negotiate fails with 503", func() {
+			b := NewBrokerChannel("test.broker", "",
+				&MockTransport{http.StatusServiceUnavailable})
+			answer, err := b.Negotiate(fakeOffer)
+			So(err, ShouldNotBeNil)
+			So(answer, ShouldBeNil)
+			So(err.Error(), ShouldResemble, BrokerError503)
+		})
+
+		Convey("BrokerChannel Negotiate fails with 400", func() {
+			b := NewBrokerChannel("test.broker", "",
+				&MockTransport{http.StatusBadRequest})
+			answer, err := b.Negotiate(fakeOffer)
+			So(err, ShouldNotBeNil)
+			So(answer, ShouldBeNil)
+			So(err.Error(), ShouldResemble, BrokerError400)
+		})
+
+		Convey("BrokerChannel Negotiate fails with unexpected", func() {
+			b := NewBrokerChannel("test.broker", "",
+				&MockTransport{123})
+			answer, err := b.Negotiate(fakeOffer)
+			So(err, ShouldNotBeNil)
+			So(answer, ShouldBeNil)
+			So(err.Error(), ShouldResemble, BrokerErrorUnexpected)
+		})
 	})
 }
diff --git a/client/rendezvous.go b/client/rendezvous.go
index 20948af..85549d3 100644
--- a/client/rendezvous.go
+++ b/client/rendezvous.go
@@ -14,19 +14,34 @@ import (
 	"github.com/keroserene/go-webrtc"
 )
 
+const (
+	BrokerError503        string = "No snowflake proxies currently available."
+	BrokerError400        string = "You sent an invalid offer in the request."
+	BrokerErrorUnexpected string = "Unexpected error, no answer."
+)
+
 // Signalling Channel to the Broker.
 type BrokerChannel struct {
 	// The Host header to put in the HTTP request (optional and may be
 	// different from the host name in URL).
-	Host string
-	url  *url.URL
+	Host      string
+	url       *url.URL
 	transport http.RoundTripper // Used to make all requests.
 }
 
+// We make a copy of DefaultTransport because we want the default Dial
+// and TLSHandshakeTimeout settings. But we want to disable the default
+// ProxyFromEnvironment setting.
+func CreateBrokerTransport() http.RoundTripper {
+	transport := http.DefaultTransport.(*http.Transport)
+	transport.Proxy = nil
+	return transport
+}
+
 // Construct a new BrokerChannel, where:
 // |broker| is the full URL of the facilitating program which assigns proxies
 // to clients, and |front| is the option fronting domain.
-func NewBrokerChannel(broker string, front string) *BrokerChannel {
+func NewBrokerChannel(broker string, front string, transport http.RoundTripper) *BrokerChannel {
 	targetURL, err := url.Parse(broker)
 	if nil != err {
 		return nil
@@ -38,11 +53,6 @@ func NewBrokerChannel(broker string, front string) *BrokerChannel {
 		bc.url.Host = front
 	}
 
-	// We make a copy of DefaultTransport because we want the default Dial
-	// and TLSHandshakeTimeout settings. But we want to disable the default
-	// ProxyFromEnvironment setting.
-	transport := http.DefaultTransport.(*http.Transport)
-	transport.Proxy = nil
 	bc.transport = transport
 	return bc
 }
@@ -79,10 +89,10 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
 		return answer, nil
 
 	case http.StatusServiceUnavailable:
-		return nil, errors.New("No snowflake proxies currently available.")
+		return nil, errors.New(BrokerError503)
 	case http.StatusBadRequest:
-		return nil, errors.New("You sent an invalid offer in the request.")
+		return nil, errors.New(BrokerError400)
 	default:
-		return nil, errors.New("Unexpected error, no answer.")
+		return nil, errors.New(BrokerErrorUnexpected)
 	}
 }
diff --git a/client/snowflake.go b/client/snowflake.go
index 8a03148..f32ddc8 100644
--- a/client/snowflake.go
+++ b/client/snowflake.go
@@ -59,7 +59,8 @@ type SnowflakeChannel interface {
 // Maintain |SnowflakeCapacity| number of available WebRTC connections, to
 // transfer to the Tor SOCKS handler when needed.
 func SnowflakeConnectLoop() {
-	broker = NewBrokerChannel(brokerURL, frontDomain)
+	transport := CreateBrokerTransport()
+	broker = NewBrokerChannel(brokerURL, frontDomain, transport)
 	for {
 		numRemotes := len(webrtcRemotes)
 		if numRemotes >= SnowflakeCapacity {



More information about the tor-commits mailing list