63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
|
package kademlia
|
||
|
|
||
|
import (
|
||
|
"encoding/binary"
|
||
|
"strings"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestSuccessPacketSerialisation(t *testing.T) {
|
||
|
|
||
|
key := []byte(strings.Repeat("k", 32))
|
||
|
value := []byte("value")
|
||
|
size := uint16(4 + len(key) + len(value))
|
||
|
|
||
|
result := SerialiseSuccessPacket(DHTSuccessPacket{
|
||
|
Key: [32]byte(key),
|
||
|
Value: value,
|
||
|
})
|
||
|
if len(result) != int(size) {
|
||
|
t.Errorf("gExpected size %d, got %d", size, len(result))
|
||
|
}
|
||
|
messageSize := binary.BigEndian.Uint16(result[:2])
|
||
|
if messageSize != size {
|
||
|
t.Errorf("Expected size value in message to be %d, got %d", size, messageSize)
|
||
|
}
|
||
|
messageType := binary.BigEndian.Uint16(result[2:4])
|
||
|
if messageType != SUCCESS {
|
||
|
t.Errorf("Expected message type to be %d, got %d", SUCCESS, messageType)
|
||
|
}
|
||
|
messageKey := result[4:36]
|
||
|
if string(messageKey) != string(key) {
|
||
|
t.Errorf("Expected key %s, got %s", key, messageKey)
|
||
|
}
|
||
|
messageValue := result[36:]
|
||
|
if string(messageValue) != string(value) {
|
||
|
t.Errorf("Expected value %s, got %s", value, messageValue)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestFailurePacketSerialisation(t *testing.T) {
|
||
|
|
||
|
key := []byte(strings.Repeat("k", 32))
|
||
|
|
||
|
size := uint16(4 + len(key))
|
||
|
|
||
|
result := SerialiseFailurePacket(DHTFailurePacket{Key: [32]byte(key)})
|
||
|
if len(result) != int(size) {
|
||
|
t.Errorf("Expected size %d, got %d", size, len(result))
|
||
|
}
|
||
|
messageSize := binary.BigEndian.Uint16(result[:2])
|
||
|
if messageSize != size {
|
||
|
t.Errorf("Expected size value in message to be %d, got %d", size, messageSize)
|
||
|
}
|
||
|
messageType := binary.BigEndian.Uint16(result[2:4])
|
||
|
if messageType != FAILURE {
|
||
|
t.Errorf("Expected message type to be %d, got %d", FAILURE, messageType)
|
||
|
}
|
||
|
messageKey := result[4:]
|
||
|
if string(messageKey) != string(key) {
|
||
|
t.Errorf("Expected key %s, got %s", key, messageKey)
|
||
|
}
|
||
|
}
|