From 78dadea3b8bd919456ea7b7570d35730048a8cd5 Mon Sep 17 00:00:00 2001 From: Richard Meissner Date: Mon, 7 Nov 2022 14:45:09 +0100 Subject: [PATCH] Add test for module pagination --- contracts/base/ModuleManager.sol | 3 +- test/core/GnosisSafe.ModuleManager.spec.ts | 33 +++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/contracts/base/ModuleManager.sol b/contracts/base/ModuleManager.sol index e6a280a10..65c0b66d2 100644 --- a/contracts/base/ModuleManager.sol +++ b/contracts/base/ModuleManager.sol @@ -118,12 +118,13 @@ contract ModuleManager is SelfAuthorized, Executor { // Populate return array uint256 moduleCount = 0; address currentModule = modules[start]; + while (currentModule != address(0x0) && currentModule != SENTINEL_MODULES && moduleCount < pageSize) { array[moduleCount] = currentModule; + next = currentModule; currentModule = modules[currentModule]; moduleCount++; } - next = currentModule; // Set correct size of returned array // solhint-disable-next-line no-inline-assembly assembly { diff --git a/test/core/GnosisSafe.ModuleManager.spec.ts b/test/core/GnosisSafe.ModuleManager.spec.ts index 1cf700a48..49f1dc3ce 100644 --- a/test/core/GnosisSafe.ModuleManager.spec.ts +++ b/test/core/GnosisSafe.ModuleManager.spec.ts @@ -9,7 +9,7 @@ import { AddressOne } from "../../src/utils/constants"; describe("ModuleManager", async () => { - const [user1, user2] = waffle.provider.getWallets(); + const [user1, user2, user3] = waffle.provider.getWallets(); const setupTests = deployments.createFixture(async ({ deployments }) => { await deployments.fixture(); @@ -244,4 +244,35 @@ describe("ModuleManager", async () => { ).to.be.deep.eq([false, "0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000013536f6d652072616e646f6d206d65737361676500000000000000000000000000"]) }) }) + + describe("getModulesPaginated", async () => { + it('Returns all modules over multiple pages', async () => { + const { safe } = await setupTests() + await expect( + executeContractCallWithSigners(safe, safe, "enableModule", [user1.address], [user1]) + ).to.emit(safe, "EnabledModule").withArgs(user1.address) + + await expect( + executeContractCallWithSigners(safe, safe, "enableModule", [user2.address], [user1]) + ).to.emit(safe, "EnabledModule").withArgs(user2.address) + + await expect( + executeContractCallWithSigners(safe, safe, "enableModule", [user3.address], [user1]) + ).to.emit(safe, "EnabledModule").withArgs(user3.address) + + await expect(await safe.isModuleEnabled(user1.address)).to.be.true + await expect(await safe.isModuleEnabled(user2.address)).to.be.true + await expect(await safe.isModuleEnabled(user3.address)).to.be.true + + + await expect(await safe.getModulesPaginated(AddressOne, 1)).to.be.deep.equal([[user3.address], user3.address]) + await expect(await safe.getModulesPaginated(user3.address, 1)).to.be.deep.equal([[user2.address], user2.address]) + await expect(await safe.getModulesPaginated(user2.address, 1)).to.be.deep.equal([[user1.address], user1.address]) + await expect(await safe.getModulesPaginated(user1.address, 1)).to.be.deep.equal([[], AddressZero]) + + await expect(await safe.getModulesPaginated(AddressOne, 2)).to.be.deep.equal([[user3.address, user2.address], user2.address]) + + await expect(await safe.getModulesPaginated(AddressOne, 3)).to.be.deep.equal([[user3.address, user2.address, user1.address], user1.address]) + }) + }) }) \ No newline at end of file