2014-01-01 17:16:15 +01:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
2013-08-06 23:55:22 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-07-06 08:04:24 +02:00
|
|
|
package addrmgr_test
|
2013-08-06 23:55:22 +02:00
|
|
|
|
|
|
|
import (
|
2014-07-06 08:04:24 +02:00
|
|
|
"errors"
|
2015-02-27 20:34:09 +01:00
|
|
|
"fmt"
|
2013-08-06 23:55:22 +02:00
|
|
|
"net"
|
2015-02-27 20:34:09 +01:00
|
|
|
"reflect"
|
2013-08-06 23:55:22 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
2014-07-02 15:50:08 +02:00
|
|
|
|
2015-01-17 07:48:13 +01:00
|
|
|
"github.com/btcsuite/btcd/addrmgr"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2013-08-06 23:55:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// naTest is used to describe a test to be perfomed against the NetAddressKey
|
|
|
|
// method.
|
|
|
|
type naTest struct {
|
2015-02-05 22:16:39 +01:00
|
|
|
in wire.NetAddress
|
2013-08-06 23:55:22 +02:00
|
|
|
want string
|
|
|
|
}
|
|
|
|
|
|
|
|
// naTests houses all of the tests to be performed against the NetAddressKey
|
|
|
|
// method.
|
|
|
|
var naTests = make([]naTest, 0)
|
|
|
|
|
2015-02-27 20:34:09 +01:00
|
|
|
// Put some IP in here for convenience. Points to google.
|
|
|
|
var someIP = "173.194.115.66"
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
// addNaTests
|
|
|
|
func addNaTests() {
|
|
|
|
// IPv4
|
|
|
|
// Localhost
|
|
|
|
addNaTest("127.0.0.1", 8333, "127.0.0.1:8333")
|
|
|
|
addNaTest("127.0.0.1", 8334, "127.0.0.1:8334")
|
|
|
|
|
|
|
|
// Class A
|
|
|
|
addNaTest("1.0.0.1", 8333, "1.0.0.1:8333")
|
|
|
|
addNaTest("2.2.2.2", 8334, "2.2.2.2:8334")
|
|
|
|
addNaTest("27.253.252.251", 8335, "27.253.252.251:8335")
|
|
|
|
addNaTest("123.3.2.1", 8336, "123.3.2.1:8336")
|
|
|
|
|
|
|
|
// Private Class A
|
|
|
|
addNaTest("10.0.0.1", 8333, "10.0.0.1:8333")
|
|
|
|
addNaTest("10.1.1.1", 8334, "10.1.1.1:8334")
|
|
|
|
addNaTest("10.2.2.2", 8335, "10.2.2.2:8335")
|
|
|
|
addNaTest("10.10.10.10", 8336, "10.10.10.10:8336")
|
|
|
|
|
|
|
|
// Class B
|
|
|
|
addNaTest("128.0.0.1", 8333, "128.0.0.1:8333")
|
|
|
|
addNaTest("129.1.1.1", 8334, "129.1.1.1:8334")
|
|
|
|
addNaTest("180.2.2.2", 8335, "180.2.2.2:8335")
|
|
|
|
addNaTest("191.10.10.10", 8336, "191.10.10.10:8336")
|
|
|
|
|
|
|
|
// Private Class B
|
|
|
|
addNaTest("172.16.0.1", 8333, "172.16.0.1:8333")
|
|
|
|
addNaTest("172.16.1.1", 8334, "172.16.1.1:8334")
|
|
|
|
addNaTest("172.16.2.2", 8335, "172.16.2.2:8335")
|
|
|
|
addNaTest("172.16.172.172", 8336, "172.16.172.172:8336")
|
|
|
|
|
|
|
|
// Class C
|
|
|
|
addNaTest("193.0.0.1", 8333, "193.0.0.1:8333")
|
|
|
|
addNaTest("200.1.1.1", 8334, "200.1.1.1:8334")
|
|
|
|
addNaTest("205.2.2.2", 8335, "205.2.2.2:8335")
|
|
|
|
addNaTest("223.10.10.10", 8336, "223.10.10.10:8336")
|
|
|
|
|
|
|
|
// Private Class C
|
|
|
|
addNaTest("192.168.0.1", 8333, "192.168.0.1:8333")
|
|
|
|
addNaTest("192.168.1.1", 8334, "192.168.1.1:8334")
|
|
|
|
addNaTest("192.168.2.2", 8335, "192.168.2.2:8335")
|
|
|
|
addNaTest("192.168.192.192", 8336, "192.168.192.192:8336")
|
|
|
|
|
|
|
|
// IPv6
|
|
|
|
// Localhost
|
|
|
|
addNaTest("::1", 8333, "[::1]:8333")
|
|
|
|
addNaTest("fe80::1", 8334, "[fe80::1]:8334")
|
|
|
|
|
|
|
|
// Link-local
|
|
|
|
addNaTest("fe80::1:1", 8333, "[fe80::1:1]:8333")
|
|
|
|
addNaTest("fe91::2:2", 8334, "[fe91::2:2]:8334")
|
|
|
|
addNaTest("fea2::3:3", 8335, "[fea2::3:3]:8335")
|
|
|
|
addNaTest("feb3::4:4", 8336, "[feb3::4:4]:8336")
|
|
|
|
|
|
|
|
// Site-local
|
|
|
|
addNaTest("fec0::1:1", 8333, "[fec0::1:1]:8333")
|
|
|
|
addNaTest("fed1::2:2", 8334, "[fed1::2:2]:8334")
|
|
|
|
addNaTest("fee2::3:3", 8335, "[fee2::3:3]:8335")
|
|
|
|
addNaTest("fef3::4:4", 8336, "[fef3::4:4]:8336")
|
|
|
|
}
|
|
|
|
|
2014-07-06 15:47:42 +02:00
|
|
|
func addNaTest(ip string, port uint16, want string) {
|
|
|
|
nip := net.ParseIP(ip)
|
2015-02-05 22:16:39 +01:00
|
|
|
na := wire.NetAddress{
|
2014-07-06 15:47:42 +02:00
|
|
|
Timestamp: time.Now(),
|
2015-02-05 22:16:39 +01:00
|
|
|
Services: wire.SFNodeNetwork,
|
2014-07-06 15:47:42 +02:00
|
|
|
IP: nip,
|
|
|
|
Port: port,
|
2013-10-02 23:34:25 +02:00
|
|
|
}
|
2014-07-06 15:47:42 +02:00
|
|
|
test := naTest{na, want}
|
|
|
|
naTests = append(naTests, test)
|
2013-10-02 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 15:47:42 +02:00
|
|
|
func lookupFunc(host string) ([]net.IP, error) {
|
|
|
|
return nil, errors.New("not implemented")
|
|
|
|
}
|
2013-10-02 23:34:25 +02:00
|
|
|
|
2015-02-27 20:34:09 +01:00
|
|
|
func TestStartStop(t *testing.T) {
|
|
|
|
n := addrmgr.New("teststartstop", lookupFunc)
|
|
|
|
n.Start()
|
|
|
|
err := n.Stop()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Address Manager failed to stop: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddAddressByIP(t *testing.T) {
|
|
|
|
fmtErr := fmt.Errorf("")
|
|
|
|
addrErr := &net.AddrError{}
|
|
|
|
var tests = []struct {
|
|
|
|
addrIP string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
someIP + ":8333",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
someIP,
|
|
|
|
addrErr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
someIP[:12] + ":8333",
|
|
|
|
fmtErr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
someIP + ":abcd",
|
|
|
|
fmtErr,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
amgr := addrmgr.New("testaddressbyip", nil)
|
|
|
|
for i, test := range tests {
|
|
|
|
err := amgr.AddAddressByIP(test.addrIP)
|
|
|
|
if test.err != nil && err == nil {
|
|
|
|
t.Errorf("TestGood test %d failed expected an error and got none", i)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if test.err == nil && err != nil {
|
|
|
|
t.Errorf("TestGood test %d failed expected no error and got one", i)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
|
|
|
t.Errorf("TestGood test %d failed got %v, want %v", i,
|
|
|
|
reflect.TypeOf(err), reflect.TypeOf(test.err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 15:47:42 +02:00
|
|
|
func TestAddLocalAddress(t *testing.T) {
|
|
|
|
var tests = []struct {
|
2015-02-27 20:34:09 +01:00
|
|
|
address wire.NetAddress
|
|
|
|
priority addrmgr.AddressPriority
|
|
|
|
valid bool
|
2014-07-06 15:47:42 +02:00
|
|
|
}{
|
|
|
|
{
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.NetAddress{IP: net.ParseIP("192.168.0.100")},
|
2015-02-27 20:34:09 +01:00
|
|
|
addrmgr.InterfacePrio,
|
2014-07-06 15:47:42 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.NetAddress{IP: net.ParseIP("204.124.1.1")},
|
2015-02-27 20:34:09 +01:00
|
|
|
addrmgr.InterfacePrio,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
wire.NetAddress{IP: net.ParseIP("204.124.1.1")},
|
|
|
|
addrmgr.BoundPrio,
|
2014-07-06 15:47:42 +02:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.NetAddress{IP: net.ParseIP("::1")},
|
2015-02-27 20:34:09 +01:00
|
|
|
addrmgr.InterfacePrio,
|
2014-07-06 15:47:42 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.NetAddress{IP: net.ParseIP("fe80::1")},
|
2015-02-27 20:34:09 +01:00
|
|
|
addrmgr.InterfacePrio,
|
2014-07-06 15:47:42 +02:00
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.NetAddress{IP: net.ParseIP("2620:100::1")},
|
2015-02-27 20:34:09 +01:00
|
|
|
addrmgr.InterfacePrio,
|
2014-07-06 15:47:42 +02:00
|
|
|
true,
|
|
|
|
},
|
2013-10-02 23:34:25 +02:00
|
|
|
}
|
2015-02-27 20:34:09 +01:00
|
|
|
amgr := addrmgr.New("testaddlocaladdress", nil)
|
2014-07-06 15:47:42 +02:00
|
|
|
for x, test := range tests {
|
2015-02-27 20:34:09 +01:00
|
|
|
result := amgr.AddLocalAddress(&test.address, test.priority)
|
2014-07-06 15:47:42 +02:00
|
|
|
if result == nil && !test.valid {
|
|
|
|
t.Errorf("TestAddLocalAddress test #%d failed: %s should have "+
|
|
|
|
"been accepted", x, test.address.IP)
|
2013-10-02 23:34:25 +02:00
|
|
|
continue
|
|
|
|
}
|
2014-07-06 15:47:42 +02:00
|
|
|
if result != nil && test.valid {
|
|
|
|
t.Errorf("TestAddLocalAddress test #%d failed: %s should not have "+
|
2015-01-16 17:13:51 +01:00
|
|
|
"been accepted", x, test.address.IP)
|
2013-10-02 23:34:25 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2014-07-06 15:47:42 +02:00
|
|
|
}
|
|
|
|
|
2015-02-27 20:34:09 +01:00
|
|
|
func TestAttempt(t *testing.T) {
|
|
|
|
n := addrmgr.New("testattempt", lookupFunc)
|
|
|
|
|
|
|
|
// Add a new address and get it
|
|
|
|
err := n.AddAddressByIP(someIP + ":8333")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Adding address failed: %v", err)
|
|
|
|
}
|
|
|
|
ka := n.GetAddress("any", 100)
|
|
|
|
|
|
|
|
if !ka.LastAttempt().IsZero() {
|
|
|
|
t.Errorf("Address should not have attempts, but does")
|
|
|
|
}
|
|
|
|
|
|
|
|
na := ka.NetAddress()
|
|
|
|
n.Attempt(na)
|
|
|
|
|
|
|
|
if ka.LastAttempt().IsZero() {
|
|
|
|
t.Errorf("Address should have an attempt, but does not")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConnected(t *testing.T) {
|
|
|
|
n := addrmgr.New("testconnected", lookupFunc)
|
|
|
|
|
|
|
|
// Add a new address and get it
|
|
|
|
err := n.AddAddressByIP(someIP + ":8333")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Adding address failed: %v", err)
|
|
|
|
}
|
|
|
|
ka := n.GetAddress("any", 100)
|
|
|
|
na := ka.NetAddress()
|
|
|
|
na.Timestamp = time.Now().Add(time.Hour * -1) // make it an hour ago
|
|
|
|
|
|
|
|
n.Connected(na)
|
|
|
|
|
|
|
|
if !ka.NetAddress().Timestamp.After(na.Timestamp) {
|
|
|
|
t.Errorf("Address should have a new timestamp, but does not")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNeedMoreAddresses(t *testing.T) {
|
|
|
|
n := addrmgr.New("testneedmoreaddresses", lookupFunc)
|
|
|
|
addrsToAdd := 1500
|
|
|
|
b := n.NeedMoreAddresses()
|
|
|
|
if b == false {
|
|
|
|
t.Errorf("Expected that we need more addresses")
|
|
|
|
}
|
|
|
|
addrs := make([]*wire.NetAddress, addrsToAdd)
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for i := 0; i < addrsToAdd; i++ {
|
|
|
|
s := fmt.Sprintf("%d.%d.173.147:8333", i/128+60, i%128+60)
|
|
|
|
addrs[i], err = n.DeserializeNetAddress(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to turn %s into an address: %v", s, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
srcAddr := &wire.NetAddress{
|
|
|
|
Timestamp: now,
|
|
|
|
Services: 0,
|
|
|
|
IP: net.IPv4(173, 144, 173, 111),
|
|
|
|
Port: 8333,
|
|
|
|
}
|
|
|
|
|
|
|
|
n.AddAddresses(addrs, srcAddr)
|
|
|
|
numAddrs := n.NumAddresses()
|
|
|
|
if numAddrs > addrsToAdd {
|
|
|
|
t.Errorf("Number of addresses is too many %d vs %d", numAddrs, addrsToAdd)
|
|
|
|
}
|
|
|
|
|
|
|
|
b = n.NeedMoreAddresses()
|
|
|
|
if b == true {
|
|
|
|
t.Errorf("Expected that we don't need more addresses")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGood(t *testing.T) {
|
|
|
|
n := addrmgr.New("testgood", lookupFunc)
|
|
|
|
addrsToAdd := 64 * 64
|
|
|
|
addrs := make([]*wire.NetAddress, addrsToAdd)
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for i := 0; i < addrsToAdd; i++ {
|
|
|
|
s := fmt.Sprintf("%d.173.147.%d:8333", i/64+60, i%64+60)
|
|
|
|
addrs[i], err = n.DeserializeNetAddress(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to turn %s into an address: %v", s, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
srcAddr := &wire.NetAddress{
|
|
|
|
Timestamp: now,
|
|
|
|
Services: 0,
|
|
|
|
IP: net.IPv4(173, 144, 173, 111),
|
|
|
|
Port: 8333,
|
|
|
|
}
|
|
|
|
|
|
|
|
n.AddAddresses(addrs, srcAddr)
|
|
|
|
for _, addr := range addrs {
|
|
|
|
n.Good(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
numAddrs := n.NumAddresses()
|
|
|
|
if numAddrs >= addrsToAdd {
|
|
|
|
t.Errorf("Number of addresses is too many: %d vs %d", numAddrs, addrsToAdd)
|
|
|
|
}
|
|
|
|
|
|
|
|
numCache := len(n.AddressCache())
|
|
|
|
if numCache >= numAddrs/4 {
|
|
|
|
t.Errorf("Number of addresses in cache: got %d, want %d", numCache, numAddrs/4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 15:47:42 +02:00
|
|
|
func TestGetAddress(t *testing.T) {
|
2015-02-27 20:34:09 +01:00
|
|
|
n := addrmgr.New("testgetaddress", lookupFunc)
|
|
|
|
|
|
|
|
// Get an address from an empty set (should error)
|
2014-07-06 15:47:42 +02:00
|
|
|
if rv := n.GetAddress("any", 10); rv != nil {
|
|
|
|
t.Errorf("GetAddress failed: got: %v want: %v\n", rv, nil)
|
2013-10-02 23:34:25 +02:00
|
|
|
}
|
2015-02-27 20:34:09 +01:00
|
|
|
|
|
|
|
// Add a new address and get it
|
|
|
|
err := n.AddAddressByIP(someIP + ":8333")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Adding address failed: %v", err)
|
|
|
|
}
|
|
|
|
ka := n.GetAddress("any", 120) // 100 bias is max, but shouldn't error
|
|
|
|
if ka == nil {
|
|
|
|
t.Fatalf("Did not get an address where there is one in the pool")
|
|
|
|
}
|
|
|
|
if ka.NetAddress().IP.String() != someIP {
|
|
|
|
t.Errorf("Wrong IP: got %v, want %v", ka.NetAddress().IP.String(), someIP)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark this as a good address and get it
|
|
|
|
n.Good(ka.NetAddress())
|
|
|
|
ka = n.GetAddress("any", -10) // 0 bias is min, but shouldn't error
|
|
|
|
if ka == nil {
|
|
|
|
t.Fatalf("Did not get an address where there is one in the pool")
|
|
|
|
}
|
|
|
|
if ka.NetAddress().IP.String() != someIP {
|
|
|
|
t.Errorf("Wrong IP: got %v, want %v", ka.NetAddress().IP.String(), someIP)
|
|
|
|
}
|
|
|
|
|
|
|
|
numAddrs := n.NumAddresses()
|
|
|
|
if numAddrs != 1 {
|
|
|
|
t.Errorf("Wrong number of addresses: got %d, want %d", numAddrs, 1)
|
|
|
|
}
|
2014-07-06 15:47:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetBestLocalAddress(t *testing.T) {
|
2015-02-05 22:16:39 +01:00
|
|
|
localAddrs := []wire.NetAddress{
|
2014-07-06 15:47:42 +02:00
|
|
|
{IP: net.ParseIP("192.168.0.100")},
|
|
|
|
{IP: net.ParseIP("::1")},
|
|
|
|
{IP: net.ParseIP("fe80::1")},
|
|
|
|
{IP: net.ParseIP("2001:470::1")},
|
2013-10-02 23:34:25 +02:00
|
|
|
}
|
2014-07-06 15:47:42 +02:00
|
|
|
|
|
|
|
var tests = []struct {
|
2015-02-05 22:16:39 +01:00
|
|
|
remoteAddr wire.NetAddress
|
2015-02-27 20:34:09 +01:00
|
|
|
want0 wire.NetAddress
|
2015-02-05 22:16:39 +01:00
|
|
|
want1 wire.NetAddress
|
|
|
|
want2 wire.NetAddress
|
|
|
|
want3 wire.NetAddress
|
2014-07-06 15:47:42 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
// Remote connection from public IPv4
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.NetAddress{IP: net.ParseIP("204.124.8.1")},
|
|
|
|
wire.NetAddress{IP: net.IPv4zero},
|
2015-02-27 20:34:09 +01:00
|
|
|
wire.NetAddress{IP: net.IPv4zero},
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.NetAddress{IP: net.ParseIP("204.124.8.100")},
|
|
|
|
wire.NetAddress{IP: net.ParseIP("fd87:d87e:eb43:25::1")},
|
2014-07-06 15:47:42 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Remote connection from private IPv4
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.NetAddress{IP: net.ParseIP("172.16.0.254")},
|
|
|
|
wire.NetAddress{IP: net.IPv4zero},
|
|
|
|
wire.NetAddress{IP: net.IPv4zero},
|
|
|
|
wire.NetAddress{IP: net.IPv4zero},
|
2015-02-27 20:34:09 +01:00
|
|
|
wire.NetAddress{IP: net.IPv4zero},
|
2014-07-06 15:47:42 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// Remote connection from public IPv6
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.NetAddress{IP: net.ParseIP("2602:100:abcd::102")},
|
2015-02-27 20:34:09 +01:00
|
|
|
wire.NetAddress{IP: net.IPv6zero},
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.NetAddress{IP: net.ParseIP("2001:470::1")},
|
|
|
|
wire.NetAddress{IP: net.ParseIP("2001:470::1")},
|
|
|
|
wire.NetAddress{IP: net.ParseIP("2001:470::1")},
|
2014-07-06 15:47:42 +02:00
|
|
|
},
|
|
|
|
/* XXX
|
|
|
|
{
|
|
|
|
// Remote connection from Tor
|
2015-02-05 22:16:39 +01:00
|
|
|
wire.NetAddress{IP: net.ParseIP("fd87:d87e:eb43::100")},
|
|
|
|
wire.NetAddress{IP: net.IPv4zero},
|
|
|
|
wire.NetAddress{IP: net.ParseIP("204.124.8.100")},
|
|
|
|
wire.NetAddress{IP: net.ParseIP("fd87:d87e:eb43:25::1")},
|
2014-07-06 15:47:42 +02:00
|
|
|
},
|
|
|
|
*/
|
2013-10-02 23:34:25 +02:00
|
|
|
}
|
2014-07-06 15:47:42 +02:00
|
|
|
|
2015-02-27 20:34:09 +01:00
|
|
|
amgr := addrmgr.New("testgetbestlocaladdress", nil)
|
|
|
|
|
|
|
|
// Test against default when there's no address
|
|
|
|
for x, test := range tests {
|
|
|
|
got := amgr.GetBestLocalAddress(&test.remoteAddr)
|
|
|
|
if !test.want0.IP.Equal(got.IP) {
|
|
|
|
t.Errorf("TestGetBestLocalAddress test1 #%d failed for remote address %s: want %s got %s",
|
|
|
|
x, test.remoteAddr.IP, test.want1.IP, got.IP)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-06 15:47:42 +02:00
|
|
|
for _, localAddr := range localAddrs {
|
|
|
|
amgr.AddLocalAddress(&localAddr, addrmgr.InterfacePrio)
|
2013-10-02 23:34:25 +02:00
|
|
|
}
|
2014-07-06 15:47:42 +02:00
|
|
|
|
|
|
|
// Test against want1
|
|
|
|
for x, test := range tests {
|
|
|
|
got := amgr.GetBestLocalAddress(&test.remoteAddr)
|
|
|
|
if !test.want1.IP.Equal(got.IP) {
|
|
|
|
t.Errorf("TestGetBestLocalAddress test1 #%d failed for remote address %s: want %s got %s",
|
|
|
|
x, test.remoteAddr.IP, test.want1.IP, got.IP)
|
2013-10-02 23:34:25 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2014-07-06 15:47:42 +02:00
|
|
|
|
|
|
|
// Add a public IP to the list of local addresses.
|
2015-02-05 22:16:39 +01:00
|
|
|
localAddr := wire.NetAddress{IP: net.ParseIP("204.124.8.100")}
|
2014-07-06 15:47:42 +02:00
|
|
|
amgr.AddLocalAddress(&localAddr, addrmgr.InterfacePrio)
|
|
|
|
|
|
|
|
// Test against want2
|
|
|
|
for x, test := range tests {
|
|
|
|
got := amgr.GetBestLocalAddress(&test.remoteAddr)
|
|
|
|
if !test.want2.IP.Equal(got.IP) {
|
|
|
|
t.Errorf("TestGetBestLocalAddress test2 #%d failed for remote address %s: want %s got %s",
|
|
|
|
x, test.remoteAddr.IP, test.want2.IP, got.IP)
|
2013-10-02 23:34:25 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2014-07-06 15:47:42 +02:00
|
|
|
/*
|
|
|
|
// Add a tor generated IP address
|
2015-02-05 22:16:39 +01:00
|
|
|
localAddr = wire.NetAddress{IP: net.ParseIP("fd87:d87e:eb43:25::1")}
|
2014-07-06 15:47:42 +02:00
|
|
|
amgr.AddLocalAddress(&localAddr, addrmgr.ManualPrio)
|
|
|
|
|
|
|
|
// Test against want3
|
|
|
|
for x, test := range tests {
|
|
|
|
got := amgr.GetBestLocalAddress(&test.remoteAddr)
|
|
|
|
if !test.want3.IP.Equal(got.IP) {
|
|
|
|
t.Errorf("TestGetBestLocalAddress test3 #%d failed for remote address %s: want %s got %s",
|
|
|
|
x, test.remoteAddr.IP, test.want3.IP, got.IP)
|
|
|
|
continue
|
|
|
|
}
|
2013-10-02 23:34:25 +02:00
|
|
|
}
|
2014-07-06 15:47:42 +02:00
|
|
|
*/
|
2013-10-02 23:34:25 +02:00
|
|
|
}
|
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
func TestNetAddressKey(t *testing.T) {
|
|
|
|
addNaTests()
|
|
|
|
|
|
|
|
t.Logf("Running %d tests", len(naTests))
|
|
|
|
for i, test := range naTests {
|
2014-07-06 08:04:24 +02:00
|
|
|
key := addrmgr.NetAddressKey(&test.in)
|
2013-08-06 23:55:22 +02:00
|
|
|
if key != test.want {
|
|
|
|
t.Errorf("NetAddressKey #%d\n got: %s want: %s", i, key, test.want)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 23:34:25 +02:00
|
|
|
|
2013-08-06 23:55:22 +02:00
|
|
|
}
|