forked from OpenZeppelin/ethernaut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Force.test.js
75 lines (65 loc) · 2.03 KB
/
Force.test.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const ForceFactory = artifacts.require('./levels/ForceFactory.sol');
const ForceAttack = artifacts.require('./attacks/ForceAttack.sol');
const Force = artifacts.require('./levels/Force.sol');
const Ethernaut = artifacts.require('./Ethernaut.sol');
const {
BN,
constants,
expectEvent,
expectRevert,
} = require('openzeppelin-test-helpers');
const utils = require('../utils/TestUtils');
const { ethers, upgrades } = require('hardhat');
contract('Force', function (accounts) {
let ethernaut;
let level;
let owner = accounts[1];
let player = accounts[0];
let statproxy;
before(async function () {
ethernaut = await utils.getEthernautWithStatsProxy();
level = await ForceFactory.new();
await ethernaut.registerLevel(level.address);
});
it('should allow the player to solve the level', async function () {
const instance = await utils.createLevelInstance(
ethernaut,
level.address,
player,
Force
);
// Check init balances
let balance = await utils.getBalance(web3, instance.address);
//console.log(`init instance balance: ${balance}`)
assert.equal(balance, 0);
// Sending funds should not work
await expectRevert.unspecified(
web3.eth.sendTransaction({
from: player,
to: instance.address,
value: web3.utils.toWei('0.01', 'ether'),
})
);
balance = await utils.getBalance(web3, instance.address);
//console.log(`instance balance: ${balance}`)
assert.equal(balance, 0);
// Attack
const attacker = await ForceAttack.new({
from: player,
value: web3.utils.toWei('0.01', 'ether'),
});
await attacker.attack(instance.address);
// Check init balances
balance = await utils.getBalance(web3, instance.address);
//console.log(`post instance balance: ${balance}`)
assert.notEqual(balance, 0);
// Check win.
const ethCompleted = await utils.submitLevelInstance(
ethernaut,
level.address,
instance.address,
player
);
assert.equal(ethCompleted, true);
});
});