Skip to content

Commit

Permalink
Changes in test/ resulting from the audit
Browse files Browse the repository at this point in the history
  • Loading branch information
IaroslavMazur committed Jul 4, 2023
1 parent 36b976b commit e8afcd1
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 77 deletions.
6 changes: 3 additions & 3 deletions test/Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ abstract contract Base_Test is Assertions, Events, StdCheats, StdUtils {
}

/// @dev Generates an address by hashing the name, labels the address and funds it with test assets.
function createUser(string memory name) internal returns (address payable addr) {
function createUser(string memory name) private returns (address payable addr) {
addr = payable(makeAddr(name));
vm.deal({ account: addr, newBalance: 100 ether });
}
Expand All @@ -138,7 +138,7 @@ abstract contract Base_Test is Assertions, Events, StdCheats, StdUtils {
}

/// @dev Conditionally deploy the registry either normally or from a source precompiled with `--via-ir`.
function deployRegistryConditionally() internal {
function deployRegistryConditionally() private {
if (!isTestOptimizedProfile()) {
registry = new PRBProxyRegistry();
} else {
Expand All @@ -149,7 +149,7 @@ abstract contract Base_Test is Assertions, Events, StdCheats, StdUtils {
}

/// @dev Reads the proxy bytecode either normally or from precompiled source.
function getProxyBytecode() internal returns (bytes memory) {
function getProxyBytecode() private returns (bytes memory) {
if (!isTestOptimizedProfile()) {
return type(PRBProxy).creationCode;
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/mocks/plugins/PluginReverter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract PluginReverter is IPRBProxyPlugin, TargetReverter {
methods[1] = this.withCustomError.selector;
methods[2] = this.withRequire.selector;
methods[3] = this.withReasonString.selector;
methods[4] = this.dueToNoPayableModifier.selector;
methods[4] = this.dueToNoPayableFunction.selector;

return methods;
}
Expand Down
8 changes: 0 additions & 8 deletions test/mocks/targets/TargetPayable.sol

This file was deleted.

2 changes: 1 addition & 1 deletion test/mocks/targets/TargetReverter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract TargetReverter {
revert("You shall not pass");
}

function dueToNoPayableModifier() external pure returns (uint256) {
function dueToNoPayableFunction() external pure returns (uint256) {
return 0;
}
}
2 changes: 1 addition & 1 deletion test/proxy/Proxy.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ contract Proxy_Test is Base_Test {

// Deploy and label the default proxy.
proxy = registry.deployFor({ owner: users.alice });
vm.label({ account: address(proxy), newLabel: "Alice Proxy" });
vm.label({ account: address(proxy), newLabel: "Alice's Proxy" });
}
}
4 changes: 2 additions & 2 deletions test/proxy/execute/execute.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ contract Execute_Test is Proxy_Test {
proxy.execute(address(targets.reverter), data);
}

function test_RevertWhen_Error_NoPayableModifier()
function test_RevertWhen_Error_NoPayableFunction()
external
whenCallerAuthorized
whenTargetContract
whenDelegateCallReverts
{
bytes memory data = bytes.concat(targets.reverter.dueToNoPayableModifier.selector);
bytes memory data = bytes.concat(targets.reverter.dueToNoPayableFunction.selector);
vm.expectRevert();
proxy.execute{ value: 0.1 ether }(address(targets.reverter), data);
}
Expand Down
2 changes: 1 addition & 1 deletion test/proxy/execute/execute.tree
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ execute.t.sol
│ ├── it should revert with a custom error
│ ├── it should revert with a require
│ ├── it should revert with a reason string
│ └── it should revert due to no payable modifier
│ └── it should revert due to no payable function
└── when the delegate call does not revert
├── when Ether is sent
│ └── it should return the Ether amount
Expand Down
7 changes: 4 additions & 3 deletions test/proxy/receive/receive.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ pragma solidity >=0.8.19 <=0.9.0;
import { Proxy_Test } from "../Proxy.t.sol";

contract Receive_Test is Proxy_Test {
uint256 private value = 1 ether;

function setUp() public virtual override {
Proxy_Test.setUp();
}

function test_RevertWhen_CallDataNonEmpty() external {
uint256 value = 1 ether;
bytes memory data = bytes.concat("0xcafe");
(bool condition,) = address(proxy).call{ value: value }(data);
assertFalse(condition);
Expand All @@ -20,12 +21,12 @@ contract Receive_Test is Proxy_Test {
}

function test_Receive() external whenCallDataEmpty {
uint256 value = 1 ether;
uint256 previousBalance = address(proxy).balance;
(bool condition,) = address(proxy).call{ value: value }("");
assertTrue(condition);

uint256 actualBalance = address(proxy).balance;
uint256 expectedBalance = value;
uint256 expectedBalance = previousBalance + value;
assertEq(actualBalance, expectedBalance, "proxy balance mismatch");
}
}
34 changes: 12 additions & 22 deletions test/proxy/run-plugin/runPlugin.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ contract RunPlugin_Test is Proxy_Test {

function test_RevertWhen_Panic_DivisionByZero() external whenPluginInstalled whenDelegateCallReverts {
registry.installPlugin(plugins.panic);
vm.expectRevert(stdError.arithmeticError);
vm.expectRevert(stdError.divisionError);
(bool success,) = address(proxy).call(abi.encodeWithSelector(plugins.panic.divisionByZero.selector));
success;
}

function test_RevertWhen_Panic_IndexOOB() external whenPluginInstalled whenDelegateCallReverts {
registry.installPlugin(plugins.panic);
vm.expectRevert(stdError.arithmeticError);
vm.expectRevert(stdError.indexOOBError);
(bool success,) = address(proxy).call(abi.encodeWithSelector(plugins.panic.indexOOB.selector));
success;
}
Expand All @@ -76,7 +76,7 @@ contract RunPlugin_Test is Proxy_Test {

function test_RevertWhen_Error_Require() external whenPluginInstalled whenDelegateCallReverts {
registry.installPlugin(plugins.reverter);
vm.expectRevert(TargetReverter.SomeError.selector);
vm.expectRevert();
(bool success,) = address(proxy).call(abi.encodeWithSelector(plugins.reverter.withRequire.selector));
success;
}
Expand All @@ -92,7 +92,7 @@ contract RunPlugin_Test is Proxy_Test {
_;
}

function test_RunPlugin_EtherSent()
function test_RunPlugin_PluginReceivesEther()
external
whenPluginInstalled
whenDelegateCallReverts
Expand Down Expand Up @@ -126,9 +126,10 @@ contract RunPlugin_Test is Proxy_Test {

// Install the plugin and run it.
registry.installPlugin(plugins.selfDestructer);
(bool success,) =
(bool success, bytes memory response) =
address(proxy).call(abi.encodeWithSelector(plugins.selfDestructer.destroyMe.selector, users.bob));
success;
assertTrue(success);
assertEq(response.length, 0);

// Assert that Bob's balance has increased by the contract's balance.
uint256 actualBobBalance = users.bob.balance;
Expand All @@ -147,20 +148,6 @@ contract RunPlugin_Test is Proxy_Test {
whenDelegateCallDoesNotRevert
whenNoEtherSent
whenPluginDoesNotSelfDestruct
{
registry.installPlugin(plugins.basic);
(, bytes memory actualResponse) = address(proxy).call(abi.encodeWithSelector(plugins.basic.foo.selector));
bytes memory expectedResponse = abi.encode(bytes("foo"));
assertEq(actualResponse, expectedResponse, "basic.foo response mismatch");
}

function test_RunPlugin_Event()
external
whenPluginInstalled
whenDelegateCallReverts
whenDelegateCallDoesNotRevert
whenNoEtherSent
whenPluginDoesNotSelfDestruct
{
registry.installPlugin(plugins.basic);
vm.expectEmit({ emitter: address(proxy) });
Expand All @@ -169,7 +156,10 @@ contract RunPlugin_Test is Proxy_Test {
data: abi.encodeWithSelector(TargetBasic.foo.selector),
response: abi.encode(bytes("foo"))
});
(bool success,) = address(proxy).call(abi.encodeWithSelector(plugins.basic.foo.selector));
success;
(bool success, bytes memory actualResponse) =
address(proxy).call(abi.encodeWithSelector(plugins.basic.foo.selector));
assertTrue(success);
bytes memory expectedResponse = abi.encode(bytes("foo"));
assertEq(actualResponse, expectedResponse, "basic.foo response mismatch");
}
}
3 changes: 1 addition & 2 deletions test/proxy/run-plugin/runPlugin.tree
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ runPlugin.t.sol
├── when the plugin self-destructs
│ └── it should return an empty response and send the ETH to the SELFDESTRUCT recipient
└── when the plugin does not self-destruct
├── it should run the plugin
└── it should emit a {RunPlugin} event
└── it should run the plugin and emit a {RunPlugin} event

Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,26 @@ contract DeployAndExecuteAndInstallPlugin_Test is Registry_Test {
assertEq(actualProxyAddress, expectedProxyAddress, "proxy address mismatch");
}

function testFuzz_DeployAndExecuteAndInstallPlugin_Plugin() external whenOwnerDoesNotHaveProxy {
function testFuzz_DeployAndExecuteAndInstallPlugin_Plugin(address owner) external whenOwnerDoesNotHaveProxy {
changePrank({ msgSender: owner });

registry.deployAndExecuteAndInstallPlugin(target, data, plugins.basic);
bytes4[] memory pluginMethods = plugins.basic.getMethods();
for (uint256 i = 0; i < pluginMethods.length; ++i) {
IPRBProxyPlugin actualPlugin = registry.getPluginByOwner({ owner: users.alice, method: pluginMethods[i] });
IPRBProxyPlugin actualPlugin = registry.getPluginByOwner({ owner: owner, method: pluginMethods[i] });
IPRBProxyPlugin expectedPlugin = plugins.basic;
assertEq(actualPlugin, expectedPlugin, "plugin method not installed");
}
}

function testFuzz_DeployAndExecuteAndInstallPlugin_PluginReverseMapping() external whenOwnerDoesNotHaveProxy {
function testFuzz_DeployAndExecuteAndInstallPlugin_PluginReverseMapping(address owner)
external
whenOwnerDoesNotHaveProxy
{
changePrank({ msgSender: owner });

registry.deployAndExecuteAndInstallPlugin(target, data, plugins.basic);
bytes4[] memory actualMethods = registry.getMethodsByOwner({ owner: users.alice, plugin: plugins.basic });
bytes4[] memory actualMethods = registry.getMethodsByOwner({ owner: owner, plugin: plugins.basic });
bytes4[] memory expectedMethods = plugins.basic.getMethods();
assertEq(actualMethods, expectedMethods, "methods not saved in reverse mapping");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ contract DeployAndInstallPlugin_Test is Registry_Test {
vm.expectRevert(
abi.encodeWithSelector(IPRBProxyRegistry.PRBProxyRegistry_OwnerHasProxy.selector, users.alice, proxy)
);
registry.deploy();
registry.deployAndInstallPlugin(plugins.basic);
}

modifier whenOwnerDoesNotHaveProxy() {
Expand Down Expand Up @@ -48,19 +48,23 @@ contract DeployAndInstallPlugin_Test is Registry_Test {
assertEq(actualProxyAddress, expectedProxyAddress, "proxy address mismatch");
}

function testFuzz_DeployAndInstallPlugin_Plugin() external whenOwnerDoesNotHaveProxy {
function testFuzz_DeployAndInstallPlugin_Plugin(address owner) external whenOwnerDoesNotHaveProxy {
changePrank({ msgSender: owner });
registry.deployAndInstallPlugin(plugins.basic);

bytes4[] memory pluginMethods = plugins.basic.getMethods();
for (uint256 i = 0; i < pluginMethods.length; ++i) {
IPRBProxyPlugin actualPlugin = registry.getPluginByOwner({ owner: users.alice, method: pluginMethods[i] });
IPRBProxyPlugin actualPlugin = registry.getPluginByOwner({ owner: owner, method: pluginMethods[i] });
IPRBProxyPlugin expectedPlugin = plugins.basic;
assertEq(actualPlugin, expectedPlugin, "plugin method not installed");
}
}

function testFuzz_DeployAndInstallPlugin_PluginReverseMapping() external whenOwnerDoesNotHaveProxy {
function testFuzz_DeployAndInstallPlugin_PluginReverseMapping(address owner) external whenOwnerDoesNotHaveProxy {
changePrank({ msgSender: owner });
registry.deployAndInstallPlugin(plugins.basic);
bytes4[] memory actualMethods = registry.getMethodsByOwner({ owner: users.alice, plugin: plugins.basic });

bytes4[] memory actualMethods = registry.getMethodsByOwner({ owner: owner, plugin: plugins.basic });
bytes4[] memory expectedMethods = plugins.basic.getMethods();
assertEq(actualMethods, expectedMethods, "methods not saved in reverse mapping");
}
Expand Down
2 changes: 1 addition & 1 deletion test/registry/deploy-for/deployFor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract DeployFor_Test is Registry_Test {
}

function test_RevertWhen_OwnerHasProxy() external {
IPRBProxy proxy = registry.deployFor({ owner: users.alice });
IPRBProxy proxy = registry.deploy();
vm.expectRevert(
abi.encodeWithSelector(IPRBProxyRegistry.PRBProxyRegistry_OwnerHasProxy.selector, users.alice, proxy)
);
Expand Down
3 changes: 1 addition & 2 deletions test/registry/install-plugin/installPlugin.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import { Registry_Test } from "../Registry.t.sol";
contract InstallPlugin_Test is Registry_Test {
function test_RevertWhen_CallerDoesNotHaveProxy() external {
vm.expectRevert(
abi.encodeWithSelector(IPRBProxyRegistry.PRBProxyRegistry_CallerDoesNotHaveProxy.selector, users.bob)
abi.encodeWithSelector(IPRBProxyRegistry.PRBProxyRegistry_CallerDoesNotHaveProxy.selector, users.alice)
);
changePrank({ msgSender: users.bob });
registry.installPlugin(plugins.empty);
}

Expand Down
18 changes: 10 additions & 8 deletions test/registry/set-permission/setPermission.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import { Registry_Test } from "../Registry.t.sol";
contract SetPermission_Test is Registry_Test {
function test_RevertWhen_CallerDoesNotHaveProxy() external {
vm.expectRevert(
abi.encodeWithSelector(IPRBProxyRegistry.PRBProxyRegistry_CallerDoesNotHaveProxy.selector, users.bob)
abi.encodeWithSelector(IPRBProxyRegistry.PRBProxyRegistry_CallerDoesNotHaveProxy.selector, users.alice)
);
changePrank({ msgSender: users.bob });
registry.setPermission({ envoy: users.envoy, target: address(targets.basic), permission: true });
}

Expand All @@ -19,26 +18,26 @@ contract SetPermission_Test is Registry_Test {
_;
}

function test_SetPermission_PermissionNotSet() external whenCallerHasProxy {
function test_SetPermission_PermissionNotSetPreviously() external whenCallerHasProxy {
registry.setPermission({ envoy: users.envoy, target: address(targets.basic), permission: true });
bool permission =
registry.getPermissionByOwner({ owner: users.alice, envoy: users.envoy, target: address(targets.basic) });
assertTrue(permission, "permission mismatch");
}

modifier whenPermissionSet() {
modifier whenPermissionSetPreviously() {
registry.setPermission({ envoy: users.envoy, target: address(targets.basic), permission: true });
_;
}

function test_SetPermission_ResetPermission() external whenCallerHasProxy whenPermissionSet {
function test_SetPermission_ResetPermission() external whenCallerHasProxy whenPermissionSetPreviously {
registry.setPermission({ envoy: users.envoy, target: address(targets.basic), permission: true });
bool permission =
registry.getPermissionByOwner({ owner: users.alice, envoy: users.envoy, target: address(targets.basic) });
assertTrue(permission, "permission mismatch");
}

function test_SetPermission_ResetPermission_Event() external whenCallerHasProxy whenPermissionSet {
function test_SetPermission_ResetPermission_Event() external whenCallerHasProxy whenPermissionSetPreviously {
vm.expectEmit({ emitter: address(registry) });
emit SetPermission({
owner: users.alice,
Expand All @@ -50,11 +49,14 @@ contract SetPermission_Test is Registry_Test {
registry.setPermission({ envoy: users.envoy, target: address(targets.basic), permission: true });
}

function test_SetPermission_UnsetPermission() external whenCallerHasProxy whenPermissionSet {
function test_SetPermission_UnsetPermission() external whenCallerHasProxy whenPermissionSetPreviously {
registry.setPermission({ envoy: users.envoy, target: address(targets.basic), permission: false });
bool permission =
registry.getPermissionByOwner({ owner: users.alice, envoy: users.envoy, target: address(targets.basic) });
assertFalse(permission, "permission mismatch");
}

function test_SetPermission_UnsetPermission_Event() external whenCallerHasProxy whenPermissionSet {
function test_SetPermission_UnsetPermission_Event() external whenCallerHasProxy whenPermissionSetPreviously {
vm.expectEmit({ emitter: address(registry) });
emit SetPermission({
owner: users.alice,
Expand Down
8 changes: 4 additions & 4 deletions test/registry/set-permission/setPermission.tree
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
setPermission.t.sol
├── when the caller is not the owner
├── when the caller doesn't have a proxy
│ └── it should revert
└── when the caller is the owner
├── when the permission has not been set
└── when the caller has a proxy
├── when the permission has not been set previously
│ └── it should set the permission
└── when the permission has been set
└── when the permission has been set previously
├── when resetting the permission
│ ├── it should reset the permission
│ └── it should emit a {SetPermission} event
Expand Down
Loading

0 comments on commit e8afcd1

Please sign in to comment.