github.com/btcsuite/btcd@v0.24.0/addrmgr/addrmanager_internal_test.go (about)

     1  package addrmgr
     2  
     3  import (
     4  	"io/ioutil"
     5  	"math/rand"
     6  	"net"
     7  	"os"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/btcsuite/btcd/wire"
    12  )
    13  
    14  // randAddr generates a *wire.NetAddressV2 backed by a random IPv4/IPv6
    15  // address.
    16  func randAddr(t *testing.T) *wire.NetAddressV2 {
    17  	t.Helper()
    18  
    19  	ipv4 := rand.Intn(2) == 0
    20  	var ip net.IP
    21  	if ipv4 {
    22  		var b [4]byte
    23  		if _, err := rand.Read(b[:]); err != nil {
    24  			t.Fatal(err)
    25  		}
    26  		ip = b[:]
    27  	} else {
    28  		var b [16]byte
    29  		if _, err := rand.Read(b[:]); err != nil {
    30  			t.Fatal(err)
    31  		}
    32  		ip = b[:]
    33  	}
    34  
    35  	services := wire.ServiceFlag(rand.Uint64())
    36  	port := uint16(rand.Uint32())
    37  
    38  	return wire.NetAddressV2FromBytes(
    39  		time.Now(), services, ip, port,
    40  	)
    41  }
    42  
    43  // assertAddr ensures that the two addresses match. The timestamp is not
    44  // checked as it does not affect uniquely identifying a specific address.
    45  func assertAddr(t *testing.T, got, expected *wire.NetAddressV2) {
    46  	if got.Services != expected.Services {
    47  		t.Fatalf("expected address services %v, got %v",
    48  			expected.Services, got.Services)
    49  	}
    50  	gotAddr := got.Addr.String()
    51  	expectedAddr := expected.Addr.String()
    52  	if gotAddr != expectedAddr {
    53  		t.Fatalf("expected address IP %v, got %v", expectedAddr,
    54  			gotAddr)
    55  	}
    56  	if got.Port != expected.Port {
    57  		t.Fatalf("expected address port %d, got %d", expected.Port,
    58  			got.Port)
    59  	}
    60  }
    61  
    62  // assertAddrs ensures that the manager's address cache matches the given
    63  // expected addresses.
    64  func assertAddrs(t *testing.T, addrMgr *AddrManager,
    65  	expectedAddrs map[string]*wire.NetAddressV2) {
    66  
    67  	t.Helper()
    68  
    69  	addrs := addrMgr.getAddresses()
    70  
    71  	if len(addrs) != len(expectedAddrs) {
    72  		t.Fatalf("expected to find %d addresses, found %d",
    73  			len(expectedAddrs), len(addrs))
    74  	}
    75  
    76  	for _, addr := range addrs {
    77  		addrStr := NetAddressKey(addr)
    78  		expectedAddr, ok := expectedAddrs[addrStr]
    79  		if !ok {
    80  			t.Fatalf("expected to find address %v", addrStr)
    81  		}
    82  
    83  		assertAddr(t, addr, expectedAddr)
    84  	}
    85  }
    86  
    87  // TestAddrManagerSerialization ensures that we can properly serialize and
    88  // deserialize the manager's current address cache.
    89  func TestAddrManagerSerialization(t *testing.T) {
    90  	t.Parallel()
    91  
    92  	// We'll start by creating our address manager backed by a temporary
    93  	// directory.
    94  	tempDir, err := ioutil.TempDir("", "addrmgr")
    95  	if err != nil {
    96  		t.Fatalf("unable to create temp dir: %v", err)
    97  	}
    98  	defer os.RemoveAll(tempDir)
    99  
   100  	addrMgr := New(tempDir, nil)
   101  
   102  	// We'll be adding 5 random addresses to the manager.
   103  	const numAddrs = 5
   104  
   105  	expectedAddrs := make(map[string]*wire.NetAddressV2, numAddrs)
   106  	for i := 0; i < numAddrs; i++ {
   107  		addr := randAddr(t)
   108  		expectedAddrs[NetAddressKey(addr)] = addr
   109  		addrMgr.AddAddress(addr, randAddr(t))
   110  	}
   111  
   112  	// Now that the addresses have been added, we should be able to retrieve
   113  	// them.
   114  	assertAddrs(t, addrMgr, expectedAddrs)
   115  
   116  	// Then, we'll persist these addresses to disk and restart the address
   117  	// manager.
   118  	addrMgr.savePeers()
   119  	addrMgr = New(tempDir, nil)
   120  
   121  	// Finally, we'll read all of the addresses from disk and ensure they
   122  	// match as expected.
   123  	addrMgr.loadPeers()
   124  	assertAddrs(t, addrMgr, expectedAddrs)
   125  }
   126  
   127  // TestAddrManagerV1ToV2 ensures that we can properly upgrade the serialized
   128  // version of the address manager from v1 to v2.
   129  func TestAddrManagerV1ToV2(t *testing.T) {
   130  	t.Parallel()
   131  
   132  	// We'll start by creating our address manager backed by a temporary
   133  	// directory.
   134  	tempDir, err := ioutil.TempDir("", "addrmgr")
   135  	if err != nil {
   136  		t.Fatalf("unable to create temp dir: %v", err)
   137  	}
   138  	defer os.RemoveAll(tempDir)
   139  
   140  	addrMgr := New(tempDir, nil)
   141  
   142  	// As we're interested in testing the upgrade path from v1 to v2, we'll
   143  	// override the manager's current version.
   144  	addrMgr.version = 1
   145  
   146  	// We'll be adding 5 random addresses to the manager. Since this is v1,
   147  	// each addresses' services will not be stored.
   148  	const numAddrs = 5
   149  
   150  	expectedAddrs := make(map[string]*wire.NetAddressV2, numAddrs)
   151  	for i := 0; i < numAddrs; i++ {
   152  		addr := randAddr(t)
   153  		expectedAddrs[NetAddressKey(addr)] = addr
   154  		addrMgr.AddAddress(addr, randAddr(t))
   155  	}
   156  
   157  	// Then, we'll persist these addresses to disk and restart the address
   158  	// manager - overriding its version back to v1.
   159  	addrMgr.savePeers()
   160  	addrMgr = New(tempDir, nil)
   161  	addrMgr.version = 1
   162  
   163  	// When we read all of the addresses back from disk, we should expect to
   164  	// find all of them, but their services will be set to a default of
   165  	// SFNodeNetwork since they were not previously stored. After ensuring
   166  	// that this default is set, we'll override each addresses' services
   167  	// with the original value from when they were created.
   168  	addrMgr.loadPeers()
   169  	addrs := addrMgr.getAddresses()
   170  	if len(addrs) != len(expectedAddrs) {
   171  		t.Fatalf("expected to find %d adddresses, found %d",
   172  			len(expectedAddrs), len(addrs))
   173  	}
   174  	for _, addr := range addrs {
   175  		addrStr := NetAddressKey(addr)
   176  		expectedAddr, ok := expectedAddrs[addrStr]
   177  		if !ok {
   178  			t.Fatalf("expected to find address %v", addrStr)
   179  		}
   180  
   181  		if addr.Services != wire.SFNodeNetwork {
   182  			t.Fatalf("expected address services to be %v, got %v",
   183  				wire.SFNodeNetwork, addr.Services)
   184  		}
   185  
   186  		addrMgr.SetServices(addr, expectedAddr.Services)
   187  	}
   188  
   189  	// We'll also bump up the manager's version to v2, which should signal
   190  	// that it should include the address services when persisting its
   191  	// state.
   192  	addrMgr.version = 2
   193  	addrMgr.savePeers()
   194  
   195  	// Finally, we'll recreate the manager and ensure that the services were
   196  	// persisted correctly.
   197  	addrMgr = New(tempDir, nil)
   198  	addrMgr.loadPeers()
   199  	assertAddrs(t, addrMgr, expectedAddrs)
   200  }