Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StorageReadable unit tests #154

Merged
merged 9 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 0 additions & 84 deletions test/reader/StorageReadable.spec.ts

This file was deleted.

59 changes: 59 additions & 0 deletions test/reader/StorageReadable.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
pragma solidity >=0.7.6 <0.9.0;
pragma abicoder v2;

import {Test} from "forge-std/Test.sol";
import {StorageAccessibleWrapper} from "src/contracts/test/vendor/StorageAccessibleWrapper.sol";

contract StorageReadableTest is Test {
StorageAccessibleWrapper instance;

function setUp() public {
instance = new StorageAccessibleWrapper();
}

function test_can_read_statically_sized_words() public {
instance.setFoo(42);
bytes memory actualBytes = instance.getStorageAt(instance.SLOT_FOO(), 1);
bytes memory expectedBytes = abi.encode(42);
assertEq(actualBytes, expectedBytes);
}

function test_can_read_fields_that_are_packed_into_single_storage_slot() public {
instance.setBar(7);
instance.setBam(13);
bytes memory actualBytes = instance.getStorageAt(instance.SLOT_BAR(), 1);
bytes memory expectedBytes = abi.encodePacked(new bytes(8), uint64(13), uint128(7));
assertEq(actualBytes, expectedBytes);
}

function test_can_read_arrays_in_one_go() public {
uint8 slot = instance.SLOT_BAZ();
uint256[] memory arr = new uint256[](2);
arr[0] = 42;
arr[1] = 1337;
instance.setBaz(arr);
bytes memory data = instance.getStorageAt(slot, 1);
uint256 length = abi.decode(data, (uint256));
assertEq(length, 2);
bytes memory packed = instance.getStorageAt(uint256(keccak256(abi.encode(slot))), length);
(uint256 firstValue, uint256 secondValue) = abi.decode(packed, (uint256, uint256));
assertEq(firstValue, 42);
assertEq(secondValue, 1337);
}

function test_can_read_mappings() public {
instance.setQuxKeyValue(42, 69);
bytes memory data = instance.getStorageAt(uint256(keccak256(abi.encode([42, instance.SLOT_QUX()]))), 1);
uint256 value = abi.decode(data, (uint256));
assertEq(value, 69);
}

function test_can_read_structs() public {
instance.setFoobar(19, 21);
bytes memory packed = instance.getStorageAt(instance.SLOT_FOOBAR(), 10);
(uint256 firstValue, uint256 secondValue) = abi.decode(packed, (uint256, uint256));
assertEq(firstValue, 19);
assertEq(secondValue, 21);
}
}
Loading