Skip to content

Commit

Permalink
Clippy and cleanup in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey committed Apr 19, 2021
1 parent 667ddb3 commit 46f2126
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 65 deletions.
114 changes: 55 additions & 59 deletions rust/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,60 +88,54 @@ mod tests {

let hash = do_hash(HashOp::Sha256, b"food");
assert!(
hash == hex::decode("c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b")
.unwrap(),
hash == decode("c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b"),
"sha256 hash fails"
);

let hash = do_hash(HashOp::Sha512, b"food");
assert!(hash == hex::decode("c235548cfe84fc87678ff04c9134e060cdcd7512d09ed726192151a995541ed8db9fda5204e72e7ac268214c322c17787c70530513c59faede52b7dd9ce64331").unwrap(), "sha512 hash fails");
assert!(hash == decode("c235548cfe84fc87678ff04c9134e060cdcd7512d09ed726192151a995541ed8db9fda5204e72e7ac268214c322c17787c70530513c59faede52b7dd9ce64331"), "sha512 hash fails");

let hash = do_hash(HashOp::Ripemd160, b"food");
assert!(
hash == hex::decode("b1ab9988c7c7c5ec4b2b291adfeeee10e77cdd46").unwrap(),
hash == decode("b1ab9988c7c7c5ec4b2b291adfeeee10e77cdd46"),
"ripemd160 hash fails"
);

let hash = do_hash(HashOp::Bitcoin, b"food");
assert!(
hash == hex::decode("0bcb587dfb4fc10b36d57f2bba1878f139b75d24").unwrap(),
hash == decode("0bcb587dfb4fc10b36d57f2bba1878f139b75d24"),
"bitcoin hash fails"
);

let hash = do_hash(HashOp::Sha512_256, b"food");
assert!(
hash == hex::decode("5b3a452a6acbf1fc1e553a40c501585d5bd3cca176d562e0a0e19a3c43804e88")
.unwrap(),
hash == decode("5b3a452a6acbf1fc1e553a40c501585d5bd3cca176d562e0a0e19a3c43804e88"),
"sha512/256 hash fails"
);
}

#[test]
fn length_prefix() -> Result<()> {
let prefixed = do_length(LengthOp::NoPrefix, b"food")?;
ensure!(
prefixed == hex::decode("666f6f64")?,
"no prefix modifies data"
);
fn length_prefix() {
let prefixed = do_length(LengthOp::NoPrefix, b"food").unwrap();
assert!(prefixed == decode("666f6f64"), "no prefix modifies data");

let prefixed = do_length(LengthOp::VarProto, b"food")?;
ensure!(
prefixed == hex::decode("04666f6f64")?,
let prefixed = do_length(LengthOp::VarProto, b"food").unwrap();
assert!(
prefixed == decode("04666f6f64"),
"proto prefix returned {}",
hex::encode(&prefixed),
);

let prefixed = do_length(LengthOp::Fixed32Little, b"food")?;
ensure!(
prefixed == hex::decode("04000000666f6f64")?,
let prefixed = do_length(LengthOp::Fixed32Little, b"food").unwrap();
assert!(
prefixed == decode("04000000666f6f64"),
"proto prefix returned {}",
hex::encode(&prefixed),
);
Ok(())
}

#[test]
fn apply_leaf_hash() -> Result<()> {
fn apply_leaf_hash() {
let leaf = LeafOp {
hash: HashOp::Sha256.into(),
prehash_key: 0,
Expand All @@ -151,13 +145,15 @@ mod tests {
};
let key = b"foo";
let val = b"bar";
let hash = hex::decode("c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2")?;
ensure!(hash == apply_leaf(&leaf, key, val)?, "unexpected leaf hash");
Ok(())
let hash = decode("c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2");
assert!(
hash == apply_leaf(&leaf, key, val).unwrap(),
"unexpected leaf hash"
);
}

#[test]
fn apply_leaf_hash_512() -> Result<()> {
fn apply_leaf_hash_512() {
let leaf = LeafOp {
hash: HashOp::Sha512.into(),
prehash_key: 0,
Expand All @@ -167,13 +163,15 @@ mod tests {
};
let key = b"f";
let val = b"oobaz";
let hash = hex::decode("4f79f191298ec7461d60136c60f77c2ae8ddd85dbf6168bb925092d51bfb39b559219b39ae5385ba04946c87f64741385bef90578ea6fe6dac85dbf7ad3f79e1")?;
ensure!(hash == apply_leaf(&leaf, key, val)?, "unexpected leaf hash");
Ok(())
let hash = decode("4f79f191298ec7461d60136c60f77c2ae8ddd85dbf6168bb925092d51bfb39b559219b39ae5385ba04946c87f64741385bef90578ea6fe6dac85dbf7ad3f79e1");
assert!(
hash == apply_leaf(&leaf, key, val).unwrap(),
"unexpected leaf hash"
);
}

#[test]
fn apply_leaf_hash_length() -> Result<()> {
fn apply_leaf_hash_length() {
let leaf = LeafOp {
hash: HashOp::Sha256.into(),
prehash_key: 0,
Expand All @@ -183,13 +181,15 @@ mod tests {
};
let key = b"food";
let val = b"some longer text";
let hash = hex::decode("b68f5d298e915ae1753dd333da1f9cf605411a5f2e12516be6758f365e6db265")?;
ensure!(hash == apply_leaf(&leaf, key, val)?, "unexpected leaf hash");
Ok(())
let hash = decode("b68f5d298e915ae1753dd333da1f9cf605411a5f2e12516be6758f365e6db265");
assert!(
hash == apply_leaf(&leaf, key, val).unwrap(),
"unexpected leaf hash"
);
}

#[test]
fn apply_leaf_prehash_length() -> Result<()> {
fn apply_leaf_prehash_length() {
let leaf = LeafOp {
hash: HashOp::Sha256.into(),
prehash_key: 0,
Expand All @@ -199,51 +199,49 @@ mod tests {
};
let key = b"food";
let val = b"yet another long string";
let hash = hex::decode("87e0483e8fb624aef2e2f7b13f4166cda485baa8e39f437c83d74c94bedb148f")?;
ensure!(hash == apply_leaf(&leaf, key, val)?, "unexpected leaf hash");
Ok(())
let hash = decode("87e0483e8fb624aef2e2f7b13f4166cda485baa8e39f437c83d74c94bedb148f");
assert!(
hash == apply_leaf(&leaf, key, val).unwrap(),
"unexpected leaf hash"
);
}

#[test]
fn apply_inner_prefix_suffix() -> Result<()> {
fn apply_inner_prefix_suffix() {
let inner = InnerOp {
hash: HashOp::Sha256.into(),
prefix: hex::decode("0123456789")?,
suffix: hex::decode("deadbeef")?,
prefix: decode("0123456789"),
suffix: decode("deadbeef"),
};
let child = hex::decode("00cafe00")?;
let child = decode("00cafe00");

// echo -n 012345678900cafe00deadbeef | xxd -r -p | sha256sum
let expected =
hex::decode("0339f76086684506a6d42a60da4b5a719febd4d96d8b8d85ae92849e3a849a5e")?;
ensure!(
expected == apply_inner(&inner, &child)?,
let expected = decode("0339f76086684506a6d42a60da4b5a719febd4d96d8b8d85ae92849e3a849a5e");
assert!(
expected == apply_inner(&inner, &child).unwrap(),
"unexpected inner hash"
);
Ok(())
}

#[test]
fn apply_inner_prefix_only() -> Result<()> {
fn apply_inner_prefix_only() {
let inner = InnerOp {
hash: HashOp::Sha256.into(),
prefix: hex::decode("00204080a0c0e0")?,
prefix: decode("00204080a0c0e0"),
suffix: vec![],
};
let child = hex::decode("ffccbb997755331100")?;
let child = decode("ffccbb997755331100");

// echo -n 00204080a0c0e0ffccbb997755331100 | xxd -r -p | sha256sum
let expected =
hex::decode("45bece1678cf2e9f4f2ae033e546fc35a2081b2415edcb13121a0e908dca1927")?;
ensure!(
expected == apply_inner(&inner, &child)?,
let expected = decode("45bece1678cf2e9f4f2ae033e546fc35a2081b2415edcb13121a0e908dca1927");
assert!(
expected == apply_inner(&inner, &child).unwrap(),
"unexpected inner hash"
);
Ok(())
}

#[test]
fn apply_inner_suffix_only() -> Result<()> {
fn apply_inner_suffix_only() {
let inner = InnerOp {
hash: HashOp::Sha256.into(),
prefix: vec![],
Expand All @@ -252,12 +250,10 @@ mod tests {
let child = b"this is a sha256 hash, really....".to_vec();

// echo -n 'this is a sha256 hash, really.... just kidding!' | sha256sum
let expected =
hex::decode("79ef671d27e42a53fba2201c1bbc529a099af578ee8a38df140795db0ae2184b")?;
ensure!(
expected == apply_inner(&inner, &child)?,
let expected = decode("79ef671d27e42a53fba2201c1bbc529a099af578ee8a38df140795db0ae2184b");
assert!(
expected == apply_inner(&inner, &child).unwrap(),
"unexpected inner hash"
);
Ok(())
}
}
12 changes: 6 additions & 6 deletions rust/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ mod tests {
proof: ExistenceProof {
key: b"foo".to_vec(),
value: b"bar".to_vec(),
leaf: Some(invalid_leaf.clone()),
leaf: Some(invalid_leaf),
path: vec![],
},
spec: api::iavl_spec(),
Expand Down Expand Up @@ -440,7 +440,7 @@ mod tests {
key: b"foo".to_vec(),
value: b"bar".to_vec(),
leaf: Some(leaf.clone()),
path: vec![invalid_inner.clone()],
path: vec![invalid_inner],
},
spec: api::iavl_spec(),
valid: false,
Expand All @@ -453,7 +453,7 @@ mod tests {
key: b"foo".to_vec(),
value: b"bar".to_vec(),
leaf: Some(leaf.clone()),
path: vec![invalid_inner_hash.clone()],
path: vec![invalid_inner_hash],
},
spec: api::iavl_spec(),
valid: false,
Expand Down Expand Up @@ -495,16 +495,16 @@ mod tests {
proof: ExistenceProof {
key: b"foo".to_vec(),
value: b"bar".to_vec(),
leaf: Some(leaf.clone()),
leaf: Some(leaf),
path: vec![
valid_inner.clone(),
valid_inner.clone(),
valid_inner.clone(),
valid_inner.clone(),
valid_inner.clone(),
valid_inner,
],
},
spec: depth_limited_spec.clone(),
spec: depth_limited_spec,
valid: false,
},
),
Expand Down

0 comments on commit 46f2126

Please sign in to comment.