Exemplo n.º 1
0
func TestConversion(t *testing.T) {
	buf := make([]byte, 8)
	message.Uint64ToByteArray(buf, testId)
	id := message.ByteArrayToUint64(buf)
	assert.Equal(t, testId, id, "uint64 conversion is wrong!")

	buf = make([]byte, 4)
	message.Uint32ToByteArray(buf, testNum)
	n := message.ByteArrayToUint32(buf)
	assert.Equal(t, testNum, n, "uint32 conversion is wrong!")

	a1 := message.Uint64ArrayToByteArray(nil)
	a2 := message.Uint64ArrayToByteArray(make([]uint64, 0))
	a3 := message.Uint64ArrayToByteArray(testList)

	assert.Nil(t, a1, "Should return nil with nil as input")
	assert.Len(t, a2, 0, "Should give empty byte list")
	assert.Len(t, a3, len(testList)*8, "One element takes 8 bytes of space")

	l1 := message.ByteArrayToUint64Array(nil)
	l2 := message.ByteArrayToUint64Array(make([]byte, 7))
	l3 := message.ByteArrayToUint64Array(make([]byte, 0))
	l4 := message.ByteArrayToUint64Array(a3)

	assert.Nil(t, l1, "Should return nil with nil as input")
	assert.Nil(t, l2, "Should return nil with incorrect length")
	assert.Len(t, l3, 0, "Should give empty byte list")
	assert.Nil(t, testutils.CompareList(l4, testList), "Lists should be the same")
}
Exemplo n.º 2
0
func TestCreateList(t *testing.T) {
	req := message.NewRequest(message.List)
	ans := message.NewAnswerList(testList)
	ans2 := message.NewAnswerList(make([]uint64, 0))

	if assert.NotNil(t, req, "List Request should not be nil") {
		assert.Equal(t, req.Type(), message.List, "MessageType should be List")
	}

	assert.Nil(t, req.Receivers, "Receivers should be nil")
	assert.Nil(t, req.Body, "Body should be nil")

	if assert.NotNil(t, ans, "List Answer should not be nil") {
		assert.Equal(t, ans.Type(), message.List, "MessageType should be List")
	}

	assert.Nil(t, testutils.CompareList(testList, ans.List()), "List not encoded correctly")
	assert.Equal(t, uint64(0), ans.Id(), "ID should return invalid value")
	assert.Len(t, ans.List(), len(testList), "List length shoud match")
	assert.Len(t, ans2.List(), 0, "Empty List should stay empty")
	assert.Nil(t, ans.Body(), "Body should return invalid value")
}
Exemplo n.º 3
0
func TestCreateRelay(t *testing.T) {

	req := message.NewRelayRequest(testList, testBody)
	ans := message.NewAnswerRelay(testBody)

	if assert.NotNil(t, req, "Relay Request should not be nil") {
		assert.Equal(t, req.Type(), message.Relay, "MessageType should be Relay")
	}

	if assert.NotNil(t, req.Receivers, "Receivers should not be nil") {
		if err := testutils.CompareList(testList, req.Receivers); err != nil {
			t.Error(err)
		}
	}

	if assert.NotNil(t, req.Body, "Body should not be nil") {
		assert.Nil(t, testutils.CompareBytes(testBody, req.Body), "Body should be the same of the one provided")
	}

	assert.Nil(t, testutils.CompareBytes(testBody, ans.Body()), "Body should be the same of the one provided")
	assert.Equal(t, uint64(0), ans.Id(), "ID should return invalid value")
	assert.Nil(t, ans.List(), "List should return invalid value")

}