From 7960a49e4d7e95fcf33443ac1d6fba5699a8fc77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Ujv=C3=A1ri?= Date: Tue, 7 May 2024 16:43:50 +0200 Subject: [PATCH] Fix: Act timestamp header nil check; Uploadhandler UT --- pkg/api/dynamicaccess.go | 2 +- pkg/dynamicaccess/controller.go | 8 +++-- pkg/dynamicaccess/controller_test.go | 51 ++++++++++++++++++++++------ 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/pkg/api/dynamicaccess.go b/pkg/api/dynamicaccess.go index b69a984fa92..bbf3abbc69e 100644 --- a/pkg/api/dynamicaccess.go +++ b/pkg/api/dynamicaccess.go @@ -96,7 +96,7 @@ func (s *Service) actDecryptionHandler() func(h http.Handler) http.Handler { } timestamp := time.Now().Unix() - if headers.Timestamp == nil { + if headers.Timestamp != nil { timestamp = *headers.Timestamp } diff --git a/pkg/dynamicaccess/controller.go b/pkg/dynamicaccess/controller.go index ad5ab1f1c8f..5deb69c9ffe 100644 --- a/pkg/dynamicaccess/controller.go +++ b/pkg/dynamicaccess/controller.go @@ -201,9 +201,11 @@ func (c *controller) HandleGrantees( if err != nil { return swarm.ZeroAddress, swarm.ZeroAddress, swarm.ZeroAddress, swarm.ZeroAddress, err } - err = c.accessLogic.AddPublisher(ctx, act, publisher) - if err != nil { - return swarm.ZeroAddress, swarm.ZeroAddress, swarm.ZeroAddress, swarm.ZeroAddress, err + if historyref.IsZero() { + err = c.accessLogic.AddPublisher(ctx, act, publisher) + if err != nil { + return swarm.ZeroAddress, swarm.ZeroAddress, swarm.ZeroAddress, swarm.ZeroAddress, err + } } granteesToAdd = gl.Get() } else { diff --git a/pkg/dynamicaccess/controller_test.go b/pkg/dynamicaccess/controller_test.go index 0f6950dcf20..2b3065899a1 100644 --- a/pkg/dynamicaccess/controller_test.go +++ b/pkg/dynamicaccess/controller_test.go @@ -47,25 +47,52 @@ func getHistoryFixture(ctx context.Context, ls file.LoadSaver, al dynamicaccess. return h.Store(ctx) } -func TestController_NewUpload(t *testing.T) { +func TestController_UploadHandler(t *testing.T) { ctx := context.Background() publisher := getPrivKey(0) diffieHellman := dynamicaccess.NewDefaultSession(publisher) al := dynamicaccess.NewLogic(diffieHellman) c := dynamicaccess.NewController(al) - ref := swarm.RandAddress(t) ls := createLs() - _, hRef, encRef, err := c.UploadHandler(ctx, ls, ref, &publisher.PublicKey, swarm.ZeroAddress) - h, err := dynamicaccess.NewHistoryReference(ls, hRef) - entry, err := h.Lookup(ctx, time.Now().Unix()) - actRef := entry.Reference() - act, err := kvs.NewReference(ls, actRef) - expRef, err := al.EncryptRef(ctx, act, &publisher.PublicKey, ref) + t.Run("New upload", func(t *testing.T) { + ref := swarm.RandAddress(t) + _, hRef, encRef, err := c.UploadHandler(ctx, ls, ref, &publisher.PublicKey, swarm.ZeroAddress) + assert.NoError(t, err) - assert.NoError(t, err) - assert.Equal(t, encRef, expRef) - assert.NotEqual(t, hRef, swarm.ZeroAddress) + h, _ := dynamicaccess.NewHistoryReference(ls, hRef) + entry, _ := h.Lookup(ctx, time.Now().Unix()) + actRef := entry.Reference() + act, _ := kvs.NewReference(ls, actRef) + expRef, err := al.EncryptRef(ctx, act, &publisher.PublicKey, ref) + + assert.NoError(t, err) + assert.Equal(t, encRef, expRef) + assert.NotEqual(t, hRef, swarm.ZeroAddress) + }) + + t.Run("Upload to same history", func(t *testing.T) { + ref := swarm.RandAddress(t) + _, hRef1, _, err := c.UploadHandler(ctx, ls, ref, &publisher.PublicKey, swarm.ZeroAddress) + assert.NoError(t, err) + _, hRef2, encRef, err := c.UploadHandler(ctx, ls, ref, &publisher.PublicKey, hRef1) + assert.NoError(t, err) + h, err := dynamicaccess.NewHistoryReference(ls, hRef2) + assert.NoError(t, err) + hRef2, err = h.Store(ctx) + assert.NoError(t, err) + assert.True(t, hRef1.Equal(hRef2)) + + h, _ = dynamicaccess.NewHistoryReference(ls, hRef2) + entry, _ := h.Lookup(ctx, time.Now().Unix()) + actRef := entry.Reference() + act, _ := kvs.NewReference(ls, actRef) + expRef, err := al.EncryptRef(ctx, act, &publisher.PublicKey, ref) + + assert.NoError(t, err) + assert.Equal(t, encRef, expRef) + assert.NotEqual(t, hRef2, swarm.ZeroAddress) + }) } func TestController_PublisherDownload(t *testing.T) { @@ -133,6 +160,7 @@ func TestController_HandleGrantees(t *testing.T) { t.Run("add to new list", func(t *testing.T) { addList := []*ecdsa.PublicKey{&grantee.PublicKey} granteeRef, _, _, _, err := c.HandleGrantees(ctx, ls, ls, swarm.ZeroAddress, swarm.ZeroAddress, &publisher.PublicKey, addList, nil) + assert.NoError(t, err) gl, err := dynamicaccess.NewGranteeListReference(ls, granteeRef) @@ -142,6 +170,7 @@ func TestController_HandleGrantees(t *testing.T) { t.Run("add to existing list", func(t *testing.T) { addList := []*ecdsa.PublicKey{&grantee.PublicKey} granteeRef, eglref, _, _, err := c.HandleGrantees(ctx, ls, gls, swarm.ZeroAddress, href, &publisher.PublicKey, addList, nil) + assert.NoError(t, err) gl, err := dynamicaccess.NewGranteeListReference(ls, granteeRef)