github.com/ethersphere/bee/v2@v2.2.0/pkg/swarm/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 swarm_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/ethersphere/bee/v2/pkg/swarm"
    11  )
    12  
    13  func Test_ContainsAddress(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	addrs := swarm.RandAddresses(t, 10)
    17  	tt := []struct {
    18  		addresses []swarm.Address
    19  		search    swarm.Address
    20  		contains  bool
    21  	}{
    22  		{addresses: nil, search: swarm.Address{}},
    23  		{addresses: nil, search: swarm.RandAddress(t)},
    24  		{addresses: make([]swarm.Address, 10), search: swarm.Address{}, contains: true},
    25  		{addresses: swarm.RandAddresses(t, 0), search: swarm.RandAddress(t)},
    26  		{addresses: swarm.RandAddresses(t, 10), search: swarm.RandAddress(t)},
    27  		{addresses: addrs, search: addrs[0], contains: true},
    28  		{addresses: addrs, search: addrs[1], contains: true},
    29  		{addresses: addrs, search: addrs[3], contains: true},
    30  		{addresses: addrs, search: addrs[9], contains: true},
    31  	}
    32  
    33  	for _, tc := range tt {
    34  		contains := swarm.ContainsAddress(tc.addresses, tc.search)
    35  		if contains != tc.contains {
    36  			t.Fatalf("got %v, want %v", contains, tc.contains)
    37  		}
    38  	}
    39  }
    40  
    41  func Test_IndexOfAddress(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	addrs := swarm.RandAddresses(t, 10)
    45  	tt := []struct {
    46  		addresses []swarm.Address
    47  		search    swarm.Address
    48  		result    int
    49  	}{
    50  		{addresses: nil, search: swarm.Address{}, result: -1},
    51  		{addresses: nil, search: swarm.RandAddress(t), result: -1},
    52  		{addresses: swarm.RandAddresses(t, 0), search: swarm.RandAddress(t), result: -1},
    53  		{addresses: swarm.RandAddresses(t, 10), search: swarm.RandAddress(t), result: -1},
    54  		{addresses: addrs, search: addrs[0], result: 0},
    55  		{addresses: addrs, search: addrs[1], result: 1},
    56  		{addresses: addrs, search: addrs[3], result: 3},
    57  		{addresses: addrs, search: addrs[9], result: 9},
    58  	}
    59  
    60  	for _, tc := range tt {
    61  		result := swarm.IndexOfAddress(tc.addresses, tc.search)
    62  		if result != tc.result {
    63  			t.Fatalf("got %v, want %v", result, tc.result)
    64  		}
    65  	}
    66  }
    67  
    68  func Test_RemoveAddress(t *testing.T) {
    69  	t.Parallel()
    70  
    71  	addrs := swarm.RandAddresses(t, 10)
    72  	tt := []struct {
    73  		addresses []swarm.Address
    74  		remove    swarm.Address
    75  	}{
    76  		{addresses: nil, remove: swarm.Address{}},
    77  		{addresses: nil, remove: swarm.RandAddress(t)},
    78  		{addresses: swarm.RandAddresses(t, 0), remove: swarm.RandAddress(t)},
    79  		{addresses: swarm.RandAddresses(t, 10), remove: swarm.RandAddress(t)},
    80  		{addresses: addrs, remove: addrs[0]},
    81  		{addresses: addrs, remove: addrs[1]},
    82  		{addresses: addrs, remove: addrs[3]},
    83  		{addresses: addrs, remove: addrs[9]},
    84  		{addresses: addrs, remove: addrs[9]},
    85  	}
    86  
    87  	for i, tc := range tt {
    88  		contains := swarm.ContainsAddress(tc.addresses, tc.remove)
    89  		containsAfterRemove := swarm.ContainsAddress(
    90  			swarm.RemoveAddress(cloneAddresses(tc.addresses), tc.remove),
    91  			tc.remove,
    92  		)
    93  
    94  		if contains && containsAfterRemove {
    95  			t.Fatalf("%d %d  address should be removed", len(tc.addresses), i)
    96  		}
    97  	}
    98  }
    99  
   100  func Test_IndexOfChunkWithAddress(t *testing.T) {
   101  	t.Parallel()
   102  
   103  	chunks := []swarm.Chunk{
   104  		swarm.NewChunk(swarm.RandAddress(t), nil),
   105  		swarm.NewChunk(swarm.RandAddress(t), nil),
   106  		swarm.NewChunk(swarm.RandAddress(t), nil),
   107  	}
   108  	tt := []struct {
   109  		chunks  []swarm.Chunk
   110  		address swarm.Address
   111  		result  int
   112  	}{
   113  		{chunks: nil, address: swarm.Address{}, result: -1},
   114  		{chunks: nil, address: swarm.RandAddress(t), result: -1},
   115  		{chunks: make([]swarm.Chunk, 0), address: swarm.RandAddress(t), result: -1},
   116  		{chunks: make([]swarm.Chunk, 10), address: swarm.RandAddress(t), result: -1},
   117  		{chunks: make([]swarm.Chunk, 10), address: swarm.Address{}, result: -1},
   118  		{chunks: chunks, address: swarm.RandAddress(t), result: -1},
   119  		{chunks: chunks, address: chunks[0].Address(), result: 0},
   120  		{chunks: chunks, address: chunks[1].Address(), result: 1},
   121  		{chunks: chunks, address: chunks[2].Address(), result: 2},
   122  	}
   123  
   124  	for _, tc := range tt {
   125  		result := swarm.IndexOfChunkWithAddress(tc.chunks, tc.address)
   126  		if result != tc.result {
   127  			t.Fatalf("got %v, want %v", result, tc.result)
   128  		}
   129  	}
   130  }
   131  
   132  func Test_ContainsChunkWithData(t *testing.T) {
   133  	t.Parallel()
   134  
   135  	chunks := []swarm.Chunk{
   136  		swarm.NewChunk(swarm.RandAddress(t), nil),
   137  		swarm.NewChunk(swarm.RandAddress(t), []byte{1, 1, 1}),
   138  		swarm.NewChunk(swarm.RandAddress(t), []byte{2, 2, 2}),
   139  	}
   140  	tt := []struct {
   141  		chunks   []swarm.Chunk
   142  		data     []byte
   143  		contains bool
   144  	}{
   145  		// contains
   146  		{chunks: chunks, data: nil, contains: true},
   147  		{chunks: chunks, data: []byte{1, 1, 1}, contains: true},
   148  		{chunks: chunks, data: []byte{2, 2, 2}, contains: true},
   149  
   150  		// do not contain
   151  		{chunks: nil, data: nil},
   152  		{chunks: chunks, data: []byte{3, 3, 3}},
   153  		{chunks: chunks, data: []byte{1}},
   154  		{chunks: chunks, data: []byte{2}},
   155  		{chunks: make([]swarm.Chunk, 0), data: []byte{1, 1, 1}},
   156  		{chunks: make([]swarm.Chunk, 10), data: nil},
   157  	}
   158  
   159  	for _, tc := range tt {
   160  		contains := swarm.ContainsChunkWithData(tc.chunks, tc.data)
   161  		if contains != tc.contains {
   162  			t.Fatalf("got %v, want %v", contains, tc.contains)
   163  		}
   164  	}
   165  }
   166  
   167  func Test_FindStampWithBatchID(t *testing.T) {
   168  	t.Parallel()
   169  
   170  	stamps := []swarm.Stamp{
   171  		makeStamp(t),
   172  		makeStamp(t),
   173  		makeStamp(t),
   174  	}
   175  	tt := []struct {
   176  		stamps   []swarm.Stamp
   177  		batchID  []byte
   178  		contains bool
   179  	}{
   180  		// contains
   181  		{stamps: stamps, batchID: stamps[0].BatchID(), contains: true},
   182  		{stamps: stamps, batchID: stamps[1].BatchID(), contains: true},
   183  		{stamps: stamps, batchID: stamps[2].BatchID(), contains: true},
   184  
   185  		// do not contain
   186  		{stamps: nil, batchID: nil},
   187  		{stamps: nil, batchID: makeStamp(t).BatchID()},
   188  		{stamps: make([]swarm.Stamp, 0), batchID: swarm.RandBatchID(t)},
   189  		{stamps: make([]swarm.Stamp, 10), batchID: swarm.RandBatchID(t)},
   190  		{stamps: make([]swarm.Stamp, 10), batchID: nil},
   191  		{stamps: stamps, batchID: swarm.RandBatchID(t)},
   192  	}
   193  
   194  	for _, tc := range tt {
   195  		st, found := swarm.FindStampWithBatchID(tc.stamps, tc.batchID)
   196  		if found != tc.contains {
   197  			t.Fatalf("got %v, want %v", found, tc.contains)
   198  		}
   199  		if found && st == nil {
   200  			t.Fatal("stamp should not be nil")
   201  		}
   202  	}
   203  }
   204  
   205  func cloneAddresses(addrs []swarm.Address) []swarm.Address {
   206  	result := make([]swarm.Address, len(addrs))
   207  	for i := 0; i < len(addrs); i++ {
   208  		result[i] = addrs[i].Clone()
   209  	}
   210  	return result
   211  }
   212  
   213  func makeStamp(t *testing.T) swarm.Stamp {
   214  	t.Helper()
   215  
   216  	return stamp{
   217  		batchID: swarm.RandBatchID(t),
   218  	}
   219  }
   220  
   221  type stamp struct {
   222  	batchID []byte
   223  }
   224  
   225  func (s stamp) BatchID() []byte { return s.batchID }
   226  
   227  func (s stamp) Index() []byte { return nil }
   228  
   229  func (s stamp) Sig() []byte { return nil }
   230  
   231  func (s stamp) Timestamp() []byte { return nil }
   232  
   233  func (s stamp) MarshalBinary() (data []byte, err error) { return nil, nil }
   234  
   235  func (s stamp) UnmarshalBinary(data []byte) error { return nil }
   236  
   237  func (s stamp) Clone() swarm.Stamp { return s }
   238  
   239  func (s stamp) Hash() ([]byte, error) { return nil, nil }