Skip to content

Commit 1a8e92d

Browse files
test: integrate synctest in access control pkg
1 parent aed9d55 commit 1a8e92d

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,17 @@ endif
121121
.PHONY: test-ci
122122
test-ci:
123123
ifdef cover
124-
$(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./...
124+
$(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/accesscontrol
125125
else
126-
$(GO) test -run "[^FLAKY]$$" ./...
126+
$(GO) test -run "[^FLAKY]$$" ./pkg/accesscontrol
127127
endif
128128

129129
.PHONY: test-ci-race
130130
test-ci-race:
131131
ifdef cover
132-
$(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./...
132+
$(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/accesscontrol
133133
else
134-
$(GO) test -race -run "[^FLAKY]$$" ./...
134+
$(GO) test -race -run "[^FLAKY]$$" ./pkg/accesscontrol
135135
endif
136136

137137
.PHONY: test-ci-flaky

pkg/accesscontrol/controller_test.go

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"crypto/ecdsa"
1010
"reflect"
1111
"testing"
12+
"testing/synctest"
1213
"time"
1314

1415
"github.com/ethersphere/bee/v2/pkg/accesscontrol"
@@ -235,44 +236,46 @@ func TestController_UpdateHandler(t *testing.T) {
235236
assert.Len(t, gl.Get(), 2)
236237
})
237238
t.Run("add and revoke then get from history", func(t *testing.T) {
238-
addRevokeList := []*ecdsa.PublicKey{&grantee.PublicKey}
239-
ref := swarm.RandAddress(t)
240-
_, hRef, encRef, err := c.UploadHandler(ctx, ls, ref, &publisher.PublicKey, swarm.ZeroAddress)
241-
require.NoError(t, err)
242-
243-
// Need to wait a second before each update call so that a new history mantaray fork is created for the new key(timestamp) entry
244-
time.Sleep(1 * time.Second)
245-
beforeRevokeTS := time.Now().Unix()
246-
_, egranteeRef, hrefUpdate1, _, err := c.UpdateHandler(ctx, ls, gls, swarm.ZeroAddress, hRef, &publisher.PublicKey, addRevokeList, nil)
247-
require.NoError(t, err)
248-
249-
time.Sleep(1 * time.Second)
250-
granteeRef, _, hrefUpdate2, _, err := c.UpdateHandler(ctx, ls, gls, egranteeRef, hrefUpdate1, &publisher.PublicKey, nil, addRevokeList)
251-
require.NoError(t, err)
252-
253-
gl, err := accesscontrol.NewGranteeListReference(ctx, ls, granteeRef)
254-
require.NoError(t, err)
255-
assert.Empty(t, gl.Get())
256-
// expect history reference to be different after grantee list update
257-
assert.NotEqual(t, hrefUpdate1, hrefUpdate2)
258-
259-
granteeDH := accesscontrol.NewDefaultSession(grantee)
260-
granteeAl := accesscontrol.NewLogic(granteeDH)
261-
granteeCtrl := accesscontrol.NewController(granteeAl)
262-
// download with grantee shall still work with the timestamp before the revoke
263-
decRef, err := granteeCtrl.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, beforeRevokeTS)
264-
require.NoError(t, err)
265-
assert.Equal(t, ref, decRef)
266-
267-
// download with grantee shall NOT work with the latest timestamp
268-
decRef, err = granteeCtrl.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, time.Now().Unix())
269-
require.Error(t, err)
270-
assert.Equal(t, swarm.ZeroAddress, decRef)
271-
272-
// publisher shall still be able to download with the timestamp before the revoke
273-
decRef, err = c.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, beforeRevokeTS)
274-
require.NoError(t, err)
275-
assert.Equal(t, ref, decRef)
239+
synctest.Test(t, func(t *testing.T) {
240+
addRevokeList := []*ecdsa.PublicKey{&grantee.PublicKey}
241+
ref := swarm.RandAddress(t)
242+
_, hRef, encRef, err := c.UploadHandler(ctx, ls, ref, &publisher.PublicKey, swarm.ZeroAddress)
243+
require.NoError(t, err)
244+
245+
// Need to wait a second before each update call so that a new history mantaray fork is created for the new key(timestamp) entry
246+
time.Sleep(1 * time.Second)
247+
beforeRevokeTS := time.Now().Unix()
248+
_, egranteeRef, hrefUpdate1, _, err := c.UpdateHandler(ctx, ls, gls, swarm.ZeroAddress, hRef, &publisher.PublicKey, addRevokeList, nil)
249+
require.NoError(t, err)
250+
251+
time.Sleep(1 * time.Second)
252+
granteeRef, _, hrefUpdate2, _, err := c.UpdateHandler(ctx, ls, gls, egranteeRef, hrefUpdate1, &publisher.PublicKey, nil, addRevokeList)
253+
require.NoError(t, err)
254+
255+
gl, err := accesscontrol.NewGranteeListReference(ctx, ls, granteeRef)
256+
require.NoError(t, err)
257+
assert.Empty(t, gl.Get())
258+
// expect history reference to be different after grantee list update
259+
assert.NotEqual(t, hrefUpdate1, hrefUpdate2)
260+
261+
granteeDH := accesscontrol.NewDefaultSession(grantee)
262+
granteeAl := accesscontrol.NewLogic(granteeDH)
263+
granteeCtrl := accesscontrol.NewController(granteeAl)
264+
// download with grantee shall still work with the timestamp before the revoke
265+
decRef, err := granteeCtrl.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, beforeRevokeTS)
266+
require.NoError(t, err)
267+
assert.Equal(t, ref, decRef)
268+
269+
// download with grantee shall NOT work with the latest timestamp
270+
decRef, err = granteeCtrl.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, time.Now().Unix())
271+
require.Error(t, err)
272+
assert.Equal(t, swarm.ZeroAddress, decRef)
273+
274+
// publisher shall still be able to download with the timestamp before the revoke
275+
decRef, err = c.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, beforeRevokeTS)
276+
require.NoError(t, err)
277+
assert.Equal(t, ref, decRef)
278+
})
276279
})
277280
t.Run("add twice", func(t *testing.T) {
278281
addList := []*ecdsa.PublicKey{&grantee.PublicKey, &grantee.PublicKey}

0 commit comments

Comments
 (0)