SlotDerivation.t.sol 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: MIT
  2. // This file was procedurally generated from scripts/generate/templates/SlotDerivation.t.js.
  3. pragma solidity ^0.8.20;
  4. import {Test} from "forge-std/Test.sol";
  5. import {SymTest} from "halmos-cheatcodes/SymTest.sol";
  6. import {SlotDerivation} from "@openzeppelin/contracts/utils/SlotDerivation.sol";
  7. contract SlotDerivationTest is Test, SymTest {
  8. using SlotDerivation for bytes32;
  9. bytes[] private _array;
  10. function symbolicDeriveArray(uint256 length, uint256 offset) public {
  11. vm.assume(length > 0);
  12. vm.assume(offset < length);
  13. _assertDeriveArray(length, offset);
  14. }
  15. function testDeriveArray(uint256 length, uint256 offset) public {
  16. length = bound(length, 1, type(uint256).max);
  17. offset = bound(offset, 0, length - 1);
  18. _assertDeriveArray(length, offset);
  19. }
  20. function _assertDeriveArray(uint256 length, uint256 offset) public {
  21. bytes32 baseSlot;
  22. assembly {
  23. baseSlot := _array.slot
  24. sstore(baseSlot, length) // store length so solidity access does not revert
  25. }
  26. bytes storage derived = _array[offset];
  27. bytes32 derivedSlot;
  28. assembly {
  29. derivedSlot := derived.slot
  30. }
  31. assertEq(baseSlot.deriveArray().offset(offset), derivedSlot);
  32. }
  33. mapping(address => bytes) private _addressMapping;
  34. function testSymbolicDeriveMappingAddress(address key) public view {
  35. bytes32 baseSlot;
  36. assembly {
  37. baseSlot := _addressMapping.slot
  38. }
  39. bytes storage derived = _addressMapping[key];
  40. bytes32 derivedSlot;
  41. assembly {
  42. derivedSlot := derived.slot
  43. }
  44. assertEq(baseSlot.deriveMapping(key), derivedSlot);
  45. }
  46. mapping(bool => bytes) private _boolMapping;
  47. function testSymbolicDeriveMappingBoolean(bool key) public view {
  48. bytes32 baseSlot;
  49. assembly {
  50. baseSlot := _boolMapping.slot
  51. }
  52. bytes storage derived = _boolMapping[key];
  53. bytes32 derivedSlot;
  54. assembly {
  55. derivedSlot := derived.slot
  56. }
  57. assertEq(baseSlot.deriveMapping(key), derivedSlot);
  58. }
  59. mapping(bytes32 => bytes) private _bytes32Mapping;
  60. function testSymbolicDeriveMappingBytes32(bytes32 key) public view {
  61. bytes32 baseSlot;
  62. assembly {
  63. baseSlot := _bytes32Mapping.slot
  64. }
  65. bytes storage derived = _bytes32Mapping[key];
  66. bytes32 derivedSlot;
  67. assembly {
  68. derivedSlot := derived.slot
  69. }
  70. assertEq(baseSlot.deriveMapping(key), derivedSlot);
  71. }
  72. mapping(bytes4 => bytes) private _bytes4Mapping;
  73. function testSymbolicDeriveMappingBytes4(bytes4 key) public view {
  74. bytes32 baseSlot;
  75. assembly {
  76. baseSlot := _bytes4Mapping.slot
  77. }
  78. bytes storage derived = _bytes4Mapping[key];
  79. bytes32 derivedSlot;
  80. assembly {
  81. derivedSlot := derived.slot
  82. }
  83. assertEq(baseSlot.deriveMapping(key), derivedSlot);
  84. }
  85. mapping(uint256 => bytes) private _uint256Mapping;
  86. function testSymbolicDeriveMappingUint256(uint256 key) public view {
  87. bytes32 baseSlot;
  88. assembly {
  89. baseSlot := _uint256Mapping.slot
  90. }
  91. bytes storage derived = _uint256Mapping[key];
  92. bytes32 derivedSlot;
  93. assembly {
  94. derivedSlot := derived.slot
  95. }
  96. assertEq(baseSlot.deriveMapping(key), derivedSlot);
  97. }
  98. mapping(uint32 => bytes) private _uint32Mapping;
  99. function testSymbolicDeriveMappingUint32(uint32 key) public view {
  100. bytes32 baseSlot;
  101. assembly {
  102. baseSlot := _uint32Mapping.slot
  103. }
  104. bytes storage derived = _uint32Mapping[key];
  105. bytes32 derivedSlot;
  106. assembly {
  107. derivedSlot := derived.slot
  108. }
  109. assertEq(baseSlot.deriveMapping(key), derivedSlot);
  110. }
  111. mapping(int256 => bytes) private _int256Mapping;
  112. function testSymbolicDeriveMappingInt256(int256 key) public view {
  113. bytes32 baseSlot;
  114. assembly {
  115. baseSlot := _int256Mapping.slot
  116. }
  117. bytes storage derived = _int256Mapping[key];
  118. bytes32 derivedSlot;
  119. assembly {
  120. derivedSlot := derived.slot
  121. }
  122. assertEq(baseSlot.deriveMapping(key), derivedSlot);
  123. }
  124. mapping(int32 => bytes) private _int32Mapping;
  125. function testSymbolicDeriveMappingInt32(int32 key) public view {
  126. bytes32 baseSlot;
  127. assembly {
  128. baseSlot := _int32Mapping.slot
  129. }
  130. bytes storage derived = _int32Mapping[key];
  131. bytes32 derivedSlot;
  132. assembly {
  133. derivedSlot := derived.slot
  134. }
  135. assertEq(baseSlot.deriveMapping(key), derivedSlot);
  136. }
  137. mapping(string => bytes) private _stringMapping;
  138. function testDeriveMappingString(string memory key) public view {
  139. _assertDeriveMappingString(key);
  140. }
  141. function symbolicDeriveMappingString() public view {
  142. _assertDeriveMappingString(svm.createString(256, "DeriveMappingStringInput"));
  143. }
  144. function _assertDeriveMappingString(string memory key) internal view {
  145. bytes32 baseSlot;
  146. assembly {
  147. baseSlot := _stringMapping.slot
  148. }
  149. bytes storage derived = _stringMapping[key];
  150. bytes32 derivedSlot;
  151. assembly {
  152. derivedSlot := derived.slot
  153. }
  154. assertEq(baseSlot.deriveMapping(key), derivedSlot);
  155. }
  156. mapping(bytes => bytes) private _bytesMapping;
  157. function testDeriveMappingBytes(bytes memory key) public view {
  158. _assertDeriveMappingBytes(key);
  159. }
  160. function symbolicDeriveMappingBytes() public view {
  161. _assertDeriveMappingBytes(svm.createBytes(256, "DeriveMappingBytesInput"));
  162. }
  163. function _assertDeriveMappingBytes(bytes memory key) internal view {
  164. bytes32 baseSlot;
  165. assembly {
  166. baseSlot := _bytesMapping.slot
  167. }
  168. bytes storage derived = _bytesMapping[key];
  169. bytes32 derivedSlot;
  170. assembly {
  171. derivedSlot := derived.slot
  172. }
  173. assertEq(baseSlot.deriveMapping(key), derivedSlot);
  174. }
  175. function testSymbolicDeriveMappingBooleanDirty(bytes32 dirtyKey) public {
  176. bool key;
  177. assembly {
  178. key := dirtyKey
  179. }
  180. // run the "normal" test using a potentially dirty value
  181. testSymbolicDeriveMappingBoolean(key);
  182. }
  183. function testSymbolicDeriveMappingAddressDirty(bytes32 dirtyKey) public {
  184. address key;
  185. assembly {
  186. key := dirtyKey
  187. }
  188. // run the "normal" test using a potentially dirty value
  189. testSymbolicDeriveMappingAddress(key);
  190. }
  191. }