2018-04-27 22:52:41 +02:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net"
|
2019-01-17 14:11:56 +01:00
|
|
|
"strings"
|
2018-04-27 22:52:41 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/tor"
|
|
|
|
)
|
|
|
|
|
2018-06-28 02:25:48 +02:00
|
|
|
type unknownAddrType struct{}
|
|
|
|
|
2018-07-31 10:29:12 +02:00
|
|
|
func (t unknownAddrType) Network() string { return "unknown" }
|
|
|
|
func (t unknownAddrType) String() string { return "unknown" }
|
2018-04-27 22:52:41 +02:00
|
|
|
|
2019-01-17 14:11:56 +01:00
|
|
|
var testIP4 = net.ParseIP("192.168.1.1")
|
|
|
|
var testIP6 = net.ParseIP("2001:0db8:0000:0000:0000:ff00:0042:8329")
|
|
|
|
|
2018-06-28 02:25:48 +02:00
|
|
|
var addrTests = []struct {
|
|
|
|
expAddr net.Addr
|
2019-01-17 14:11:56 +01:00
|
|
|
serErr string
|
2018-06-28 02:25:48 +02:00
|
|
|
}{
|
2019-01-17 14:11:56 +01:00
|
|
|
// Valid addresses.
|
2018-06-28 02:25:48 +02:00
|
|
|
{
|
|
|
|
expAddr: &net.TCPAddr{
|
2019-01-17 14:11:56 +01:00
|
|
|
IP: testIP4,
|
2018-04-27 22:52:41 +02:00
|
|
|
Port: 12345,
|
|
|
|
},
|
2018-06-28 02:25:48 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expAddr: &net.TCPAddr{
|
2019-01-17 14:11:56 +01:00
|
|
|
IP: testIP6,
|
2018-04-27 22:52:41 +02:00
|
|
|
Port: 65535,
|
|
|
|
},
|
2018-06-28 02:25:48 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expAddr: &tor.OnionAddr{
|
2018-04-27 22:52:41 +02:00
|
|
|
OnionService: "3g2upl4pq6kufc4m.onion",
|
|
|
|
Port: 9735,
|
|
|
|
},
|
2018-06-28 02:25:48 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expAddr: &tor.OnionAddr{
|
2018-04-27 22:52:41 +02:00
|
|
|
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion",
|
|
|
|
Port: 80,
|
|
|
|
},
|
2018-06-28 02:25:48 +02:00
|
|
|
},
|
2019-01-17 14:11:56 +01:00
|
|
|
|
|
|
|
// Invalid addresses.
|
2018-06-28 02:25:48 +02:00
|
|
|
{
|
|
|
|
expAddr: unknownAddrType{},
|
2019-01-17 14:11:56 +01:00
|
|
|
serErr: ErrUnknownAddressType.Error(),
|
2018-06-28 02:25:48 +02:00
|
|
|
},
|
2019-01-17 14:11:56 +01:00
|
|
|
{
|
|
|
|
expAddr: &net.TCPAddr{
|
|
|
|
// Remove last byte of IPv4 address.
|
|
|
|
IP: testIP4[:len(testIP4)-1],
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
serErr: "unable to encode",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expAddr: &net.TCPAddr{
|
|
|
|
// Add an extra byte of IPv4 address.
|
|
|
|
IP: append(testIP4, 0xff),
|
|
|
|
Port: 12345,
|
|
|
|
},
|
|
|
|
serErr: "unable to encode",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expAddr: &net.TCPAddr{
|
|
|
|
// Remove last byte of IPv6 address.
|
|
|
|
IP: testIP6[:len(testIP6)-1],
|
|
|
|
Port: 65535,
|
|
|
|
},
|
|
|
|
serErr: "unable to encode",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expAddr: &net.TCPAddr{
|
|
|
|
// Add an extra byte to the IPv6 address.
|
|
|
|
IP: append(testIP6, 0xff),
|
|
|
|
Port: 65535,
|
|
|
|
},
|
|
|
|
serErr: "unable to encode",
|
|
|
|
},
|
2019-01-17 14:11:56 +01:00
|
|
|
{
|
|
|
|
expAddr: &tor.OnionAddr{
|
|
|
|
// Invalid suffix.
|
|
|
|
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.inion",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
serErr: "invalid suffix",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expAddr: &tor.OnionAddr{
|
|
|
|
// Invalid length.
|
|
|
|
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyy.onion",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
serErr: "unknown onion service length",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
expAddr: &tor.OnionAddr{
|
|
|
|
// Invalid encoding.
|
|
|
|
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyA.onion",
|
|
|
|
Port: 80,
|
|
|
|
},
|
|
|
|
serErr: "illegal base32",
|
|
|
|
},
|
2018-06-28 02:25:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestAddrSerialization tests that the serialization method used by channeldb
|
|
|
|
// for net.Addr's works as intended.
|
|
|
|
func TestAddrSerialization(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-04-27 22:52:41 +02:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
2018-06-28 02:25:48 +02:00
|
|
|
for _, test := range addrTests {
|
|
|
|
err := serializeAddr(&b, test.expAddr)
|
2019-01-17 14:11:56 +01:00
|
|
|
switch {
|
|
|
|
case err == nil && 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):
|
2018-06-28 02:25:48 +02:00
|
|
|
t.Fatalf("unexpected serialization err for addr %v, "+
|
2019-01-17 14:11:56 +01:00
|
|
|
"want: %v, got %v", test.expAddr, test.serErr,
|
|
|
|
err)
|
|
|
|
|
|
|
|
case err != nil:
|
2018-06-28 02:25:48 +02:00
|
|
|
continue
|
2018-04-27 22:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := deserializeAddr(&b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to deserialize address: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-06-28 02:25:48 +02:00
|
|
|
if addr.String() != test.expAddr.String() {
|
2018-04-27 22:52:41 +02:00
|
|
|
t.Fatalf("expected address %v after serialization, "+
|
2018-06-28 02:25:48 +02:00
|
|
|
"got %v", addr, test.expAddr)
|
2018-04-27 22:52:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|