forked from Bitcoin-ABC/bitcoin-abc
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integration of property based testing into Bitcoin Cash Node
Summary: This is an initial integration of the rapidcheck property based tests. The original test has been slightly modified to adapt to our codebase. The feature has been added to CMake and the documentation updated accordingly. Note that as opposed to autotools, which enables the test automatically if `rapidcheck` is installed, it is disabled by default with cmake and requires a flag to be set by the user. Backport of core [[bitcoin/bitcoin#12775 | PR12775]], [[bitcoin/bitcoin#16622 | PR16622]] and [[bitcoin/bitcoin#16645 | PR16645]]. Test Plan: Read the doc and install rapidcheck. ../configure Ensure that rapidcheck is used by looking at the summary. make check Ensure there is no regression: cmake -GNinja .. ninja check cmake -GNinja .. -DENABLE_PROPERTY_BASED_TESTS=ON Check in the output that `rapidcheck` is enabled by searching for a couple lines similar to: ``` -- Found Rapidcheck: /usr/local/include -- Found Rapidcheck: /usr/local/lib/librapidcheck.a ``` ninja check Assuming you are running a 64-bit Linux machine: cd depends RAPIDCHECK=1 make build-linux64 Check the `rapidcheck` package is built. cd .. && mkdir buildLinux64 && cd buildLinux64 cmake -GNinja .. \ -DCMAKE_TOOLCHAIN_FILE=../cmake/platforms/Linux64.cmake \ -DENABLE_PROPERTY_BASED_TESTS=ON Check the `rapidcheck` lib found is the one from the `depends`. ninja check Reviewers: #bitcoin_abc, jasonbcox Reviewed By: #bitcoin_abc, jasonbcox Subscribers: jasonbcox Differential Revision: https://reviews.bitcoinabc.org/D5323
- Loading branch information
1 parent
093c91f
commit 9613af3
Showing
12 changed files
with
348 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Copyright (c) 2020 The Bitcoin developers | ||
# Distributed under the MIT software license, see the accompanying | ||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
# .rst: | ||
# FindRapidcheck | ||
# -------------- | ||
# | ||
# Find the Rapidcheck library. | ||
# | ||
# This will define the following variables:: | ||
# | ||
# Rapidcheck_FOUND - True if the Rapidcheck library is found. | ||
# Rapidcheck_INCLUDE_DIRS - List of the header include directories. | ||
# Rapidcheck_LIBRARIES - List of the libraries. | ||
# | ||
# And the following imported targets:: | ||
# | ||
# Rapidcheck::Rapidcheck | ||
|
||
find_path(Rapidcheck_INCLUDE_DIR | ||
NAMES rapidcheck.h | ||
PATH_SUFFIXES rapidcheck | ||
) | ||
|
||
find_library(Rapidcheck_LIBRARY | ||
NAMES rapidcheck | ||
PATH_SUFFIXES rapidcheck | ||
) | ||
|
||
# TODO: extract a version number. | ||
# For now rapidcheck does not provide such a version number, and has no release. | ||
# See https://github.com/emil-e/rapidcheck/issues/235 for reference. | ||
|
||
include(FindPackageHandleStandardArgs) | ||
find_package_handle_standard_args(Rapidcheck | ||
DEFAULT_MSG | ||
Rapidcheck_INCLUDE_DIR | ||
Rapidcheck_LIBRARY | ||
) | ||
|
||
if(Rapidcheck_FOUND) | ||
set(Rapidcheck_INCLUDE_DIRS "${Rapidcheck_INCLUDE_DIR}") | ||
set(Rapidcheck_LIBRARIES "${Rapidcheck_LIBRARY}") | ||
|
||
include(FindPackageMessage) | ||
find_package_message(Rapidcheck | ||
"Found Rapidcheck: ${Rapidcheck_LIBRARIES}" | ||
"[${Rapidcheck_LIBRARIES}][${Rapidcheck_INCLUDE_DIRS}]" | ||
) | ||
|
||
if(NOT TARGET Rapidcheck::Rapidcheck) | ||
add_library(Rapidcheck::Rapidcheck UNKNOWN IMPORTED) | ||
set_target_properties(Rapidcheck::Rapidcheck PROPERTIES | ||
INTERFACE_INCLUDE_DIRECTORIES "${Rapidcheck_INCLUDE_DIR}" | ||
IMPORTED_LOCATION "${Rapidcheck_LIBRARY}" | ||
) | ||
endif() | ||
endif() | ||
|
||
mark_as_advanced( | ||
Rapidcheck_INCLUDE_DIR | ||
Rapidcheck_LIBRARY | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package=rapidcheck | ||
$(package)_version=10fc0cb | ||
$(package)_download_path=https://github.com/MarcoFalke/rapidcheck/archive | ||
$(package)_file_name=$(package)-$($(package)_version).tar.gz | ||
$(package)_sha256_hash=9640926223c00af45bce4c7df8b756b5458a89b2ba74cfe3e404467f13ce26df | ||
|
||
define $(package)_config_cmds | ||
cmake -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=true . | ||
endef | ||
|
||
define $(package)_build_cmds | ||
$(MAKE) -j$(JOBS) && \ | ||
mkdir -p $($(package)_staging_dir)$(host_prefix)/include && \ | ||
cp -a include/* $($(package)_staging_dir)$(host_prefix)/include/ && \ | ||
cp -a extras/boost_test/include/rapidcheck/* $($(package)_staging_dir)$(host_prefix)/include/rapidcheck/ && \ | ||
mkdir -p $($(package)_staging_dir)$(host_prefix)/lib && \ | ||
cp -a librapidcheck.a $($(package)_staging_dir)$(host_prefix)/lib/ | ||
endef |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# RapidCheck property-based testing for Bitcoin Cash Node | ||
|
||
## Concept | ||
|
||
Property-based testing is experimentally being added to Bitcoin Cash Node with | ||
[RapidCheck](https://github.com/emil-e/rapidcheck), a C++ framework for | ||
property-based testing inspired by the Haskell library | ||
[QuickCheck](https://hackage.haskell.org/package/QuickCheck). | ||
|
||
RapidCheck performs random testing of program properties. A specification of the | ||
program is given in the form of properties which functions should satisfy, and | ||
RapidCheck tests that the properties hold in a large number of randomly | ||
generated cases. | ||
|
||
If an exception is found, RapidCheck tries to find the smallest case, for some | ||
definition of smallest, for which the property is still false and displays it as | ||
a counter-example. For example, if the input is an integer, RapidCheck tries to | ||
find the smallest integer for which the property is false. | ||
|
||
## Setup | ||
|
||
The following instructions have been tested with Linux Debian and macOS. | ||
|
||
1. Clone the RapidCheck source code and cd into the repository. | ||
|
||
```shell | ||
git clone https://github.com/emil-e/rapidcheck.git | ||
cd rapidcheck | ||
``` | ||
|
||
2. Build RapidCheck. | ||
|
||
```shell | ||
mkdir build | ||
cd build | ||
cmake -GNinja .. -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DRC_ENABLE_BOOST_TEST=ON | ||
ninja | ||
sudo ninja install/strip | ||
``` | ||
|
||
3. Configure Bitcoin Cash Node with RapidCheck. | ||
|
||
`cd` to the directory of your local Bitcoin Cash Node repository, create a build | ||
directory and run cmake: | ||
|
||
```shell | ||
mkdir build | ||
cd build | ||
cmake -GNinja .. -DENABLE_PROPERTY_BASED_TESTS=ON | ||
``` | ||
|
||
In the output you should see something similar to: | ||
|
||
```shell | ||
[...] | ||
-- Found Rapidcheck: /usr/local/include | ||
-- Found Rapidcheck: /usr/local/lib/librapidcheck.a | ||
[...] | ||
``` | ||
|
||
4. Build Bitcoin Cash Node with RapidCheck. | ||
|
||
Now you can run `ninja check` to build and run the unit tests, including the | ||
property-based tests. You can also build and run a single test by using | ||
`ninja check-bitcoin-<test_name>`. | ||
|
||
Example: `ninja check-bitcoin-key_properties` | ||
|
||
That's it! You are now running property-based tests in Bitcoin Cash Node. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) 2018 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
#include <test/gen/crypto_gen.h> | ||
|
||
#include <key.h> | ||
|
||
#include <rapidcheck/Gen.h> | ||
#include <rapidcheck/gen/Arbitrary.h> | ||
#include <rapidcheck/gen/Container.h> | ||
#include <rapidcheck/gen/Predicate.h> | ||
|
||
/** Generates 1 to 20 keys for OP_CHECKMULTISIG */ | ||
rc::Gen<std::vector<CKey>> MultisigKeys() { | ||
return rc::gen::suchThat(rc::gen::arbitrary<std::vector<CKey>>(), | ||
[](const std::vector<CKey> &keys) { | ||
return keys.size() >= 1 && keys.size() <= 15; | ||
}); | ||
}; |
Oops, something went wrong.