NoncesKeyed.t.sol 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // SPDX-License-Identifier: MIT
  2. // This file was procedurally generated from scripts/generate/templates/Packing.t.js.
  3. pragma solidity ^0.8.20;
  4. import {Test} from "forge-std/Test.sol";
  5. import {NoncesKeyed} from "@openzeppelin/contracts/utils/NoncesKeyed.sol";
  6. import {Nonces} from "@openzeppelin/contracts/utils/Nonces.sol";
  7. // CAUTION: Unsafe mock for testing purposes.
  8. contract NoncesKeyedMock is NoncesKeyed {
  9. function useNonce(address owner, uint192 key) public returns (uint256) {
  10. return _useNonce(owner, key);
  11. }
  12. function useCheckedNonce(address owner, uint192 key, uint64 nonce) public {
  13. _useCheckedNonce(owner, key, nonce);
  14. }
  15. }
  16. contract NoncesKeyedTest is Test {
  17. NoncesKeyedMock private _mock;
  18. function setUp() public {
  19. _mock = new NoncesKeyedMock();
  20. }
  21. function testSymbolicUseNonce(address owner, uint192 key) public {
  22. uint256 prevNonce = _mock.useNonce(owner, key);
  23. assertEq(prevNonce + 1, _mock.nonces(owner, key));
  24. }
  25. function testSymbolicUseCheckedNonceLiveness(address owner, uint192 key) public {
  26. uint256 currNonce = _mock.nonces(owner, key);
  27. // Does not revert
  28. _mock.useCheckedNonce(owner, key, uint64(currNonce));
  29. assertEq(currNonce + 1, _mock.nonces(owner, key));
  30. }
  31. function testUseCheckedNonce(address owner, uint192 key, uint64 nonce) public {
  32. uint256 currNonce = _mock.nonces(owner, key);
  33. if (uint64(currNonce) == nonce) {
  34. _mock.useCheckedNonce(owner, key, nonce);
  35. } else {
  36. vm.expectRevert(abi.encodeWithSelector(Nonces.InvalidAccountNonce.selector, owner, currNonce));
  37. _mock.useCheckedNonce(owner, key, nonce);
  38. }
  39. }
  40. }