2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-11 13:11:12 +00:00
loop/loopdb/codec_test.go
Andras Banki-Horvath 86db43a2cb loopdb: store protocol version alongside with swaps
This commit adds the protocol version to each stored swap. This will be
used to ensure that when swaps are resumed after a restart, they're
correctly handled given any breaking protocol changes.
2020-09-09 19:54:01 +02:00

54 lines
1.6 KiB
Go

package loopdb
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestProtocolVersionMarshalUnMarshal tests that marshalling and unmarshalling
// looprpc.ProtocolVersion works correctly.
func TestProtocolVersionMarshalUnMarshal(t *testing.T) {
t.Parallel()
testVersions := [...]ProtocolVersion{
ProtocolVersionLegacy,
ProtocolVersionMultiLoopOut,
ProtocolVersionSegwitLoopIn,
ProtocolVersionPreimagePush,
ProtocolVersionUserExpiryLoopOut,
}
bogusVersion := []byte{0xFF, 0xFF, 0xFF, 0xFF}
invalidSlice := []byte{0xFF, 0xFF, 0xFF}
for i := 0; i < len(testVersions); i++ {
testVersion := testVersions[i]
// Test that unmarshal(marshal(v)) == v.
version, err := UnmarshalProtocolVersion(
MarshalProtocolVersion(testVersion),
)
require.NoError(t, err)
require.Equal(t, testVersion, version)
// Test that unmarshalling a nil slice returns the default
// version along with no error.
version, err = UnmarshalProtocolVersion(nil)
require.NoError(t, err)
require.Equal(t, ProtocolVersionUnrecorded, version)
// Test that unmarshalling an unknown version returns the
// default version along with an error.
version, err = UnmarshalProtocolVersion(bogusVersion)
require.Error(t, err, "expected invalid version")
require.Equal(t, ProtocolVersionUnrecorded, version)
// Test that unmarshalling an invalid slice returns the
// default version along with an error.
version, err = UnmarshalProtocolVersion(invalidSlice)
require.Error(t, err, "expected invalid size")
require.Equal(t, ProtocolVersionUnrecorded, version)
}
}