2018-04-27 23:52:41 +03:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/tor"
|
|
|
|
)
|
|
|
|
|
2018-06-28 03:25:48 +03:00
|
|
|
type unknownAddrType struct{}
|
|
|
|
|
2018-07-31 11:29:12 +03:00
|
|
|
func (t unknownAddrType) Network() string { return "unknown" }
|
|
|
|
func (t unknownAddrType) String() string { return "unknown" }
|
2018-04-27 23:52:41 +03:00
|
|
|
|
2018-06-28 03:25:48 +03:00
|
|
|
var addrTests = []struct {
|
|
|
|
expAddr net.Addr
|
|
|
|
serErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
expAddr: &net.TCPAddr{
|
2018-04-27 23:52:41 +03:00
|
|
|
IP: net.ParseIP("192.168.1.1"),
|
|
|
|
Port: 12345,
|
|
|
|
},
|
2018-06-28 03:25:48 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expAddr: &net.TCPAddr{
|
2018-04-27 23:52:41 +03:00
|
|
|
IP: net.ParseIP("2001:0db8:0000:0000:0000:ff00:0042:8329"),
|
|
|
|
Port: 65535,
|
|
|
|
},
|
2018-06-28 03:25:48 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expAddr: &tor.OnionAddr{
|
2018-04-27 23:52:41 +03:00
|
|
|
OnionService: "3g2upl4pq6kufc4m.onion",
|
|
|
|
Port: 9735,
|
|
|
|
},
|
2018-06-28 03:25:48 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
expAddr: &tor.OnionAddr{
|
2018-04-27 23:52:41 +03:00
|
|
|
OnionService: "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion",
|
|
|
|
Port: 80,
|
|
|
|
},
|
2018-06-28 03:25:48 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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()
|
2018-04-27 23:52:41 +03:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
2018-06-28 03:25:48 +03:00
|
|
|
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
|
2018-04-27 23:52:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := deserializeAddr(&b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to deserialize address: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-06-28 03:25:48 +03:00
|
|
|
if addr.String() != test.expAddr.String() {
|
2018-04-27 23:52:41 +03:00
|
|
|
t.Fatalf("expected address %v after serialization, "+
|
2018-06-28 03:25:48 +03:00
|
|
|
"got %v", addr, test.expAddr)
|
2018-04-27 23:52:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|