github.com/ethersphere/bee/v2@v2.2.0/pkg/accesscontrol/kvs/kvs_test.go (about)

     1  // Copyright 2024 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  //nolint:ireturn
     6  package kvs_test
     7  
     8  import (
     9  	"context"
    10  	"testing"
    11  
    12  	"github.com/ethersphere/bee/v2/pkg/accesscontrol/kvs"
    13  	"github.com/ethersphere/bee/v2/pkg/file"
    14  	"github.com/ethersphere/bee/v2/pkg/file/loadsave"
    15  	"github.com/ethersphere/bee/v2/pkg/file/pipeline"
    16  	"github.com/ethersphere/bee/v2/pkg/file/pipeline/builder"
    17  	"github.com/ethersphere/bee/v2/pkg/file/redundancy"
    18  	"github.com/ethersphere/bee/v2/pkg/storage"
    19  	mockstorer "github.com/ethersphere/bee/v2/pkg/storer/mock"
    20  	"github.com/ethersphere/bee/v2/pkg/swarm"
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  //nolint:gochecknoglobals
    25  var mockStorer = mockstorer.New()
    26  
    27  func requestPipelineFactory(ctx context.Context, s storage.Putter, encrypt bool, rLevel redundancy.Level) func() pipeline.Interface {
    28  	return func() pipeline.Interface {
    29  		return builder.NewPipelineBuilder(ctx, s, encrypt, rLevel)
    30  	}
    31  }
    32  
    33  func createLs() file.LoadSaver {
    34  	return loadsave.New(mockStorer.ChunkStore(), mockStorer.Cache(), requestPipelineFactory(context.Background(), mockStorer.Cache(), false, redundancy.NONE))
    35  }
    36  
    37  func keyValuePair(t *testing.T) ([]byte, []byte) {
    38  	t.Helper()
    39  	return swarm.RandAddress(t).Bytes(), swarm.RandAddress(t).Bytes()
    40  }
    41  
    42  func TestKvs(t *testing.T) {
    43  	t.Parallel()
    44  	s, err := kvs.New(createLs())
    45  	assert.NoError(t, err)
    46  
    47  	key, val := keyValuePair(t)
    48  	ctx := context.Background()
    49  
    50  	t.Run("Get non-existent key should return error", func(t *testing.T) {
    51  		_, err := s.Get(ctx, []byte{1})
    52  		assert.Error(t, err)
    53  	})
    54  
    55  	t.Run("Multiple Get with same key, no error", func(t *testing.T) {
    56  		err := s.Put(ctx, key, val)
    57  		assert.NoError(t, err)
    58  
    59  		// get #1
    60  		v, err := s.Get(ctx, key)
    61  		assert.NoError(t, err)
    62  		assert.Equal(t, val, v)
    63  		// get #2
    64  		v, err = s.Get(ctx, key)
    65  		assert.NoError(t, err)
    66  		assert.Equal(t, val, v)
    67  	})
    68  
    69  	t.Run("Get should return value equal to put value", func(t *testing.T) {
    70  		var (
    71  			key1 = []byte{1}
    72  			key2 = []byte{2}
    73  			key3 = []byte{3}
    74  		)
    75  		testCases := []struct {
    76  			name string
    77  			key  []byte
    78  			val  []byte
    79  		}{
    80  			{
    81  				name: "Test key = 1",
    82  				key:  key1,
    83  				val:  []byte{11},
    84  			},
    85  			{
    86  				name: "Test key = 2",
    87  				key:  key2,
    88  				val:  []byte{22},
    89  			},
    90  			{
    91  				name: "Test overwrite key = 1",
    92  				key:  key1,
    93  				val:  []byte{111},
    94  			},
    95  			{
    96  				name: "Test key = 3",
    97  				key:  key3,
    98  				val:  []byte{33},
    99  			},
   100  			{
   101  				name: "Test key = 3 with same value",
   102  				key:  key3,
   103  				val:  []byte{33},
   104  			},
   105  			{
   106  				name: "Test key = 3 with value for key1",
   107  				key:  key3,
   108  				val:  []byte{11},
   109  			},
   110  		}
   111  
   112  		for _, tc := range testCases {
   113  			t.Run(tc.name, func(t *testing.T) {
   114  				err := s.Put(ctx, tc.key, tc.val)
   115  				assert.NoError(t, err)
   116  				retVal, err := s.Get(ctx, tc.key)
   117  				assert.NoError(t, err)
   118  				assert.Equal(t, tc.val, retVal)
   119  			})
   120  		}
   121  	})
   122  }
   123  
   124  func TestKvs_Save(t *testing.T) {
   125  	t.Parallel()
   126  	ctx := context.Background()
   127  
   128  	key1, val1 := keyValuePair(t)
   129  	key2, val2 := keyValuePair(t)
   130  	t.Run("Save empty KVS return error", func(t *testing.T) {
   131  		s, err := kvs.New(createLs())
   132  		assert.NoError(t, err)
   133  		_, err = s.Save(ctx)
   134  		assert.ErrorIs(t, err, kvs.ErrNothingToSave)
   135  	})
   136  	t.Run("Save not empty KVS return valid swarm address", func(t *testing.T) {
   137  		s, err := kvs.New(createLs())
   138  		assert.NoError(t, err)
   139  		err = s.Put(ctx, key1, val1)
   140  		assert.NoError(t, err)
   141  		ref, err := s.Save(ctx)
   142  		assert.NoError(t, err)
   143  		assert.True(t, ref.IsValidNonEmpty())
   144  	})
   145  	t.Run("Save KVS with one item, no error, pre-save value exist", func(t *testing.T) {
   146  		ls := createLs()
   147  		s1, err := kvs.New(ls)
   148  		assert.NoError(t, err)
   149  
   150  		err = s1.Put(ctx, key1, val1)
   151  		assert.NoError(t, err)
   152  
   153  		ref, err := s1.Save(ctx)
   154  		assert.NoError(t, err)
   155  
   156  		s2, err := kvs.NewReference(ls, ref)
   157  		assert.NoError(t, err)
   158  
   159  		val, err := s2.Get(ctx, key1)
   160  		assert.NoError(t, err)
   161  		assert.Equal(t, val1, val)
   162  	})
   163  	t.Run("Save KVS and add one item, no error, after-save value exist", func(t *testing.T) {
   164  		ls := createLs()
   165  
   166  		kvs1, err := kvs.New(ls)
   167  		assert.NoError(t, err)
   168  
   169  		err = kvs1.Put(ctx, key1, val1)
   170  		assert.NoError(t, err)
   171  		ref, err := kvs1.Save(ctx)
   172  		assert.NoError(t, err)
   173  
   174  		// New KVS
   175  		kvs2, err := kvs.NewReference(ls, ref)
   176  		assert.NoError(t, err)
   177  		err = kvs2.Put(ctx, key2, val2)
   178  		assert.NoError(t, err)
   179  
   180  		val, err := kvs2.Get(ctx, key2)
   181  		assert.NoError(t, err)
   182  		assert.Equal(t, val2, val)
   183  	})
   184  }