Esempio n. 1
0
func SetupTwoPeers(t *testing.T) (testKeyA, testKeyB cipher.PubKey,
	stubTransportA, stubTransportB *StubTransport,
	transportA, transportB *Transport,
	receivedA, receivedB chan []byte) {
	testKeyA = cipher.NewPubKey([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	testKeyB = cipher.NewPubKey([]byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	configA := TransportConfig{
		MyPeerID:                        testKeyA,
		PhysicalReceivedChannelLength:   10,
		ExpireMessagesInterval:          6 * time.Second,
		RememberMessageReceivedDuration: 6 * time.Second,
		RetransmitDuration:              time.Second,
	}
	stubTransportA = NewStubTransport(t, 512)
	receivedA = make(chan []byte, 10)
	transportA = NewTransport(stubTransportA, configA)
	transportA.SetReceiveChannel(receivedA)

	configB := TransportConfig{
		MyPeerID:                        testKeyB,
		PhysicalReceivedChannelLength:   10,
		ExpireMessagesInterval:          6 * time.Second,
		RememberMessageReceivedDuration: 6 * time.Second,
		RetransmitDuration:              time.Second,
	}
	stubTransportB = NewStubTransport(t, 512)
	receivedB = make(chan []byte, 10)
	transportB = NewTransport(stubTransportB, configB)
	transportB.SetReceiveChannel(receivedB)

	stubTransportA.SetStubbedPeer(testKeyB, stubTransportB)
	stubTransportB.SetStubbedPeer(testKeyA, stubTransportA)

	return
}
Esempio n. 2
0
func SetupAB(encrypt bool, t *testing.T) (
	*UDPTransport, cipher.PubKey,
	*UDPTransport, cipher.PubKey) {
	transportA, err := NewUDPTransport(staticTestConfig)
	assert.Nil(t, err)
	assert.NotNil(t, transportA)

	configB := staticTestConfig
	configB.ListenPort = 10400

	transportB, err := NewUDPTransport(configB)
	assert.Nil(t, err)
	assert.NotNil(t, transportB)

	tc := &TestCryptoStruct{}
	transportA.SetCrypto(tc)
	transportB.SetCrypto(tc)

	testKeyA := cipher.NewPubKey([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	testKeyB := cipher.NewPubKey([]byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	testKeyC := cipher.NewPubKey([]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	assert.Nil(t, transportA.ConnectToPeer(testKeyB, transportB.GetTransportConnectInfo()))
	assert.True(t, transportA.ConnectedToPeer(testKeyB))
	assert.False(t, transportA.ConnectedToPeer(testKeyC))
	assert.Nil(t, transportB.ConnectToPeer(testKeyA, transportA.GetTransportConnectInfo()))
	assert.True(t, transportB.ConnectedToPeer(testKeyA))
	assert.False(t, transportB.ConnectedToPeer(testKeyC))

	return transportA, testKeyA, transportB, testKeyB
}
Esempio n. 3
0
// Validates that public keys generated to random default are different.
func TestPubKey(t *testing.T) {
	b1 := cipher.RandByte(33)
	pubKeyRandom1 := cipher.NewPubKey(b1)
	fmt.Fprintf(os.Stdout, "Public Key Random 1: %v \n", pubKeyRandom1)
	assert.True(t, bytes.Equal(pubKeyRandom1[:], b1))

	b2 := cipher.RandByte(33)
	pubKeyRandom2 := cipher.NewPubKey(b2)
	fmt.Fprintf(os.Stdout, "Public Key Random 2: %v \n", pubKeyRandom2)
	assert.True(t, bytes.Equal(pubKeyRandom2[:], b2))

	assert.False(t, bytes.Equal(pubKeyRandom1[:], pubKeyRandom2[:]))
}
Esempio n. 4
0
func TestMessageLength(t *testing.T) {
	testKeyA := cipher.NewPubKey([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	testKeyB := cipher.NewPubKey([]byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	configA := TransportConfig{
		MyPeerID:                        testKeyA,
		PhysicalReceivedChannelLength:   10,
		ExpireMessagesInterval:          6 * time.Second,
		RememberMessageReceivedDuration: 6 * time.Second,
		RetransmitDuration:              time.Second,
	}
	stubTransportA := NewStubTransport(t, 512)
	transportA := NewTransport(stubTransportA, configA)
	assert.NotEqual(t, (uint)(512), (uint)(transportA.GetMaximumMessageSizeToPeer(testKeyB)))
	transportA.Close()
}
Esempio n. 5
0
func TestManageTransports(t *testing.T) {
	// Arrange
	transportA := transport.NewStubTransport(t, 512)
	transportB := transport.NewStubTransport(t, 512)
	testKeyA := cipher.NewPubKey([]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	nodeConfig := NodeConfig{
		PubKey: testKeyA,
		MaximumForwardingDuration:     time.Minute,
		RefreshRouteDuration:          10 * time.Second,
		ExpireRoutesInterval:          time.Second,
		TransportMessageChannelLength: 100,
		//ChaCha20Key:                   [32]byte{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3},
	}

	// Act & assert
	node, err := NewNode(nodeConfig)
	assert.Nil(t, err)
	assert.Equal(t, []transport.ITransport{}, node.GetTransports())

	node.AddTransport(transportA)
	assert.Equal(t, []transport.ITransport{transportA}, node.GetTransports())

	node.AddTransport(transportB)
	assert.Equal(t, []transport.ITransport{transportA, transportB}, node.GetTransports())

	node.RemoveTransport(transportA)
	assert.Equal(t, []transport.ITransport{transportB}, node.GetTransports())

	node.RemoveTransport(transportB)
	assert.Equal(t, []transport.ITransport{}, node.GetTransports())
}
Esempio n. 6
0
func TestAddress1(t *testing.T) {
	a := "02fa939957e9fc52140e180264e621c2576a1bfe781f88792fb315ca3d1786afb8"
	b, err := hex.DecodeString(a)
	if err != nil {
		t.Fatal(err)
	}
	addr := cipher.AddressFromPubKey(cipher.NewPubKey(b))
	_ = addr

	///func SignHash(hash cipher.SHA256, sec SecKey) (Sig, error) {

}
Esempio n. 7
0
func TestSendDatagram(t *testing.T) {
	transportA, keyA, transportB, keyB := SetupAB(false, t)
	defer transportA.Close()
	defer transportB.Close()

	testKeyC := cipher.NewPubKey([]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	assert.Zero(t, transportA.GetMaximumMessageSizeToPeer(keyA))
	assert.NotZero(t, transportA.GetMaximumMessageSizeToPeer(keyB))
	assert.NotZero(t, transportB.GetMaximumMessageSizeToPeer(keyA))
	assert.Zero(t, transportB.GetMaximumMessageSizeToPeer(keyB))
	assert.Zero(t, transportA.GetMaximumMessageSizeToPeer(testKeyC))
	assert.Zero(t, transportB.GetMaximumMessageSizeToPeer(testKeyC))

	sendBytesA := []byte{66, 44, 33, 2, 123, 100, 22}
	sendBytesB := []byte{23, 33, 12, 88, 43, 120}

	assert.Nil(t, transportA.SendMessage(keyB, sendBytesA, nil))
	assert.Nil(t, transportB.SendMessage(keyA, sendBytesB, nil))

	chanA := make(chan []byte, 10)
	chanB := make(chan []byte, 10)

	transportA.SetReceiveChannel(chanA)
	transportB.SetReceiveChannel(chanB)

	gotA := false
	gotB := false

	for !gotA || !gotB {
		select {
		case msg_a := <-chanA:
			{
				assert.Equal(t, sendBytesB, msg_a)
				gotA = true
				break
			}
		case msg_b := <-chanB:
			{
				assert.Equal(t, sendBytesA, msg_b)
				gotB = true
				break
			}
		case <-time.After(5 * time.Second):
			panic("Test timed out")
		}
	}
}
Esempio n. 8
0
func TestConnectedPeers(t *testing.T) {
	// Arrange
	peerA := cipher.NewPubKey([]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	peerB := cipher.NewPubKey([]byte{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	peerC := cipher.NewPubKey([]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	transportA := transport.NewStubTransport(t, 512)
	transportA.SetStubbedPeer(peerA, nil)
	transportB := transport.NewStubTransport(t, 512)
	transportB.SetStubbedPeer(peerB, nil)
	transportC := transport.NewStubTransport(t, 512)
	transportC.SetStubbedPeer(peerC, nil)

	testKeyA := cipher.NewPubKey([]byte{3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
	nodeConfig := NodeConfig{
		PubKey: testKeyA,
		//ChaCha20Key:                   [32]byte{0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3},
		MaximumForwardingDuration:     time.Minute,
		RefreshRouteDuration:          10 * time.Second,
		ExpireRoutesInterval:          time.Second,
		TransportMessageChannelLength: 100, // Transport message channel length
	}

	// Act & assert
	node, err := NewNode(nodeConfig)
	assert.Nil(t, err)
	assert.False(t, node.ConnectedToPeer(peerA))
	assert.False(t, node.ConnectedToPeer(peerB))
	assert.False(t, node.ConnectedToPeer(peerC))
	assert.Equal(t, []cipher.PubKey{}, sortPubKeys(node.GetConnectedPeers()))

	node.AddTransport(transportA)
	assert.Equal(t, []cipher.PubKey{peerA}, sortPubKeys(node.GetConnectedPeers()))
	assert.True(t, node.ConnectedToPeer(peerA))
	assert.False(t, node.ConnectedToPeer(peerB))
	assert.False(t, node.ConnectedToPeer(peerC))

	node.AddTransport(transportB)
	assert.Equal(t, []cipher.PubKey{peerA, peerB}, sortPubKeys(node.GetConnectedPeers()))
	assert.True(t, node.ConnectedToPeer(peerA))
	assert.True(t, node.ConnectedToPeer(peerB))
	assert.False(t, node.ConnectedToPeer(peerC))
	assert.True(t, transportA.ConnectedToPeer(peerA))

	node.AddTransport(transportC)
	assert.Equal(t, []cipher.PubKey{peerA, peerB, peerC}, sortPubKeys(node.GetConnectedPeers()))
	assert.True(t, node.ConnectedToPeer(peerA))
	assert.True(t, node.ConnectedToPeer(peerB))
	assert.True(t, node.ConnectedToPeer(peerC))

	node.RemoveTransport(transportA)
	assert.False(t, node.ConnectedToPeer(peerA))
	assert.True(t, node.ConnectedToPeer(peerB))
	assert.True(t, node.ConnectedToPeer(peerC))
	assert.Equal(t, []cipher.PubKey{peerB, peerC}, sortPubKeys(node.GetConnectedPeers()))

	node.RemoveTransport(transportB)
	assert.Equal(t, []cipher.PubKey{peerC}, sortPubKeys(node.GetConnectedPeers()))
	assert.False(t, node.ConnectedToPeer(peerA))
	assert.False(t, node.ConnectedToPeer(peerB))
	assert.True(t, node.ConnectedToPeer(peerC))
}