Skip to content

Commit 04daf12

Browse files
shutter-service: added unit tests
1 parent 8774a69 commit 04daf12

File tree

3 files changed

+122
-4
lines changed

3 files changed

+122
-4
lines changed

bindings/shutterregistry/shutterregistry.go

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/shutter-service/ShutterRegistry.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ contract ShutterRegistry is Ownable {
1515
// Custom error for when a provided timestamp is in the past.
1616
error TimestampInThePast();
1717

18+
// Custom error for when a identityPrefix provided is empty.
19+
error InvalidIdentityPrefix();
20+
1821
struct RegistrationData {
1922
uint64 eon;
2023
uint64 timestamp;
@@ -62,6 +65,9 @@ contract ShutterRegistry is Ownable {
6265
// Ensure the timestamp is not in the past.
6366
require(timestamp >= block.timestamp, TimestampInThePast());
6467

68+
// Ensure identityPrefix passed in correct.
69+
require(identityPrefix != bytes32(0), InvalidIdentityPrefix());
70+
6571
// Generate the identity hash from the provided prefix and the sender's address.
6672
bytes32 identity = keccak256(
6773
abi.encodePacked(identityPrefix, msg.sender)

test/ShutterRegistry.t.sol

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.22;
3+
4+
import "forge-std/Test.sol";
5+
import "../src/shutter-service/ShutterRegistry.sol";
6+
7+
contract ShutterRegistryTest is Test{
8+
ShutterRegistry public shutterRegistry;
9+
10+
function setUp() public {
11+
shutterRegistry = new ShutterRegistry();
12+
}
13+
14+
function testIdentityRegistration() public {
15+
uint64 eon = 5;
16+
bytes32 identityPrefix = hex"001122";
17+
uint64 timestamp = uint64(block.timestamp) + 100;
18+
address sender = makeAddr("sender");
19+
20+
vm.expectEmit(address(shutterRegistry));
21+
emit ShutterRegistry.IdentityRegistered(
22+
eon,
23+
identityPrefix,
24+
sender,
25+
timestamp
26+
);
27+
28+
hoax(sender);
29+
shutterRegistry.register(
30+
eon,
31+
identityPrefix,
32+
timestamp
33+
);
34+
35+
bytes32 identity = keccak256(
36+
abi.encodePacked(identityPrefix, sender)
37+
);
38+
(uint64 registeredEon, uint64 registeredTimestamp) = shutterRegistry.registrations(identity);
39+
40+
//verifying registered timestamp
41+
assertEqUint(registeredEon, eon);
42+
assertEqUint(registeredTimestamp, timestamp);
43+
}
44+
45+
function testDuplicateRegistration() public {
46+
uint64 eon = 5;
47+
bytes32 identityPrefix = hex"001122";
48+
uint64 timestamp = uint64(block.timestamp) + 100;
49+
address sender = makeAddr("sender");
50+
51+
vm.expectEmit(address(shutterRegistry));
52+
emit ShutterRegistry.IdentityRegistered(
53+
eon,
54+
identityPrefix,
55+
sender,
56+
timestamp
57+
);
58+
59+
hoax(sender);
60+
shutterRegistry.register(
61+
eon,
62+
identityPrefix,
63+
timestamp
64+
);
65+
66+
uint64 newTimestamp = uint64(block.timestamp) + 200;
67+
vm.expectRevert(ShutterRegistry.AlreadyRegistered.selector);
68+
hoax(sender);
69+
shutterRegistry.register(
70+
eon,
71+
identityPrefix,
72+
newTimestamp
73+
);
74+
75+
//verifying registered timestamp
76+
bytes32 identity = keccak256(
77+
abi.encodePacked(identityPrefix, sender)
78+
);
79+
(, uint64 registeredTimestamp) = shutterRegistry.registrations(identity);
80+
assertEqUint(registeredTimestamp, timestamp);
81+
}
82+
83+
function testInvalidTimestamp() public {
84+
uint64 eon = 5;
85+
bytes32 identityPrefix = hex"001122";
86+
uint64 timestamp = uint64(block.timestamp) - 1;
87+
address sender = makeAddr("sender");
88+
89+
vm.expectRevert(ShutterRegistry.TimestampInThePast.selector);
90+
hoax(sender);
91+
shutterRegistry.register(
92+
eon,
93+
identityPrefix,
94+
timestamp
95+
);
96+
}
97+
98+
function testMissingIdentity() public {
99+
uint64 eon = 5;
100+
// zero bytes for identity prefix should fail
101+
bytes32 identityPrefix = hex"00";
102+
uint64 timestamp = uint64(block.timestamp) + 100;
103+
address sender = makeAddr("sender");
104+
105+
vm.expectRevert(ShutterRegistry.InvalidIdentityPrefix.selector);
106+
hoax(sender);
107+
shutterRegistry.register(
108+
eon,
109+
identityPrefix,
110+
timestamp
111+
);
112+
}
113+
}

0 commit comments

Comments
 (0)