Smart Contract Automated Testing Guidelines
Smart Contract Automated Testing Guidelines
Smart Contract Automated Testing Guidelines
Author: [email protected]
Foreword
The documents aim to recap my experience in smart contract automated testing besides the manual testing. I also put the issues that I faced during the execution,
indeed, solutions are given.
For a complex project, you can't just copy paste the single sol file and let it run. To make our life easier, Remix has localhost connection which allows you to interact
with your project in your local machine remotely.
This is something I'm used to doing when the project has a large number of inheritant contracts. Obviously, this make our life easier than ever by just downloading the
git project and do some commands.
Steps:
1. Compile your truffle contract if needed with npm install (!remember remvove the package-lock.json, if it does have it). Otherwise, the remix wouldn't be
able to load all libraries for the contracts that are being called.
3. The message box pops up and you just need to read carefully and copy the command shown in the box to connect your localhost
Important:
eg:
Issues
Issue 1
Cannot connect to the remixd daemon. Please make sure you have the remixd running in the background.
What I usually do is just switch to a new terminal tab and re-type the remixd command. If needed, you can just uinstall and reinstall the remixd (Close VS-Code to do
this, if you have it opened)
https://remix-ide.readthedocs.io/en/latest/remixd.html
Issue 2
Issues
You need to just switch the version of solc quickly by a command. The version of solc is kindda painful, depending on the tools and project, you need to use a specific
and exact version to compile.. otherwise broke.
During my audit, I've suffered with solc-select installations. I used to install via the shell command, but now they've upraded to pip3. The thing is that some docker
containers do not support pip3, so you would need to install solc-selct into that docker but pip3. Therefore, I'v a copied version of the solc-select installed via shell.
Installation
Usage:
And use it
solc --version
Tools
1. Slither
Source: https://github.com/crytic/slither
Features
Detects vulnerable Solidity code with low false positives (see the list of trophies)
Identifies where the error condition occurs in the source code
Easily integrates into continuous integration and Truffle builds
Built-in 'printers' quickly report crucial contract information
Detector API to write custom analyses in Python
Ability to analyze contracts written with Solidity >= 0.4
Intermediate representation (SlithIR) enables simple, high-precision analyses
Correctly parses 99.9% of all public Solidity code
Average execution time of less than 1 second per contract
How to install
Using Pip
Using Git
We recommend using an Python virtual environment, as detailed in the Developer Installation Instructions, if you prefer to install Slither via git.
Using Docker
Use the eth-security-toolbox docker image. It includes all of our security tools and every major version of Solidity in a single image. /home/share will be
mounted to /share in the container.
Usage
slither <file-name>.sol
Isssue
Fixed: the --allow-path does not work, just download the library and copy them into the dir.. casual way :/
2. Mythril
Mythril detects a range of security issues, including integer underflows, owner-overwrite-to-Ether-withdrawal, and others. Note that Mythril is targeted at finding
common vulnerabilities, and is not able to discover issues in the business logic of an application. Furthermore, Mythril and symbolic executors are generally unsound,
as they are often unable to explore all possible states of a program.
Source: https://github.com/ConsenSys/mythril
How to install
Note: In my exprience, I prefer using mythril version installed via pip3 rather than Docker. I've faced so many issues with the docker version, and I decided to switch to
pip3 one.
Usage
Issues
Issue 1
mythril.mythril.mythril_disassembler [ERROR]: The file Token.sol does not contain a compilable contract. mythril.interfaces.cli [ERROR]: input files do not contain
any valid contracts
Issue 2
Evn: MacOS
Just in case the command Pip3 install mythril does not work. I don't remember what happened exactly but something does not work with pip3 in MacOS :)
Issue 3
Error
### 3. Manticore
#### Features
Program Exploration: Manticore can execute a program with symbolic inputs and explore all the possible states it can reach
Input Generation: Manticore can automatically produce concrete inputs that result in a given program state
Error Discovery: Manticore can detect crashes and other failure cases in binaries and smart contracts
Instrumentation: Manticore provides fine-grained control of state exploration via event callbacks and instruction hooks
Programmatic Interface: Manticore exposes programmatic access to its analysis engine via a Python API
#### Installation
```bash
pip install manticore
Option 2: Installing from PyPI, with extra dependencies needed to execute native binaries:
Once installed, the manticore CLI tool and Python API will be available.
Usage
manticore <file-name>.sol
Note:
Manticore takes quite a long time to complete the scan by default, so usually I also use --quick-mode option for quick exploration. Disable gas, generate testcase
only for alive states, do not explore constant functions. Disable all detectors.
4. Theo
Source: https://github.com/cleanunicorn/theo
Features
Installation
Usage
Usually I deploy the smart contract in Ganache local network, from that, I can freely have the private keys of many accounts. If you have metamask installed, you can
deploy in the testnet and get the private key of the accounts.
eg:
5. SmartCheck
Souce: https://www.npmjs.com/package/@smartdec/smartcheck
SmartCheck is an extensible static analysis tool for discovering vulnerabilities and other code issues in Ethereum smart contracts written in the Solidity programming
language
Installation
Usage
6. Securitfy2
Source: https://github.com/eth-sri/securify2
Furture
Supports 38 vulnerabilities (see table below)
Implements novel context-sensitive static analysis written in Datalog
Analyzes contracts written in Solidity >= 0.5.8
Installation
eg:
7. Sohint
Source: https://github.com/duaraghav8/Ethlint
Ethlint (Formerly Solium) analyzes your Solidity code for style & security issues and fixes them.
Installation
Usage
solium --init
This creates .soliumrc.json file, which contains configuration that tells Solium how to lint your project. You should modify this file to configure rules, plugins and
sharable configs.
{
"extends": "solium:recommended"
}
solium -f foobar.sol
or
solium -d contracts/
8. Spell check
Source: https://github.com/streetsidesoftware/cspell
Installation
Usage
cspell-cli <contract-name>.sol
Source: https://github.com/ConsenSys/surya
Surya is an utility tool for smart contract systems. It provides a number of visual outputs and information about the contracts' structure. Also supports querying the
function call graph in multiple ways to aid in the manual inspection of contracts.
Installation
Install graphviz
Install surya
Usage
Here is my list:
1. Name: vscode-slither
3. Name: Slither
5. Name: mythril
6. Name: solidity