From 1a8e92d30d6081533364b5598c3b271ff9f448b3 Mon Sep 17 00:00:00 2001 From: akrem-chabchoub Date: Mon, 1 Dec 2025 23:28:55 +0100 Subject: [PATCH 1/2] test: integrate synctest in access control pkg --- Makefile | 8 +-- pkg/accesscontrol/controller_test.go | 79 +++++++++++++++------------- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/Makefile b/Makefile index 047f5d4f8be..5ec36a0c196 100644 --- a/Makefile +++ b/Makefile @@ -121,17 +121,17 @@ endif .PHONY: test-ci test-ci: ifdef cover - $(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./... + $(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/accesscontrol else - $(GO) test -run "[^FLAKY]$$" ./... + $(GO) test -run "[^FLAKY]$$" ./pkg/accesscontrol endif .PHONY: test-ci-race test-ci-race: ifdef cover - $(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./... + $(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/accesscontrol else - $(GO) test -race -run "[^FLAKY]$$" ./... + $(GO) test -race -run "[^FLAKY]$$" ./pkg/accesscontrol endif .PHONY: test-ci-flaky diff --git a/pkg/accesscontrol/controller_test.go b/pkg/accesscontrol/controller_test.go index 1fa3da681ce..c282ef825fb 100644 --- a/pkg/accesscontrol/controller_test.go +++ b/pkg/accesscontrol/controller_test.go @@ -9,6 +9,7 @@ import ( "crypto/ecdsa" "reflect" "testing" + "testing/synctest" "time" "github.com/ethersphere/bee/v2/pkg/accesscontrol" @@ -235,44 +236,46 @@ func TestController_UpdateHandler(t *testing.T) { assert.Len(t, gl.Get(), 2) }) t.Run("add and revoke then get from history", func(t *testing.T) { - addRevokeList := []*ecdsa.PublicKey{&grantee.PublicKey} - ref := swarm.RandAddress(t) - _, hRef, encRef, err := c.UploadHandler(ctx, ls, ref, &publisher.PublicKey, swarm.ZeroAddress) - require.NoError(t, err) - - // Need to wait a second before each update call so that a new history mantaray fork is created for the new key(timestamp) entry - time.Sleep(1 * time.Second) - beforeRevokeTS := time.Now().Unix() - _, egranteeRef, hrefUpdate1, _, err := c.UpdateHandler(ctx, ls, gls, swarm.ZeroAddress, hRef, &publisher.PublicKey, addRevokeList, nil) - require.NoError(t, err) - - time.Sleep(1 * time.Second) - granteeRef, _, hrefUpdate2, _, err := c.UpdateHandler(ctx, ls, gls, egranteeRef, hrefUpdate1, &publisher.PublicKey, nil, addRevokeList) - require.NoError(t, err) - - gl, err := accesscontrol.NewGranteeListReference(ctx, ls, granteeRef) - require.NoError(t, err) - assert.Empty(t, gl.Get()) - // expect history reference to be different after grantee list update - assert.NotEqual(t, hrefUpdate1, hrefUpdate2) - - granteeDH := accesscontrol.NewDefaultSession(grantee) - granteeAl := accesscontrol.NewLogic(granteeDH) - granteeCtrl := accesscontrol.NewController(granteeAl) - // download with grantee shall still work with the timestamp before the revoke - decRef, err := granteeCtrl.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, beforeRevokeTS) - require.NoError(t, err) - assert.Equal(t, ref, decRef) - - // download with grantee shall NOT work with the latest timestamp - decRef, err = granteeCtrl.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, time.Now().Unix()) - require.Error(t, err) - assert.Equal(t, swarm.ZeroAddress, decRef) - - // publisher shall still be able to download with the timestamp before the revoke - decRef, err = c.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, beforeRevokeTS) - require.NoError(t, err) - assert.Equal(t, ref, decRef) + synctest.Test(t, func(t *testing.T) { + addRevokeList := []*ecdsa.PublicKey{&grantee.PublicKey} + ref := swarm.RandAddress(t) + _, hRef, encRef, err := c.UploadHandler(ctx, ls, ref, &publisher.PublicKey, swarm.ZeroAddress) + require.NoError(t, err) + + // Need to wait a second before each update call so that a new history mantaray fork is created for the new key(timestamp) entry + time.Sleep(1 * time.Second) + beforeRevokeTS := time.Now().Unix() + _, egranteeRef, hrefUpdate1, _, err := c.UpdateHandler(ctx, ls, gls, swarm.ZeroAddress, hRef, &publisher.PublicKey, addRevokeList, nil) + require.NoError(t, err) + + time.Sleep(1 * time.Second) + granteeRef, _, hrefUpdate2, _, err := c.UpdateHandler(ctx, ls, gls, egranteeRef, hrefUpdate1, &publisher.PublicKey, nil, addRevokeList) + require.NoError(t, err) + + gl, err := accesscontrol.NewGranteeListReference(ctx, ls, granteeRef) + require.NoError(t, err) + assert.Empty(t, gl.Get()) + // expect history reference to be different after grantee list update + assert.NotEqual(t, hrefUpdate1, hrefUpdate2) + + granteeDH := accesscontrol.NewDefaultSession(grantee) + granteeAl := accesscontrol.NewLogic(granteeDH) + granteeCtrl := accesscontrol.NewController(granteeAl) + // download with grantee shall still work with the timestamp before the revoke + decRef, err := granteeCtrl.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, beforeRevokeTS) + require.NoError(t, err) + assert.Equal(t, ref, decRef) + + // download with grantee shall NOT work with the latest timestamp + decRef, err = granteeCtrl.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, time.Now().Unix()) + require.Error(t, err) + assert.Equal(t, swarm.ZeroAddress, decRef) + + // publisher shall still be able to download with the timestamp before the revoke + decRef, err = c.DownloadHandler(ctx, ls, encRef, &publisher.PublicKey, hrefUpdate2, beforeRevokeTS) + require.NoError(t, err) + assert.Equal(t, ref, decRef) + }) }) t.Run("add twice", func(t *testing.T) { addList := []*ecdsa.PublicKey{&grantee.PublicKey, &grantee.PublicKey} From d6f6daf6499ae2bd4ee37211f50a0f954d53dd6c Mon Sep 17 00:00:00 2001 From: akrem-chabchoub Date: Tue, 2 Dec 2025 00:16:05 +0100 Subject: [PATCH 2/2] chore: revert makefile test changes --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 5ec36a0c196..047f5d4f8be 100644 --- a/Makefile +++ b/Makefile @@ -121,17 +121,17 @@ endif .PHONY: test-ci test-ci: ifdef cover - $(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/accesscontrol + $(GO) test -run "[^FLAKY]$$" -coverprofile=cover.out ./... else - $(GO) test -run "[^FLAKY]$$" ./pkg/accesscontrol + $(GO) test -run "[^FLAKY]$$" ./... endif .PHONY: test-ci-race test-ci-race: ifdef cover - $(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./pkg/accesscontrol + $(GO) test -race -run "[^FLAKY]$$" -coverprofile=cover.out ./... else - $(GO) test -race -run "[^FLAKY]$$" ./pkg/accesscontrol + $(GO) test -race -run "[^FLAKY]$$" ./... endif .PHONY: test-ci-flaky