Merge pull request #1329 from yourowncrypto/fix-fuzz

Update fuzz in core crate
This commit is contained in:
hashmap 2018-08-09 14:09:40 +02:00 committed by GitHub
commit f11a3b1994
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 18 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "grin_core-fuzz"
version = "0.0.1"
version = "0.0.3"
authors = ["Grin Developers <mimblewimble@lists.launchpad.net>"]
publish = false
@ -10,7 +10,9 @@ cargo-fuzz = true
[dependencies]
grin_core = { path = ".."}
grin_keychain = { path = "../../keychain"}
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" }
grin_wallet = { path = "../../wallet"}
[dependencies.libfuzzer-sys]
git = "https://github.com/rust-fuzz/libfuzzer-sys.git"
# Prevent this from interfering with workspaces
[workspace]

View file

@ -1,6 +1,7 @@
# Fuzz testing
## Installation
You have to use Rust nightly at the moment.
Cargo-fuzz (https://github.com/rust-fuzz/cargo-fuzz) has been used.
To install it:
@ -24,6 +25,7 @@ Fuzz test is basically infinite test, run it for some period of time then
stop if no failures are found.
To run the tests make sure youre in folder `core` otherwise you may get
some misleading errors, then run one of the following tests:
```
cargo fuzz run tx_read
@ -32,4 +34,9 @@ cargo fuzz run block_read
cargo fuzz run compact_block_read
```
Check `fuzz/Cargo.toml` for the full list of targets.
Run
```
cargo fuzz list
```
or check `fuzz/Cargo.toml` for the full list of targets.

View file

@ -1,11 +1,14 @@
extern crate grin_core;
extern crate grin_keychain;
extern crate grin_wallet;
use grin_core::core::build::{input, output, transaction_with_offset, with_fee};
use grin_core::core::target::Difficulty;
use grin_core::core::{Block, BlockHeader, CompactBlock, Transaction};
use grin_core::ser;
use grin_keychain::keychain::Keychain;
use grin_keychain::keychain::ExtKeychain;
use grin_keychain::Keychain;
use grin_wallet::libtx::build::{input, output, transaction_with_offset, with_fee};
use grin_wallet::libtx::reward;
use std::fs::{self, File};
use std::path::Path;
@ -18,12 +21,16 @@ fn main() {
fn generate<W: ser::Writeable>(target: &str, obj: W) -> Result<(), ser::Error> {
let dir_path = Path::new("corpus").join(target);
if !dir_path.is_dir() {
fs::create_dir(&dir_path).map_err(|e| ser::Error::IOErr(e))?;
fs::create_dir_all(&dir_path).map_err(|e| {
println!("fail: {}", e);
ser::Error::IOErr("can't create corpus directory".to_owned(), e.kind())
})?;
}
let pattern_path = dir_path.join("pattern");
if !pattern_path.exists() {
let mut file = File::create(&pattern_path).map_err(|e| ser::Error::IOErr(e))?;
let mut file = File::create(&pattern_path)
.map_err(|e| ser::Error::IOErr("can't create a pattern file".to_owned(), e.kind()))?;
ser::serialize(&mut file, &obj)
} else {
Ok(())
@ -31,19 +38,19 @@ fn generate<W: ser::Writeable>(target: &str, obj: W) -> Result<(), ser::Error> {
}
fn block() -> Block {
let keychain = Keychain::from_random_seed().unwrap();
let keychain = ExtKeychain::from_random_seed().unwrap();
let key_id = keychain.derive_key_id(1).unwrap();
let mut tx1 = tx();
let mut tx2 = tx();
let mut txs = Vec::new();
for _ in 1..10 {
txs.push(tx());
}
Block::new(
&BlockHeader::default(),
vec![&mut tx1, &mut tx2],
&keychain,
&key_id,
Difficulty::one(),
).unwrap()
let header = BlockHeader::default();
let reward = reward::output(&keychain, &key_id, 0, header.height).unwrap();
Block::new(&header, txs, Difficulty::one(), reward).unwrap()
}
fn compact_block() -> CompactBlock {
@ -57,7 +64,7 @@ fn compact_block() -> CompactBlock {
}
fn tx() -> Transaction {
let keychain = Keychain::from_random_seed().unwrap();
let keychain = ExtKeychain::from_random_seed().unwrap();
let key_id1 = keychain.derive_key_id(1).unwrap();
let key_id2 = keychain.derive_key_id(2).unwrap();
let key_id3 = keychain.derive_key_id(3).unwrap();