Skip to content

Commit

Permalink
channledb/addr_test: add negative test for unknown types
Browse files Browse the repository at this point in the history
  • Loading branch information
cfromknecht committed Jul 4, 2018
1 parent 21674c0 commit c6c442f
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions channeldb/addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,69 @@ import (
"github.com/lightningnetwork/lnd/tor"
)

// TestAddrSerialization tests that the serialization method used by channeldb
// for net.Addr's works as intended.
func TestAddrSerialization(t *testing.T) {
t.Parallel()
type unknownAddrType struct{}

func (_ unknownAddrType) Network() string { return "unknown" }
func (_ unknownAddrType) String() string { return "unknown" }

testAddrs := []net.Addr{
&net.TCPAddr{
var addrTests = []struct {
expAddr net.Addr
serErr error
}{
{
expAddr: &net.TCPAddr{
IP: net.ParseIP("192.168.1.1"),
Port: 12345,
},
&net.TCPAddr{
},
{
expAddr: &net.TCPAddr{
IP: net.ParseIP("2001:0db8:0000:0000:0000:ff00:0042:8329"),
Port: 65535,
},
&tor.OnionAddr{
},
{
expAddr: &tor.OnionAddr{
OnionService: "3g2upl4pq6kufc4m.onion",
Port: 9735,
},
&tor.OnionAddr{
},
{
expAddr: &tor.OnionAddr{
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion",
Port: 80,
},
}
},
{
expAddr: unknownAddrType{},
serErr: ErrUnknownAddressType,
},
}

// TestAddrSerialization tests that the serialization method used by channeldb
// for net.Addr's works as intended.
func TestAddrSerialization(t *testing.T) {
t.Parallel()

var b bytes.Buffer
for _, expectedAddr := range testAddrs {
if err := serializeAddr(&b, expectedAddr); err != nil {
t.Fatalf("unable to serialize address: %v", err)
for _, test := range addrTests {
err := serializeAddr(&b, test.expAddr)
if err != test.serErr {
t.Fatalf("unexpected serialization err for addr %v, "+
"want: %v, got %v",
test.expAddr, test.serErr, err)
} else if test.serErr != nil {
continue
}

addr, err := deserializeAddr(&b)
if err != nil {
t.Fatalf("unable to deserialize address: %v", err)
}

if addr.String() != expectedAddr.String() {
if addr.String() != test.expAddr.String() {
t.Fatalf("expected address %v after serialization, "+
"got %v", addr, expectedAddr)
"got %v", addr, test.expAddr)
}
}
}

0 comments on commit c6c442f

Please sign in to comment.