Skip to content

Commit

Permalink
Add Module.sol tests
Browse files Browse the repository at this point in the history
  • Loading branch information
auryn-macmillan committed Aug 27, 2021
1 parent f2200fa commit ea0aac7
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 67 deletions.
4 changes: 0 additions & 4 deletions contracts/interfaces/IModule.sol

This file was deleted.

File renamed without changes.
53 changes: 37 additions & 16 deletions contracts/test/TestGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,47 @@ import "../factory/FactoryFriendly.sol";
import "@gnosis.pm/safe-contracts/contracts/GnosisSafe.sol";
import "@gnosis.pm/safe-contracts/contracts/interfaces/IERC165.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "../core/Module.sol";

contract TestGuard is FactoryFriendly, OwnableUpgradeable, BaseGuard {
event preChecked(bool checked);
event postChecked(bool checked);

address public module;

constructor(address _module) {
__Ownable_init();
module = _module;
}

function setModule(address _module) public {
module = _module;
}

function checkTransaction(
address to,
uint256 value,
bytes memory data,
Enum.Operation operation,
uint256 safeTxGas,
uint256 baseGas,
uint256 gasPrice,
address gasToken,
address payable refundReceiver,
bytes memory signatures,
address msgSender
) public override {}

function checkAfterExecution(bytes32 txHash, bool success)
public
override
{}
uint256,
bytes memory,
Enum.Operation,
uint256,
uint256,
uint256,
address,
address payable,
bytes memory,
address
) public override {
require(to != address(0), "Cannot send to zero address");
emit preChecked(true);
}

function checkAfterExecution(bytes32, bool) public override {
require(
Module(module).guard() == address(this),
"Module cannot remove its own guard."
);
emit postChecked(true);
}

function setUp(bytes calldata initializeParams) public override {}
}
145 changes: 98 additions & 47 deletions test/02_Module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ describe("Module", async () => {
const Module = await hre.ethers.getContractFactory("TestModule");
const module = await Module.deploy(iAvatar.address);
await avatar.enableModule(module.address);
const Guard = await hre.ethers.getContractFactory("TestGuard");
const guard = await Guard.deploy(module.address);
const tx = {
to: avatar.address,
value: 0,
Expand All @@ -32,6 +34,7 @@ describe("Module", async () => {
};
return {
iAvatar,
guard,
module,
tx,
};
Expand Down Expand Up @@ -61,64 +64,112 @@ describe("Module", async () => {

describe("exec", async () => {
it("skips guard pre-check if no guard is set", async () => {
const { iAvatar, module, tx } = await setupTests();
const { iAvatar, guard, module, tx } = await setupTests();
await expect(
module.executeTransaction(tx.to, tx.value, tx.data, tx.operation)
);
});

it("pre-checks transaction if guard is set", async () => {
const { iAvatar, guard, module, tx } = await setupTests();
await module.setGuard(guard.address);
await expect(
module.executeTransaction(tx.to, tx.value, tx.data, tx.operation)
)
.to.emit(guard, "preChecked")
.withArgs(true);
});

it("executes a transaction", async () => {
const { iAvatar, module, tx } = await setupTests();
await expect(
module.executeTransaction(tx.to, tx.value, tx.data, tx.operation)
);
});

// it("executes a transaction", async () => {
// const { iAvatar, module } = await setupTests();
// await expect(true).to.be.equals(false);
// });
//
// it("skips post-check if no guard is enabled", async () => {
// const { iAvatar, module } = await setupTests();
// await expect(true).to.be.equals(false);
// });
//
// it("post-checks transaction if guard is set", async () => {
// const { iAvatar, module } = await setupTests();
// await expect(true).to.be.equals(false);
// });
// });
//
// describe("execAndReturnData", async () => {
// it("cannot be called by external address", async () => {
// const { iAvatar, module } = await setupTests();
// await expect(true).to.be.equals(false);
// });
//
// it("skips guard pre-check if no guard is set", async () => {
// const { iAvatar, module } = await setupTests();
// await expect(true).to.be.equals(false);
// });
//
// it("pre-checks transaction if guard is set", async () => {
// const { iAvatar, module } = await setupTests();
// await expect(true).to.be.equals(false);
// });
//
// it("executes a transaction", async () => {
// const { iAvatar, module } = await setupTests();
// await expect(true).to.be.equals(false);
// });
//
// it("skips post-check if no guard is enabled", async () => {
// const { iAvatar, module } = await setupTests();
// await expect(true).to.be.equals(false);
// });
//
// it("post-checks transaction if guard is set", async () => {
// const { iAvatar, module } = await setupTests();
// await expect(true).to.be.equals(false);
// });
it("skips post-check if no guard is enabled", async () => {
const { iAvatar, guard, module, tx } = await setupTests();
await expect(
module.executeTransaction(tx.to, tx.value, tx.data, tx.operation)
).not.to.emit(guard, "postChecked");
});

it("post-checks transaction if guard is set", async () => {
const { iAvatar, guard, module, tx } = await setupTests();
await module.setGuard(guard.address);
await expect(
module.executeTransaction(tx.to, tx.value, tx.data, tx.operation)
)
.to.emit(guard, "postChecked")
.withArgs(true);
});
});

describe("execAndReturnData", async () => {
it("skips guard pre-check if no guard is set", async () => {
const { iAvatar, guard, module, tx } = await setupTests();
await expect(
module.executeTransactionReturnData(
tx.to,
tx.value,
tx.data,
tx.operation
)
);
});

it("pre-checks transaction if guard is set", async () => {
const { iAvatar, guard, module, tx } = await setupTests();
await module.setGuard(guard.address);
await expect(
module.executeTransactionReturnData(
tx.to,
tx.value,
tx.data,
tx.operation
)
)
.to.emit(guard, "preChecked")
.withArgs(true);
});

it("executes a transaction", async () => {
const { iAvatar, module, tx } = await setupTests();
await expect(
module.executeTransactionReturnData(
tx.to,
tx.value,
tx.data,
tx.operation
)
);
});

it("skips post-check if no guard is enabled", async () => {
const { iAvatar, guard, module, tx } = await setupTests();
await expect(
module.executeTransactionReturnData(
tx.to,
tx.value,
tx.data,
tx.operation
)
).not.to.emit(guard, "postChecked");
});

it("post-checks transaction if guard is set", async () => {
const { iAvatar, guard, module, tx } = await setupTests();
await module.setGuard(guard.address);
await expect(
module.executeTransactionReturnData(
tx.to,
tx.value,
tx.data,
tx.operation
)
)
.to.emit(guard, "postChecked")
.withArgs(true);
});
});
});

0 comments on commit ea0aac7

Please sign in to comment.