GivingThanks.t.sol
lacked coverage for several critical functions and edge cases, including:Access Control for updateRegistry: Tests did not include scenarios for unauthorized access attempts to updateRegistry
.
Donations to Unverified Charities: Edge cases were not sufficiently tested for donating to unverified charities.
Token Minting Edge Cases: The minting process was not tested thoroughly, particularly in cases where the recipient might be a smart contract not supporting ERC721 tokens.
Event Emissions: There were no tests verifying the correct emission of events, such as DonateToCharity
.
Address(0) Checks: Scenarios where invalid addresses (e.g., address(0)
) were passed to functions were not covered.
Unexpected Contract Behavior: Missed edge cases and incorrect assumptions can cause bugs to surface during deployment or interaction with users.
Security Vulnerabilities: Limited testing increases the risk of undetected issues, including potential exploits related to access control or improper function inputs.
Reduced Confidence in Smart Contract: Incomplete testing reduces the confidence developers and users can have in the reliability of the contract, possibly impacting adoption and usage.
Foundry for running tests (forge test
)
Coverage Analysis using Foundry’s built-in coverage tool (forge coverage
)
Test Breakdown and Explanations:
CharityRegistry.sol
testCharityRegistryChangeAdminWithAdmin
Purpose: Verifies that only the current admin can change the admin address.
Explanation:
Sets up a new address (newAdmin
).
Impersonates the current admin using vm.startPrank(admin)
.
Calls changeAdmin
to update the admin address.
Asserts that the admin address has been successfully updated to newAdmin
.
testCharityRegistryChangeAdminWithAdminRevert
Purpose: Ensures that a non-admin user cannot change the admin address.
Explanation:
Sets up a new address (newAdmin
).
Impersonates a non-admin user (donor
).
Expects the call to changeAdmin
to revert with the message "Only admin can change admin".
testCharityVerifyCharityAdminWithAdmin
Purpose: Confirms that only the admin can verify a registered charity.
Explanation:
Registers a new charity (newCharity
).
Impersonates the admin and calls verifyCharity
to verify the charity.
Asserts that the charity is now verified (isVerified(newCharity)
returns true
).
testCharityVerifyCharityAdminWithAdminRevert
Purpose: Ensures that a non-admin user cannot verify a charity.
Explanation:
Registers a new charity (newCharity
).
Impersonates a non-admin user (donor
) and attempts to call verifyCharity
.
Expects the call to revert with the message "Only admin can verify".
testCharityVerifyCharityAdminWithAdminRevertNotRegisteredCharities
Purpose: Tests that a charity cannot be verified if it was not registered first.
Explanation:
Creates a new charity address (newCharity
) without registering it.
Impersonates the admin and attempts to verify the charity.
Expects the call to revert with the message "Charity not registered".
GivingThanks.sol
testGivingThanksUpdateRegistryNotProtected
Purpose: Checks if the updateRegistry
function can be called by anyone, demonstrating the lack of access control.
Explanation:
Stores the current registry address.
Impersonates an attacker and calls updateRegistry
with a new address.
Asserts that the registry address has changed, indicating the function is unprotected.
testGivingThanksCreateTokenURIReturnCorrectValues
Purpose: Verifies that the _createTokenURI
function generates the correct token URI and ensures the donation process works properly.
Explanation:
Sets up a donation amount and retrieves the initial token counter.
Funds the donor
and impersonates them for the donation.
Asserts that the token counter has incremented by 1 and checks the owner of the new token.
Verifies the charity's balance to ensure the donation was received.
Calls _createTokenURI
and compares the returned URI with the stored value (tokenURI(0)
).
forge coverage
before adding new test functions:
forge coverage
after adding new test functions:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.