Example #1
0
// TODO refactor and use SetupNodes() instead
func SetupNodes2(t *testing.T, n uint, connections [][]int) ([]*Node, []*transport.StubTransport) {
	nodes := make([]*Node, n)
	transports := []*transport.StubTransport{}
	for i := (uint)(0); i < n; i++ {
		pubKey := cipher.PubKey{}
		pubKey[0] = (byte)(i + 1)
		nodes[i] = SetupNode2(t, pubKey)
	}

	for i := (uint)(0); i < n; i++ {
		transportsFrom := []*transport.StubTransport{}
		for j := (uint)(0); j < n; j++ {
			if connections[i][j] != 0 {
				transportFrom := transport.NewStubTransport(t, 512)
				transportTo := transport.NewStubTransport(t, 512)
				transportFrom.SetStubbedPeer(nodes[j].GetConfig().PubKey, transportTo)
				transportFrom.MessagesReceived = nodes[i].transportsMessagesReceived
				transportTo.MessagesReceived = nodes[j].transportsMessagesReceived
				transportsFrom = append(transportsFrom, transportFrom)
				nodes[i].AddTransport(transportFrom)
			}
		}
		transports = append(transports, transportsFrom...)
	}
	return nodes, transports
}
Example #2
0
func SetupNodes(n uint, connections [][]int, t *testing.T) (nodes []*Node, to_close chan []byte, transports []*transport.StubTransport) {
	nodes = make([]*Node, n)
	transports = []*transport.StubTransport{}
	to_close = make(chan []byte, 20)
	sentMessages := make(chan []byte, 20)
	maxDatagramLengths := []uint{512, 450, 1000, 150, 200}
	for i := (uint)(0); i < n; i++ {
		pubKey := cipher.PubKey{}
		pubKey[0] = (byte)(i + 1)
		nodes[i] = SetupNode(t, maxDatagramLengths[i%((uint)(len(maxDatagramLengths)))], pubKey)
	}

	for i := (uint)(0); i < n; i++ {
		transportsFrom := []*transport.StubTransport{}
		for j := (uint)(0); j < n; j++ {
			if connections[i][j] != 0 {
				transportFrom := transport.NewStubTransport(t, maxDatagramLengths[i%((uint)(len(maxDatagramLengths)))])
				transportTo := transport.NewStubTransport(t, maxDatagramLengths[j%((uint)(len(maxDatagramLengths)))])
				transportFrom.SetStubbedPeer(nodes[j].GetConfig().PubKey, transportTo)
				transportFrom.MessagesReceived = nodes[i].transportsMessagesReceived
				transportTo.MessagesReceived = nodes[j].transportsMessagesReceived
				transportsFrom = append(transportsFrom, transportFrom)
				nodes[i].AddTransport(transportFrom)
			}
		}
		transports = append(transports, transportsFrom...)
	}
	return nodes, sentMessages, transports
}
Example #3
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())
}
Example #4
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))
}