-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(LibInitializeGuard): test cases for LibInitializeGuard
- Loading branch information
1 parent
324ed7f
commit 1850606
Showing
12 changed files
with
583 additions
and
3 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,115 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.25; | ||
|
||
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol"; | ||
import "@openzeppelin-4.9.3/contracts/utils/Address.sol"; | ||
|
||
abstract contract InitializableTesting { | ||
/** | ||
* @dev Indicates that the contract has been initialized. | ||
* @custom:oz-retyped-from bool | ||
*/ | ||
uint8 internal _initialized; | ||
|
||
/** | ||
* @dev Indicates that the contract is in the process of being initialized. | ||
*/ | ||
bool internal _initializing; | ||
|
||
/** | ||
* @dev Triggered when the contract has been initialized or reinitialized. | ||
*/ | ||
event Initialized(uint8 version); | ||
|
||
/** | ||
* @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope, | ||
* `onlyInitializing` functions can be used to initialize parent contracts. | ||
* | ||
* Similar to `reinitializer(1)`, except that functions marked with `initializer` can be nested in the context of a | ||
* constructor. | ||
* | ||
* Emits an {Initialized} event. | ||
*/ | ||
modifier initializer() { | ||
bool isTopLevelCall = !_initializing; | ||
require( | ||
(isTopLevelCall && _initialized < 1) || (!Address.isContract(address(this)) && _initialized == 1), | ||
"Initializable: contract is already initialized" | ||
); | ||
_initialized = 1; | ||
if (isTopLevelCall) { | ||
_initializing = true; | ||
} | ||
_; | ||
if (isTopLevelCall) { | ||
_initializing = false; | ||
emit Initialized(1); | ||
} | ||
} | ||
|
||
/** | ||
* @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the | ||
* contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be | ||
* used to initialize parent contracts. | ||
* | ||
* A reinitializer may be used after the original initialization step. This is essential to configure modules that | ||
* are added through upgrades and that require initialization. | ||
* | ||
* When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer` | ||
* cannot be nested. If one is invoked in the context of another, execution will revert. | ||
* | ||
* Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in | ||
* a contract, executing them in the right order is up to the developer or operator. | ||
* | ||
* WARNING: setting the version to 255 will prevent any future reinitialization. | ||
* | ||
* Emits an {Initialized} event. | ||
*/ | ||
modifier reinitializer(uint8 version) { | ||
require(!_initializing && _initialized < version, "Initializable: contract is already initialized"); | ||
_initialized = version; | ||
_initializing = true; | ||
_; | ||
_initializing = false; | ||
emit Initialized(version); | ||
} | ||
|
||
/** | ||
* @dev Modifier to protect an initialization function so that it can only be invoked by functions with the | ||
* {initializer} and {reinitializer} modifiers, directly or indirectly. | ||
*/ | ||
modifier onlyInitializing() { | ||
require(_initializing, "Initializable: contract is not initializing"); | ||
_; | ||
} | ||
|
||
/** | ||
* @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call. | ||
* Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized | ||
* to any version. It is recommended to use this to lock implementation contracts that are designed to be called | ||
* through proxies. | ||
* | ||
* Emits an {Initialized} event the first time it is successfully executed. | ||
*/ | ||
function _disableInitializers() internal virtual { | ||
require(!_initializing, "Initializable: contract is initializing"); | ||
if (_initialized != type(uint8).max) { | ||
_initialized = type(uint8).max; | ||
emit Initialized(type(uint8).max); | ||
} | ||
} | ||
|
||
/** | ||
* @dev Returns the highest version that has been initialized. See {reinitializer}. | ||
*/ | ||
function _getInitializedVersion() internal view returns (uint8) { | ||
return _initialized; | ||
} | ||
|
||
/** | ||
* @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}. | ||
*/ | ||
function _isInitializing() internal view returns (bool) { | ||
return _initializing; | ||
} | ||
} |
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,30 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.25; | ||
|
||
import { Initializable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/proxy/utils/Initializable.sol"; | ||
import { Initializable as InitializableV5 } from | ||
"../../../dependencies/@openzeppelin-v5-5.0.2/contracts/proxy/utils/Initializable.sol"; | ||
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol"; | ||
|
||
contract SampleProxyForTestingPurpose is Ownable, InitializableV5 { | ||
uint256[50] private __gap; | ||
|
||
string internal _message; | ||
address internal _addr; | ||
|
||
constructor() { | ||
// _disableInitializers(); | ||
} | ||
|
||
function initialize(string calldata message) external initializer { | ||
_message = message; | ||
} | ||
|
||
function setMessage(string memory message) public { | ||
_message = message; | ||
} | ||
|
||
function getMessage() public view returns (string memory) { | ||
return _message; | ||
} | ||
} |
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,30 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.25; | ||
|
||
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol"; | ||
import "./InitializableTesting.sol"; | ||
|
||
contract SampleProxyForTestingPurpose2 is Ownable, InitializableTesting { | ||
uint256[50] private __gap; | ||
|
||
string internal _message; | ||
address internal _addr; | ||
|
||
constructor() { | ||
_disableInitializers(); | ||
} | ||
|
||
function initialize(string calldata message) public { | ||
_message = message; | ||
} | ||
|
||
function abcXYZ(string calldata message) public { } | ||
|
||
function setMessage(string memory message) public { | ||
_message = message; | ||
} | ||
|
||
function getMessage() public view returns (string memory) { | ||
return _message; | ||
} | ||
} |
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,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.25; | ||
|
||
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol"; | ||
import "./InitializableTesting.sol"; | ||
|
||
contract SampleProxyForTestingPurpose3 is Ownable, InitializableTesting { | ||
uint256[50] private __gap; | ||
|
||
string internal _message; | ||
address internal _addr; | ||
|
||
constructor() { | ||
_disableInitializers(); | ||
} | ||
|
||
function initialize(string calldata message) public initializer { | ||
_message = message; | ||
_initialized = type(uint8).max - 10; | ||
} | ||
|
||
function setMessage(string memory message) public { | ||
_message = message; | ||
} | ||
|
||
function getMessage() public view returns (string memory) { | ||
return _message; | ||
} | ||
} |
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,30 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.25; | ||
|
||
import { Initializable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/proxy/utils/Initializable.sol"; | ||
import { Initializable as InitializableV5 } from | ||
"../../../dependencies/@openzeppelin-v5-5.0.2/contracts/proxy/utils/Initializable.sol"; | ||
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol"; | ||
|
||
contract SampleProxyForTestingPurpose4 is Ownable, Initializable { | ||
uint256[50] private __gap; | ||
|
||
string internal _message; | ||
address internal _addr; | ||
|
||
constructor() { | ||
_disableInitializers(); | ||
} | ||
|
||
function initialize(string calldata message) external initializer { | ||
_message = message; | ||
} | ||
|
||
function setMessage(string memory message) public { | ||
_message = message; | ||
} | ||
|
||
function getMessage() public view returns (string memory) { | ||
return _message; | ||
} | ||
} |
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,35 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.25; | ||
|
||
import { Initializable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/proxy/utils/Initializable.sol"; | ||
import { Initializable as InitializableV5 } from | ||
"../../../dependencies/@openzeppelin-v5-5.0.2/contracts/proxy/utils/Initializable.sol"; | ||
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol"; | ||
|
||
contract SampleProxyForTestingPurpose5 is Ownable, Initializable { | ||
uint256[50] private __gap; | ||
|
||
string internal _message; | ||
address internal _addr; | ||
uint256 internal _newVariable; | ||
|
||
constructor() { | ||
_disableInitializers(); | ||
} | ||
|
||
function initialize(string calldata message) external initializer { | ||
_message = message; | ||
} | ||
|
||
function initializeV2(uint256 newValues) external reinitializer(2) { | ||
_newVariable = newValues; | ||
} | ||
|
||
function setMessage(string memory message) public { | ||
_message = message; | ||
} | ||
|
||
function getMessage() public view returns (string memory) { | ||
return _message; | ||
} | ||
} |
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,40 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.25; | ||
|
||
import { Initializable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/proxy/utils/Initializable.sol"; | ||
import { Initializable as InitializableV5 } from | ||
"../../../dependencies/@openzeppelin-v5-5.0.2/contracts/proxy/utils/Initializable.sol"; | ||
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol"; | ||
|
||
contract SampleProxyForTestingPurpose6 is Ownable, Initializable { | ||
uint256[50] private __gap; | ||
|
||
string internal _message; | ||
address internal _addr; | ||
uint256 internal _newVariable; | ||
uint256 internal _newVariable2; | ||
|
||
constructor() { | ||
_disableInitializers(); | ||
} | ||
|
||
function initialize(string calldata message) external initializer { | ||
_message = message; | ||
} | ||
|
||
function initializeV2(uint256 newValues) external reinitializer(2) { | ||
_newVariable = newValues; | ||
} | ||
|
||
function initializeV3(uint256 newValues) external reinitializer(3) { | ||
_newVariable2 = newValues; | ||
} | ||
|
||
function setMessage(string memory message) public { | ||
_message = message; | ||
} | ||
|
||
function getMessage() public view returns (string memory) { | ||
return _message; | ||
} | ||
} |
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,48 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.25; | ||
|
||
import { Initializable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/proxy/utils/Initializable.sol"; | ||
import { Initializable as InitializableV5 } from | ||
"../../../dependencies/@openzeppelin-v5-5.0.2/contracts/proxy/utils/Initializable.sol"; | ||
import { Ownable } from "../../../dependencies/@openzeppelin-4.9.3/contracts/access/Ownable.sol"; | ||
|
||
contract SampleProxyForTestingPurpose7 is Ownable, Initializable { | ||
uint256[50] private __gap; | ||
|
||
string internal _message; | ||
address internal _addr; | ||
uint256 internal _newVariable; | ||
uint256 internal _newVariable2; | ||
|
||
constructor() { | ||
_disableInitializers(); | ||
} | ||
|
||
function initialize(string calldata message) external initializer { | ||
_message = message; | ||
} | ||
|
||
function initializeV2(uint256 newValues) external reinitializer(2) { | ||
_newVariable = newValues; | ||
} | ||
|
||
function initializeV3(uint256 newValues) external reinitializer(3) { | ||
_newVariable2 = newValues; | ||
} | ||
|
||
function initializeV4(uint256 newValues) external reinitializer(3) { | ||
_newVariable2 = newValues; | ||
} | ||
|
||
function initializeV5(uint256 newValues) external reinitializer(5) { | ||
_newVariable2 = newValues; | ||
} | ||
|
||
function setMessage(string memory message) public { | ||
_message = message; | ||
} | ||
|
||
function getMessage() public view returns (string memory) { | ||
return _message; | ||
} | ||
} |
Oops, something went wrong.