github.com/ethersphere/bee/v2@v2.2.0/pkg/accesscontrol/history_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  	"reflect"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/ethersphere/bee/v2/pkg/accesscontrol"
    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/storage"
    18  	mockstorer "github.com/ethersphere/bee/v2/pkg/storer/mock"
    19  	"github.com/ethersphere/bee/v2/pkg/swarm"
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestHistoryAdd(t *testing.T) {
    24  	t.Parallel()
    25  	h, err := accesscontrol.NewHistory(nil)
    26  	assertNoError(t, "create history", err)
    27  
    28  	addr := swarm.NewAddress([]byte("addr"))
    29  
    30  	ctx := context.Background()
    31  
    32  	err = h.Add(ctx, addr, nil, nil)
    33  	assertNoError(t, "history add", err)
    34  }
    35  
    36  func TestSingleNodeHistoryLookup(t *testing.T) {
    37  	t.Parallel()
    38  	storer := mockstorer.New()
    39  	ctx := context.Background()
    40  	ls := loadsave.New(storer.ChunkStore(), storer.Cache(), pipelineFactory(storer.Cache(), false))
    41  
    42  	h, err := accesscontrol.NewHistory(ls)
    43  	assertNoError(t, "create history", err)
    44  
    45  	testActRef := swarm.RandAddress(t)
    46  	err = h.Add(ctx, testActRef, nil, nil)
    47  	assertNoError(t, "history add", err)
    48  
    49  	_, err = h.Store(ctx)
    50  	assertNoError(t, "store history", err)
    51  
    52  	searchedTime := time.Now().Unix()
    53  	entry, err := h.Lookup(ctx, searchedTime)
    54  	assertNoError(t, "history lookup", err)
    55  	actRef := entry.Reference()
    56  	assert.True(t, actRef.Equal(testActRef))
    57  	assert.Nil(t, entry.Metadata())
    58  }
    59  
    60  func TestMultiNodeHistoryLookup(t *testing.T) {
    61  	t.Parallel()
    62  	storer := mockstorer.New()
    63  	ctx := context.Background()
    64  	ls := loadsave.New(storer.ChunkStore(), storer.Cache(), pipelineFactory(storer.Cache(), false))
    65  
    66  	h, err := accesscontrol.NewHistory(ls)
    67  	assertNoError(t, "create history", err)
    68  
    69  	testActRef1 := swarm.NewAddress([]byte("39a5ea87b141fe44aa609c3327ecd891"))
    70  	firstTime := time.Date(1994, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
    71  	mtdt1 := map[string]string{"firstTime": "1994-04-01"}
    72  	err = h.Add(ctx, testActRef1, &firstTime, &mtdt1)
    73  	assertNoError(t, "1st history add", err)
    74  
    75  	testActRef2 := swarm.NewAddress([]byte("39a5ea87b141fe44aa609c3327ecd892"))
    76  	secondTime := time.Date(2000, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
    77  	mtdt2 := map[string]string{"secondTime": "2000-04-01"}
    78  	err = h.Add(ctx, testActRef2, &secondTime, &mtdt2)
    79  	assertNoError(t, "2nd history add", err)
    80  
    81  	testActRef3 := swarm.NewAddress([]byte("39a5ea87b141fe44aa609c3327ecd893"))
    82  	thirdTime := time.Date(2015, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
    83  	mtdt3 := map[string]string{"thirdTime": "2015-04-01"}
    84  	err = h.Add(ctx, testActRef3, &thirdTime, &mtdt3)
    85  	assertNoError(t, "3rd history add", err)
    86  
    87  	testActRef4 := swarm.NewAddress([]byte("39a5ea87b141fe44aa609c3327ecd894"))
    88  	fourthTime := time.Date(2020, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
    89  	mtdt4 := map[string]string{"fourthTime": "2020-04-01"}
    90  	err = h.Add(ctx, testActRef4, &fourthTime, &mtdt4)
    91  	assertNoError(t, "4th history add", err)
    92  
    93  	testActRef5 := swarm.NewAddress([]byte("39a5ea87b141fe44aa609c3327ecd895"))
    94  	fifthTime := time.Date(2030, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
    95  	mtdt5 := map[string]string{"fifthTime": "2030-04-01"}
    96  	err = h.Add(ctx, testActRef5, &fifthTime, &mtdt5)
    97  	assertNoError(t, "5th history add", err)
    98  
    99  	// latest
   100  	searchedTime := time.Date(1980, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
   101  	entry, err := h.Lookup(ctx, searchedTime)
   102  	assertNoError(t, "1st history lookup", err)
   103  	actRef := entry.Reference()
   104  	assert.True(t, actRef.Equal(testActRef1))
   105  	assert.True(t, reflect.DeepEqual(mtdt1, entry.Metadata()))
   106  
   107  	// before first time
   108  	searchedTime = time.Date(2021, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
   109  	entry, err = h.Lookup(ctx, searchedTime)
   110  	assertNoError(t, "2nd history lookup", err)
   111  	actRef = entry.Reference()
   112  	assert.True(t, actRef.Equal(testActRef4))
   113  	assert.True(t, reflect.DeepEqual(mtdt4, entry.Metadata()))
   114  
   115  	// same time
   116  	searchedTime = time.Date(2000, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
   117  	entry, err = h.Lookup(ctx, searchedTime)
   118  	assertNoError(t, "3rd history lookup", err)
   119  	actRef = entry.Reference()
   120  	assert.True(t, actRef.Equal(testActRef2))
   121  	assert.True(t, reflect.DeepEqual(mtdt2, entry.Metadata()))
   122  
   123  	// after time
   124  	searchedTime = time.Date(2045, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
   125  	entry, err = h.Lookup(ctx, searchedTime)
   126  	assertNoError(t, "4th history lookup", err)
   127  	actRef = entry.Reference()
   128  	assert.True(t, actRef.Equal(testActRef5))
   129  	assert.True(t, reflect.DeepEqual(mtdt5, entry.Metadata()))
   130  }
   131  
   132  func TestHistoryStore(t *testing.T) {
   133  	t.Parallel()
   134  	storer := mockstorer.New()
   135  	ctx := context.Background()
   136  	ls := loadsave.New(storer.ChunkStore(), storer.Cache(), pipelineFactory(storer.Cache(), false))
   137  
   138  	h1, err := accesscontrol.NewHistory(ls)
   139  	assertNoError(t, "create history", err)
   140  
   141  	testActRef1 := swarm.NewAddress([]byte("39a5ea87b141fe44aa609c3327ecd891"))
   142  	firstTime := time.Date(1994, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
   143  	mtdt1 := map[string]string{"firstTime": "1994-04-01"}
   144  	err = h1.Add(ctx, testActRef1, &firstTime, &mtdt1)
   145  	assertNoError(t, "history add", err)
   146  
   147  	href1, err := h1.Store(ctx)
   148  	assertNoError(t, "store history", err)
   149  
   150  	h2, err := accesscontrol.NewHistoryReference(ls, href1)
   151  	assertNoError(t, "create history ref", err)
   152  
   153  	entry1, err := h2.Lookup(ctx, firstTime)
   154  	assertNoError(t, "history lookup", err)
   155  	actRef1 := entry1.Reference()
   156  	assert.True(t, actRef1.Equal(testActRef1))
   157  	assert.True(t, reflect.DeepEqual(mtdt1, entry1.Metadata()))
   158  }
   159  
   160  func pipelineFactory(s storage.Putter, encrypt bool) func() pipeline.Interface {
   161  	return func() pipeline.Interface {
   162  		return builder.NewPipelineBuilder(context.Background(), s, encrypt, 0)
   163  	}
   164  }