forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathClones.t.sol
More file actions
92 lines (73 loc) · 3.45 KB
/
Copy pathClones.t.sol
File metadata and controls
92 lines (73 loc) · 3.45 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Test} from "forge-std/Test.sol";
import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol";
contract ClonesTest is Test {
function getNumber() external pure returns (uint256) {
return 42;
}
function testSymbolicPredictDeterministicAddressSpillage(address implementation, bytes32 salt) public {
address predicted = Clones.predictDeterministicAddress(implementation, salt);
bytes32 spillage;
/// @solidity memory-safe-assembly
assembly {
spillage := and(predicted, 0xffffffffffffffffffffffff0000000000000000000000000000000000000000)
}
assertEq(spillage, bytes32(0));
}
function testSymbolicPredictWithImmutableArgsDeterministicAddressSpillage(
address implementation,
bytes32 salt,
bytes memory args
) public {
vm.assume(args.length < 0xffd3);
address predicted = Clones.predictWithImmutableArgsDeterministicAddress(implementation, args, salt);
bytes32 spillage;
/// @solidity memory-safe-assembly
assembly {
spillage := and(predicted, 0xffffffffffffffffffffffff0000000000000000000000000000000000000000)
}
assertEq(spillage, bytes32(0));
}
function testCloneDirty() external {
address cloneClean = Clones.clone(address(this));
address cloneDirty = Clones.clone(_dirty(address(this)));
// both clones have the same code
assertEq(cloneClean.code, cloneDirty.code);
// both clones behave as expected
assertEq(ClonesTest(cloneClean).getNumber(), this.getNumber());
assertEq(ClonesTest(cloneDirty).getNumber(), this.getNumber());
}
function testCloneDeterministicDirty(bytes32 salt) external {
address cloneClean = Clones.cloneDeterministic(address(this), salt);
address cloneDirty = Clones.cloneDeterministic(_dirty(address(this)), ~salt);
// both clones have the same code
assertEq(cloneClean.code, cloneDirty.code);
// both clones behave as expected
assertEq(ClonesTest(cloneClean).getNumber(), this.getNumber());
assertEq(ClonesTest(cloneDirty).getNumber(), this.getNumber());
}
function testPredictDeterministicAddressDirty(bytes32 salt) external {
address predictClean = Clones.predictDeterministicAddress(address(this), salt);
address predictDirty = Clones.predictDeterministicAddress(_dirty(address(this)), salt);
//prediction should be similar
assertEq(predictClean, predictDirty);
}
function testFetchCloneArgs(bytes memory args, bytes32 salt) external {
vm.assume(args.length < 0xffd3);
address instance1 = Clones.cloneWithImmutableArgs(address(this), args);
address instance2 = Clones.cloneWithImmutableArgsDeterministic(address(this), args, salt);
// both clones have the same code
assertEq(instance1.code, instance2.code);
// both clones behave as expected and args can be fetched
assertEq(ClonesTest(instance1).getNumber(), this.getNumber());
assertEq(ClonesTest(instance2).getNumber(), this.getNumber());
assertEq(Clones.fetchCloneArgs(instance1), args);
assertEq(Clones.fetchCloneArgs(instance2), args);
}
function _dirty(address input) private pure returns (address output) {
assembly ("memory-safe") {
output := or(input, shl(160, not(0)))
}
}
}