github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/internal/reserve/items_test.go (about)

     1  // Copyright 2023 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 reserve_test
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/storage"
    12  	"github.com/ethersphere/bee/v2/pkg/storage/storagetest"
    13  	"github.com/ethersphere/bee/v2/pkg/storer/internal/reserve"
    14  	"github.com/ethersphere/bee/v2/pkg/swarm"
    15  )
    16  
    17  func TestReserveItems(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	tests := []struct {
    21  		name string
    22  		test *storagetest.ItemMarshalAndUnmarshalTest
    23  	}{
    24  		{
    25  			name: "BatchRadiusItem",
    26  			test: &storagetest.ItemMarshalAndUnmarshalTest{
    27  				Item: &reserve.BatchRadiusItem{
    28  					BatchID:   storagetest.MaxAddressBytes[:],
    29  					Address:   swarm.NewAddress(storagetest.MaxAddressBytes[:]),
    30  					Bin:       9,
    31  					BinID:     100,
    32  					StampHash: storagetest.MaxAddressBytes[:],
    33  				},
    34  				Factory: func() storage.Item { return new(reserve.BatchRadiusItem) },
    35  			},
    36  		},
    37  		{
    38  			name: "ChunkBinItem",
    39  			test: &storagetest.ItemMarshalAndUnmarshalTest{
    40  				Item: &reserve.ChunkBinItem{
    41  					Address:   swarm.NewAddress(storagetest.MaxAddressBytes[:]),
    42  					BatchID:   storagetest.MaxAddressBytes[:],
    43  					Bin:       9,
    44  					BinID:     100,
    45  					StampHash: storagetest.MaxAddressBytes[:],
    46  				},
    47  				Factory: func() storage.Item { return new(reserve.ChunkBinItem) },
    48  			},
    49  		},
    50  		{
    51  			name: "BinItem",
    52  			test: &storagetest.ItemMarshalAndUnmarshalTest{
    53  				Item: &reserve.BinItem{
    54  					BinID: 100,
    55  				},
    56  				Factory: func() storage.Item { return new(reserve.BinItem) },
    57  			},
    58  		},
    59  		{
    60  			name: "RadiusItem",
    61  			test: &storagetest.ItemMarshalAndUnmarshalTest{
    62  				Item: &reserve.RadiusItem{
    63  					Radius: 9,
    64  				},
    65  				Factory: func() storage.Item { return new(reserve.RadiusItem) },
    66  			},
    67  		},
    68  		{
    69  			name: "BatchRadiusItem zero address",
    70  			test: &storagetest.ItemMarshalAndUnmarshalTest{
    71  				Item: &reserve.BatchRadiusItem{
    72  					BatchID: storagetest.MaxAddressBytes[:],
    73  				},
    74  				Factory:    func() storage.Item { return new(reserve.BatchRadiusItem) },
    75  				MarshalErr: reserve.ErrMarshalInvalidAddress,
    76  			},
    77  		},
    78  		{
    79  			name: "ChunkBinItem zero address",
    80  			test: &storagetest.ItemMarshalAndUnmarshalTest{
    81  				Item:       &reserve.ChunkBinItem{},
    82  				Factory:    func() storage.Item { return new(reserve.ChunkBinItem) },
    83  				MarshalErr: reserve.ErrMarshalInvalidAddress,
    84  			},
    85  		},
    86  		{
    87  			name: "BatchRadiusItem invalid size",
    88  			test: &storagetest.ItemMarshalAndUnmarshalTest{
    89  				Item: &storagetest.ItemStub{
    90  					MarshalBuf:   []byte{0xFF},
    91  					UnmarshalBuf: []byte{0xFF},
    92  				},
    93  				Factory:      func() storage.Item { return new(reserve.BatchRadiusItem) },
    94  				UnmarshalErr: reserve.ErrUnmarshalInvalidSize,
    95  			},
    96  		},
    97  		{
    98  			name: "ChunkBinItem invalid size",
    99  			test: &storagetest.ItemMarshalAndUnmarshalTest{
   100  				Item: &storagetest.ItemStub{
   101  					MarshalBuf:   []byte{0xFF},
   102  					UnmarshalBuf: []byte{0xFF},
   103  				},
   104  				Factory:      func() storage.Item { return new(reserve.ChunkBinItem) },
   105  				UnmarshalErr: reserve.ErrUnmarshalInvalidSize,
   106  			},
   107  		},
   108  		{
   109  			name: "BinItem invalid size",
   110  			test: &storagetest.ItemMarshalAndUnmarshalTest{
   111  				Item: &storagetest.ItemStub{
   112  					MarshalBuf:   []byte{0xFF},
   113  					UnmarshalBuf: []byte{0xFF},
   114  				},
   115  				Factory:      func() storage.Item { return new(reserve.BinItem) },
   116  				UnmarshalErr: reserve.ErrUnmarshalInvalidSize,
   117  			},
   118  		},
   119  		{
   120  			name: "RadiusItem invalid size",
   121  			test: &storagetest.ItemMarshalAndUnmarshalTest{
   122  				Item: &storagetest.ItemStub{
   123  					MarshalBuf:   []byte{0xFF, 0xFF},
   124  					UnmarshalBuf: []byte{0xFF, 0xFF},
   125  				},
   126  				Factory:      func() storage.Item { return new(reserve.RadiusItem) },
   127  				UnmarshalErr: reserve.ErrUnmarshalInvalidSize,
   128  			},
   129  		},
   130  	}
   131  
   132  	for _, tc := range tests {
   133  		tc := tc
   134  
   135  		t.Run(fmt.Sprintf("%s marshal/unmarshal", tc.name), func(t *testing.T) {
   136  			t.Parallel()
   137  
   138  			storagetest.TestItemMarshalAndUnmarshal(t, tc.test)
   139  		})
   140  
   141  		t.Run(fmt.Sprintf("%s clone", tc.name), func(t *testing.T) {
   142  			t.Parallel()
   143  
   144  			storagetest.TestItemClone(t, &storagetest.ItemCloneTest{
   145  				Item:    tc.test.Item,
   146  				CmpOpts: tc.test.CmpOpts,
   147  			})
   148  		})
   149  	}
   150  }