forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArbitraryStorage.t.sol
155 lines (131 loc) · 5.01 KB
/
ArbitraryStorage.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.18;
import "ds-test/test.sol";
import "cheats/Vm.sol";
contract Counter {
uint256 public a;
address public b;
int8 public c;
address[] public owners;
function setA(uint256 _a) public {
a = _a;
}
function setB(address _b) public {
b = _b;
}
function getOwner(uint256 pos) public view returns (address) {
return owners[pos];
}
function setOwner(uint256 pos, address owner) public {
owners[pos] = owner;
}
}
contract CounterArbitraryStorageWithSeedTest is DSTest {
Vm vm = Vm(HEVM_ADDRESS);
function test_fresh_storage() public {
uint256 index = 55;
Counter counter = new Counter();
vm.setArbitraryStorage(address(counter));
// Next call would fail with array out of bounds without arbitrary storage.
address owner = counter.getOwner(index);
// Subsequent calls should retrieve same value
assertEq(counter.getOwner(index), owner);
// Change slot and make sure new value retrieved
counter.setOwner(index, address(111));
assertEq(counter.getOwner(index), address(111));
}
function test_arbitrary_storage_warm() public {
Counter counter = new Counter();
vm.setArbitraryStorage(address(counter));
assertGt(counter.a(), 0);
counter.setA(0);
// This should remain 0 if explicitly set.
assertEq(counter.a(), 0);
counter.setA(11);
assertEq(counter.a(), 11);
}
function test_arbitrary_storage_multiple_read_writes() public {
Counter counter = new Counter();
vm.setArbitraryStorage(address(counter));
uint256 slot1 = vm.randomUint(0, 100);
uint256 slot2 = vm.randomUint(0, 100);
require(slot1 != slot2, "random positions should be different");
address alice = counter.owners(slot1);
address bob = counter.owners(slot2);
require(alice != bob, "random storage values should be different");
counter.setOwner(slot1, bob);
counter.setOwner(slot2, alice);
assertEq(alice, counter.owners(slot2));
assertEq(bob, counter.owners(slot1));
}
}
contract AContract {
uint256[] public a;
address[] public b;
int8[] public c;
bytes32[] public d;
}
contract AContractArbitraryStorageWithSeedTest is DSTest {
Vm vm = Vm(HEVM_ADDRESS);
function test_arbitrary_storage_with_seed() public {
AContract target = new AContract();
vm.setArbitraryStorage(address(target));
assertEq(target.a(11), 112807530564575719000382171275495171195982096112439764207649185248041477080234);
assertEq(target.b(22), 0x9dce87df97C81f2529877E8127b4b8c13E4b2b31);
assertEq(target.c(33), 85);
assertEq(target.d(44), 0x6ceda712fc9d694d72afeea6c44d370b789a18e1a3d640068c11069e421d25f6);
}
}
contract SymbolicStore {
uint256 public testNumber = 1337; // slot 0
constructor() {}
}
contract SymbolicStorageWithSeedTest is DSTest {
Vm vm = Vm(HEVM_ADDRESS);
function test_SymbolicStorage() public {
uint256 slot = vm.randomUint(0, 100);
address addr = 0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8;
vm.setArbitraryStorage(addr);
bytes32 value = vm.load(addr, bytes32(slot));
assertEq(uint256(value), 112807530564575719000382171275495171195982096112439764207649185248041477080234);
// Load slot again and make sure we get same value.
bytes32 value1 = vm.load(addr, bytes32(slot));
assertEq(uint256(value), uint256(value1));
}
function test_SymbolicStorage1() public {
uint256 slot = vm.randomUint(0, 100);
SymbolicStore myStore = new SymbolicStore();
vm.setArbitraryStorage(address(myStore));
bytes32 value = vm.load(address(myStore), bytes32(uint256(slot)));
assertEq(uint256(value), 112807530564575719000382171275495171195982096112439764207649185248041477080234);
}
function testEmptyInitialStorage(uint256 slot) public {
bytes32 storage_value = vm.load(address(vm), bytes32(slot));
assertEq(uint256(storage_value), 0);
}
}
// <https://github.com/foundry-rs/foundry/issues/10084>
contract ArbitraryStorageOverwriteWithSeedTest is DSTest {
Vm vm = Vm(HEVM_ADDRESS);
uint256 _value;
function testArbitraryStorageFalse(uint256 value) public {
_value = value;
vm.setArbitraryStorage(address(this), false);
assertEq(_value, value);
}
function testArbitraryStorageTrue(uint256 value) public {
_value = value;
vm.setArbitraryStorage(address(this), true);
assertTrue(_value != value);
}
function testArbitraryStorageFalse_setAfter(uint256 value) public {
vm.setArbitraryStorage(address(this), false);
_value = value;
assertEq(_value, value);
}
function testArbitraryStorageTrue_setAfter(uint256 value) public {
vm.setArbitraryStorage(address(this), true);
_value = value;
assertEq(_value, value);
}
}