graph/db: update TestAddrSerialization

This commit is contained in:
Elle Mouton
2025-08-07 12:30:26 +02:00
committed by Mohamed Awnallah
parent 3fa137e187
commit 6b38f8d9df

View File

@@ -3,7 +3,6 @@ package graphdb
import ( import (
"bytes" "bytes"
"net" "net"
"strings"
"testing" "testing"
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
@@ -54,36 +53,39 @@ var (
) )
var addrTests = []struct { var addrTests = []struct {
inputAddr net.Addr
// If expAddr is not set, then the test will expect it to be equal
// to inputAddr.
expAddr net.Addr expAddr net.Addr
serErr string serErr string
}{ }{
// Valid addresses. // Valid addresses.
{ {
expAddr: testIPV4Addr, inputAddr: testIPV4Addr,
}, },
{ {
expAddr: testIPV6Addr, inputAddr: testIPV6Addr,
}, },
{ {
expAddr: testOnionV2Addr, inputAddr: testOnionV2Addr,
}, },
{ {
expAddr: testOnionV3Addr, inputAddr: testOnionV3Addr,
}, },
{ {
expAddr: testOpaqueAddr, inputAddr: testOpaqueAddr,
}, },
{ {
expAddr: testDNSAddr, inputAddr: testDNSAddr,
}, },
// Invalid addresses. // Invalid addresses.
{ {
expAddr: unknownAddrType{}, inputAddr: unknownAddrType{},
serErr: ErrUnknownAddressType.Error(), serErr: ErrUnknownAddressType.Error(),
}, },
{ {
expAddr: &net.TCPAddr{ inputAddr: &net.TCPAddr{
// Remove last byte of IPv4 address. // Remove last byte of IPv4 address.
IP: testIP4[:len(testIP4)-1], IP: testIP4[:len(testIP4)-1],
Port: 12345, Port: 12345,
@@ -91,7 +93,7 @@ var addrTests = []struct {
serErr: "unable to encode", serErr: "unable to encode",
}, },
{ {
expAddr: &net.TCPAddr{ inputAddr: &net.TCPAddr{
// Add an extra byte of IPv4 address. // Add an extra byte of IPv4 address.
IP: append(testIP4, 0xff), IP: append(testIP4, 0xff),
Port: 12345, Port: 12345,
@@ -99,7 +101,7 @@ var addrTests = []struct {
serErr: "unable to encode", serErr: "unable to encode",
}, },
{ {
expAddr: &net.TCPAddr{ inputAddr: &net.TCPAddr{
// Remove last byte of IPv6 address. // Remove last byte of IPv6 address.
IP: testIP6[:len(testIP6)-1], IP: testIP6[:len(testIP6)-1],
Port: 65535, Port: 65535,
@@ -107,7 +109,7 @@ var addrTests = []struct {
serErr: "unable to encode", serErr: "unable to encode",
}, },
{ {
expAddr: &net.TCPAddr{ inputAddr: &net.TCPAddr{
// Add an extra byte to the IPv6 address. // Add an extra byte to the IPv6 address.
IP: append(testIP6, 0xff), IP: append(testIP6, 0xff),
Port: 65535, Port: 65535,
@@ -115,7 +117,7 @@ var addrTests = []struct {
serErr: "unable to encode", serErr: "unable to encode",
}, },
{ {
expAddr: &tor.OnionAddr{ inputAddr: &tor.OnionAddr{
// Invalid suffix. // Invalid suffix.
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.inion", OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.inion",
Port: 80, Port: 80,
@@ -123,7 +125,7 @@ var addrTests = []struct {
serErr: "invalid suffix", serErr: "invalid suffix",
}, },
{ {
expAddr: &tor.OnionAddr{ inputAddr: &tor.OnionAddr{
// Invalid length. // Invalid length.
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyy.onion", OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyy.onion",
Port: 80, Port: 80,
@@ -131,7 +133,7 @@ var addrTests = []struct {
serErr: "unknown onion service length", serErr: "unknown onion service length",
}, },
{ {
expAddr: &tor.OnionAddr{ inputAddr: &tor.OnionAddr{
// Invalid encoding. // Invalid encoding.
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyA.onion", OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyA.onion",
Port: 80, Port: 80,
@@ -147,30 +149,20 @@ func TestAddrSerialization(t *testing.T) {
var b bytes.Buffer var b bytes.Buffer
for _, test := range addrTests { for _, test := range addrTests {
err := SerializeAddr(&b, test.expAddr) err := SerializeAddr(&b, test.inputAddr)
switch { if test.serErr != "" {
case err == nil && test.serErr != "": require.ErrorContains(t, err, test.serErr)
t.Fatalf("expected serialization err for addr %v",
test.expAddr)
case err != nil && test.serErr == "":
t.Fatalf("unexpected serialization err for addr %v: %v",
test.expAddr, err)
case err != nil && !strings.Contains(err.Error(), test.serErr):
t.Fatalf("unexpected serialization err for addr %v, "+
"want: %v, got %v", test.expAddr, test.serErr,
err)
case err != nil:
continue continue
} }
require.NoError(t, err)
addr, err := DeserializeAddr(&b) addr, err := DeserializeAddr(&b)
if err != nil { require.NoError(t, err)
t.Fatalf("unable to deserialize address: %v", err)
}
require.Equal(t, test.expAddr, addr) if test.expAddr != nil {
require.Equal(t, test.expAddr, addr)
} else {
require.Equal(t, test.inputAddr, addr)
}
} }
} }