Skip to content

Commit

Permalink
Fix: Act timestamp header nil check; Uploadhandler UT
Browse files Browse the repository at this point in the history
  • Loading branch information
bosi95 committed May 7, 2024
1 parent 933c5f8 commit 7960a49
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/api/dynamicaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/dynamicaccess/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
51 changes: 40 additions & 11 deletions pkg/dynamicaccess/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 7960a49

Please sign in to comment.