-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(security): beginning security monitor testing
- Loading branch information
Showing
3 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,5 +44,3 @@ src = 'src/dollar' | |
runs = 100000 | ||
max_test_rejects = 900000 | ||
|
||
[lint] | ||
foundry-test-functions = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
packages/contracts/test/dollar/core/SecurityMonitorTest.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |