Пример #1
0
// TestIntegration tests against existing domain-fronted servers running on
// CloudFlare.
func TestIntegration(t *testing.T) {
	dialedDomain := ""
	dialedAddr := ""
	actualResolutionTime := time.Duration(0)
	actualConnectTime := time.Duration(0)
	actualHandshakeTime := time.Duration(0)
	var statsMutex sync.Mutex

	statsFunc := func(success bool, domain, addr string, resolutionTime, connectTime, handshakeTime time.Duration) {
		if success {
			statsMutex.Lock()
			defer statsMutex.Unlock()
			dialedDomain = domain
			dialedAddr = addr
			actualResolutionTime = resolutionTime
			actualConnectTime = connectTime
			actualHandshakeTime = handshakeTime
		}
	}

	d := integrationDialer(t, statsFunc)
	defer func() {
		if err := d.Close(); err != nil {
			t.Fatalf("Unable to close dialer: %v", err)
		}
	}()

	hc := &http.Client{
		Transport: &http.Transport{
			Dial: d.Dial,
		},
	}

	resp, err := hc.Get("https://www.google.com/humans.txt")
	if err != nil {
		t.Fatalf("Unable to fetch from Google: %s", err)
	}
	defer func() {
		if err := resp.Body.Close(); err != nil {
			t.Fatalf("Unable to close response body: %v", err)
		}
	}()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatalf("Unable to read response from Google: %s", err)
	}
	assert.Equal(t, expectedGoogleResponse, string(b), "Didn't get expected response from Google")

	statsMutex.Lock()
	defer statsMutex.Unlock()
	assert.True(t, dialedDomain == "100partnerprogramme.de" || dialedDomain == "10minutemail.com", "Dialed domain didn't match one of the masquerade domains", dialedDomain)
	assert.NotEqual(t, "", dialedAddr, "Should have received an addr")
	assert.NotEqual(t, time.Duration(0), actualResolutionTime, "Should have received a resolutionTime")
	assert.NotEqual(t, time.Duration(0), actualConnectTime, "Should have received a connectTime")
	assert.NotEqual(t, time.Duration(0), actualHandshakeTime, "Should have received a handshakeTime")
}
Пример #2
0
func TestNestedMapEntry(t *testing.T) {
	d := makeData()
	orig := d.MapB["3"]

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

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

	gotten, err := Parse("MapB/3/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")
	_, found := d.MapB["3"]
	assert.False(t, found, "struct should be gone from map 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")
}
Пример #3
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")
}
Пример #4
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")
}
Пример #5
0
func TestExec(t *testing.T) {
	data, err := Asset(program)
	if err != nil {
		t.Fatalf("Unable to read helloworld program: %s", err)
	}
	be := createByteExec(t, data)

	// Concurrently create some other BEs and make sure they don't get errors
	var wg sync.WaitGroup
	wg.Add(concurrency)
	for i := 0; i < concurrency; i++ {
		_, err := New(data, program)
		assert.NoError(t, err, "Concurrent New should have succeeded")
		wg.Done()
	}
	wg.Wait()

	originalInfo := testByteExec(t, be)

	// Recreate be and make sure file is reused
	be = createByteExec(t, data)
	updatedInfo := testByteExec(t, be)
	assert.Equal(t, originalInfo.ModTime(), updatedInfo.ModTime(), "File modification time should be unchanged after creating new ByteExec")

	// Now mess with the file permissions and make sure that we can still run
	err = os.Chmod(be.Filename, 0655)
	if err != nil {
		t.Fatalf("Unable to chmod test executable %s: %s", be.Filename, err)
	}
	be = createByteExec(t, data)
	updatedInfo = testByteExec(t, be)
	assert.True(t, fileMode == updatedInfo.Mode(), fmt.Sprintf("File mode was %v instead of %v", updatedInfo.Mode(), fileMode))

	// Now mess with the file contents and make sure it gets overwritten on next
	// ByteExec
	if err := ioutil.WriteFile(be.Filename, []byte("Junk"), 0755); err != nil {
		t.Fatalf("Unable to write file: %v", err)
	}
	be = createByteExec(t, data)
	updatedInfo = testByteExec(t, be)
	assert.NotEqual(t, originalInfo.ModTime(), updatedInfo.ModTime(), "File modification time should be changed after creating new ByteExec on bad data")
}
Пример #6
0
func TestConnect(t *testing.T) {
	p := &testprotector{}
	Configure(p.Protect, "8.8.8.8")
	client := &http.Client{
		Transport: &http.Transport{
			Dial: func(netw, addr string) (net.Conn, error) {
				resolved, err := Resolve(addr)
				if err != nil {
					return nil, err
				}
				return Dial(netw, resolved.String(), 10*time.Second)
			},
			ResponseHeaderTimeout: time.Second * 2,
		},
	}
	err := sendTestRequest(client, testAddr)
	if assert.NoError(t, err, "Request should have succeeded") {
		assert.NotEqual(t, 0, p.lastProtected, "Should have gotten file descriptor from protecting")
	}
}
Пример #7
0
func TestRandom(t *testing.T) {
	id1 := Random()
	id2 := Random()
	assert.NotEqual(t, id1.ToBytes(), id2.ToBytes())
}
Пример #8
0
func TestAll(t *testing.T) {
	globals.InstanceId = "testinstance"

	// Set up fake statshub
	reportCh := make(chan report)

	// Set up two dim groups with a dim in common and a dim different
	dg1 := Dim("a", "1").And("b", "1")
	dg2 := Dim("b", "2").And("a", "1")

	// Start reporting
	err := doConfigure(&Config{
		ReportingPeriod: 100 * time.Millisecond,
	}, func(r report) error {
		go func() {
			reportCh <- r
		}()
		return nil
	})
	if err != nil {
		t.Fatalf("Unable to configure statreporter: %s", err)
	}

	// Add stats
	dg1.Increment("incra").Add(1)
	dg1.Increment("incra").Add(1)
	dg1.Increment("incrb").Set(1)
	dg1.Increment("incrb").Set(25)
	dg1.Gauge("gaugea").Add(2)
	dg1.Gauge("gaugea").Add(2)
	dg1.Gauge("gaugeb").Set(2)
	dg1.Gauge("gaugeb").Set(48)
	dg1.Member("membera", "I")

	originalReporter := currentReporter

	// Reconfigure reporting
	err = doConfigure(&Config{
		ReportingPeriod: 200 * time.Millisecond,
	}, func(r report) error {
		go func() {
			reportCh <- r
		}()
		return nil
	})
	if err != nil {
		t.Fatalf("Unable to reconfigure reporting: %v", err)
	}

	// Get the first report
	report1 := <-reportCh

	dg2.Increment("incra").Add(1)
	dg2.Increment("incra").Add(1)
	dg2.Increment("incrb").Set(1)
	dg2.Increment("incrb").Set(25)
	dg2.Gauge("gaugea").Add(2)
	dg2.Gauge("gaugea").Add(2)
	dg2.Gauge("gaugeb").Set(2)
	dg2.Gauge("gaugeb").Set(48)
	dg2.Member("membera", "II")
	dg2.Member("membera", "II")

	updatedReporter := currentReporter

	assert.NotEqual(t, originalReporter, updatedReporter, "Reporter should have changed after reconfiguring")

	expectedReport1 := report{
		"dims": map[string]string{
			"a":       "1",
			"b":       "1",
			"country": "us",
		},
		"increments": stats{
			"incra": int64(2),
			"incrb": int64(25),
		},
		"gauges": stats{
			"gaugea": int64(4),
			"gaugeb": int64(48),
		},
		"multiMembers": stats{
			"membera": []string{"I"},
		},
	}
	expectedReport2 := report{
		"dims": map[string]string{
			"a":       "1",
			"b":       "2",
			"country": "cn",
		},
		"increments": stats{
			"incra": int64(2),
			"incrb": int64(25),
		},
		"gauges": stats{
			"gaugea": int64(4),
			"gaugeb": int64(48),
		},
		"multiMembers": stats{
			"membera": []string{"II"},
		},
	}

	// Get the 2nd report
	report2 := <-reportCh

	// Since reports can be made in unpredictable order, figure out which one
	// is which
	if report1["dims"].(map[string]string)["b"] == "2" {
		// switch
		report1, report2 = report2, report1
	}

	compareReports(t, expectedReport1, report1, "1st")
	compareReports(t, expectedReport2, report2, "2nd")
}