Skip to content

Commit

Permalink
refactor: reorganize helper test contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
amusingaxl committed May 14, 2024
1 parent b8601a6 commit cd7db94
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 34 deletions.
47 changes: 14 additions & 33 deletions src/Protego.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ pragma solidity ^0.8.16;

import {DssTest} from "dss-test/DssTest.sol";
import {Protego, DropSpell, DsSpellLike} from "../src/Protego.sol";
import {DssEndTestSpell} from "./test/TestSpell.sol";
import {ConformingSpell} from "./test/ConformingSpell.sol";
import {NonConformingSpell} from "./test/NonConformingSpell.sol";

interface ChainlogLike {
function getAddress(bytes32) external view returns (address);
Expand Down Expand Up @@ -54,50 +55,30 @@ struct BadSpells {
uint256 eta;
}

contract MaliciousSpell {
DsPauseLike public immutable pause;

constructor(address pause_) {
pause = DsPauseLike(pause_);
}

function plot(address usr, bytes32 tag, bytes memory fax, uint256 eta) public {
pause.plot(usr, tag, fax, eta);
}

function drop(address usr, bytes32 tag, bytes memory fax, uint256 eta) public {
pause.drop(usr, tag, fax, eta);
}

function exec(address usr, bytes32 tag, bytes memory fax, uint256 eta) public returns (bytes memory) {
return pause.exec(usr, tag, fax, eta);
}
}

contract ProtegoTest is DssTest {
DsPauseLike pause;
DsChiefLike chief;
GemLike gov;
Protego protego;
address pause;
address end;

function setUp() public {
vm.createSelectFork("mainnet");

ChainlogLike chainlog = ChainlogLike(0xdA0Ab1e0017DEbCd72Be8599041a2aa3bA7e740F);
pause = DsPauseLike(chainlog.getAddress("MCD_PAUSE"));
chief = DsChiefLike(chainlog.getAddress("MCD_ADM"));
gov = GemLike(chief.GOV());
protego = new Protego(address(pause));
pause = chainlog.getAddress("MCD_PAUSE");
protego = new Protego(pause);
end = chainlog.getAddress("MCD_END");
}

function testPause() public view {
assertEq(protego.pause(), address(pause));
assertEq(protego.pause(), pause);
}

function testDeploySpell() public {
DssEndTestSpell badSpell = new DssEndTestSpell(address(pause), end);
ConformingSpell badSpell = new ConformingSpell(pause, end);

assertFalse(protego.planned(DsSpellLike(address(badSpell))));

Expand All @@ -114,7 +95,7 @@ contract ProtegoTest is DssTest {
}

function testDeploySpellParams() public {
DssEndTestSpell badSpell = new DssEndTestSpell(address(pause), end);
ConformingSpell badSpell = new ConformingSpell(pause, end);

assertFalse(protego.planned(DsSpellLike(address(badSpell))));

Expand All @@ -131,7 +112,7 @@ contract ProtegoTest is DssTest {
}

function testPlanned() public {
DssEndTestSpell badSpell = new DssEndTestSpell(address(pause), end);
ConformingSpell badSpell = new ConformingSpell(pause, end);

assertFalse(protego.planned(DsSpellLike(address(badSpell))), "Spell already planned");

Expand All @@ -149,7 +130,7 @@ contract ProtegoTest is DssTest {
}

function testId() public {
DssEndTestSpell badSpell = new DssEndTestSpell(address(pause), end);
ConformingSpell badSpell = new ConformingSpell(pause, end);

address usr = badSpell.action();
bytes32 tag = badSpell.tag();
Expand All @@ -164,7 +145,7 @@ contract ProtegoTest is DssTest {

// Test drop of conformant spell
function testDropSpell() public {
DssEndTestSpell badSpell = new DssEndTestSpell(address(pause), end);
ConformingSpell badSpell = new ConformingSpell(pause, end);

assertFalse(protego.planned(DsSpellLike(address(badSpell))), "Spell already planned");

Expand Down Expand Up @@ -193,7 +174,7 @@ contract ProtegoTest is DssTest {
bytes memory sig = abi.encodeWithSignature("destroy(bool)", true);
uint256 eta = block.timestamp + 1000 days;

MaliciousSpell badSpell = new MaliciousSpell(address(pause));
NonConformingSpell badSpell = new NonConformingSpell(pause);

assertFalse(protego.planned(usr, tag, sig, eta), "Spell already planned");

Expand All @@ -215,7 +196,7 @@ contract ProtegoTest is DssTest {
BadSpells[] memory badSpells = new BadSpells[](iter);

for (uint256 i = 0; i < iter; i++) {
DssEndTestSpell badSpell = new DssEndTestSpell(address(pause), end);
ConformingSpell badSpell = new ConformingSpell(pause, end);
_vote(address(badSpell));
badSpell.schedule();

Expand All @@ -241,7 +222,7 @@ contract ProtegoTest is DssTest {
BadSpells[] memory badSpells = new BadSpells[](iter);

for (uint256 i = 0; i < iter; i++) {
DssEndTestSpell badSpell = new DssEndTestSpell(address(pause), end);
ConformingSpell badSpell = new ConformingSpell(pause, end);
_vote(address(badSpell));
badSpell.schedule();

Expand Down
2 changes: 1 addition & 1 deletion src/test/TestSpell.sol → src/test/ConformingSpell.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ contract SpellAction {
}
}

contract DssEndTestSpell {
contract ConformingSpell {
DsPauseLike public pause;
address public action;
bytes32 public tag;
Expand Down
44 changes: 44 additions & 0 deletions src/test/NonConformingSpell.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-FileCopyrightText: © 2024 Dai Foundation <www.daifoundation.org>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity ^0.8.16;

interface DsPauseLike {
function plot(address, bytes32, bytes calldata, uint256) external;
function drop(address, bytes32, bytes calldata, uint256) external;
function exec(address, bytes32, bytes calldata, uint256) external returns (bytes memory);
}

contract NonConformingSpell {
DsPauseLike public immutable pause;

constructor(address pause_) {
pause = DsPauseLike(pause_);
}

function plot(address usr, bytes32 tag, bytes memory fax, uint256 eta) public {
pause.plot(usr, tag, fax, eta);
}

function drop(address usr, bytes32 tag, bytes memory fax, uint256 eta) public {
pause.drop(usr, tag, fax, eta);
}

function exec(address usr, bytes32 tag, bytes memory fax, uint256 eta) public returns (bytes memory) {
return pause.exec(usr, tag, fax, eta);
}
}


0 comments on commit cd7db94

Please sign in to comment.