github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/addrmgr/addrmanager_test.go (about)

     1  // Copyright (c) 2013-2014 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package addrmgr_test
     7  
     8  import (
     9  	"errors"
    10  	"fmt"
    11  	"net"
    12  	"reflect"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/dashpay/godash/addrmgr"
    17  	"github.com/dashpay/godash/wire"
    18  )
    19  
    20  // naTest is used to describe a test to be performed against the NetAddressKey
    21  // method.
    22  type naTest struct {
    23  	in   wire.NetAddress
    24  	want string
    25  }
    26  
    27  // naTests houses all of the tests to be performed against the NetAddressKey
    28  // method.
    29  var naTests = make([]naTest, 0)
    30  
    31  // Put some IP in here for convenience. Points to google.
    32  var someIP = "173.194.115.66"
    33  
    34  // addNaTests
    35  func addNaTests() {
    36  	// IPv4
    37  	// Localhost
    38  	addNaTest("127.0.0.1", 8333, "127.0.0.1:8333")
    39  	addNaTest("127.0.0.1", 8334, "127.0.0.1:8334")
    40  
    41  	// Class A
    42  	addNaTest("1.0.0.1", 8333, "1.0.0.1:8333")
    43  	addNaTest("2.2.2.2", 8334, "2.2.2.2:8334")
    44  	addNaTest("27.253.252.251", 8335, "27.253.252.251:8335")
    45  	addNaTest("123.3.2.1", 8336, "123.3.2.1:8336")
    46  
    47  	// Private Class A
    48  	addNaTest("10.0.0.1", 8333, "10.0.0.1:8333")
    49  	addNaTest("10.1.1.1", 8334, "10.1.1.1:8334")
    50  	addNaTest("10.2.2.2", 8335, "10.2.2.2:8335")
    51  	addNaTest("10.10.10.10", 8336, "10.10.10.10:8336")
    52  
    53  	// Class B
    54  	addNaTest("128.0.0.1", 8333, "128.0.0.1:8333")
    55  	addNaTest("129.1.1.1", 8334, "129.1.1.1:8334")
    56  	addNaTest("180.2.2.2", 8335, "180.2.2.2:8335")
    57  	addNaTest("191.10.10.10", 8336, "191.10.10.10:8336")
    58  
    59  	// Private Class B
    60  	addNaTest("172.16.0.1", 8333, "172.16.0.1:8333")
    61  	addNaTest("172.16.1.1", 8334, "172.16.1.1:8334")
    62  	addNaTest("172.16.2.2", 8335, "172.16.2.2:8335")
    63  	addNaTest("172.16.172.172", 8336, "172.16.172.172:8336")
    64  
    65  	// Class C
    66  	addNaTest("193.0.0.1", 8333, "193.0.0.1:8333")
    67  	addNaTest("200.1.1.1", 8334, "200.1.1.1:8334")
    68  	addNaTest("205.2.2.2", 8335, "205.2.2.2:8335")
    69  	addNaTest("223.10.10.10", 8336, "223.10.10.10:8336")
    70  
    71  	// Private Class C
    72  	addNaTest("192.168.0.1", 8333, "192.168.0.1:8333")
    73  	addNaTest("192.168.1.1", 8334, "192.168.1.1:8334")
    74  	addNaTest("192.168.2.2", 8335, "192.168.2.2:8335")
    75  	addNaTest("192.168.192.192", 8336, "192.168.192.192:8336")
    76  
    77  	// IPv6
    78  	// Localhost
    79  	addNaTest("::1", 8333, "[::1]:8333")
    80  	addNaTest("fe80::1", 8334, "[fe80::1]:8334")
    81  
    82  	// Link-local
    83  	addNaTest("fe80::1:1", 8333, "[fe80::1:1]:8333")
    84  	addNaTest("fe91::2:2", 8334, "[fe91::2:2]:8334")
    85  	addNaTest("fea2::3:3", 8335, "[fea2::3:3]:8335")
    86  	addNaTest("feb3::4:4", 8336, "[feb3::4:4]:8336")
    87  
    88  	// Site-local
    89  	addNaTest("fec0::1:1", 8333, "[fec0::1:1]:8333")
    90  	addNaTest("fed1::2:2", 8334, "[fed1::2:2]:8334")
    91  	addNaTest("fee2::3:3", 8335, "[fee2::3:3]:8335")
    92  	addNaTest("fef3::4:4", 8336, "[fef3::4:4]:8336")
    93  }
    94  
    95  func addNaTest(ip string, port uint16, want string) {
    96  	nip := net.ParseIP(ip)
    97  	na := wire.NetAddress{
    98  		Timestamp: time.Now(),
    99  		Services:  wire.SFNodeNetwork,
   100  		IP:        nip,
   101  		Port:      port,
   102  	}
   103  	test := naTest{na, want}
   104  	naTests = append(naTests, test)
   105  }
   106  
   107  func lookupFunc(host string) ([]net.IP, error) {
   108  	return nil, errors.New("not implemented")
   109  }
   110  
   111  func TestStartStop(t *testing.T) {
   112  	n := addrmgr.New("teststartstop", lookupFunc)
   113  	n.Start()
   114  	err := n.Stop()
   115  	if err != nil {
   116  		t.Fatalf("Address Manager failed to stop: %v", err)
   117  	}
   118  }
   119  
   120  func TestAddAddressByIP(t *testing.T) {
   121  	fmtErr := fmt.Errorf("")
   122  	addrErr := &net.AddrError{}
   123  	var tests = []struct {
   124  		addrIP string
   125  		err    error
   126  	}{
   127  		{
   128  			someIP + ":8333",
   129  			nil,
   130  		},
   131  		{
   132  			someIP,
   133  			addrErr,
   134  		},
   135  		{
   136  			someIP[:12] + ":8333",
   137  			fmtErr,
   138  		},
   139  		{
   140  			someIP + ":abcd",
   141  			fmtErr,
   142  		},
   143  	}
   144  
   145  	amgr := addrmgr.New("testaddressbyip", nil)
   146  	for i, test := range tests {
   147  		err := amgr.AddAddressByIP(test.addrIP)
   148  		if test.err != nil && err == nil {
   149  			t.Errorf("TestGood test %d failed expected an error and got none", i)
   150  			continue
   151  		}
   152  		if test.err == nil && err != nil {
   153  			t.Errorf("TestGood test %d failed expected no error and got one", i)
   154  			continue
   155  		}
   156  		if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
   157  			t.Errorf("TestGood test %d failed got %v, want %v", i,
   158  				reflect.TypeOf(err), reflect.TypeOf(test.err))
   159  			continue
   160  		}
   161  	}
   162  }
   163  
   164  func TestAddLocalAddress(t *testing.T) {
   165  	var tests = []struct {
   166  		address  wire.NetAddress
   167  		priority addrmgr.AddressPriority
   168  		valid    bool
   169  	}{
   170  		{
   171  			wire.NetAddress{IP: net.ParseIP("192.168.0.100")},
   172  			addrmgr.InterfacePrio,
   173  			false,
   174  		},
   175  		{
   176  			wire.NetAddress{IP: net.ParseIP("204.124.1.1")},
   177  			addrmgr.InterfacePrio,
   178  			true,
   179  		},
   180  		{
   181  			wire.NetAddress{IP: net.ParseIP("204.124.1.1")},
   182  			addrmgr.BoundPrio,
   183  			true,
   184  		},
   185  		{
   186  			wire.NetAddress{IP: net.ParseIP("::1")},
   187  			addrmgr.InterfacePrio,
   188  			false,
   189  		},
   190  		{
   191  			wire.NetAddress{IP: net.ParseIP("fe80::1")},
   192  			addrmgr.InterfacePrio,
   193  			false,
   194  		},
   195  		{
   196  			wire.NetAddress{IP: net.ParseIP("2620:100::1")},
   197  			addrmgr.InterfacePrio,
   198  			true,
   199  		},
   200  	}
   201  	amgr := addrmgr.New("testaddlocaladdress", nil)
   202  	for x, test := range tests {
   203  		result := amgr.AddLocalAddress(&test.address, test.priority)
   204  		if result == nil && !test.valid {
   205  			t.Errorf("TestAddLocalAddress test #%d failed: %s should have "+
   206  				"been accepted", x, test.address.IP)
   207  			continue
   208  		}
   209  		if result != nil && test.valid {
   210  			t.Errorf("TestAddLocalAddress test #%d failed: %s should not have "+
   211  				"been accepted", x, test.address.IP)
   212  			continue
   213  		}
   214  	}
   215  }
   216  
   217  func TestAttempt(t *testing.T) {
   218  	n := addrmgr.New("testattempt", lookupFunc)
   219  
   220  	// Add a new address and get it
   221  	err := n.AddAddressByIP(someIP + ":8333")
   222  	if err != nil {
   223  		t.Fatalf("Adding address failed: %v", err)
   224  	}
   225  	ka := n.GetAddress("any")
   226  
   227  	if !ka.LastAttempt().IsZero() {
   228  		t.Errorf("Address should not have attempts, but does")
   229  	}
   230  
   231  	na := ka.NetAddress()
   232  	n.Attempt(na)
   233  
   234  	if ka.LastAttempt().IsZero() {
   235  		t.Errorf("Address should have an attempt, but does not")
   236  	}
   237  }
   238  
   239  func TestConnected(t *testing.T) {
   240  	n := addrmgr.New("testconnected", lookupFunc)
   241  
   242  	// Add a new address and get it
   243  	err := n.AddAddressByIP(someIP + ":8333")
   244  	if err != nil {
   245  		t.Fatalf("Adding address failed: %v", err)
   246  	}
   247  	ka := n.GetAddress("any")
   248  	na := ka.NetAddress()
   249  	na.Timestamp = time.Now().Add(time.Hour * -1) // make it an hour ago
   250  
   251  	n.Connected(na)
   252  
   253  	if !ka.NetAddress().Timestamp.After(na.Timestamp) {
   254  		t.Errorf("Address should have a new timestamp, but does not")
   255  	}
   256  }
   257  
   258  func TestNeedMoreAddresses(t *testing.T) {
   259  	n := addrmgr.New("testneedmoreaddresses", lookupFunc)
   260  	addrsToAdd := 1500
   261  	b := n.NeedMoreAddresses()
   262  	if b == false {
   263  		t.Errorf("Expected that we need more addresses")
   264  	}
   265  	addrs := make([]*wire.NetAddress, addrsToAdd)
   266  	now := time.Now()
   267  
   268  	var err error
   269  	for i := 0; i < addrsToAdd; i++ {
   270  		s := fmt.Sprintf("%d.%d.173.147:8333", i/128+60, i%128+60)
   271  		addrs[i], err = n.DeserializeNetAddress(s)
   272  		if err != nil {
   273  			t.Errorf("Failed to turn %s into an address: %v", s, err)
   274  		}
   275  	}
   276  
   277  	srcAddr := &wire.NetAddress{
   278  		Timestamp: now,
   279  		Services:  0,
   280  		IP:        net.IPv4(173, 144, 173, 111),
   281  		Port:      8333,
   282  	}
   283  
   284  	n.AddAddresses(addrs, srcAddr)
   285  	numAddrs := n.NumAddresses()
   286  	if numAddrs > addrsToAdd {
   287  		t.Errorf("Number of addresses is too many %d vs %d", numAddrs, addrsToAdd)
   288  	}
   289  
   290  	b = n.NeedMoreAddresses()
   291  	if b == true {
   292  		t.Errorf("Expected that we don't need more addresses")
   293  	}
   294  }
   295  
   296  func TestGood(t *testing.T) {
   297  	n := addrmgr.New("testgood", lookupFunc)
   298  	addrsToAdd := 64 * 64
   299  	addrs := make([]*wire.NetAddress, addrsToAdd)
   300  	now := time.Now()
   301  
   302  	var err error
   303  	for i := 0; i < addrsToAdd; i++ {
   304  		s := fmt.Sprintf("%d.173.147.%d:8333", i/64+60, i%64+60)
   305  		addrs[i], err = n.DeserializeNetAddress(s)
   306  		if err != nil {
   307  			t.Errorf("Failed to turn %s into an address: %v", s, err)
   308  		}
   309  	}
   310  
   311  	srcAddr := &wire.NetAddress{
   312  		Timestamp: now,
   313  		Services:  0,
   314  		IP:        net.IPv4(173, 144, 173, 111),
   315  		Port:      8333,
   316  	}
   317  
   318  	n.AddAddresses(addrs, srcAddr)
   319  	for _, addr := range addrs {
   320  		n.Good(addr)
   321  	}
   322  
   323  	numAddrs := n.NumAddresses()
   324  	if numAddrs >= addrsToAdd {
   325  		t.Errorf("Number of addresses is too many: %d vs %d", numAddrs, addrsToAdd)
   326  	}
   327  
   328  	numCache := len(n.AddressCache())
   329  	if numCache >= numAddrs/4 {
   330  		t.Errorf("Number of addresses in cache: got %d, want %d", numCache, numAddrs/4)
   331  	}
   332  }
   333  
   334  func TestGetAddress(t *testing.T) {
   335  	n := addrmgr.New("testgetaddress", lookupFunc)
   336  
   337  	// Get an address from an empty set (should error)
   338  	if rv := n.GetAddress("any"); rv != nil {
   339  		t.Errorf("GetAddress failed: got: %v want: %v\n", rv, nil)
   340  	}
   341  
   342  	// Add a new address and get it
   343  	err := n.AddAddressByIP(someIP + ":8333")
   344  	if err != nil {
   345  		t.Fatalf("Adding address failed: %v", err)
   346  	}
   347  	ka := n.GetAddress("any")
   348  	if ka == nil {
   349  		t.Fatalf("Did not get an address where there is one in the pool")
   350  	}
   351  	if ka.NetAddress().IP.String() != someIP {
   352  		t.Errorf("Wrong IP: got %v, want %v", ka.NetAddress().IP.String(), someIP)
   353  	}
   354  
   355  	// Mark this as a good address and get it
   356  	n.Good(ka.NetAddress())
   357  	ka = n.GetAddress("any")
   358  	if ka == nil {
   359  		t.Fatalf("Did not get an address where there is one in the pool")
   360  	}
   361  	if ka.NetAddress().IP.String() != someIP {
   362  		t.Errorf("Wrong IP: got %v, want %v", ka.NetAddress().IP.String(), someIP)
   363  	}
   364  
   365  	numAddrs := n.NumAddresses()
   366  	if numAddrs != 1 {
   367  		t.Errorf("Wrong number of addresses: got %d, want %d", numAddrs, 1)
   368  	}
   369  }
   370  
   371  func TestGetBestLocalAddress(t *testing.T) {
   372  	localAddrs := []wire.NetAddress{
   373  		{IP: net.ParseIP("192.168.0.100")},
   374  		{IP: net.ParseIP("::1")},
   375  		{IP: net.ParseIP("fe80::1")},
   376  		{IP: net.ParseIP("2001:470::1")},
   377  	}
   378  
   379  	var tests = []struct {
   380  		remoteAddr wire.NetAddress
   381  		want0      wire.NetAddress
   382  		want1      wire.NetAddress
   383  		want2      wire.NetAddress
   384  		want3      wire.NetAddress
   385  	}{
   386  		{
   387  			// Remote connection from public IPv4
   388  			wire.NetAddress{IP: net.ParseIP("204.124.8.1")},
   389  			wire.NetAddress{IP: net.IPv4zero},
   390  			wire.NetAddress{IP: net.IPv4zero},
   391  			wire.NetAddress{IP: net.ParseIP("204.124.8.100")},
   392  			wire.NetAddress{IP: net.ParseIP("fd87:d87e:eb43:25::1")},
   393  		},
   394  		{
   395  			// Remote connection from private IPv4
   396  			wire.NetAddress{IP: net.ParseIP("172.16.0.254")},
   397  			wire.NetAddress{IP: net.IPv4zero},
   398  			wire.NetAddress{IP: net.IPv4zero},
   399  			wire.NetAddress{IP: net.IPv4zero},
   400  			wire.NetAddress{IP: net.IPv4zero},
   401  		},
   402  		{
   403  			// Remote connection from public IPv6
   404  			wire.NetAddress{IP: net.ParseIP("2602:100:abcd::102")},
   405  			wire.NetAddress{IP: net.IPv6zero},
   406  			wire.NetAddress{IP: net.ParseIP("2001:470::1")},
   407  			wire.NetAddress{IP: net.ParseIP("2001:470::1")},
   408  			wire.NetAddress{IP: net.ParseIP("2001:470::1")},
   409  		},
   410  		/* XXX
   411  		{
   412  			// Remote connection from Tor
   413  			wire.NetAddress{IP: net.ParseIP("fd87:d87e:eb43::100")},
   414  			wire.NetAddress{IP: net.IPv4zero},
   415  			wire.NetAddress{IP: net.ParseIP("204.124.8.100")},
   416  			wire.NetAddress{IP: net.ParseIP("fd87:d87e:eb43:25::1")},
   417  		},
   418  		*/
   419  	}
   420  
   421  	amgr := addrmgr.New("testgetbestlocaladdress", nil)
   422  
   423  	// Test against default when there's no address
   424  	for x, test := range tests {
   425  		got := amgr.GetBestLocalAddress(&test.remoteAddr)
   426  		if !test.want0.IP.Equal(got.IP) {
   427  			t.Errorf("TestGetBestLocalAddress test1 #%d failed for remote address %s: want %s got %s",
   428  				x, test.remoteAddr.IP, test.want1.IP, got.IP)
   429  			continue
   430  		}
   431  	}
   432  
   433  	for _, localAddr := range localAddrs {
   434  		amgr.AddLocalAddress(&localAddr, addrmgr.InterfacePrio)
   435  	}
   436  
   437  	// Test against want1
   438  	for x, test := range tests {
   439  		got := amgr.GetBestLocalAddress(&test.remoteAddr)
   440  		if !test.want1.IP.Equal(got.IP) {
   441  			t.Errorf("TestGetBestLocalAddress test1 #%d failed for remote address %s: want %s got %s",
   442  				x, test.remoteAddr.IP, test.want1.IP, got.IP)
   443  			continue
   444  		}
   445  	}
   446  
   447  	// Add a public IP to the list of local addresses.
   448  	localAddr := wire.NetAddress{IP: net.ParseIP("204.124.8.100")}
   449  	amgr.AddLocalAddress(&localAddr, addrmgr.InterfacePrio)
   450  
   451  	// Test against want2
   452  	for x, test := range tests {
   453  		got := amgr.GetBestLocalAddress(&test.remoteAddr)
   454  		if !test.want2.IP.Equal(got.IP) {
   455  			t.Errorf("TestGetBestLocalAddress test2 #%d failed for remote address %s: want %s got %s",
   456  				x, test.remoteAddr.IP, test.want2.IP, got.IP)
   457  			continue
   458  		}
   459  	}
   460  	/*
   461  		// Add a tor generated IP address
   462  		localAddr = wire.NetAddress{IP: net.ParseIP("fd87:d87e:eb43:25::1")}
   463  		amgr.AddLocalAddress(&localAddr, addrmgr.ManualPrio)
   464  
   465  		// Test against want3
   466  		for x, test := range tests {
   467  			got := amgr.GetBestLocalAddress(&test.remoteAddr)
   468  			if !test.want3.IP.Equal(got.IP) {
   469  				t.Errorf("TestGetBestLocalAddress test3 #%d failed for remote address %s: want %s got %s",
   470  					x, test.remoteAddr.IP, test.want3.IP, got.IP)
   471  				continue
   472  			}
   473  		}
   474  	*/
   475  }
   476  
   477  func TestNetAddressKey(t *testing.T) {
   478  	addNaTests()
   479  
   480  	t.Logf("Running %d tests", len(naTests))
   481  	for i, test := range naTests {
   482  		key := addrmgr.NetAddressKey(&test.in)
   483  		if key != test.want {
   484  			t.Errorf("NetAddressKey #%d\n got: %s want: %s", i, key, test.want)
   485  			continue
   486  		}
   487  	}
   488  
   489  }