mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-23 14:32:32 +00:00

* [NOD-1223] Move all network stuff into a new network package. * [NOD-1223] Delete the unused package testutil. * [NOD-1223] Move infrastructure stuff into a new instrastructure package. * [NOD-1223] Move domain stuff into a new domain package.
63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package ff
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFlatFileLocationSerialization(t *testing.T) {
|
|
location := &flatFileLocation{
|
|
fileNumber: 1,
|
|
fileOffset: 2,
|
|
dataLength: 3,
|
|
}
|
|
|
|
serializedLocation := serializeLocation(location)
|
|
expectedSerializedLocation := []byte{1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0}
|
|
if !bytes.Equal(serializedLocation, expectedSerializedLocation) {
|
|
t.Fatalf("TestFlatFileLocationSerialization: serializeLocation "+
|
|
"returned unexpected bytes. Want: %s, got: %s",
|
|
hex.EncodeToString(expectedSerializedLocation), hex.EncodeToString(serializedLocation))
|
|
}
|
|
|
|
deserializedLocation, err := deserializeLocation(serializedLocation)
|
|
if err != nil {
|
|
t.Fatalf("TestFlatFileLocationSerialization: deserializeLocation "+
|
|
"unexpectedly failed: %s", err)
|
|
}
|
|
if !reflect.DeepEqual(deserializedLocation, location) {
|
|
t.Fatalf("TestFlatFileLocationSerialization: original "+
|
|
"location and deserialized location aren't the same. Want: %v, "+
|
|
"got: %v", location, deserializedLocation)
|
|
}
|
|
}
|
|
|
|
func TestFlatFileLocationDeserializationErrors(t *testing.T) {
|
|
expectedError := "unexpected serializedLocation length"
|
|
|
|
tooShortSerializedLocation := []byte{0, 1, 2, 3, 4, 5}
|
|
_, err := deserializeLocation(tooShortSerializedLocation)
|
|
if err == nil {
|
|
t.Fatalf("TestFlatFileLocationSerialization: deserializeLocation " +
|
|
"unexpectedly succeeded")
|
|
}
|
|
if !strings.Contains(err.Error(), expectedError) {
|
|
t.Fatalf("TestFlatFileLocationSerialization: deserializeLocation "+
|
|
"returned unexpected error. Want: %s, got: %s", expectedError, err)
|
|
}
|
|
|
|
tooLongSerializedLocation := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
|
|
_, err = deserializeLocation(tooLongSerializedLocation)
|
|
if err == nil {
|
|
t.Fatalf("TestFlatFileLocationSerialization: deserializeLocation " +
|
|
"unexpectedly succeeded")
|
|
}
|
|
if !strings.Contains(err.Error(), expectedError) {
|
|
t.Fatalf("TestFlatFileLocationSerialization: deserializeLocation "+
|
|
"returned unexpected error. Want: %s, got: %s", expectedError, err)
|
|
}
|
|
}
|