forked from Vectorized/solady
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimedRoles.t.sol
177 lines (156 loc) · 6.77 KB
/
TimedRoles.t.sol
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import {LibSort} from "../src/utils/LibSort.sol";
import {DynamicArrayLib} from "../src/utils/DynamicArrayLib.sol";
import "./utils/SoladyTest.sol";
import "./utils/mocks/MockTimedRoles.sol";
contract TimedRolesTest is SoladyTest {
using DynamicArrayLib for *;
event TimedRoleSet(
address indexed holder, uint256 indexed timedRole, uint40 start, uint40 expires
);
MockTimedRoles mockTimedRoles;
function setUp() public {
mockTimedRoles = new MockTimedRoles();
mockTimedRoles.setMaxTimedRole(type(uint256).max);
mockTimedRoles.setOwner(address(this));
}
struct TimedRoleConfig {
address holder;
uint256 role;
uint40 start;
uint40 expires;
}
function _sampleTimedRoleConfig() internal returns (TimedRoleConfig memory c) {
uint256 m = 0xf00000000000000000000000000000000000000000000000000000000000000f;
c.holder = _randomNonZeroAddress();
c.role = _randomUniform() & m;
(c.start, c.expires) = _sampleValidActiveTimeRange();
}
function _hasDuplicateKeys(TimedRoleConfig[] memory a) internal pure returns (bool) {
bytes32[] memory hashes = new bytes32[](a.length);
for (uint256 i; i != a.length; ++i) {
hashes[i] = keccak256(abi.encode(a[i].holder, a[i].role));
}
LibSort.insertionSort(hashes);
LibSort.uniquifySorted(hashes);
return hashes.length != a.length;
}
function _sampleTimedRoleConfigs() internal returns (TimedRoleConfig[] memory a) {
a = new TimedRoleConfig[](_randomUniform() & 3);
for (uint256 i; i != a.length; ++i) {
a[i] = _sampleTimedRoleConfig();
}
}
function _sampleActiveTimeRange() internal returns (uint40 start, uint40 expires) {
if (_randomChance(2)) {
start = uint40(_random());
expires = uint40(_random());
} else {
start = uint8(_random());
expires = uint8(_random());
}
}
function _sampleValidActiveTimeRange() internal returns (uint40 start, uint40 expires) {
do {
(start, expires) = _sampleActiveTimeRange();
} while (expires < start);
}
function _sampleInvalidActiveTimeRange() internal returns (uint40 start, uint40 expires) {
do {
(start, expires) = _sampleActiveTimeRange();
} while (!(expires < start));
}
function testSetAndGetTimedRoles(bytes32) public {
TimedRoleConfig[] memory a = _sampleTimedRoleConfigs();
uint256 targetTimestamp = _bound(_random(), 0, _randomChance(2) ? 0xff : 2 ** 41 - 1);
vm.warp(targetTimestamp);
for (uint256 i; i != a.length; ++i) {
TimedRoleConfig memory c = a[i];
vm.expectEmit(true, true, true, true);
emit TimedRoleSet(c.holder, c.role, c.start, c.expires);
mockTimedRoles.setTimedRole(c.holder, c.role, c.start, c.expires);
(bool isActive, uint40 start, uint40 expires) =
mockTimedRoles.timedRoleActive(c.holder, c.role);
assertEq(start, c.start);
assertEq(expires, c.expires);
assertEq(isActive, start <= targetTimestamp && targetTimestamp <= expires);
}
if (!_hasDuplicateKeys(a)) {
for (uint256 i; i != a.length; ++i) {
TimedRoleConfig memory c = a[i];
(bool isActive, uint40 start, uint40 expires) =
mockTimedRoles.timedRoleActive(c.holder, c.role);
assertEq(start, c.start);
assertEq(expires, c.expires);
assertEq(isActive, start <= targetTimestamp && targetTimestamp <= expires);
}
}
if (_randomChance(16)) {
TimedRoleConfig memory c = _sampleTimedRoleConfig();
(c.start, c.expires) = _sampleInvalidActiveTimeRange();
vm.expectRevert(TimedRoles.InvalidTimedRoleRange.selector);
mockTimedRoles.setTimedRole(c.holder, c.role, c.start, c.expires);
}
if (_randomChance(16)) {
TimedRoleConfig memory c = _sampleTimedRoleConfig();
mockTimedRoles.setOwner(_randomUniqueHashedAddress());
vm.expectRevert(TimedRoles.TimedRolesUnauthorized.selector);
mockTimedRoles.setTimedRole(c.holder, c.role, c.start, c.expires);
mockTimedRoles.setOwner(address(this));
if (_randomChance(16)) {
c.holder = address(0);
vm.expectRevert(TimedRoles.TimedRoleHolderIsZeroAddress.selector);
}
mockTimedRoles.setTimedRole(c.holder, c.role, c.start, c.expires);
}
if (_randomChance(16)) {
uint256 maxTimedRole = _random();
mockTimedRoles.setMaxTimedRole(maxTimedRole);
TimedRoleConfig memory c = _sampleTimedRoleConfig();
if (c.role > maxTimedRole) {
vm.expectRevert(TimedRoles.InvalidTimedRole.selector);
}
mockTimedRoles.setTimedRole(c.holder, c.role, c.start, c.expires);
}
}
function testTimedRolesModifiers(bytes32) public {
TimedRoleConfig memory c = _sampleTimedRoleConfig();
c.start = 0;
c.expires = 0xffffffffff;
mockTimedRoles.setTimedRole(c.holder, c.role, c.start, c.expires);
uint256[] memory allowedTimeRoles = _sampleRoles(3);
mockTimedRoles.setAllowedTimedRole(allowedTimeRoles[0]);
vm.warp(_bound(_randomUniform(), c.start, c.expires));
if (allowedTimeRoles[0] == c.role) {
vm.prank(c.holder);
mockTimedRoles.guardedByOnlyOwnerOrTimedRole();
} else {
vm.prank(c.holder);
vm.expectRevert(TimedRoles.TimedRolesUnauthorized.selector);
mockTimedRoles.guardedByOnlyOwnerOrTimedRole();
}
mockTimedRoles.setAllowedTimedRolesEncoded(abi.encodePacked(allowedTimeRoles));
if (allowedTimeRoles.contains(c.role)) {
vm.prank(c.holder);
mockTimedRoles.guardedByOnlyOwnerOrTimedRoles();
} else {
vm.prank(c.holder);
vm.expectRevert(TimedRoles.TimedRolesUnauthorized.selector);
mockTimedRoles.guardedByOnlyOwnerOrTimedRoles();
}
if (_randomChance(128)) {
mockTimedRoles.guardedByOnlyOwnerOrTimedRole();
mockTimedRoles.guardedByOnlyOwnerOrTimedRoles();
}
}
function _sampleRoles(uint256 n) internal returns (uint256[] memory roles) {
unchecked {
uint256 m = 0xf00000000000000000000000000000000000000000000000000000000000000f;
roles = DynamicArrayLib.malloc(n);
for (uint256 i; i != n; ++i) {
roles.set(i, _randomUniform() & m);
}
}
}
}