github.com/ethersphere/bee/v2@v2.2.0/pkg/accesscontrol/grantee_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  package accesscontrol_test
     6  
     7  import (
     8  	"context"
     9  	"crypto/ecdsa"
    10  	"crypto/rand"
    11  	"testing"
    12  
    13  	"github.com/btcsuite/btcd/btcec/v2"
    14  	"github.com/ethersphere/bee/v2/pkg/accesscontrol"
    15  	"github.com/ethersphere/bee/v2/pkg/file"
    16  	"github.com/ethersphere/bee/v2/pkg/file/loadsave"
    17  	"github.com/ethersphere/bee/v2/pkg/file/pipeline"
    18  	"github.com/ethersphere/bee/v2/pkg/file/pipeline/builder"
    19  	"github.com/ethersphere/bee/v2/pkg/file/redundancy"
    20  	"github.com/ethersphere/bee/v2/pkg/storage"
    21  	mockstorer "github.com/ethersphere/bee/v2/pkg/storer/mock"
    22  	"github.com/ethersphere/bee/v2/pkg/swarm"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  var mockStorer = mockstorer.New()
    27  
    28  func requestPipelineFactory(ctx context.Context, s storage.Putter, encrypt bool, rLevel redundancy.Level) func() pipeline.Interface {
    29  	return func() pipeline.Interface {
    30  		return builder.NewPipelineBuilder(ctx, s, encrypt, rLevel)
    31  	}
    32  }
    33  
    34  func createLs() file.LoadSaver {
    35  	return loadsave.New(mockStorer.ChunkStore(), mockStorer.Cache(), requestPipelineFactory(context.Background(), mockStorer.Cache(), false, redundancy.NONE))
    36  }
    37  
    38  func generateKeyListFixture() ([]*ecdsa.PublicKey, error) {
    39  	key1, err := ecdsa.GenerateKey(btcec.S256(), rand.Reader)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	key2, err := ecdsa.GenerateKey(btcec.S256(), rand.Reader)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	key3, err := ecdsa.GenerateKey(btcec.S256(), rand.Reader)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return []*ecdsa.PublicKey{&key1.PublicKey, &key2.PublicKey, &key3.PublicKey}, nil
    52  }
    53  
    54  func TestGranteeAddGet(t *testing.T) {
    55  	t.Parallel()
    56  	gl := accesscontrol.NewGranteeList(createLs())
    57  	keys, err := generateKeyListFixture()
    58  	assertNoError(t, "key generation", err)
    59  
    60  	t.Run("Get empty grantee list should return", func(t *testing.T) {
    61  		val := gl.Get()
    62  		assert.Empty(t, val)
    63  	})
    64  
    65  	t.Run("Get should return value equal to put value", func(t *testing.T) {
    66  		var (
    67  			keys2, err = generateKeyListFixture()
    68  			addList1   = []*ecdsa.PublicKey{keys[0]}
    69  			addList2   = []*ecdsa.PublicKey{keys[1], keys[2]}
    70  			addList3   = keys2
    71  		)
    72  		assertNoError(t, "key generation", err)
    73  		testCases := []struct {
    74  			name string
    75  			list []*ecdsa.PublicKey
    76  		}{
    77  			{
    78  				name: "Test list = 1",
    79  				list: addList1,
    80  			},
    81  			{
    82  				name: "Test list = duplicate1",
    83  				list: addList1,
    84  			},
    85  			{
    86  				name: "Test list = 2",
    87  				list: addList2,
    88  			},
    89  			{
    90  				name: "Test list = 3",
    91  				list: addList3,
    92  			},
    93  			{
    94  				name: "Test empty add list",
    95  				list: nil,
    96  			},
    97  		}
    98  
    99  		expList := []*ecdsa.PublicKey{}
   100  		for _, tc := range testCases {
   101  			t.Run(tc.name, func(t *testing.T) {
   102  				err := gl.Add(tc.list)
   103  				if tc.list == nil {
   104  					assertError(t, "granteelist add", err)
   105  				} else {
   106  					assertNoError(t, "granteelist add", err)
   107  					if tc.name != "Test list = duplicate1" {
   108  						expList = append(expList, tc.list...)
   109  					}
   110  					retVal := gl.Get()
   111  					assert.Equal(t, expList, retVal)
   112  				}
   113  			})
   114  		}
   115  	})
   116  }
   117  
   118  func TestGranteeRemove(t *testing.T) {
   119  	t.Parallel()
   120  	gl := accesscontrol.NewGranteeList(createLs())
   121  	keys, err := generateKeyListFixture()
   122  	assertNoError(t, "key generation", err)
   123  
   124  	t.Run("Add should NOT return", func(t *testing.T) {
   125  		err := gl.Add(keys)
   126  		assertNoError(t, "granteelist add", err)
   127  		retVal := gl.Get()
   128  		assert.Equal(t, keys, retVal)
   129  	})
   130  	removeList1 := []*ecdsa.PublicKey{keys[0]}
   131  	removeList2 := []*ecdsa.PublicKey{keys[2], keys[1]}
   132  	t.Run("Remove the first item should return NO", func(t *testing.T) {
   133  		err := gl.Remove(removeList1)
   134  		assertNoError(t, "granteelist remove", err)
   135  		retVal := gl.Get()
   136  		assert.Equal(t, removeList2, retVal)
   137  	})
   138  	t.Run("Remove non-existent item should return NO", func(t *testing.T) {
   139  		err := gl.Remove(removeList1)
   140  		assertNoError(t, "granteelist remove", err)
   141  		retVal := gl.Get()
   142  		assert.Equal(t, removeList2, retVal)
   143  	})
   144  	t.Run("Remove second and third item should return NO", func(t *testing.T) {
   145  		err := gl.Remove(removeList2)
   146  		assertNoError(t, "granteelist remove", err)
   147  		retVal := gl.Get()
   148  		assert.Empty(t, retVal)
   149  	})
   150  	t.Run("Remove from empty grantee list should return", func(t *testing.T) {
   151  		err := gl.Remove(removeList1)
   152  		assertError(t, "remove from empty grantee list", err)
   153  		retVal := gl.Get()
   154  		assert.Empty(t, retVal)
   155  	})
   156  	t.Run("Remove empty remove list should return", func(t *testing.T) {
   157  		err := gl.Remove(nil)
   158  		assertError(t, "remove empty list", err)
   159  		retVal := gl.Get()
   160  		assert.Empty(t, retVal)
   161  	})
   162  }
   163  
   164  func TestGranteeSave(t *testing.T) {
   165  	t.Parallel()
   166  	ctx := context.Background()
   167  	keys, err := generateKeyListFixture()
   168  	assertNoError(t, "key generation", err)
   169  
   170  	t.Run("Create grantee list with invalid reference, expect", func(t *testing.T) {
   171  		gl, err := accesscontrol.NewGranteeListReference(ctx, createLs(), swarm.RandAddress(t))
   172  		assertError(t, "create grantee list ref", err)
   173  		assert.Nil(t, gl)
   174  	})
   175  	t.Run("Save empty grantee list return NO", func(t *testing.T) {
   176  		gl := accesscontrol.NewGranteeList(createLs())
   177  		_, err := gl.Save(ctx)
   178  		assertNoError(t, "granteelist save", err)
   179  	})
   180  	t.Run("Save not empty grantee list return valid swarm address", func(t *testing.T) {
   181  		gl := accesscontrol.NewGranteeList(createLs())
   182  		err = gl.Add(keys)
   183  		assertNoError(t, "granteelist add", err)
   184  		ref, err := gl.Save(ctx)
   185  		assertNoError(t, "granteelist save", err)
   186  		assert.True(t, ref.IsValidNonEmpty())
   187  	})
   188  	t.Run("Save grantee list with one item, no error, pre-save value exist", func(t *testing.T) {
   189  		ls := createLs()
   190  		gl1 := accesscontrol.NewGranteeList(ls)
   191  
   192  		err := gl1.Add(keys)
   193  		assertNoError(t, "granteelist add", err)
   194  
   195  		ref, err := gl1.Save(ctx)
   196  		assertNoError(t, "1st granteelist save", err)
   197  
   198  		gl2, err := accesscontrol.NewGranteeListReference(ctx, ls, ref)
   199  		assertNoError(t, "create grantee list ref", err)
   200  		val := gl2.Get()
   201  		assertNoError(t, "2nd granteelist save", err)
   202  		assert.Equal(t, keys, val)
   203  	})
   204  	t.Run("Save grantee list and add one item, no error, after-save value exist", func(t *testing.T) {
   205  		ls := createLs()
   206  		keys2, err := generateKeyListFixture()
   207  		assertNoError(t, "key generation", err)
   208  
   209  		gl1 := accesscontrol.NewGranteeList(ls)
   210  
   211  		err = gl1.Add(keys)
   212  		assertNoError(t, "granteelist1 add", err)
   213  
   214  		ref, err := gl1.Save(ctx)
   215  		assertNoError(t, "granteelist1 save", err)
   216  
   217  		gl2, err := accesscontrol.NewGranteeListReference(ctx, ls, ref)
   218  		assertNoError(t, "create grantee list ref", err)
   219  		err = gl2.Add(keys2)
   220  		assertNoError(t, "create grantee list ref", err)
   221  
   222  		val := gl2.Get()
   223  		assert.Equal(t, append(keys, keys2...), val)
   224  	})
   225  }
   226  
   227  func TestGranteeRemoveTwo(t *testing.T) {
   228  	gl := accesscontrol.NewGranteeList(createLs())
   229  	keys, err := generateKeyListFixture()
   230  	assertNoError(t, "key generation", err)
   231  	err = gl.Add([]*ecdsa.PublicKey{keys[0]})
   232  	assertNoError(t, "1st granteelist add", err)
   233  	err = gl.Add([]*ecdsa.PublicKey{keys[0]})
   234  	assertNoError(t, "2nd granteelist add", err)
   235  	err = gl.Remove([]*ecdsa.PublicKey{keys[0]})
   236  	assertNoError(t, "granteelist remove", err)
   237  }