Esempio n. 1
0
func doTestTLS(buffered bool, t *testing.T) {
	startServers(t, false)

	_, counter, err := fdcount.Matching("TCP")
	if err != nil {
		t.Fatalf("Unable to get fdcount: %v", err)
	}

	conn, err := prepareConn(httpsAddr, buffered, false, t, nil)
	if err != nil {
		t.Fatalf("Unable to prepareConn: %s", err)
	}

	tlsConn := tls.Client(conn, &tls.Config{
		ServerName: "localhost",
		RootCAs:    cert.PoolContainingCert(),
	})
	defer func() {
		err := conn.Close()
		assert.Nil(t, err, "Closing conn should succeed")
		if !assert.NoError(t, counter.AssertDelta(2), "All file descriptors except the connection from proxy to destination site should have been closed") {
			DumpConnTrace()
		}
	}()

	err = tlsConn.Handshake()
	if err != nil {
		t.Fatalf("Unable to handshake: %s", err)
	}

	doRequests(tlsConn, t)

	assert.True(t, destsSent[httpsAddr], "https address wasn't recorded as sent destination")
	assert.True(t, destsReceived[httpsAddr], "https address wasn't recorded as received destination")
}
Esempio n. 2
0
func TestNestedField(t *testing.T) {
	d := makeData()
	orig := d.B

	p := Parse("B")
	err := p.Set(d, &B{
		S: "10",
		I: 10,
	})

	assert.NoError(t, err, "Setting struct should succeed")
	assert.Equal(t, "10", d.B.S, "string field should reflect value from new struct")
	assert.Equal(t, 10, d.B.I, "int field should reflect value from new struct")
	assert.NotEqual(t, d.B, orig, "struct should change")

	gotten, err := Parse("B/S").Get(d)
	assert.NoError(t, err, "Getting nested string should succeed")
	assert.Equal(t, "10", gotten, "Getting nested string should have gotten right value")

	err = p.Clear(d)
	assert.NoError(t, err, "Clearing struct should succeed")
	assert.Nil(t, d.B, "struct should be nil after clearing")

	zv, err := p.ZeroValue(d)
	assert.NoError(t, err, "Getting zero value of struct should succeed")
	assert.Equal(t, &B{}, zv, "Zero value of struct should match expected")
}
Esempio n. 3
0
func TestTimeout(t *testing.T) {
	text, timedOut, err := Do(10*time.Millisecond, func() (interface{}, error) {
		time.Sleep(11 * time.Millisecond)
		return expectedText, expectedErr
	})
	assert.True(t, timedOut, "Should have timed out")
	assert.NotNil(t, err, "There should be an error")
	assert.Nil(t, text, "Text should be nil")
	assert.Equal(t, timeoutErrorString, err.Error(), "Error should contain correct string")
}
Esempio n. 4
0
func TestAnalyticsRequest(t *testing.T) {

	httpClient = &http.Client{}

	p := samplePayload()

	status, err := SendRequest(p)
	assert.Nil(t, err)
	assert.Equal(t, true, status)

}
Esempio n. 5
0
func doTestPlainText(buffered bool, useHostFn bool, t *testing.T) {
	var counter *fdcount.Counter
	var err error

	startServers(t, useHostFn)

	err = fdcount.WaitUntilNoneMatch("CLOSE_WAIT", 5*time.Second)
	if err != nil {
		t.Fatalf("Unable to wait until no more connections are in CLOSE_WAIT: %v", err)
	}

	_, counter, err = fdcount.Matching("TCP")
	if err != nil {
		t.Fatalf("Unable to get fdcount: %v", err)
	}

	var reportedHost string
	var reportedHostMutex sync.Mutex
	onResponse := func(resp *http.Response) {
		reportedHostMutex.Lock()
		reportedHost = resp.Header.Get(X_ENPROXY_PROXY_HOST)
		reportedHostMutex.Unlock()
	}

	conn, err := prepareConn(httpAddr, buffered, false, t, onResponse)
	if err != nil {
		t.Fatalf("Unable to prepareConn: %s", err)
	}
	defer func() {
		err := conn.Close()
		assert.Nil(t, err, "Closing conn should succeed")
		if !assert.NoError(t, counter.AssertDelta(2), "All file descriptors except the connection from proxy to destination site should have been closed") {
			DumpConnTrace()
		}
	}()

	doRequests(conn, t)

	assert.Equal(t, 208, bytesReceived, "Wrong number of bytes received")
	assert.Equal(t, 284, bytesSent, "Wrong number of bytes sent")
	assert.True(t, destsSent[httpAddr], "http address wasn't recorded as sent destination")
	assert.True(t, destsReceived[httpAddr], "http address wasn't recorded as received destination")

	reportedHostMutex.Lock()
	rh := reportedHost
	reportedHostMutex.Unlock()
	assert.Equal(t, "localhost", rh, "Didn't get correct reported host")
}
Esempio n. 6
0
func TestNestedSliceEntry(t *testing.T) {
	d := makeData()
	orig := d.SliceB[1]

	p := Parse("SliceB/1")
	err := p.Set(d, &B{
		S: "10",
		I: 10,
	})

	assert.NoError(t, err, "Setting struct should succeed")
	assert.Equal(t, "10", d.SliceB[1].S, "string field should reflect value from new struct")
	assert.Equal(t, 10, d.SliceB[1].I, "int field should reflect value from new struct")
	assert.NotEqual(t, d.B, orig, "struct should change")

	err = p.Clear(d)
	assert.NoError(t, err, "Clearing struct should succeed")
	assert.Nil(t, d.SliceB[1], "struct should be gone from slice after clearing")
}