Skip to content

Commit

Permalink
feat(security): beginning security monitor testing
Browse files Browse the repository at this point in the history
  • Loading branch information
LurkyLunk committed Jul 31, 2024
1 parent 33c7f6b commit 888ae1f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
2 changes: 0 additions & 2 deletions packages/contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,3 @@ src = 'src/dollar'
runs = 100000
max_test_rejects = 900000

[lint]
foundry-test-functions = false
2 changes: 1 addition & 1 deletion packages/contracts/src/dollar/utils/SecurityMonitor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity 0.8.19;

import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@chainlink/contracts/src/v0.8/AutomationCompatible.sol";
import "lib/chainlink-brownie-contracts/contracts/src/v0.8/vrf/AutomationCompatible.sol";

contract SecurityMonitor is AccessControl, AutomationCompatibleInterface {
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
Expand Down
75 changes: 75 additions & 0 deletions packages/contracts/test/dollar/core/SecurityMonitorTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
pragma solidity 0.8.19;

import "forge-std/Test.sol";
import "../../../src/dollar/utils/SecurityMonitor.sol";

contract SecurityMonitorTest is Test {
SecurityMonitor securityMonitor;
event SecurityIncident(string message);

address admin = address(this);

function setUp() public {
// Deploy the SecurityMonitor contract with this contract as the admin
securityMonitor = new SecurityMonitor(admin, 30);

// Ensure this contract has the DEFAULT_ADMIN_ROLE
assertTrue(
securityMonitor.hasRole(securityMonitor.DEFAULT_ADMIN_ROLE(), admin)
);

// Grant PAUSER_ROLE to this contract
vm.prank(admin);
securityMonitor.grantRole(securityMonitor.PAUSER_ROLE(), admin);

// Ensure this contract has the PAUSER_ROLE
assertTrue(
securityMonitor.hasRole(securityMonitor.PAUSER_ROLE(), admin)
);
}

function testPauseAllContracts() public {
vm.expectEmit(true, true, true, true);
emit SecurityIncident(
"All contracts paused due to a security incident."
);
vm.prank(admin);
securityMonitor.pauseAllContracts();
assertEq(securityMonitor.lastCheckBlock(), block.number);
}

function testNotifyTeam() public {
string memory incidentMessage = "Test incident";
vm.expectEmit(true, true, true, true);
emit SecurityIncident(incidentMessage);
vm.prank(admin);
securityMonitor.notifyTeam(incidentMessage);
}

function testCheckUpkeep() public {
(bool upkeepNeeded, ) = securityMonitor.checkUpkeep("");
assertFalse(upkeepNeeded);
}

function testPerformUpkeep() public {
// First, check if upkeep is needed
(bool upkeepNeeded, ) = securityMonitor.checkUpkeep("");

if (upkeepNeeded) {
// If upkeep is needed, expect the security incident event
vm.expectEmit(true, true, true, true);
emit SecurityIncident(
"Security incident detected: Liquidity threshold breached. Contracts paused."
);

securityMonitor.performUpkeep("");

assertEq(securityMonitor.lastCheckBlock(), block.number);
} else {
// If upkeep is not needed, we should not expect any events
// Just perform the upkeep and check that lastCheckBlock is updated
securityMonitor.performUpkeep("");
assertEq(securityMonitor.lastCheckBlock(), block.number);
}
}
}

0 comments on commit 888ae1f

Please sign in to comment.