2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-13 13:10:30 +00:00
loop/loopdb/codec_test.go

54 lines
1.6 KiB
Go
Raw Normal View History

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)
}
}