-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from alchemyplatform/dp/tests
Add tests
- Loading branch information
Showing
5 changed files
with
497 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.21; | ||
|
||
import "forge-std/Test.sol"; | ||
|
||
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; | ||
import {UUPSUpgradeable} from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol"; | ||
|
||
import {CustomSlotInitializable} from "../src/CustomSlotInitializable.sol"; | ||
|
||
contract CustomSlotInitializableTest is Test { | ||
using stdStorage for StdStorage; | ||
|
||
event Initialized(uint8 version); | ||
|
||
address v1Impl; | ||
address v2Impl; | ||
V1 v1Proxy; | ||
|
||
function setUp() public { | ||
v1Impl = address(new V1()); | ||
v2Impl = address(new V2()); | ||
v1Proxy = V1(address(new ERC1967Proxy(v1Impl, abi.encodeCall(V1.initialize, ())))); | ||
} | ||
|
||
function testSimpleInitialization() public { | ||
V1 v1 = new V1(); | ||
vm.expectEmit(false, false, false, true); | ||
emit Initialized(1); | ||
v1.initialize(); | ||
assertEq(v1.getInitializedVersion(), 1); | ||
} | ||
|
||
function testUpgrade() public { | ||
vm.expectEmit(false, false, false, true); | ||
emit Initialized(2); | ||
v1Proxy.upgradeToAndCall(v2Impl, abi.encodeCall(V2.initialize, ())); | ||
V2 v2Proxy = V2(address(v1Proxy)); | ||
assertEq(v2Proxy.getInitializedVersion(), 2); | ||
} | ||
|
||
function testCannotReinitialize() public { | ||
vm.expectRevert(bytes("Initializable: contract is already initialized")); | ||
v1Proxy.upgradeToAndCall(v1Impl, abi.encodeCall(V1.initialize, ())); | ||
} | ||
|
||
function testCannotUpgradeBackwards() public { | ||
v1Proxy.upgradeToAndCall(v2Impl, abi.encodeCall(V2.initialize, ())); | ||
V2 v2Proxy = V2(address(v1Proxy)); | ||
vm.expectRevert(bytes("Initializable: contract is already initialized")); | ||
v2Proxy.upgradeToAndCall(v1Impl, abi.encodeCall(V1.initialize, ())); | ||
} | ||
|
||
function testDisableInitializers() public { | ||
v1Proxy.disableInitializers(); | ||
vm.expectRevert(bytes("Initializable: contract is already initialized")); | ||
v1Proxy.upgradeToAndCall(v2Impl, abi.encodeCall(V2.initialize, ())); | ||
} | ||
|
||
function testCannotCallDisableInitializersInInitializer() public { | ||
DisablesInitializersWhileInitializing account = new DisablesInitializersWhileInitializing(); | ||
vm.expectRevert("Initializable: contract is initializing"); | ||
account.initialize(); | ||
} | ||
|
||
function testIsInitializing() public { | ||
IsInitializingChecker checker = new IsInitializingChecker(); | ||
checker.initialize(); | ||
assertTrue(checker.wasInitializing()); | ||
assertFalse(checker.isInitializing()); | ||
} | ||
} | ||
|
||
contract V1 is CustomSlotInitializable(keccak256("storage")), UUPSUpgradeable { | ||
function initialize() public initializer {} | ||
|
||
function getInitializedVersion() public view returns (uint8) { | ||
return _getInitializedVersion(); | ||
} | ||
|
||
function disableInitializers() public { | ||
_disableInitializers(); | ||
} | ||
|
||
function _authorizeUpgrade(address newImplementation) internal pure override { | ||
(newImplementation); | ||
} | ||
} | ||
|
||
contract V2 is CustomSlotInitializable(keccak256("storage")), UUPSUpgradeable { | ||
function initialize() public reinitializer(2) {} | ||
|
||
function getInitializedVersion() public view returns (uint8) { | ||
return _getInitializedVersion(); | ||
} | ||
|
||
function _authorizeUpgrade(address newImplementation) internal pure override { | ||
(newImplementation); | ||
} | ||
} | ||
|
||
contract DisablesInitializersWhileInitializing is CustomSlotInitializable(keccak256("storage")) { | ||
function initialize() public initializer { | ||
_disableInitializers(); | ||
} | ||
} | ||
|
||
contract IsInitializingChecker is CustomSlotInitializable(keccak256("storage")) { | ||
bool public wasInitializing; | ||
|
||
function initialize() public initializer { | ||
wasInitializing = _isInitializing(); | ||
} | ||
|
||
function isInitializing() public view returns (bool) { | ||
return _isInitializing(); | ||
} | ||
} |
Oops, something went wrong.