|
| 1 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | +pragma solidity ^0.8.18; |
| 3 | + |
| 4 | +import "ds-test/test.sol"; |
| 5 | +import "cheats/Vm.sol"; |
| 6 | +import "../../default/logs/console.sol"; |
| 7 | + |
| 8 | +contract Storage { |
| 9 | + uint256 public slot0 = 10; |
| 10 | + uint256 public slot1 = 20; |
| 11 | +} |
| 12 | + |
| 13 | +contract StoreTest is DSTest { |
| 14 | + Vm constant vm = Vm(HEVM_ADDRESS); |
| 15 | + Storage store; |
| 16 | + |
| 17 | + function setUp() public { |
| 18 | + vm.pvm(true); |
| 19 | + store = new Storage(); |
| 20 | + } |
| 21 | + |
| 22 | + function testStoreWorks() public { |
| 23 | + assertEq(store.slot0(), 10, "initial value for slot 0 is incorrect"); |
| 24 | + assertEq(store.slot1(), 20, "initial value for slot 1 is incorrect"); |
| 25 | + |
| 26 | + vm.store(address(store), bytes32(0), bytes32(uint256(1))); |
| 27 | + assertEq(store.slot0(), 1, "store failed"); |
| 28 | + assertEq(store.slot1(), 20, "store failed"); |
| 29 | + } |
| 30 | + |
| 31 | + function testStoreNotAvailableOnPrecompiles() public { |
| 32 | + assertEq(store.slot0(), 10, "initial value for slot 0 is incorrect"); |
| 33 | + assertEq(store.slot1(), 20, "initial value for slot 1 is incorrect"); |
| 34 | + |
| 35 | + vm._expectCheatcodeRevert("cannot use precompile 0x0000000000000000000000000000000000000001 as an argument"); |
| 36 | + vm.store(address(1), bytes32(0), bytes32(uint256(1))); |
| 37 | + } |
| 38 | + |
| 39 | + function testStoreFuzzed(uint256 slot0, uint256 slot1) public { |
| 40 | + assertEq(store.slot0(), 10, "initial value for slot 0 is incorrect"); |
| 41 | + assertEq(store.slot1(), 20, "initial value for slot 1 is incorrect"); |
| 42 | + |
| 43 | + vm.store(address(store), bytes32(0), bytes32(slot0)); |
| 44 | + vm.store(address(store), bytes32(uint256(1)), bytes32(slot1)); |
| 45 | + assertEq(store.slot0(), slot0, "store failed"); |
| 46 | + assertEq(store.slot1(), slot1, "store failed"); |
| 47 | + } |
| 48 | +} |
0 commit comments