Skip to content

Commit

Permalink
Add test for reactivate function and fix failed testcases.
Browse files Browse the repository at this point in the history
  • Loading branch information
wshino committed Jun 10, 2024
1 parent 6765ea6 commit 2b7baa0
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/contracts/UserOverrideableDKIMRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ contract UserOverrideableDKIMRegistry is IDKIMRegistry, Ownable {
) public view returns (bool) {
require(bytes(domainName).length > 0, "domain name cannot be zero");
require(publicKeyHash != bytes32(0), "public key hash cannot be zero");
require(authorizer != address(0), "user address cannot be zero");
require(authorizer != address(0), "authorizer address cannot be zero");
uint256 revokeThreshold = _computeRevokeThreshold(
publicKeyHash,
authorizer
Expand Down
109 changes: 102 additions & 7 deletions packages/contracts/test/UserOverrideableDKIMRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "../UserOverrideableDKIMRegistry.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import "./helpers/ExampleERC1271.sol";
import "./helpers/ExampleOwnable.sol";

contract UserOverrideableDKIMRegistryTest is Test {
UserOverrideableDKIMRegistry registry;
Expand All @@ -26,42 +27,65 @@ contract UserOverrideableDKIMRegistryTest is Test {

UserOverrideableDKIMRegistry registryWithContract;
ExampleERC1271 mainAuthorizerContract;
ExampleOwnable exampleOwnable;

function setUp() public {
deployer = vm.addr(1);
mainAuthorizer = vm.addr(9);
user1 = vm.addr(2);
user2 = vm.addr(3);
registry = new UserOverrideableDKIMRegistry(deployer, mainAuthorizer);

exampleOwnable = new ExampleOwnable(mainAuthorizer);
mainAuthorizerContract = new ExampleERC1271(mainAuthorizer);
registryWithContract = new UserOverrideableDKIMRegistry(
deployer,
address(mainAuthorizerContract)
);
}

function testSetDKIMPublicKeyHashByUser1() public {
vm.startPrank(user1);
function testSetDKIMPublicKeyHashByMainAuthorizer() public {
vm.startPrank(mainAuthorizer);

vm.expectEmit();
emit UserOverrideableDKIMRegistry.DKIMPublicKeyHashRegistered(
domainName,
publicKeyHash,
user1
mainAuthorizer
);
registry.setDKIMPublicKeyHash(
domainName,
publicKeyHash,
user1,
mainAuthorizer,
new bytes(0)
);
vm.stopPrank();

// Call by a Ownable contract
vm.startPrank(address(exampleOwnable));
// setThreshold = 2
require(
registry.isDKIMPublicKeyHashValid(domainName, publicKeyHash),
"Invalid public key hash"
);
vm.stopPrank();
}

function testSetDKIMPublicKeyHashByUser1() public {
vm.startPrank(user1);

vm.expectEmit();
emit UserOverrideableDKIMRegistry.DKIMPublicKeyHashRegistered(
domainName,
publicKeyHash,
user1
);
registry.setDKIMPublicKeyHash(
domainName,
publicKeyHash,
user1,
new bytes(0)
);
vm.stopPrank();

// setThreshold = 2
require(
Expand Down Expand Up @@ -303,6 +327,24 @@ contract UserOverrideableDKIMRegistryTest is Test {
vm.stopPrank();
}

function testRevokeDKIMPublicKeyHashByMainAuthorizer() public {
testSetDKIMPublicKeyHashByMainAuthorizer();

vm.startPrank(mainAuthorizer);
vm.expectEmit();
emit UserOverrideableDKIMRegistry.DKIMPublicKeyHashRevoked(
publicKeyHash,
mainAuthorizer
);
registry.revokeDKIMPublicKeyHash(
domainName,
publicKeyHash,
mainAuthorizer,
new bytes(0)
);
vm.stopPrank();
}

function testRevokeDKIMPublicKeyHashByUser1() public {
testSetDKIMPublicKeyHashByUser1();

Expand Down Expand Up @@ -497,12 +539,20 @@ contract UserOverrideableDKIMRegistryTest is Test {

// setThreshold = 2
require(
registry.isDKIMPublicKeyHashValid(domainName, publicKeyHashes[0]),
registry.isDKIMPublicKeyHashValid(
domainName,
publicKeyHashes[0],
user1
),
"Invalid public key hash"
);
// setThreshold = 2
require(
registry.isDKIMPublicKeyHashValid(domainName, publicKeyHashes[1]),
registry.isDKIMPublicKeyHashValid(
domainName,
publicKeyHashes[1],
user1
),
"Invalid public key hash"
);
vm.stopPrank();
Expand Down Expand Up @@ -535,4 +585,49 @@ contract UserOverrideableDKIMRegistryTest is Test {
);
console.log(signedMsg);
}

function testReactivateDKIMPublicKeyHashByUser1() public {
testSetDKIMPublicKeyHashByUser1();
testRevokeDKIMPublicKeyHashByMainAuthorizer();

require(
!registry.isDKIMPublicKeyHashValid(
domainName,
publicKeyHash,
user1
),
"public key hash is not revoked"
);

vm.startPrank(user1);

string memory signedMsg = registryWithContract.computeSignedMsg(
registryWithContract.REVOKE_PREFIX(),
domainName,
publicKeyHash
);
bytes32 digest = MessageHashUtils.toEthSignedMessageHash(
bytes(signedMsg)
);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(9, digest);
bytes memory signature = abi.encodePacked(r, s, v);

vm.expectEmit();
emit UserOverrideableDKIMRegistry.DKIMPublicKeyHashReactivated(
publicKeyHash,
user1
);
registry.reactivateDKIMPublicKeyHash(
domainName,
publicKeyHash,
user1,
signature
);
vm.stopPrank();

require(
registry.isDKIMPublicKeyHashValid(domainName, publicKeyHash, user1),
"Invalid public key hash"
);
}
}
8 changes: 8 additions & 0 deletions packages/contracts/test/helpers/ExampleOwnable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/Ownable.sol";

contract ExampleOwnable is Ownable {
constructor(address _owner) Ownable(_owner) {}
}

0 comments on commit 2b7baa0

Please sign in to comment.