Skip to content

Commit

Permalink
test: add Ownable 'transferOwnership' fuzz tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielstoica committed Aug 9, 2024
1 parent 58f39c9 commit 93f727e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
4 changes: 4 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ ast = true
build_info = true
extra_output = ["storageLayout"]

[profile.default.fuzz]
max_test_rejects = 500_000
runs = 100

[fmt]
bracket_spacing = true
int_types = "long"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ contract TransferOwnership_Unit_Concrete_Test is Ownable_Shared_Test {
ownableMock.transferOwnership({ newOwner: users.eve });
}

modifier whenCallerCurrentOwner() {
// Make Admin the caller for the next test suite
vm.startPrank({ msgSender: users.admin });
_;
}

function test_RevertWhen_NewOwnerZeroAddress() external whenCallerCurrentOwner {
// Expect the next call to revert with the {InvalidOwnerZeroAddress} error
vm.expectRevert(Errors.InvalidOwnerZeroAddress.selector);
Expand All @@ -35,10 +29,6 @@ contract TransferOwnership_Unit_Concrete_Test is Ownable_Shared_Test {
ownableMock.transferOwnership({ newOwner: address(0x0) });
}

modifier whenNewOwnerNotZeroAddress() {
_;
}

function test_TransferOwnership() external whenCallerCurrentOwner whenNewOwnerNotZeroAddress {
// Expect the {OwnershipTransferred} event to be emitted
vm.expectEmit();
Expand Down
42 changes: 42 additions & 0 deletions test/unit/fuzz/transferOwnership.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.26;

import { Ownable_Shared_Test } from "../shared/Ownable.t.sol";
import { Errors } from "../../utils/Errors.sol";
import { Events } from "../../utils/Events.sol";

contract TransferOwnership_Unit_Fuzz_Test is Ownable_Shared_Test {
function setUp() public virtual override {
Ownable_Shared_Test.setUp();
}

function testFuzz_RevertWhen_CallerNotCurrentOwner(address newOwner) external whenNewOwnerNotZeroAddress {
// Make sure the new owner is not the current one or the zero address
vm.assume(newOwner != users.admin && newOwner != address(0));

// Make Bob the caller for this test suite who is not the current owner
vm.startPrank({ msgSender: newOwner });

// Expect the next call to revert with the {Unauthorized} error
vm.expectRevert(Errors.Unauthorized.selector);

// Run the test
ownableMock.transferOwnership(newOwner);
}

function testFuzz_TransferOwnership(address newOwner) external whenCallerCurrentOwner whenNewOwnerNotZeroAddress {
// Make sure the new owner is not the zero address
vm.assume(newOwner != address(0));

// Expect the {OwnershipTransferred} event to be emitted
vm.expectEmit();
emit Events.OwnershipTransferred({ oldOwner: users.admin, newOwner: newOwner });

// Run the test
ownableMock.transferOwnership({ newOwner: newOwner });

// Assert the actual and expected owner
address actualOwner = ownableMock.owner();
assertEq(actualOwner, newOwner);
}
}
10 changes: 10 additions & 0 deletions test/unit/shared/Ownable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ contract Ownable_Shared_Test is Base_Test {
Base_Test.setUp();
ownableMock = new OwnableMock({ _owner: users.admin });
}

modifier whenCallerCurrentOwner() {
// Make Admin the caller for the next test suite
vm.startPrank({ msgSender: users.admin });
_;
}

modifier whenNewOwnerNotZeroAddress() {
_;
}
}

0 comments on commit 93f727e

Please sign in to comment.