-
Notifications
You must be signed in to change notification settings - Fork 0
test(contracts): expand test suite to ~95% scenario coverage 🧪 #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.20; | ||
|
|
||
| import {Test} from "forge-std/Test.sol"; | ||
| import {KeyRAAccessControl} from "../src/AccessList.sol"; | ||
|
|
||
| /// @notice Handler contract that drives invariant testing by calling | ||
| /// admin and access management functions with bounded random inputs. | ||
| /// Catches only expected errors — unexpected reverts propagate as failures. | ||
| contract KeyRAAccessControlHandler is Test { | ||
| KeyRAAccessControl public acl; | ||
|
|
||
| // Track successful operations for observability | ||
| uint256 public adminsAdded; | ||
| uint256 public adminsRemoved; | ||
| uint256 public accessGranted; | ||
| uint256 public accessRevoked; | ||
|
|
||
| constructor(KeyRAAccessControl _acl) { | ||
| acl = _acl; | ||
| } | ||
|
|
||
| function addAdmin(uint256 seed) external { | ||
| address account = _boundAddr(seed); | ||
| try acl.addAdmin(account) { | ||
| adminsAdded++; | ||
| } catch (bytes memory reason) { | ||
| _expectKnownError(reason); | ||
| } | ||
| } | ||
|
|
||
| function removeAdmin(uint256 seed) external { | ||
| address account = _boundAddr(seed); | ||
| try acl.removeAdmin(account) { | ||
| adminsRemoved++; | ||
| } catch (bytes memory reason) { | ||
| _expectKnownError(reason); | ||
| } | ||
| } | ||
|
|
||
| function grantAccess(uint256 seed) external { | ||
| address account = _boundAddr(seed); | ||
| try acl.grantAccess(account) { | ||
| accessGranted++; | ||
| } catch (bytes memory reason) { | ||
| _expectKnownError(reason); | ||
| } | ||
| } | ||
|
|
||
| function revokeAccess(uint256 seed) external { | ||
| address account = _boundAddr(seed); | ||
| try acl.revokeAccess(account) { | ||
| accessRevoked++; | ||
| } catch (bytes memory reason) { | ||
| _expectKnownError(reason); | ||
| } | ||
| } | ||
|
|
||
| function _boundAddr(uint256 seed) internal pure returns (address) { | ||
| return address(uint160(bound(seed, 1, type(uint160).max))); | ||
| } | ||
|
|
||
| /// @dev Only allow known/expected revert reasons. Unknown reverts fail the test. | ||
| function _expectKnownError(bytes memory reason) internal pure { | ||
| bytes4 selector; | ||
| if (reason.length >= 4) { | ||
| assembly { | ||
| selector := mload(add(reason, 32)) | ||
| } | ||
| } | ||
|
|
||
| if ( | ||
| selector == KeyRAAccessControl.AlreadyAdmin.selector | ||
| || selector == KeyRAAccessControl.NotAnAdmin.selector | ||
| || selector == KeyRAAccessControl.CannotRemoveLastAdmin.selector | ||
| || selector == KeyRAAccessControl.AlreadyHasAccess.selector | ||
| || selector == KeyRAAccessControl.NoAccess.selector | ||
| || selector == KeyRAAccessControl.ZeroAddress.selector | ||
| ) { | ||
| return; // expected — ignore | ||
| } | ||
|
|
||
| // Unexpected revert — propagate original revert data as test failure | ||
| assembly { | ||
| revert(add(reason, 32), mload(reason)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// @notice Invariant tests for KeyRAAccessControl. | ||
| /// Verifies properties that must hold regardless of operation sequence. | ||
| contract KeyRAAccessControlInvariantTest is Test { | ||
| KeyRAAccessControl public acl; | ||
| KeyRAAccessControlHandler public handler; | ||
|
|
||
| function setUp() public { | ||
| acl = new KeyRAAccessControl(address(this)); | ||
| handler = new KeyRAAccessControlHandler(acl); | ||
|
|
||
| // Handler acts as admin so its calls go through onlyAdmin | ||
| acl.addAdmin(address(handler)); | ||
|
|
||
| targetContract(address(handler)); | ||
| } | ||
|
|
||
| /// @notice Admin count must never reach zero — the contract guard prevents it. | ||
| function invariant_adminCountNeverZero() public view { | ||
| assertGt(acl.adminCount(), 0); | ||
| } | ||
|
|
||
| /// @notice Operations are actually being exercised — not all silently reverting. | ||
| /// Uses a call counter to skip the check on the initial setUp() invocation. | ||
| uint256 private _invariantCalls; | ||
|
|
||
| function invariant_operationsExercised() public { | ||
| _invariantCalls++; | ||
| if (_invariantCalls <= 1) return; // skip initial setUp() check | ||
|
|
||
| uint256 total = | ||
| handler.adminsAdded() + handler.adminsRemoved() + handler.accessGranted() | ||
| + handler.accessRevoked(); | ||
| assertGt(total, 0, "No operations succeeded - handler may be misconfigured"); | ||
| } | ||
Klazomenai marked this conversation as resolved.
Show resolved
Hide resolved
Klazomenai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.