|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity 0.8.15; |
| 3 | + |
| 4 | +// Testing utilities |
| 5 | +import { Test } from "forge-std/Test.sol"; |
| 6 | +import "../predeploys/NonceManager.sol"; |
| 7 | + |
| 8 | +contract NonceManagerTest is Test { |
| 9 | + address private constant aaEntryPoint = 0x0000000000000000000000000000000000007560; |
| 10 | + address private constant alice = 0x0000000000000000000000000000000000007777; |
| 11 | + uint192 public aliceKey; |
| 12 | + error InvalidLength(); |
| 13 | + NonceManager nonceManager; |
| 14 | + |
| 15 | + /// @dev Sets up the test suite. |
| 16 | + function setUp() public virtual { |
| 17 | + nonceManager = new NonceManager(); |
| 18 | + aliceKey = uint192(3); |
| 19 | + } |
| 20 | + |
| 21 | + /// @dev Tests that increasing nonce is done properly. |
| 22 | + function test_validateIncrement_succeeds() public { |
| 23 | + vm.prank(aaEntryPoint); |
| 24 | + (bool success, bytes memory returnData) = |
| 25 | + address(nonceManager).call(abi.encodePacked(alice, aliceKey, uint64(0))); |
| 26 | + assertTrue(success); |
| 27 | + assertEq(returnData, new bytes(0)); |
| 28 | + } |
| 29 | + |
| 30 | + /// @dev Tests that increasing nonce with invalid nonce fails. |
| 31 | + function test_validateIncrement_invalidNonce_fails() external { |
| 32 | + vm.prank(aaEntryPoint); |
| 33 | + (bool success,) = address(nonceManager).call(abi.encodePacked(alice, aliceKey, uint64(1))); |
| 34 | + assertFalse(success); |
| 35 | + } |
| 36 | + |
| 37 | + /// @dev Tests that getting nonce through fallback is done properly. |
| 38 | + function test_fallback_get_succeeds() external { |
| 39 | + (bool success, bytes memory returnData) = address(nonceManager).call(abi.encodePacked(alice, aliceKey)); |
| 40 | + assertTrue(success); |
| 41 | + assertEq(returnData, abi.encodePacked(uint192(aliceKey), uint64(0))); |
| 42 | + |
| 43 | + test_validateIncrement_succeeds(); |
| 44 | + |
| 45 | + (success, returnData) = address(nonceManager).call(abi.encodePacked(alice, aliceKey)); |
| 46 | + assertTrue(success); |
| 47 | + assertEq(returnData, abi.encodePacked(uint192(aliceKey), uint64(1))); |
| 48 | + } |
| 49 | + |
| 50 | + /// @dev Tests that getting nonce with invalid length fails. |
| 51 | + function test_fallback_get_invalidLength_fails() external { |
| 52 | + vm.expectRevert(InvalidLength.selector); |
| 53 | + (bool success,) = address(nonceManager).call(abi.encodePacked(alice, aliceKey, uint64(1))); |
| 54 | + (success); // silence unused variable warning |
| 55 | + } |
| 56 | + |
| 57 | + /// @dev Fuzz test for validateIncrement function. |
| 58 | + function testFuzz_validateIncrement_succeeds(uint256 n) external { |
| 59 | + bool success; |
| 60 | + bytes memory returnData; |
| 61 | + |
| 62 | + n = n % 1000; // limit the number of iterations to prevent OOG |
| 63 | + for (uint256 i = 0; i < n; i++) { |
| 64 | + vm.prank(aaEntryPoint); |
| 65 | + (success,) = address(nonceManager).call(abi.encodePacked(alice, aliceKey, uint64(i))); |
| 66 | + assertTrue(success); |
| 67 | + } |
| 68 | + |
| 69 | + (success, returnData) = address(nonceManager).call(abi.encodePacked(alice, aliceKey)); |
| 70 | + assertTrue(success); |
| 71 | + assertEq(returnData, abi.encodePacked(uint192(aliceKey), uint64(n))); |
| 72 | + } |
| 73 | +} |
0 commit comments