-
Notifications
You must be signed in to change notification settings - Fork 706
Expand file tree
/
Copy pathReentrancyGuard.t.sol
More file actions
56 lines (39 loc) · 1.28 KB
/
ReentrancyGuard.t.sol
File metadata and controls
56 lines (39 loc) · 1.28 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
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.15;
import {DSTestPlus} from "./utils/DSTestPlus.sol";
import {ReentrancyGuard} from "../utils/ReentrancyGuard.sol";
contract RiskyContract is ReentrancyGuard {
uint256 public enterTimes;
function unprotectedCall() public {
enterTimes++;
if (enterTimes > 1) return;
this.protectedCall();
}
function protectedCall() public nonReentrant {
enterTimes++;
if (enterTimes > 1) return;
this.protectedCall();
}
function overprotectedCall() public nonReentrant {}
}
contract ReentrancyGuardTest is DSTestPlus {
RiskyContract riskyContract;
function setUp() public virtual {
riskyContract = new RiskyContract();
}
function invariantReentrancyStatusAlways1() public {
assertEq(uint256(hevm.load(address(riskyContract), 0)), 1);
}
function testFailUnprotectedCall() public {
riskyContract.unprotectedCall();
assertEq(riskyContract.enterTimes(), 1);
}
function testProtectedCall() public {
try riskyContract.protectedCall() {
fail("Reentrancy Guard Failed To Stop Attacker");
} catch {}
}
function testNoReentrancy() public {
riskyContract.overprotectedCall();
}
}