Exemplo n.º 1
0
func TestEquals(t *testing.T) {
	a := csFor(&Config{
		Cloud: []string{"A", "B", "C"},
		Delta: &Delta{
			Additions: []string{"D"},
			Deletions: []string{"C"},
		},
	})
	b := csFor(&Config{
		Cloud: []string{"A", "B"},
		Delta: &Delta{
			Additions: []string{"D"},
			Deletions: []string{"C"},
		},
	})
	c := csFor(&Config{
		Cloud: []string{"A", "B", "C"},
		Delta: &Delta{
			Additions: []string{"D", "E"},
			Deletions: []string{"C"},
		},
	})
	d := csFor(&Config{
		Cloud: []string{"A", "B", "C"},
		Delta: &Delta{
			Additions: []string{"D"},
			Deletions: []string{"C", "E"},
		},
	})

	assert.True(t, a.equals(a), "a should equal itself")
	assert.False(t, a.equals(b), "a should not equal b")
	assert.False(t, a.equals(c), "a should not equal c")
	assert.False(t, a.equals(d), "a should not equal d")
}
Exemplo n.º 2
0
func doTestNonGlobalAddress(t *testing.T, overrideAddr string) {
	l := startServer(t, false, nil)
	d := dialerFor(t, l, 0)
	defer d.Close()

	gotConn := false
	var gotConnMutex sync.Mutex
	tl, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		t.Fatalf("Unable to listen: %s", err)
	}
	go func() {
		tl.Accept()
		gotConnMutex.Lock()
		gotConn = true
		gotConnMutex.Unlock()
	}()

	addr := tl.Addr().String()
	if overrideAddr != "" {
		addr = overrideAddr
	}
	conn, err := d.Dial("tcp", addr)
	if err != nil {
		t.Fatalf("Unable to dial %v: %v", addr, err)
	}

	data := []byte("Some Meaningless Data")
	conn.Write(data)
	// Give enproxy time to flush
	time.Sleep(500 * time.Millisecond)
	_, err = conn.Write(data)
	assert.Error(t, err, "Sending data after previous attempt to write to local address should have failed")
	assert.False(t, gotConn, "Sending data to local address should never have resulted in connection")
}
Exemplo n.º 3
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")
}
Exemplo n.º 4
0
func TestSuccess(t *testing.T) {
	text, timedOut, err := Do(1*time.Second, func() (interface{}, error) {
		return expectedText, expectedErr
	})
	assert.False(t, timedOut, "Should not have timed out")
	assert.Equal(t, expectedText, text, "Text should match expected")
	assert.Equal(t, expectedErr, err, "Error should match expected")
}
Exemplo n.º 5
0
func TestAllowed(t *testing.T) {
	gotConn := false
	var gotConnMutex sync.Mutex
	tl, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		t.Fatalf("Unable to listen: %s", err)
	}
	go func() {
		if _, err := tl.Accept(); err != nil {
			t.Fatalf("Unable to accept connections: %v", err)
		}
		gotConnMutex.Lock()
		gotConn = true
		gotConnMutex.Unlock()
	}()

	_, portString, err := net.SplitHostPort(tl.Addr().String())
	if err != nil {
		t.Fatalf("Unable to get port for test server: %v", err)
	}

	port, err := strconv.Atoi(portString)
	if err != nil {
		t.Fatalf("Unable to convert port %v to integer: %v", portString, err)
	}
	// Only allow some port other than the actual port
	l := startServer(t, true, []int{port + 1})
	d := dialerFor(t, l, 0)
	defer func() {
		if err := d.Close(); err != nil {
			t.Fatalf("Unable to close dialer: %v", err)
		}
	}()

	addr := tl.Addr().String()
	conn, err := d.Dial("tcp", addr)
	defer func() {
		if err := conn.Close(); err != nil {
			t.Fatalf("Unable to close connection: %v", err)
		}
	}()

	data := []byte("Some Meaningless Data")
	if _, err := conn.Write(data); err != nil {
		t.Fatalf("Unable to write connection: %v", err)
	}
	// Give enproxy time to flush
	time.Sleep(500 * time.Millisecond)
	_, err = conn.Write(data)
	assert.Error(t, err, "Sending data after previous attempt to write to disallowed port should have failed")
	assert.False(t, gotConn, "Sending data to disallowed port should never have resulted in connection")
}
Exemplo n.º 6
0
func TestOKWithoutServerName(t *testing.T) {
	fdStart := countTCPFiles()
	config := &tls.Config{
		RootCAs:    cert.PoolContainingCert(),
		ServerName: "localhost", // we manually set a ServerName to make sure it doesn't get sent
	}
	conn, err := Dial("tcp", ADDR, false, config)
	assert.NoError(t, err, "Unable to dial")
	serverName := <-receivedServerNames
	assert.Empty(t, serverName, "Unexpected ServerName on server")
	assert.False(t, config.InsecureSkipVerify, "Original config shouldn't have been modified, but it was")
	closeAndCountFDs(t, conn, err, fdStart)
}
Exemplo n.º 7
0
func TestSingle(t *testing.T) {
	v := NewValue()
	go func() {
		time.Sleep(20 * time.Millisecond)
		v.Set("hi")
	}()

	r, ok := v.Get(10 * time.Millisecond)
	assert.False(t, ok, "Get with short timeout should have timed out")

	r, ok = v.Get(20 * time.Millisecond)
	assert.True(t, ok, "Get with longer timeout should have succeed")
	assert.Equal(t, "hi", r, "Wrong result")
}
Exemplo n.º 8
0
func TestRemoveFromWhitelist(t *testing.T) {
	defer stopMockServers()
	proxiedURL, proxy := newMockServer(detourMsg)
	proxy.Timeout(200*time.Millisecond, detourMsg)
	mockURL, _ := newMockServer(directMsg)
	client := newClient(proxiedURL, 100*time.Millisecond)

	u, _ := url.Parse(mockURL)
	AddToWl(u.Host, false)
	_, err := client.Get(mockURL)
	if assert.Error(t, err, "should have error if reading times out through detour") {
		assert.False(t, whitelisted(u.Host), "should be removed from whitelist if reading times out through detour")
	}

}
Exemplo n.º 9
0
func TestRoundTrip(t *testing.T) {
	expectedA, err := ioutil.ReadFile("resources/a.txt")
	if err != nil {
		t.Fatalf("Unable to load expectedA: %v", err)
	}

	expectedB, err := ioutil.ReadFile("localresources/sub/b.txt")
	if err != nil {
		t.Fatalf("Unable to load expectedB: %v", err)
	}

	expectedC, err := ioutil.ReadFile("resources/sub/c.txt")
	if err != nil {
		t.Fatalf("Unable to load expectedC: %v", err)
	}

	tarString := bytes.NewBuffer(nil)
	err = EncodeToTarString("resources", tarString)
	if err != nil {
		t.Fatalf("Unable to encode to tar string: %v", err)
	}

	fs, err := New(tarStringToBytes(t, tarString), "localresources")
	if err != nil {
		t.Fatalf("Unable to open filesystem: %v", err)
	}

	a, err := fs.Get("a.txt")
	if assert.NoError(t, err, "a.txt should have loaded") {
		assert.Equal(t, string(expectedA), string(a), "A should have matched expected")
	}

	b, err := fs.Get("sub/b.txt")
	if assert.NoError(t, err, "b.txt should have loaded") {
		assert.Equal(t, string(expectedB), string(b), "B should have matched expected")
	}

	f, err := fs.Open("/nonexistentdirectory/")
	if assert.NoError(t, err, "Opening nonexistent directory should work") {
		fi, err := f.Stat()
		if assert.NoError(t, err, "Should be able to stat directory") {
			assert.True(t, fi.IsDir(), "Nonexistent directory should be a directory")
		}
	}

	f, err = fs.Open("/nonexistentfile")
	assert.Error(t, err, "Opening nonexistent file should fail")

	f, err = fs.Open("/sub//c.txt")
	if assert.NoError(t, err, "Opening existing file with double slash should work") {
		fi, err := f.Stat()
		if assert.NoError(t, err, "Should be able to stat file") {
			if assert.False(t, fi.IsDir(), "File should not be a directory") {
				if assert.Equal(t, len(expectedC), fi.Size(), "File info should report correct size") {
					a := bytes.NewBuffer(nil)
					_, err := io.Copy(a, f)
					if assert.NoError(t, err, "Should be able to read from file") {
						assert.Equal(t, expectedC, a.Bytes(), "Should have read correct data")
					}
				}
			}
		}
	}

	sub := fs.SubDir("sub")
	b, err = sub.Get("b.txt")
	if assert.NoError(t, err, "b.txt should have loaded from sub") {
		assert.Equal(t, string(expectedB), string(b), "B should have matched expected")
	}

	c, err := sub.Get("c.txt")
	if assert.NoError(t, err, "c.txt should have loaded from sub") {
		assert.Equal(t, string(expectedC), string(c), "C should have matched expected")
	}
}