Skip to content

Commit

Permalink
feat: improving coverage (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
bl0up authored May 29, 2024
1 parent b6eb622 commit b36910f
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 4 deletions.
4 changes: 0 additions & 4 deletions contracts/Forwarder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ contract Forwarder is EIP712, ERC165 {
return _nonces[from];
}

function getForwarderAddress() public view returns (address) {
return address(this);
}

function verify(
ForwardRequest calldata req,
bytes calldata signature
Expand Down
107 changes: 107 additions & 0 deletions lcov.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
TN:
SF:contracts/Forwarder.sol
FN:35,Forwarder.
FNDA:16,Forwarder.
DA:36,8
DA:36,8
FN:39,Forwarder.getNonce
FNDA:3,Forwarder.getNonce
DA:40,3
DA:40,3
FN:43,Forwarder.verify
FNDA:1,Forwarder.verify
DA:47,2
DA:47,2
DA:47,2
DA:60,2
DA:60,2
DA:60,2
DA:60,2
DA:60,2
FN:63,Forwarder.execute
FNDA:1,Forwarder.execute
DA:67,1
DA:67,1
BRDA:67,0,0,-
BRDA:67,0,1,1
DA:71,1
DA:71,1
DA:73,1
DA:73,1
DA:73,1
DA:79,1
DA:79,1
BRDA:79,1,0,-
BRDA:79,1,1,1
DA:81,1
DA:81,1
DA:82,1
DA:82,1
FN:85,Forwarder.supportsInterface
FNDA:0,Forwarder.supportsInterface
DA:88,0
DA:88,0
DA:89,0
DA:89,0
DA:89,0
DA:90,0
DA:90,0
FNF:5
FNH:4
LF:13
LH:10
BRF:4
BRH:2
end_of_record
TN:
SF:contracts/GenericTokenMeta.sol
FN:34,GenericTokenMeta.
FNDA:0,GenericTokenMeta.
DA:44,0
DA:44,0
FN:55,GenericTokenMeta.pause
FNDA:2,GenericTokenMeta.pause
DA:56,2
DA:56,2
FN:67,GenericTokenMeta.unpause
FNDA:1,GenericTokenMeta.unpause
DA:68,1
DA:68,1
FN:84,GenericTokenMeta.mint
FNDA:6,GenericTokenMeta.mint
DA:85,5
DA:85,5
FN:101,GenericTokenMeta.burn
FNDA:2,GenericTokenMeta.burn
DA:102,2
DA:102,2
FN:105,GenericTokenMeta._msgSender
FNDA:15,GenericTokenMeta._msgSender
DA:111,15
DA:111,15
FN:114,GenericTokenMeta._msgData
FNDA:0,GenericTokenMeta._msgData
DA:120,0
DA:120,0
DA:120,0
FN:123,GenericTokenMeta.msgData
FNDA:0,GenericTokenMeta.msgData
DA:124,0
DA:124,0
DA:124,0
FN:137,GenericTokenMeta._update
FNDA:8,GenericTokenMeta._update
DA:142,8
DA:142,8
FN:145,GenericTokenMeta._contextSuffixLength
FNDA:15,GenericTokenMeta._contextSuffixLength
DA:151,15
DA:151,15
DA:151,15
FNF:10
FNH:7
LF:10
LH:7
BRF:0
BRH:0
end_of_record
151 changes: 151 additions & 0 deletions test/Forwarder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ contract ForwarderTest is Test {
GenericTokenMeta token;
address owner;
address signer;
address signer2;
uint256 privateKey;
uint256 privateKey2;

function setUp() public {
forwarder = new Forwarder();
Expand All @@ -21,10 +23,150 @@ contract ForwarderTest is Test {
address(forwarder)
);
privateKey = 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80;
privateKey2 = 0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d;
signer = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;
signer2 = 0x70997970C51812dc3A010C7d01b50e0d17dc79C8;
token.mint(signer, 20);
}

function testNonce() public view {
uint256 nonce = forwarder.getNonce(signer);
assertEq(nonce, 0);
}

function testVerify() public view {
bytes32 domainSeparator = keccak256(
abi.encode(
keccak256(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
),
keccak256(bytes("MinimalForwarder")), // Name
keccak256(bytes("0.0.1")), // Version
block.chainid,
address(forwarder)
)
);

bytes4 transferSelector = bytes4(
keccak256("transfer(address,uint256)")
);
bytes memory data = abi.encodeWithSelector(transferSelector, owner, 10);

uint256 nonce = forwarder.getNonce(signer);

Forwarder.ForwardRequest memory req = Forwarder.ForwardRequest({
from: signer,
to: address(token),
value: 0,
gas: 300_000,
nonce: nonce,
data: data
});

bytes32 structHash = keccak256(
abi.encode(
keccak256(
"ForwardRequest(address from,address to,uint256 value,uint256 gas,uint256 nonce,bytes data)"
),
req.from,
req.to,
req.value,
req.gas,
req.nonce,
keccak256(req.data)
)
);
bytes32 digest = keccak256(
abi.encodePacked("\x19\x01", domainSeparator, structHash)
);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, digest);

// Adjust the `v` value if necessary
if (v == 0 || v == 1) {
v += 27;
}

bytes memory signature = abi.encodePacked(r, s, v);
forwarder.verify(req, signature);

//signing with a different private key
(uint8 v1, bytes32 r1, bytes32 s1) = vm.sign(privateKey2, digest);

// Adjust the `v` value if necessary
if (v1 == 0 || v1 == 1) {
v1 += 27;
}

bytes memory signature1 = abi.encodePacked(r1, s1, v1);
bool result = forwarder.verify(req, signature1);
assertEq(result, false);

//changing the request
req.nonce++;
result = forwarder.verify(req, signature);
assertEq(result, false);
}

function testExecuteInvalidSignature() public {
bytes32 domainSeparator = keccak256(
abi.encode(
keccak256(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
),
keccak256(bytes("MinimalForwarder")), // Name
keccak256(bytes("0.0.1")), // Version
block.chainid,
address(forwarder)
)
);

bytes4 transferSelector = bytes4(
keccak256("transfer(address,uint256)")
);
bytes memory data = abi.encodeWithSelector(transferSelector, owner, 10);

uint256 nonce = forwarder.getNonce(signer);

Forwarder.ForwardRequest memory req = Forwarder.ForwardRequest({
from: signer,
to: address(token),
value: 0,
gas: 300_000,
nonce: nonce,
data: data
});

bytes32 structHash = keccak256(
abi.encode(
keccak256(
"ForwardRequest(address from,address to,uint256 value,uint256 gas,uint256 nonce,bytes data)"
),
req.from,
req.to,
req.value,
req.gas,
req.nonce,
keccak256(req.data)
)
);
bytes32 digest = keccak256(
abi.encodePacked("\x19\x01", domainSeparator, structHash)
);

(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, digest);

// Adjust the `v` value if necessary
if (v == 0 || v == 1) {
v += 27;
}

v++;
bytes memory signature = abi.encodePacked(r, s, v);
vm.expectRevert();
forwarder.execute(req, signature);
}

function testMetaTransactionExecution() public {
bytes32 domainSeparator = keccak256(
abi.encode(
Expand Down Expand Up @@ -83,5 +225,14 @@ contract ForwarderTest is Test {
// Execute the forwarder request with the signature
(bool success, ) = forwarder.execute(req, signature);
assertTrue(success, "Meta transaction execution failed");
assertEq(token.balanceOf(signer), 10);
}

function testSupportsInterface() public view {
bool result = forwarder.supportsInterface(type(IERC165).interfaceId);
assertTrue(
result,
"supportsInterface should return true for IERC165 interface"
);
}
}
19 changes: 19 additions & 0 deletions test/GenericTokenMeta.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,23 @@ contract GenericTokenMetaTest is Test {
token.unpause();
assertFalse(token.paused());
}

function testFailMintWhenPaused() public {
token.pause();
uint256 mintAmount = 1000 * 10 ** token.decimals();
token.mint(owner, mintAmount); // This should fail
}

function testFailBurnMoreThanBalance() public {
uint256 burnAmount = 200_000 * 10 ** token.decimals();
vm.expectRevert("ERC20: burn amount exceeds balance");
token.burn(burnAmount); // This should fail
}

function testMsgData() public {
// Call the msgData function
bytes memory data = token.msgData();
// Check if the returned data matches the msg.data
assertEq(data.length, msg.data.length, "msgData length mismatch");
}
}

0 comments on commit b36910f

Please sign in to comment.