Skip to content

Commit eee11fd

Browse files
committed
Add arbitraries
This is incomplete as long as multiformats/rust-multihash#51 is not merged
1 parent 4f8d8f4 commit eee11fd

File tree

7 files changed

+100
-19
lines changed

7 files changed

+100
-19
lines changed

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,12 @@ no-dev-version = true
2222
multihash = "~0.8.0"
2323
multibase = "~0.6.0"
2424
integer-encoding = "~1.0.3"
25+
quickcheck = { version = "0.9.2", optional = true }
26+
rand = { version = "0.7.3", optional = true }
27+
28+
[dev-dependencies]
29+
quickcheck = "0.9.2"
30+
rand = "0.7.3"
31+
32+
[features]
33+
test = ["quickcheck", "rand"]

src/arb.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use crate::{Cid, Codec, Prefix, Version};
2+
use quickcheck::{Arbitrary, Gen};
3+
use rand::seq::SliceRandom;
4+
use rand::Rng;
5+
6+
const CODECS: [Codec; 18] = [
7+
Codec::Raw,
8+
Codec::DagProtobuf,
9+
Codec::DagCBOR,
10+
Codec::GitRaw,
11+
Codec::EthereumBlock,
12+
Codec::EthereumBlockList,
13+
Codec::EthereumTxTrie,
14+
Codec::EthereumTx,
15+
Codec::EthereumTxReceiptTrie,
16+
Codec::EthereumTxReceipt,
17+
Codec::EthereumStateTrie,
18+
Codec::EthereumAccountSnapshot,
19+
Codec::EthereumStorageTrie,
20+
Codec::BitcoinBlock,
21+
Codec::BitcoinTx,
22+
Codec::ZcashBlock,
23+
Codec::ZcashTx,
24+
Codec::DagJSON,
25+
];
26+
27+
const POPULAR: [Codec; 4] = [
28+
Codec::Raw,
29+
Codec::DagProtobuf,
30+
Codec::DagCBOR,
31+
Codec::DagJSON,
32+
];
33+
34+
impl Arbitrary for Codec {
35+
fn arbitrary<G: Gen>(g: &mut G) -> Self {
36+
// chose the most frequently used codecs more often
37+
if g.gen_bool(0.7) {
38+
*POPULAR.choose(g).unwrap()
39+
} else {
40+
*CODECS.choose(g).unwrap()
41+
}
42+
}
43+
}
44+
45+
impl Arbitrary for Prefix {
46+
fn arbitrary<G: Gen>(g: &mut G) -> Self {
47+
let version = if g.gen_bool(0.7) { 1 } else { 0 };
48+
let codec: Codec = if version == 1 {
49+
// v1 supports arbitrary encodings
50+
Arbitrary::arbitrary(g)
51+
} else {
52+
// v0 does only support DagProtobuf encoding
53+
Codec::DagProtobuf
54+
};
55+
let version = Version::from(version).unwrap();
56+
Prefix {
57+
version,
58+
codec,
59+
// todo: randomize this once https://github.com/multiformats/rust-multihash/pull/51 is merged
60+
mh_type: multihash::Hash::SHA2256,
61+
mh_len: 32,
62+
}
63+
}
64+
}
65+
66+
impl Arbitrary for Cid {
67+
fn arbitrary<G: Gen>(g: &mut G) -> Self {
68+
let mut hash = [0u8; 32];
69+
g.fill_bytes(&mut hash);
70+
let prefix: Prefix = Arbitrary::arbitrary(g);
71+
Cid::new_from_prefix(&prefix, &hash)
72+
}
73+
}

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::{fmt, error, io};
21
use multibase;
32
use multihash;
3+
use std::{error, fmt, io};
44

55
pub type Result<T> = ::std::result::Result<T, Error>;
66

src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,17 @@
22
/// !
33
/// ! Implementation of [cid](https://github.com/ipld/cid) in Rust.
44
5-
extern crate multihash;
6-
extern crate multibase;
7-
extern crate integer_encoding;
8-
9-
mod to_cid;
10-
mod error;
5+
#[cfg(any(test, feature = "test"))]
6+
mod arb;
117
mod codec;
8+
mod error;
9+
mod to_cid;
1210
mod version;
1311

14-
pub use to_cid::ToCid;
15-
pub use version::Version;
1612
pub use codec::Codec;
1713
pub use error::{Error, Result};
14+
pub use to_cid::ToCid;
15+
pub use version::Version;
1816

1917
use integer_encoding::{VarIntReader, VarIntWriter};
2018
use std::fmt;

src/to_cid.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::io::Cursor;
2-
use std::str::FromStr;
1+
use integer_encoding::VarIntReader;
32
use multibase;
43
use multihash;
5-
use integer_encoding::VarIntReader;
4+
use std::io::Cursor;
5+
use std::str::FromStr;
66

7-
use crate::{Cid, Version, Codec, Error, Result};
7+
use crate::{Cid, Codec, Error, Result, Version};
88

99
pub trait ToCid {
1010
fn to_cid(&self) -> Result<Cid>;
@@ -39,7 +39,7 @@ impl ToCid for str {
3939

4040
let hash = match self.find(IPFS_DELIMETER) {
4141
Some(index) => &self[index + IPFS_DELIMETER.len()..],
42-
_ => self
42+
_ => self,
4343
};
4444

4545
if hash.len() < 2 {
@@ -60,7 +60,6 @@ impl ToCid for str {
6060
}
6161
}
6262

63-
6463
impl FromStr for Cid {
6564
type Err = Error;
6665
fn from_str(src: &str) -> Result<Self> {

src/version.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl Version {
1313
match raw {
1414
0 => Ok(V0),
1515
1 => Ok(V1),
16-
_ => Err(Error::InvalidCidVersion)
16+
_ => Err(Error::InvalidCidVersion),
1717
}
1818
}
1919

@@ -24,7 +24,7 @@ impl Version {
2424
}
2525

2626
pub fn is_v0_binary(data: &[u8]) -> bool {
27-
data.len() == 34 && data.starts_with(&[0x12,0x20])
27+
data.len() == 34 && data.starts_with(&[0x12, 0x20])
2828
}
2929
}
3030

tests/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate cid;
22
extern crate multihash;
33

4-
use cid::{Cid, Version, Codec, Error, Prefix};
4+
use cid::{Cid, Codec, Error, Prefix, Version};
55
use std::collections::HashMap;
66

77
#[test]
@@ -37,7 +37,9 @@ fn v0_handling() {
3737

3838
#[test]
3939
fn from_str() {
40-
let cid: Cid = "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n".parse().unwrap();
40+
let cid: Cid = "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
41+
.parse()
42+
.unwrap();
4143
assert_eq!(cid.version, Version::V0);
4244

4345
let bad = "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII".parse::<Cid>();

0 commit comments

Comments
 (0)