This repository has been archived by the owner on Jul 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 467
/
Copy pathreentrancy_tests.ts
122 lines (115 loc) · 4.66 KB
/
reentrancy_tests.ts
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
import { blockchainTests, constants, describe, expect } from '@0x/contracts-test-utils';
import { BigNumber, hexUtils } from '@0x/utils';
import { DataItem, MethodAbi, TupleDataItem } from 'ethereum-types';
import * as _ from 'lodash';
import { artifacts } from './artifacts';
import { ReentrancyTesterContract } from './wrappers';
import { constants as TestConstants } from './utils/constants';
blockchainTests.resets('Reentrancy Tests', env => {
const { ONE_ETHER } = constants;
// Extract all mutator public functions from the Exchange contract.
const [NON_REENTRANT_FUNCTIONS, REENTRANT_FUNCTIONS] = (() => {
const reentrantFunctions = [] as MethodAbi[];
const nonReentrantFunctions = [] as MethodAbi[];
for (const method of artifacts.Exchange.compilerOutput.abi as MethodAbi[]) {
if (
method.type === 'function' &&
!method.constant &&
!_.includes(['view', 'pure'], method.stateMutability)
) {
if (_.includes(TestConstants.REENTRANT_FUNCTIONS as string[], method.name)) {
reentrantFunctions.push(method);
} else {
nonReentrantFunctions.push(method);
}
}
}
return [_.sortBy(nonReentrantFunctions, m => m.name), _.sortBy(reentrantFunctions, m => m.name)];
})();
let testerContract: ReentrancyTesterContract;
// Generates well-constructed input data for an exchange function.
function createFunctionInputs(item: DataItem[] | DataItem): any {
if (item instanceof Array) {
return item.map(createFunctionInputs);
}
// Handle tuples.
if (item.type === 'tuple') {
const tuple = item as TupleDataItem;
return _.zipObject(tuple.components.map(c => c.name), tuple.components.map(createFunctionInputs));
}
// Handle strings.
if (item.type === 'string') {
return _.sampleSize('abcdefghijklmnopqrstuvwxyz'.split(''), 8).join('');
}
// Handle bytes.
if (item.type === 'bytes') {
return hexUtils.random(36);
}
// Handle addresses.
if (item.type === 'address') {
return hexUtils.random(constants.ADDRESS_LENGTH);
}
// Handle bools.
if (item.type === 'bool') {
return _.sample([true, false]);
}
// Handle arrays.
let m = /^(.+)(\[(\d*)\])$/.exec(item.type);
if (m) {
const length = parseInt(m[3], 10) || 1;
const subType = item.type.substr(0, item.type.length - m[2].length);
return _.times(length, () =>
createFunctionInputs({
...item,
type: subType,
}),
);
}
// Handle integers.
m = /^u?int(\d+)?$/.exec(item.type);
if (m) {
const size = parseInt(m[1], 10) || 256;
const isSigned = item.type[0] === 'i';
let n = ONE_ETHER.mod(new BigNumber(2).pow(size));
if (isSigned) {
n = n.dividedToIntegerBy(2).times(_.sample([-1, 1]) as number);
}
return n;
}
// Handle fixed size bytes.
m = /^bytes(\d+)$/.exec(item.type);
if (m) {
const size = parseInt(m[1], 10) || 32;
return hexUtils.random(size);
}
throw new Error(`Unhandled input type: ${item.type}`);
}
before(async () => {
testerContract = await ReentrancyTesterContract.deployFrom0xArtifactAsync(
artifacts.ReentrancyTester,
env.provider,
env.txDefaults,
{},
);
});
describe('non-reentrant functions', () => {
for (const fn of NON_REENTRANT_FUNCTIONS) {
it(`${fn.name}()`, async () => {
const inputs = createFunctionInputs(fn.inputs);
const callData = (testerContract as any)[fn.name](...inputs).getABIEncodedTransactionData();
const isReentrant = await testerContract.isReentrant(callData).callAsync();
expect(isReentrant).to.be.false();
});
}
});
describe('reentrant functions', () => {
for (const fn of REENTRANT_FUNCTIONS) {
it(`${fn.name}()`, async () => {
const inputs = createFunctionInputs(fn.inputs);
const callData = (testerContract as any)[fn.name](...inputs).getABIEncodedTransactionData();
const isReentrant = await testerContract.isReentrant(callData).callAsync();
expect(isReentrant).to.be.true();
});
}
});
});