mirror of
https://github.com/mii443/merkle.rs.git
synced 2025-08-23 00:15:31 +00:00
Serde tests and minor fix
* Got rid of the double reference in the serialize function. * Added tests for mod algorithm_serde. Had to place them where they are because the module is private. We could reorganize public/ privateness of modules so they could be in the tests module though if preferred. * Ran rustfmt so Travis will pass.
This commit is contained in:
committed by
Romain Ruetschi
parent
31e26341c9
commit
cde4a6941d
@ -1,6 +1,8 @@
|
||||
#![deny(missing_docs, unused_qualifications, missing_debug_implementations,
|
||||
missing_copy_implementations, trivial_casts, trivial_numeric_casts, unsafe_code,
|
||||
unstable_features, unused_import_braces)]
|
||||
#![deny(
|
||||
missing_docs, unused_qualifications, missing_debug_implementations,
|
||||
missing_copy_implementations, trivial_casts, trivial_numeric_casts, unsafe_code,
|
||||
unstable_features, unused_import_braces
|
||||
)]
|
||||
|
||||
//! *merkle* implements a Merkle Tree in Rust.
|
||||
|
||||
|
46
src/proof.rs
46
src/proof.rs
@ -32,7 +32,7 @@ mod algorithm_serde {
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
pub fn serialize<S: Serializer>(
|
||||
algorithm: &&'static Algorithm,
|
||||
algorithm: &'static Algorithm,
|
||||
se: S,
|
||||
) -> Result<S::Ok, S::Error> {
|
||||
// The `Debug` implementation of `Algorithm` prints its ID.
|
||||
@ -49,6 +49,50 @@ mod algorithm_serde {
|
||||
_ => Err(D::Error::custom("unknown hash algorithm")),
|
||||
}
|
||||
}
|
||||
|
||||
mod test {
|
||||
use super::*;
|
||||
use ring::digest::{
|
||||
SHA1 as sha1, SHA256 as sha256, SHA384 as sha384, SHA512 as sha512,
|
||||
SHA512_256 as sha512_256,
|
||||
};
|
||||
|
||||
static SHA1: &Algorithm = &sha1;
|
||||
static SHA256: &Algorithm = &sha256;
|
||||
static SHA384: &Algorithm = &sha384;
|
||||
static SHA512: &Algorithm = &sha512;
|
||||
static SHA512_256: &Algorithm = &sha512_256;
|
||||
|
||||
#[test]
|
||||
fn test_serialize_known_algorithms() {
|
||||
extern crate serde_json;
|
||||
|
||||
for alg in [SHA1, SHA256, SHA384, SHA512, SHA512_256].iter() {
|
||||
let mut serializer = serde_json::Serializer::with_formatter(
|
||||
vec![],
|
||||
serde_json::ser::PrettyFormatter::new(),
|
||||
);
|
||||
|
||||
let _ = serialize(alg, &mut serializer).expect(&format!("{:?}", alg));
|
||||
let alg_ = deserialize(&mut serde_json::Deserializer::from_slice(
|
||||
&serializer.into_inner()[..],
|
||||
)).expect(&format!("{:?}", alg));
|
||||
|
||||
assert_eq!(*alg, alg_);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "unknown hash algorithm")]
|
||||
fn test_serialize_unknown_algorithm() {
|
||||
extern crate serde_json;
|
||||
{
|
||||
let alg_str = "\"BLAKE2b\"";
|
||||
let mut deserializer = serde_json::Deserializer::from_str(alg_str);
|
||||
let _ = deserialize(&mut deserializer).expect(&format!("{:?}", alg_str));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq> PartialEq for Proof<T> {
|
||||
|
@ -5,9 +5,9 @@ use ring::digest::Algorithm;
|
||||
pub use self::proof::{LemmaProto, ProofProto};
|
||||
use proof::{Lemma, Positioned, Proof};
|
||||
|
||||
use protobuf::Message;
|
||||
use protobuf::error::ProtobufResult;
|
||||
use protobuf::parse_from_bytes;
|
||||
use protobuf::Message;
|
||||
|
||||
impl<T> Proof<T> {
|
||||
/// Constructs a `Proof` struct from its Protobuf representation.
|
||||
|
@ -1,5 +1,8 @@
|
||||
#![cfg(test)]
|
||||
|
||||
#[cfg(feature = "serialization-serde")]
|
||||
extern crate serde_json;
|
||||
|
||||
use ring::digest::{Algorithm, Context, SHA512};
|
||||
|
||||
use hashutils::{HashUtils, Hashable};
|
||||
@ -275,12 +278,12 @@ fn test_custom_hashable_impl() {
|
||||
#[cfg(feature = "serialization-serde")]
|
||||
#[test]
|
||||
fn test_serialize_proof_with_serde() {
|
||||
extern crate serde_json;
|
||||
|
||||
let values = (1..10).map(|x| vec![x]).collect::<Vec<_>>();
|
||||
let tree = MerkleTree::from_vec(digest, values);
|
||||
let proof = tree.gen_proof(vec![5]);
|
||||
|
||||
let serialized = serde_json::to_string(&proof).expect("serialize proof");
|
||||
|
||||
assert_eq!(
|
||||
proof,
|
||||
serde_json::from_str(&serialized).expect("deserialize proof")
|
||||
|
Reference in New Issue
Block a user