-
Notifications
You must be signed in to change notification settings - Fork 492
Expand file tree
/
Copy pathLibBytes.t.sol
More file actions
484 lines (431 loc) · 17 KB
/
Copy pathLibBytes.t.sol
File metadata and controls
484 lines (431 loc) · 17 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "./utils/SoladyTest.sol";
import {LibBytes} from "../src/utils/LibBytes.sol";
contract BytesStore {
LibBytes.BytesStorage internal value;
function set(bytes calldata newValue) external {
LibBytes.setCalldata(value, newValue);
}
function length() external view returns (uint256) {
return LibBytes.length(value);
}
function at_(uint256 index) external view returns (uint8) {
return LibBytes.uint8At(value, index);
}
}
contract LibBytesTest is SoladyTest {
function testLoad(bytes memory a) public {
if (a.length < 32) a = abi.encodePacked(a, new bytes(32));
uint256 o = _bound(_random(), 0, a.length - 32);
bytes memory expected = LibBytes.slice(a, o, o + 32);
assertEq(abi.encode(LibBytes.load(a, o)), expected);
this._testLoadCalldata(a);
}
function _testLoadCalldata(bytes calldata a) public {
uint256 o = _bound(_random(), 0, a.length - 32);
bytes memory expected = LibBytes.slice(a, o, o + 32);
assertEq(abi.encode(LibBytes.loadCalldata(a, o)), expected);
}
function testTruncate(bytes memory a, uint256 n) public {
bytes memory sliced = LibBytes.slice(a, 0, n);
bytes memory truncated = LibBytes.truncate(a, n);
assertEq(truncated, sliced);
assertEq(a, sliced);
}
function testTruncatedCalldata(bytes calldata a, uint256 n) public {
bytes memory sliced = LibBytes.slice(a, 0, n);
bytes memory truncated = LibBytes.truncatedCalldata(a, n);
assertEq(truncated, sliced);
}
function testSliceCalldata(bytes calldata a, uint256 start, uint256 end) public {
bytes memory aCopy = a;
assertEq(LibBytes.sliceCalldata(a, start, end), LibBytes.slice(aCopy, start, end));
assertEq(LibBytes.sliceCalldata(a, start), LibBytes.slice(aCopy, start));
}
function testSliceCalldata() public {
bytes memory data = hex"12f712c77281c66267d947165237893ba5eca3e5481727fe76d4511ce1b564f5";
this.testSliceCalldata(data, 1, 11);
}
function testEmptyCalldata() public {
assertEq(LibBytes.emptyCalldata(), "");
}
function testDirectReturn() public {
uint256 seed = 123;
bytes[] memory expected = _generateBytesArray(seed);
bytes[] memory computed = this.generateBytesArray(seed, false);
unchecked {
for (uint256 i; i != expected.length; ++i) {
_checkMemory(computed[i]);
assertEq(computed[i], expected[i]);
}
assertEq(computed.length, expected.length);
}
}
function testDirectReturn(uint256 seed) public {
bytes[] memory expected = _generateBytesArray(seed);
(bool success, bytes memory encoded) = address(this)
.call(abi.encodeWithSignature("generateBytesArray(uint256,bool)", seed, true));
assertTrue(success);
bytes[] memory computed;
/// @solidity memory-safe-assembly
assembly {
let o := add(encoded, 0x20)
computed := add(o, mload(o))
for { let i := 0 } lt(i, mload(computed)) { i := add(i, 1) } {
let c := add(add(0x20, computed), shl(5, i))
mstore(c, add(add(0x20, computed), mload(c)))
}
}
unchecked {
for (uint256 i; i != expected.length; ++i) {
_checkMemory(computed[i]);
assertEq(computed[i], expected[i]);
}
assertEq(computed.length, expected.length);
}
if (seed & 0xf == 0) {
assertEq(abi.encode(expected), abi.encode(this.generateBytesArray(seed, true)));
}
}
function generateBytesArray(uint256 seed, bool brutalized)
public
view
returns (bytes[] memory)
{
if (brutalized) {
_misalignFreeMemoryPointer();
_brutalizeMemory();
}
LibBytes.directReturn(_generateBytesArray(seed));
}
function _generateBytesArray(uint256 seed) internal pure returns (bytes[] memory a) {
bytes memory before = "hehe";
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, seed)
mstore(0x20, 0)
function _next() -> _r {
_r := keccak256(0x00, 0x40)
mstore(0x20, _r)
}
function _nextBytes() -> _b {
_b := mload(0x40)
let n_ := and(_next(), 0x7f)
mstore(_b, n_)
for { let i_ := 0 } lt(i_, n_) { i_ := add(i_, 0x20) } {
mstore(add(add(_b, 0x20), i_), _next())
}
if and(1, _next()) {
mstore(0x40, add(n_, add(_b, 0x20)))
leave
}
mstore(add(n_, add(_b, 0x20)), 0)
mstore(0x40, add(n_, add(_b, 0x40)))
}
let n := and(_next(), 7)
a := mload(0x40)
mstore(a, n)
mstore(0x40, add(add(a, 0x20), shl(5, n)))
for { let i := 0 } lt(i, n) { i := add(1, i) } {
if iszero(and(7, _next())) {
mstore(add(add(a, 0x20), shl(5, i)), before)
continue
}
mstore(add(add(a, 0x20), shl(5, i)), _nextBytes())
}
}
}
function testCmp() public {
assertEq(LibBytes.cmp("", ""), 0);
assertEq(LibBytes.cmp("abc", "abc"), 0);
assertEq(LibBytes.cmp("abcd", "abc"), 1);
assertEq(LibBytes.cmp("abb", "abc"), -1);
assertEq(
LibBytes.cmp(
"0123456789012345678901234567890123456789abb",
"0123456789012345678901234567890123456789abc"
),
-1
);
}
function testCmpDifferential(bytes memory a, bytes memory b) public {
if (_randomChance(32)) {
_misalignFreeMemoryPointer();
_brutalizeMemory();
}
if (_randomChance(256)) {
a = b;
}
if (_randomChance(16)) {
a = abi.encodePacked(a, b);
}
if (_randomChance(16)) {
b = abi.encodePacked(b, a);
}
bytes32 aHash = keccak256(a);
bytes32 bHash = keccak256(b);
if (_randomChance(8)) {
a = _brutalizeRightPadding(a);
}
if (_randomChance(8)) {
b = _brutalizeRightPadding(b);
}
int256 computed = LibBytes.cmp(a, b);
int256 expected = cmpOriginal(a, b);
assertEq(computed, expected);
assertEq(keccak256(a), aHash);
assertEq(keccak256(b), bHash);
}
struct SampleDynamicStruct {
address target;
uint256 value;
bytes data;
}
struct SampleStaticSubStruct {
uint256 x;
uint256 y;
}
struct SampleStaticStruct {
SampleStaticSubStruct a;
SampleStaticSubStruct b;
}
function testStaticStructInCalldata() public {
SampleStaticStruct memory s;
s.a.x = 1;
s.a.y = 2;
s.b.x = 3;
s.b.y = 4;
SampleDynamicStruct memory u;
u.target = address(0xaaa);
u.value = 123;
u.data = "hehe";
this._testStaticStructInCalldata(abi.encode(s, u), 0x20 * 0, s);
this._testStaticStructInCalldata(abi.encode(u, s, u), 0x20 * 1, s);
this._testStaticStructInCalldata(abi.encode(u, u, s, u, s), 0x20 * 2, s);
this._testStaticStructInCalldata(abi.encode(u, u, s, u, s), 0x20 * 2 + 0x20 * 4 + 0x20, s);
}
function _testStaticStructInCalldata(
bytes calldata encoded,
uint256 offset,
SampleStaticStruct memory expected
) public {
bytes calldata p = LibBytes.staticStructInCalldata(encoded, offset);
assertEq(uint256(LibBytes.loadCalldata(p, 0x00)), expected.a.x);
assertEq(uint256(LibBytes.loadCalldata(p, 0x20)), expected.a.y);
assertEq(uint256(LibBytes.loadCalldata(p, 0x40)), expected.b.x);
assertEq(uint256(LibBytes.loadCalldata(p, 0x60)), expected.b.y);
}
function testDynamicStructInCalldata() public {
SampleDynamicStruct memory u;
u.target = address(1);
u.value = 123;
u.data = "hehe";
bytes memory encoded = abi.encode(u);
this._testDynamicStructInCalldata(encoded, 0x00, u);
}
function testDynamicStructInCalldata2() public {
SampleDynamicStruct memory u;
u.target = address(1);
u.value = 123;
u.data = "hehe";
SampleStaticStruct memory s;
s.a.x = _random();
s.a.y = _random();
s.b.x = _random();
s.b.y = _random();
this._testDynamicStructInCalldata(abi.encode(s, u), 0x80, u);
this._testDynamicStructInCalldata(abi.encode(s, u, s), 0x80, u);
this._testDynamicStructInCalldata(abi.encode(s, s, u), 0x80 * 2, u);
}
function testDynamicStructInCalldata(bytes32) public {
SampleDynamicStruct memory u;
u.target = _randomHashedAddress();
u.value = _randomUniform();
u.data = _truncateBytes(_randomBytes(), 100);
bytes memory encoded;
encoded = abi.encode(u);
this._testDynamicStructInCalldata(encoded, 0x00, u);
encoded = abi.encode(uint256(1), u);
this._testDynamicStructInCalldata(encoded, 0x20, u);
encoded = abi.encode(uint256(1), uint256(2), u);
if (_randomChance(32)) encoded = abi.encodePacked(encoded, _randomBytes());
this._testDynamicStructInCalldata(encoded, 0x40, u);
}
function _testDynamicStructInCalldata(
bytes calldata encoded,
uint256 offset,
SampleDynamicStruct memory expected
) public {
bytes calldata p = LibBytes.dynamicStructInCalldata(encoded, offset);
assertEq(uint256(LibBytes.loadCalldata(p, 0x00)), uint160(expected.target));
assertEq(uint256(LibBytes.loadCalldata(p, 0x20)), expected.value);
assertEq(LibBytes.bytesInCalldata(p, 0x40), expected.data);
}
function testBytesInCalldata() public {
this._testBytesInCalldata(abi.encode("hello"), 0x00, "hello");
}
function testBytesInCalldata(bytes32) public {
bytes memory u = _truncateBytes(_randomBytes(), 100);
this._testBytesInCalldata(abi.encode(u), 0x00, u);
this._testBytesInCalldata(abi.encode(uint256(1), u), 0x20, u);
if (_randomChance(16)) {
bytes memory encoded = abi.encode(uint256(1), uint256(2), u);
if (_randomChance(32)) encoded = abi.encodePacked(encoded, _randomBytes());
this._testBytesInCalldata(encoded, 0x40, u);
}
}
function _testBytesInCalldata(bytes calldata encoded, uint256 offset, bytes memory expected)
public
{
assertEq(LibBytes.bytesInCalldata(encoded, offset), expected);
}
function _brutalizeRightPadding(bytes memory s) internal returns (bytes memory result) {
uint256 n = s.length;
result = abi.encodePacked(s, _randomUniform(), _randomUniform());
/// @solidity memory-safe-assembly
assembly {
mstore(result, n)
}
}
function cmpOriginal(bytes memory a, bytes memory b) internal pure returns (int256) {
uint256 minLen = a.length < b.length ? a.length : b.length;
for (uint256 i; i < minLen; ++i) {
uint8 x = uint8(a[i]);
uint8 y = uint8(b[i]);
if (x < y) return -1;
if (x > y) return 1;
}
if (a.length < b.length) return -1;
if (a.length > b.length) return 1;
return 0;
}
function testIndexOfByteDifferential(bytes memory subject, bytes1 needle, uint256 from) public {
if (_randomChance(2)) _brutalizeMemory();
if (_randomChance(2)) _misalignFreeMemoryPointer();
if (_randomChance(2)) {
bytes memory empty;
subject = empty;
}
from = _bound(from, 0, subject.length * 2);
uint256 computed = LibBytes.indexOfByte(subject, needle, from);
uint256 expected = _indexOfByteOriginal(subject, needle, from);
assertEq(computed, expected);
}
function testIndexOfByte() public {
bytes memory subject = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
assertEq(LibBytes.indexOfByte("", "a"), LibBytes.NOT_FOUND);
assertEq(LibBytes.indexOfByte("", "a", 1), LibBytes.NOT_FOUND);
assertEq(LibBytes.indexOfByte(subject, "a"), 0);
assertEq(LibBytes.indexOfByte(subject, "a", 1), LibBytes.NOT_FOUND);
assertEq(LibBytes.indexOfByte(subject, "b"), 1);
assertEq(LibBytes.indexOfByte(subject, "X"), 49);
assertEq(LibBytes.indexOfByte(subject, "q"), 16);
assertEq(LibBytes.indexOfByte(subject, "q", 16), 16);
assertEq(LibBytes.indexOfByte(subject, "q", 17), LibBytes.NOT_FOUND);
assertEq(LibBytes.indexOfByte(subject, "q", 17), LibBytes.NOT_FOUND);
assertEq(LibBytes.indexOfByte("abcabcabc", "a", 0), 0);
assertEq(LibBytes.indexOfByte("abcabcabc", "a", 1), 3);
}
function _indexOfByteOriginal(bytes memory subject, bytes1 needle, uint256 from)
internal
pure
returns (uint256)
{
unchecked {
for (uint256 i; i < subject.length; ++i) {
if (i >= from) {
if (subject[i] == needle) return i;
}
}
return type(uint256).max;
}
}
function testBytes32ToAddress(bytes32 x) public {
uint256 msb = uint256(x) >> 96;
uint256 lsb = (uint256(x) << 96) >> 96;
assertEq(uint160(LibBytes.msbToAddress(x)), msb);
assertEq(uint160(LibBytes.lsbToAddress(x)), lsb);
}
function testCheckInCalldata(bytes memory child) public view {
this.checkInCalldata(child, abi.encode(child));
}
function testCheckInCalldata() public pure {
LibBytes.checkInCalldata(msg.data, msg.data);
}
function checkInCalldata(bytes calldata expectedChild, bytes calldata encoded) public pure {
bytes calldata child;
/// @solidity memory-safe-assembly
assembly {
child.offset := add(0x20, add(encoded.offset, calldataload(encoded.offset)))
child.length := calldataload(add(encoded.offset, calldataload(encoded.offset)))
}
LibBytes.checkInCalldata(child, encoded);
LibBytes.checkInCalldata(child, msg.data);
LibBytes.checkInCalldata(encoded, msg.data);
require(keccak256(expectedChild) == keccak256(child));
}
function testCheckInCalldata(bytes[] memory children) public view {
this.checkInCalldata(children, abi.encode(children));
}
function checkInCalldata(bytes[] calldata expectedChildren, bytes calldata encoded)
public
pure
{
bytes[] calldata children;
/// @solidity memory-safe-assembly
assembly {
children.offset := add(0x20, add(encoded.offset, calldataload(encoded.offset)))
children.length := calldataload(add(encoded.offset, calldataload(encoded.offset)))
}
LibBytes.checkInCalldata(children, encoded);
LibBytes.checkInCalldata(expectedChildren, msg.data);
LibBytes.checkInCalldata(children, msg.data);
require(expectedChildren.length == children.length);
for (uint256 i; i < children.length; ++i) {
require(keccak256(expectedChildren[i]) == keccak256(children[i]));
}
}
function testUint8AtStaleByteAfterShrink() public {
BytesStore store = new BytesStore();
bytes memory previous = new bytes(32);
previous[31] = 0xbb;
store.set(previous);
store.set(new bytes(31));
assertEq(store.length(), 31);
assertEq(store.at_(31), 0);
}
function testUint8AtPaddingAfterShortValue() public {
BytesStore store = _storeWithDirtyPadding(5);
assertEq(store.length(), 5);
assertEq(store.at_(4), 0x11);
assertEq(store.at_(5), 0);
assertEq(store.at_(30), 0);
}
function testUint8AtPaddingAfterSpilledValue() public {
BytesStore store = _storeWithDirtyPadding(40);
assertEq(store.length(), 40);
assertEq(store.at_(39), 0x11);
assertEq(store.at_(40), 0);
}
function testUint8AtPaddingAfterTaggedLongValue() public {
BytesStore store = _storeWithDirtyPadding(255);
assertEq(store.length(), 255);
assertEq(store.at_(254), 0x11);
assertEq(store.at_(255), 0);
assertEq(store.at_(256), 0);
}
function _storeWithDirtyPadding(uint256 n) internal returns (BytesStore store) {
store = new BytesStore();
bytes memory value = new bytes(n);
for (uint256 i; i < n; ++i) {
value[i] = 0x11;
}
bytes memory data = abi.encodeWithSelector(BytesStore.set.selector, value);
for (uint256 i = 4 + 32 + 32 + n; i < data.length; ++i) {
data[i] = 0xaa;
}
(bool success,) = address(store).call(data);
require(success);
}
}