Skip to content

Commit

Permalink
Merge pull request #6 from zkemail/feat/add-testcases
Browse files Browse the repository at this point in the history
Add test cases for EnumerableGuardianMap.
  • Loading branch information
JohnGuilding authored Jun 22, 2024
2 parents 66adaeb + 122e194 commit 9d5b06a
Show file tree
Hide file tree
Showing 12 changed files with 477 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/libraries/EnumerableGuardianMap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ library EnumerableGuardianMap {
returns (bool)
{
uint256 length = map._keys.length();
if (length > MAX_NUMBER_OF_GUARDIANS) {
if (length >= MAX_NUMBER_OF_GUARDIANS) {
revert MaxNumberOfGuardiansReached();
}
map._values[key] = value;
Expand Down
11 changes: 9 additions & 2 deletions test/unit/EmailRecoveryFactory/deployModuleAndManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ pragma solidity ^0.8.25;

import { console2 } from "forge-std/console2.sol";
import { UnitBase } from "../UnitBase.t.sol";
import { EmailRecoveryFactory } from "../../../src/EmailRecoveryFactory.sol";

contract EmailRecoveryManager_deployModuleAndManager_Test is UnitBase {
contract EmailRecoveryFactory_deployModuleAndManager_Test is UnitBase {
function setUp() public override {
super.setUp();
emailRecoveryFactory = new EmailRecoveryFactory();
}

function test_DeployModuleAndManager_Succeeds() public {
// TODO: test
emailRecoveryFactory.deployModuleAndManager(
address(verifier),
address(dkimRegistry),
address(emailAuthImpl),
address(emailRecoveryHandler)
);
}
}
23 changes: 18 additions & 5 deletions test/unit/EmailRecoveryManager/getGuardian.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@
pragma solidity ^0.8.25;

import { console2 } from "forge-std/console2.sol";
import { IEmailRecoveryManager } from "src/interfaces/IEmailRecoveryManager.sol";
import { EmailRecoveryModule } from "src/modules/EmailRecoveryModule.sol";
import { GuardianStorage, GuardianStatus } from "src/libraries/EnumerableGuardianMap.sol";
import { UnitBase } from "../UnitBase.t.sol";
import {IEmailRecoveryManager} from "src/interfaces/IEmailRecoveryManager.sol";
import {EmailRecoveryModule} from "src/modules/EmailRecoveryModule.sol";
import {GuardianStorage, GuardianStatus} from "src/libraries/EnumerableGuardianMap.sol";
import {UnitBase} from "../UnitBase.t.sol";

contract EmailRecoveryManager_getGuardian_Test is UnitBase {
address newGuardian = address(1);
uint256 newGuardianWeight = 1;

function setUp() public override {
super.setUp();

vm.startPrank(accountAddress);
emailRecoveryManager.addGuardian(newGuardian, newGuardianWeight);
vm.stopPrank();
}

function test_GetGuardian_Succeeds() public {
// TODO: test
GuardianStorage memory guardianStorage = emailRecoveryManager
.getGuardian(accountAddress, newGuardian);
assertEq(
uint256(guardianStorage.status),
uint256(GuardianStatus.REQUESTED)
);
assertEq(guardianStorage.weight, newGuardianWeight);
}
}
24 changes: 23 additions & 1 deletion test/unit/EmailRecoveryManager/getGuardianConfig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,33 @@ import { GuardianStorage, GuardianStatus } from "src/libraries/EnumerableGuardia
import { UnitBase } from "../UnitBase.t.sol";

contract EmailRecoveryManager_getGuardianConfig_Test is UnitBase {
address newGuardian = address(1);
uint256 newGuardianWeight = 1;

uint256 expectedGuardianCount;
uint256 expectedTotalWeight;
uint256 expectedThreshold;

function setUp() public override {
super.setUp();

expectedGuardianCount = guardians.length + 1;
expectedTotalWeight = totalWeight + newGuardianWeight;
expectedThreshold = threshold;

vm.startPrank(accountAddress);
emailRecoveryManager.addGuardian(newGuardian, newGuardianWeight);
vm.stopPrank();
}

function test_GetGuardianConfig_Succeeds() public {
// TODO: test
IEmailRecoveryManager.GuardianConfig
memory guardianConfig = emailRecoveryManager.getGuardianConfig(
accountAddress
);
console2.log(expectedGuardianCount);
assertEq(guardianConfig.guardianCount, expectedGuardianCount);
assertEq(guardianConfig.totalWeight, expectedTotalWeight);
assertEq(guardianConfig.threshold, expectedThreshold);
}
}
23 changes: 22 additions & 1 deletion test/unit/EmailRecoveryManager/getRecoveryConfig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,32 @@ import { GuardianStorage, GuardianStatus } from "src/libraries/EnumerableGuardia
import { UnitBase } from "../UnitBase.t.sol";

contract EmailRecoveryManager_getRecoveryConfig_Test is UnitBase {
uint256 newDelay = 1 days;
uint256 newExpiry = 4 weeks;

function setUp() public override {
super.setUp();

IEmailRecoveryManager.RecoveryConfig
memory recoveryConfig = IEmailRecoveryManager.RecoveryConfig(
newDelay,
newExpiry
);

vm.startPrank(accountAddress);
emailRecoveryManager.updateRecoveryConfig(recoveryConfig);

recoveryConfig = emailRecoveryManager.getRecoveryConfig(accountAddress);
assertEq(recoveryConfig.delay, newDelay);
assertEq(recoveryConfig.expiry, newExpiry);
}

function test_GetRecoveryConfig_Succeeds() public {
// TODO: test
IEmailRecoveryManager.RecoveryConfig
memory result = emailRecoveryManager.getRecoveryConfig(
accountAddress
);
assertEq(result.delay, newDelay);
assertEq(result.expiry, newExpiry);
}
}
13 changes: 12 additions & 1 deletion test/unit/EmailRecoveryManager/getRecoveryRequest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ contract EmailRecoveryManager_getRecoveryRequest_Test is UnitBase {
}

function test_GetRecoveryRequest_Succeeds() public {
// TODO: test
acceptGuardian(accountSalt1);
vm.warp(12 seconds);
handleRecovery(recoveryModuleAddress, calldataHash, accountSalt1);

IEmailRecoveryManager.RecoveryRequest
memory recoveryRequest = emailRecoveryManager.getRecoveryRequest(
accountAddress
);
assertEq(recoveryRequest.executeAfter, 0);
assertEq(recoveryRequest.executeBefore, 0);
assertEq(recoveryRequest.currentWeight, 1);
assertEq(recoveryRequest.calldataHash, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ contract SafeRecoverySubjectHandler_getPreviousOwnerInLinkedList_Test is SafeUni
}

function test_GetPreviousOwnerInLinkedList_SucceedsWithMultipleAccounts() public {
// TODO: test
address expectedPreviousOwner = address(1);
address previousOwner =
safeRecoverySubjectHandler.exposed_getPreviousOwnerInLinkedList(accountAddress1, owner1);

assertEq(expectedPreviousOwner, previousOwner);
previousOwner =
safeRecoverySubjectHandler.exposed_getPreviousOwnerInLinkedList(accountAddress1, owner2);
assertEq(expectedPreviousOwner, previousOwner);
}
}
31 changes: 28 additions & 3 deletions test/unit/libraries/EnumerableGuardianMap/get.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,42 @@
pragma solidity ^0.8.25;

import { console2 } from "forge-std/console2.sol";
import { UnitBase } from "../../UnitBase.t.sol";
import {UnitBase} from "../../UnitBase.t.sol";
import {EnumerableGuardianMap, GuardianStorage, GuardianStatus} from "../../../../src/libraries/EnumerableGuardianMap.sol";

contract EnumerableGuardianMap_get_Test is UnitBase {
using EnumerableGuardianMap for EnumerableGuardianMap.AddressToGuardianMap;
mapping(address account => EnumerableGuardianMap.AddressToGuardianMap guardian)
internal guardiansStorage;

function setUp() public override {
super.setUp();
guardiansStorage[accountAddress].set({
key: guardian1,
value: GuardianStorage(GuardianStatus.REQUESTED, guardianWeights[0])
});
}

function test_Get_GetsExistingValue() public view {
// TODO: test
GuardianStorage memory result = guardiansStorage[accountAddress].get(
guardian1
);
require(
result.status == GuardianStatus.REQUESTED,
"Expected status to be REQUESTED"
);
require(result.weight == guardianWeights[0], "Expected weight to be 1");
}

function test_Get_GetsNonExistentValue() public view {
// TODO: test
// It will returns the default value
GuardianStorage memory result = guardiansStorage[accountAddress].get(
guardian2
);
require(
result.status == GuardianStatus.NONE,
"Expected status to be NONE"
);
require(result.weight == 0, "Expected weight to be 0");
}
}
63 changes: 54 additions & 9 deletions test/unit/libraries/EnumerableGuardianMap/keys.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,68 @@
pragma solidity ^0.8.25;

import { console2 } from "forge-std/console2.sol";
import { UnitBase } from "../../UnitBase.t.sol";
import {UnitBase} from "../../UnitBase.t.sol";
import {EnumerableGuardianMap, GuardianStorage, GuardianStatus} from "../../../../src/libraries/EnumerableGuardianMap.sol";

contract EnumerableGuardianMap_keys_Test is UnitBase {
using EnumerableGuardianMap for EnumerableGuardianMap.AddressToGuardianMap;
mapping(address account => EnumerableGuardianMap.AddressToGuardianMap guardian)
internal guardiansStorage;

function setUp() public override {
super.setUp();
}

function test_Keys_StartsEmpty() public view {
// TODO: test
}
function test_Keys_ReturnsEmptyArrayOfKeys() public view {
// TODO: test
address[] memory keys = guardiansStorage[accountAddress].keys();
assertEq(keys.length, 0);
}
function test_Keys_ReturnsArrayOfKeys() public view {
// TODO: test

function test_Keys_ReturnsArrayOfKeys() public {
bool result;

for (uint256 i = 1; i <= 3; i++) {
result = guardiansStorage[accountAddress].set({
key: vm.addr(i),
value: GuardianStorage(
GuardianStatus.REQUESTED,
guardianWeights[1]
)
});
assertEq(result, true);
}
address[] memory keys = guardiansStorage[accountAddress].keys();
assertEq(keys.length, 3);
for (uint256 i = 0; i < 3; i++) {
assertEq(keys[i], vm.addr(i + 1));
}
}
function test_Keys_ReturnMaxArrayOfKeys() public view {
// TODO: test

function test_Keys_ReturnMaxArrayOfKeys() public {
bool result;

for (
uint256 i = 1;
i <= EnumerableGuardianMap.MAX_NUMBER_OF_GUARDIANS;
i++
) {
result = guardiansStorage[accountAddress].set({
key: vm.addr(i),
value: GuardianStorage(
GuardianStatus.REQUESTED,
guardianWeights[1]
)
});
assertEq(result, true);
}
address[] memory keys = guardiansStorage[accountAddress].keys();
assertEq(keys.length, EnumerableGuardianMap.MAX_NUMBER_OF_GUARDIANS);
for (
uint256 i = 0;
i < EnumerableGuardianMap.MAX_NUMBER_OF_GUARDIANS;
i++
) {
assertEq(keys[i], vm.addr(i + 1));
}
}
}
Loading

0 comments on commit 9d5b06a

Please sign in to comment.