예제 #1
0
func TestManagerWithoutTransports(t *testing.T) {
	assert := assert.New(t)

	var (
		c   = Config{}
		tr  transports.Transport
		err error
	)

	tr, err = c.Open()
	if assert.NoError(err) && assert.NotNil(tr) {

		err = tr.Close()
		assert.NoError(err)
	}
}
예제 #2
0
func TestManagerWithOneTransport(t *testing.T) {
	assert := assert.New(t)

	var (
		c   = Config{udp.Config{}}
		tr  transports.Transport
		err error
	)

	tr, err = c.Open()
	if assert.NoError(err) && assert.NotNil(tr) {
		t.Logf("addrs=%v", tr.Addrs())

		err = tr.Close()
		assert.NoError(err)
	}
}
예제 #3
0
func TestMapFromURLQuery(t *testing.T) {

	m, err := FromURLQuery("name=tyler&state=UT")
	if assert.NoError(t, err) && assert.NotNil(t, m) {
		assert.Equal(t, "tyler", m.Get("name").Str())
		assert.Equal(t, "UT", m.Get("state").Str())
	}

}
예제 #4
0
func TestLocalAddresses(t *testing.T) {
	assert := assert.New(t)
	var tab = []Config{
		{},
		{Name: "/tmp/telehash-test.sock"},
		{Name: "/tmp/telehash-test.sock", Mode: 0755},
		{Mode: 0755},
	}

	for _, factory := range tab {
		trans, err := factory.Open()
		if assert.NoError(err) && assert.NotNil(trans) {
			addrs := trans.Addrs()
			assert.NotEmpty(addrs)

			t.Logf("factory=%v addrs=%v", factory, addrs)
			err = trans.Close()
			assert.NoError(err)
		}
	}
}
예제 #5
0
func TestPingPongReliable(t *testing.T) {
	// t.Parallel()
	logs.ResetLogger()

	withTwoEndpoints(t, func(A, B *Endpoint) {
		var (
			assert = assert.New(t)
			c      *Channel
			ident  *Identity
			pkt    *lob.Packet
			err    error
		)

		go func() {
			c, err := A.Listen("ping", true).AcceptChannel()
			if assert.NoError(err) && assert.NotNil(c) {
				defer c.Close()

				pkt, err = c.ReadPacket()
				if assert.NoError(err) && assert.NotNil(pkt) {
					assert.Equal("ping", string(pkt.Body(nil)))

					err = c.WritePacket(lob.New([]byte("pong")))
					assert.NoError(err)
				}
			}
		}()

		ident, err = A.LocalIdentity()
		assert.NoError(err)

		c, err = B.Open(ident, "ping", true)
		assert.NoError(err)
		if assert.NotNil(c) {

			err = c.WritePacket(lob.New([]byte("ping")))
			assert.NoError(err)

			pkt, err = c.ReadPacket()
			assert.NoError(err)
			if assert.NotNil(pkt) {
				assert.Equal("pong", string(pkt.Body(nil)))
			}

			err = c.Close()
			assert.NoError(err)
		}
	})
}
예제 #6
0
func TestMapFromBase64String(t *testing.T) {

	base64String := "eyJuYW1lIjoiTWF0In0="

	o, err := FromBase64(base64String)

	if assert.NoError(t, err) {
		assert.Equal(t, o.Get("name").Str(), "Mat")
	}

	assert.Equal(t, MustFromBase64(base64String).Get("name").Str(), "Mat")

}
예제 #7
0
func TestConversionBase64(t *testing.T) {

	o := New(map[string]interface{}{"name": "Mat"})

	result, err := o.Base64()

	if assert.NoError(t, err) {
		assert.Equal(t, "eyJuYW1lIjoiTWF0In0=", result)
	}

	assert.Equal(t, "eyJuYW1lIjoiTWF0In0=", o.MustBase64())

}
예제 #8
0
func TestConversionSignedBase64(t *testing.T) {

	o := New(map[string]interface{}{"name": "Mat"})

	result, err := o.SignedBase64("key")

	if assert.NoError(t, err) {
		assert.Equal(t, "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6", result)
	}

	assert.Equal(t, "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6", o.MustSignedBase64("key"))

}
예제 #9
0
func TestMapFromSignedBase64String(t *testing.T) {

	base64String := "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6"

	o, err := FromSignedBase64(base64String, "key")

	if assert.NoError(t, err) {
		assert.Equal(t, o.Get("name").Str(), "Mat")
	}

	assert.Equal(t, MustFromSignedBase64(base64String, "key").Get("name").Str(), "Mat")

}
예제 #10
0
func TestResolve(t *testing.T) {
	assert := assert.New(t)

	uri, err := Parse("01.test.simonmenke.me")
	if err != nil {
		panic(err)
	}

	ident, err := Resolve(uri)
	assert.NoError(err)
	assert.NotNil(ident)
	t.Logf("ident=%v addrs=%v keys=%v", ident, ident.Addresses(), ident.Keys())
}
예제 #11
0
func TestLocalAddresses(t *testing.T) {
	assert := assert.New(t)
	var tab = []Config{
		{},
		{Network: "tcp4", Addr: "127.0.0.1:0"},
		{Network: "tcp4", Addr: "127.0.0.1:8081"},
		{Network: "tcp4", Addr: ":0"},
		{Network: "tcp6", Addr: ":0"},
	}

	for _, factory := range tab {
		trans, err := factory.Open()
		if assert.NoError(err) && assert.NotNil(trans) {
			addrs := trans.Addrs()
			assert.NotEmpty(addrs)

			t.Logf("factory=%v addrs=%v", factory, addrs)
			err = trans.Close()
			assert.NoError(err)
		}
	}
}
예제 #12
0
func Test_ComputeShared_P160(t *testing.T) {
	assert := assert.New(t)
	curve := secp160r1.P160()

	for i := 100; i > 0; i-- {
		prv1, x1, y1, err := elliptic.GenerateKey(curve, rand.Reader)
		assert.NoError(err)
		assert.NotNil(prv1)
		assert.NotNil(x1)
		assert.NotNil(y1)

		prv2, x2, y2, err := elliptic.GenerateKey(curve, rand.Reader)
		assert.NoError(err)
		assert.NotNil(prv2)
		assert.NotNil(x2)
		assert.NotNil(y2)

		shared1 := ComputeShared(curve, x2, y2, prv1)
		shared2 := ComputeShared(curve, x1, y1, prv2)

		assert.Equal(shared1, shared2)
	}
}
예제 #13
0
func TestConversionJSON(t *testing.T) {

	jsonString := `{"name":"Mat"}`
	o := MustFromJSON(jsonString)

	result, err := o.JSON()

	if assert.NoError(t, err) {
		assert.Equal(t, jsonString, result)
	}

	assert.Equal(t, jsonString, o.MustJSON())

}
예제 #14
0
func Test_resolveDNS(t *testing.T) {
	// When using boot2docker run:
	//   boot2docker stop
	//   VBoxManage modifyvm "boot2docker-vm" --natdnshostresolver1 off
	//   VBoxManage modifyvm "boot2docker-vm" --natdnsproxy1 on
	//   boot2docker start

	assert := assert.New(t)

	uri, err := Parse("01.test.simonmenke.me")
	if err != nil {
		panic(err)
	}

	ident, err := resolveSRV(uri, "udp")
	if assert.NoError(err) && assert.NotNil(ident) {
		t.Logf("ident=%v addrs=%v keys=%v", ident, ident.Addresses(), ident.Keys())
	}
}
예제 #15
0
func Test_resolveHTTP(t *testing.T) {
	assert := assert.New(t)

	e, err := e3x.Open()
	if err != nil {
		panic(err)
	}
	defer e.Close()

	s := httptest.NewServer(WellKnown(e))
	defer s.Close()

	uri, err := Parse(s.URL[7:])
	t.Logf("uri=%q parsed=%v", s.URL[7:], uri)
	if err != nil {
		panic(err)
	}

	ident, err := resolveHTTP(uri)
	assert.NoError(err)
	assert.NotNil(ident)
	t.Logf("ident=%v addrs=%v keys=%v", ident, ident.Addresses(), ident.Keys())
}
예제 #16
0
func TestSimpleEndpoint(t *testing.T) {
	t.Parallel()
	logs.ResetLogger()

	if testing.Short() {
		t.Skip("this is a long running test.")
	}

	assert := assert.New(t)

	ka, err := cipherset.GenerateKey(0x3a)
	assert.NoError(err)

	kb, err := cipherset.GenerateKey(0x3a)
	assert.NoError(err)

	ea, erra := Open(
		Keys(cipherset.Keys{0x3a: ka}),
		Transport(mux.Config{udp.Config{}, inproc.Config{}}),
		Log(nil))

	eb, errb := Open(
		Keys(cipherset.Keys{0x3a: kb}),
		Transport(mux.Config{udp.Config{}, inproc.Config{}}),
		Log(nil))
	assert.NoError(erra)
	assert.NoError(errb)

	time.Sleep(1 * time.Second)

	identA, err := ea.LocalIdentity()
	assert.NoError(err)

	identB, err := eb.LocalIdentity()
	assert.NoError(err)

	_, err = ea.Dial(identB)
	assert.NoError(err)

	_, err = ea.Dial(identB)
	assert.NoError(err)

	_, err = eb.Dial(identA)
	assert.NoError(err)

	time.Sleep(2*time.Minute + 10*time.Second)

	err = ea.Close()
	assert.NoError(err)

	err = eb.Close()
	assert.NoError(err)
}
예제 #17
0
func TestBridge(t *testing.T) {
	// given:
	// A <-> B exchange
	// B <-> R exchange
	// A x-x R no exchange
	//
	// when:
	// R --> B route token from A->B to B
	// A --x B block A from contacting B (while adding R's addresses to the exchange A->B)
	//
	// then:
	// A and B should still be able to communicate.

	assert := assert.New(t)

	var blacklist []net.Addr
	blacklistRule := func(src net.Addr) bool {
		t.Logf("FW(%v, src=%s)", blacklist, src)
		if len(blacklist) == 0 {
			return true
		}

		for _, addr := range blacklist {
			if transports.EqualAddr(addr, src) {
				return false
			}
		}

		return true
	}

	A, err := e3x.Open(
		e3x.Log(nil),
		e3x.Transport(udp.Config{}),
		Module(Config{}))
	assert.NoError(err)
	B, err := e3x.Open(
		e3x.Log(nil),
		e3x.Transport(fw.Config{Config: udp.Config{}, Allow: fw.RuleFunc(blacklistRule)}),
		Module(Config{}))
	assert.NoError(err)

	R, err := e3x.Open(
		e3x.Log(nil),
		e3x.Transport(udp.Config{}),
		Module(Config{}))
	assert.NoError(err)

	done := make(chan bool, 1)

	go func() {

		var (
			pkt   *lob.Packet
			err   error
			n     = 1
			first = true
		)

		defer func() { done <- true }()

		c, err := A.Listen("ping", true).AcceptChannel()
		defer c.Close()

		for ; n > 0; n-- {
			pkt, err = c.ReadPacket()
			if err != nil {
				t.Fatalf("ping: error: %s", err)
				return
			}

			if first {
				n, _ = pkt.Header().GetInt("n")
				first = false
			}

			t.Logf("RCV ping: %d", n)

			err = c.WritePacket(&lob.Packet{})
			if err != nil {
				t.Fatalf("ping: error: %s", err)
				return
			}

			t.Logf("SND pong: %d", n)
		}
	}()

	Aident, err := A.LocalIdentity()
	assert.NoError(err)
	Bident, err := B.LocalIdentity()
	assert.NoError(err)

	{
		addr, err := transports.ResolveAddr("peer", string(R.LocalHashname()))
		assert.NoError(err)
		Bident = Bident.AddPathCandiate(addr)
	}

	log.Println("\x1B[31m------------------------------------------------\x1B[0m")

	// blacklist A
	blacklist = append(blacklist, Aident.Addresses()...)
	log.Println("\x1B[32mblacklist:\x1B[0m", blacklist)

	log.Println("\x1B[31m------------------------------------------------\x1B[0m")

	_, err = R.Dial(Bident)
	assert.NoError(err)
	_, err = R.Dial(Aident)
	assert.NoError(err)
	ABex, err := A.Dial(Bident)
	assert.NoError(err)

	log.Println("\x1B[31m------------------------------------------------\x1B[0m")
	log.Printf("ab-local-token  = %x", ABex.LocalToken())
	log.Printf("ab-remote-token = %x", ABex.RemoteToken())
	log.Println("\x1B[31m------------------------------------------------\x1B[0m")

	{
		ch, err := B.Open(Aident, "ping", true)
		assert.NoError(err)

		for n := 10; n > 0; n-- {
			pkt := &lob.Packet{}
			pkt.Header().SetInt("n", n)
			err = ch.WritePacket(pkt)
			if err != nil {
				t.Fatalf("ping: error: %s", err)
			}
			t.Logf("SND ping: %d", n)

			_, err = ch.ReadPacket()
			if err != nil {
				t.Fatalf("ping: error: %s", err)
			}
			t.Logf("RCV pong: %d", n)
		}

		ch.Close()
	}

	<-done

	assert.NoError(A.Close())
	assert.NoError(B.Close())
	assert.NoError(R.Close())
}
예제 #18
0
// NoError asserts that a function returned no error (i.e. `nil`).
//
//   actualObj, err := SomeFunction()
//   require.NoError(t, err)
//   require.Equal(t, actualObj, expectedObj)
//
// Returns whether the assertion was successful (true) or not (false).
func NoError(t TestingT, err error, msgAndArgs ...interface{}) {
	if !assert.NoError(t, err, msgAndArgs...) {
		t.FailNow()
	}
}
예제 #19
0
func TestFloodReliable(t *testing.T) {
	if testing.Short() {
		t.Skip("this is a long running test.")
	}

	withTwoEndpoints(t, func(A, B *Endpoint) {
		A.setOptions(DisableLog())
		B.setOptions(DisableLog())

		var (
			assert = assert.New(t)
			c      *Channel
			ident  *Identity
			pkt    *lob.Packet
			err    error
		)

		go func() {
			c, err := A.Listen("flood", true).AcceptChannel()
			if assert.NoError(err) && assert.NotNil(c) {
				defer c.Close()

				pkt, err = c.ReadPacket()
				assert.NoError(err)
				assert.NotNil(pkt)

				for i := 0; i < 100000; i++ {
					pkt := lob.New(nil)
					pkt.Header().SetInt("flood_id", i)
					err = c.WritePacket(pkt)
					assert.NoError(err)
				}
			}
		}()

		ident, err = A.LocalIdentity()
		assert.NoError(err)

		c, err = B.Open(ident, "flood", true)
		assert.NoError(err)
		assert.NotNil(c)

		defer c.Close()

		err = c.WritePacket(lob.New(nil))
		assert.NoError(err)

		lastID := -1
		for {
			pkt, err = c.ReadPacket()
			if err == io.EOF {
				break
			}
			assert.NoError(err)
			assert.NotNil(pkt)
			if err != nil {
				break
			}
			if pkt != nil {
				id, _ := pkt.Header().GetInt("flood_id")
				assert.True(lastID < id)
				lastID = id
			}
		}
	})
}