Testing your detector
Testing is key, not only when writing smart contracts but also when writing the detectors to test them.
Testing an Aderyn detector is extremely easy, as it's built in Rust and can be tested as any other Rust code, with the only difference being that you will need a Solidity smart contract implementing the vulnerability your detector has to find.
Luckily, Cyfrin Aderyn has a series of smart contracts in the tests/contracts_playground/src
folder, which you can use to test your detectors.
You can also find the same contracts in the standalone contracts-playground repository.
Now, let's say we want to test the detector we've built in the Detectors quickstart guide, whose only job is ensuring that all modifiers assigned to every function are used and that there are no useless modifiers.
Simplified, the detector looks like this:
Source: https://github.com/Cyfrin/aderyn/blob/dev/aderyn_core/src/detect/low/useless_modifier.rs
To test it, we will use the OnceModifierExample.sol contract available in the tests/contracts_playground/contracts_playground
folder:
As you can notice, the perform()
function has the onlyeOnce
modifier assigned only, which does nothing, our detector should find it and report it - to test this is true, let's copy-paste the following code under our detector declaration:
Your code should look as follows: https://github.com/Cyfrin/aderyn/blob/dev/aderyn_core/src/detect/low/useless_modifier.rs#L58
In the code above, we are:
Importing the OnceModifierExample.sol smart contract AST
Run the detect function
Assert if we've effectively found the vulnerability
To run the one test we just made, run the following command in your terminal:
Or, to run all the tests:
If the vulnerability was found, the detector successfully worked, and you're ready to use it, publish it, and contribute to Aderyn. TODO
You can learn more about creating tests on Rusts using the official documentation.
Last updated