more complete and improved client rendezvous tests

This commit is contained in:
Serene Han 2016-05-18 13:08:10 -07:00
parent 361da32ecb
commit 00196bbd74
3 changed files with 58 additions and 20 deletions

View file

@ -35,13 +35,14 @@ func (m *MockResponse) Close() error {
} }
type MockTransport struct { type MockTransport struct {
statusOverride int
} }
// Just returns a response with fake SDP answer. // Just returns a response with fake SDP answer.
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) { func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
s := ioutil.NopCloser(strings.NewReader(`{"type":"answer","sdp":"fake"}`)) s := ioutil.NopCloser(strings.NewReader(`{"type":"answer","sdp":"fake"}`))
r := &http.Response{ r := &http.Response{
StatusCode: http.StatusOK, StatusCode: m.statusOverride,
Body: s, Body: s,
} }
return r, nil return r, nil
@ -119,16 +120,19 @@ func TestConnect(t *testing.T) {
}) })
Convey("Rendezvous", t, func() { Convey("Rendezvous", t, func() {
webrtc.SetLoggingVerbosity(0)
transport := &MockTransport{http.StatusOK}
fakeOffer := webrtc.DeserializeSessionDescription("test")
Convey("BrokerChannel with no front domain", func() { Convey("BrokerChannel with no front domain", func() {
b := NewBrokerChannel("test.broker", "") b := NewBrokerChannel("test.broker", "", transport)
So(b.url, ShouldNotBeNil) So(b.url, ShouldNotBeNil)
So(b.url.Path, ShouldResemble, "test.broker") So(b.url.Path, ShouldResemble, "test.broker")
So(b.transport, ShouldNotBeNil) So(b.transport, ShouldNotBeNil)
}) })
Convey("BrokerChannel with front domain", func() { Convey("BrokerChannel with front domain", func() {
b := NewBrokerChannel("test.broker", "front") b := NewBrokerChannel("test.broker", "front", transport)
So(b.url, ShouldNotBeNil) So(b.url, ShouldNotBeNil)
So(b.url.Path, ShouldResemble, "test.broker") So(b.url.Path, ShouldResemble, "test.broker")
So(b.url.Host, ShouldResemble, "front") So(b.url.Host, ShouldResemble, "front")
@ -136,15 +140,38 @@ func TestConnect(t *testing.T) {
}) })
Convey("BrokerChannel Negotiate responds with answer", func() { Convey("BrokerChannel Negotiate responds with answer", func() {
b := NewBrokerChannel("test.broker", "") b := NewBrokerChannel("test.broker", "", transport)
sdp := webrtc.DeserializeSessionDescription("test") answer, err := b.Negotiate(fakeOffer)
// Replace transport with a mock.
b.transport = &MockTransport{}
answer, err := b.Negotiate(sdp)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(answer, ShouldNotBeNil) So(answer, ShouldNotBeNil)
So(answer.Sdp, ShouldResemble, "fake") 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)
})
}) })
} }

View file

@ -14,19 +14,34 @@ import (
"github.com/keroserene/go-webrtc" "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. // Signalling Channel to the Broker.
type BrokerChannel struct { type BrokerChannel struct {
// The Host header to put in the HTTP request (optional and may be // The Host header to put in the HTTP request (optional and may be
// different from the host name in URL). // different from the host name in URL).
Host string Host string
url *url.URL url *url.URL
transport http.RoundTripper // Used to make all requests. 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: // Construct a new BrokerChannel, where:
// |broker| is the full URL of the facilitating program which assigns proxies // |broker| is the full URL of the facilitating program which assigns proxies
// to clients, and |front| is the option fronting domain. // 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) targetURL, err := url.Parse(broker)
if nil != err { if nil != err {
return nil return nil
@ -38,11 +53,6 @@ func NewBrokerChannel(broker string, front string) *BrokerChannel {
bc.url.Host = front 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 bc.transport = transport
return bc return bc
} }
@ -79,10 +89,10 @@ func (bc *BrokerChannel) Negotiate(offer *webrtc.SessionDescription) (
return answer, nil return answer, nil
case http.StatusServiceUnavailable: case http.StatusServiceUnavailable:
return nil, errors.New("No snowflake proxies currently available.") return nil, errors.New(BrokerError503)
case http.StatusBadRequest: case http.StatusBadRequest:
return nil, errors.New("You sent an invalid offer in the request.") return nil, errors.New(BrokerError400)
default: default:
return nil, errors.New("Unexpected error, no answer.") return nil, errors.New(BrokerErrorUnexpected)
} }
} }

View file

@ -59,7 +59,8 @@ type SnowflakeChannel interface {
// Maintain |SnowflakeCapacity| number of available WebRTC connections, to // Maintain |SnowflakeCapacity| number of available WebRTC connections, to
// transfer to the Tor SOCKS handler when needed. // transfer to the Tor SOCKS handler when needed.
func SnowflakeConnectLoop() { func SnowflakeConnectLoop() {
broker = NewBrokerChannel(brokerURL, frontDomain) transport := CreateBrokerTransport()
broker = NewBrokerChannel(brokerURL, frontDomain, transport)
for { for {
numRemotes := len(webrtcRemotes) numRemotes := len(webrtcRemotes)
if numRemotes >= SnowflakeCapacity { if numRemotes >= SnowflakeCapacity {