github.com/ethersphere/bee/v2@v2.2.0/pkg/bzz/utilities_test.go (about)

     1  // Copyright 2022 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bzz_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	ma "github.com/multiformats/go-multiaddr"
    11  
    12  	"github.com/ethersphere/bee/v2/pkg/bzz"
    13  	"github.com/ethersphere/bee/v2/pkg/swarm"
    14  	"github.com/ethersphere/bee/v2/pkg/util/testutil"
    15  )
    16  
    17  func Test_ContainsAddress(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	addrs := makeAddreses(t, 10)
    21  	tt := []struct {
    22  		addresses []bzz.Address
    23  		search    bzz.Address
    24  		contains  bool
    25  	}{
    26  		{addresses: nil, search: bzz.Address{}},
    27  		{addresses: nil, search: makeAddress(t)},
    28  		{addresses: make([]bzz.Address, 10), search: bzz.Address{}, contains: true},
    29  		{addresses: makeAddreses(t, 0), search: makeAddress(t)},
    30  		{addresses: makeAddreses(t, 10), search: makeAddress(t)},
    31  		{addresses: addrs, search: addrs[0], contains: true},
    32  		{addresses: addrs, search: addrs[1], contains: true},
    33  		{addresses: addrs, search: addrs[3], contains: true},
    34  		{addresses: addrs, search: addrs[9], contains: true},
    35  	}
    36  
    37  	for _, tc := range tt {
    38  		contains := bzz.ContainsAddress(tc.addresses, &tc.search)
    39  		if contains != tc.contains {
    40  			t.Fatalf("got %v, want %v", contains, tc.contains)
    41  		}
    42  	}
    43  }
    44  func makeAddreses(t *testing.T, count int) []bzz.Address {
    45  	t.Helper()
    46  
    47  	result := make([]bzz.Address, count)
    48  	for i := 0; i < count; i++ {
    49  		result[i] = makeAddress(t)
    50  	}
    51  	return result
    52  }
    53  
    54  func makeAddress(t *testing.T) bzz.Address {
    55  	t.Helper()
    56  
    57  	multiaddr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/1634/p2p/16Uiu2HAkx8ULY8cTXhdVAcMmLcH9AsTKz6uBQ7DPLKRjMLgBVYkA")
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	return bzz.Address{
    63  		Underlay:        multiaddr,
    64  		Overlay:         swarm.RandAddress(t),
    65  		Signature:       testutil.RandBytes(t, 12),
    66  		Nonce:           testutil.RandBytes(t, 12),
    67  		EthereumAddress: testutil.RandBytes(t, 32),
    68  	}
    69  }