Skip to content

Commit

Permalink
test(serde): add serde_json tests
Browse files Browse the repository at this point in the history
Signed-off-by: becominginsane <francesco@zondax.ch>
  • Loading branch information
Karrq committed Mar 18, 2021
1 parent 0663da1 commit 0d2be05
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ sha2 = "0.9.3"
sha3 = "0.9.1"
bincode = "1.3.2"
criterion = "0.3.4"
serde_json = "1.0.64"

[[bench]]
name = "schnorr_benchmarks"
Expand Down
47 changes: 47 additions & 0 deletions src/serdey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ mod test {
use std::vec::Vec;

use bincode::{serialize, serialized_size, deserialize};
use serde_json::{to_value, from_value, to_string, from_str};

use curve25519_dalek::ristretto::{CompressedRistretto};

Expand Down Expand Up @@ -92,6 +93,21 @@ mod test {
assert_eq!(signature, decoded_signature);
}

#[test]
fn serialize_deserialize_signature_json() {
let signature: Signature = Signature::from_bytes(&SIGNATURE_BYTES).unwrap();

let encoded_signature = to_value(&signature).unwrap();
let decoded_signature: Signature = from_value(encoded_signature).unwrap();

assert_eq!(signature, decoded_signature);

let encoded_signature = to_string(&signature).unwrap();
let decoded_signature: Signature = from_str(&encoded_signature).unwrap();

assert_eq!(signature, decoded_signature);
}

#[test]
fn serialize_deserialize_public_key() {
let public_key = PublicKey::from_compressed(COMPRESSED_PUBLIC_KEY).unwrap();
Expand All @@ -101,6 +117,20 @@ mod test {
assert_eq!(public_key, decoded_public_key);
}

#[test]
fn serialize_deserialize_public_key_json() {
let public_key = PublicKey::from_compressed(COMPRESSED_PUBLIC_KEY).unwrap();
let encoded_public_key = to_value(&public_key).unwrap();
let decoded_public_key: PublicKey = from_value(encoded_public_key).unwrap();

assert_eq!(public_key, decoded_public_key);

let encoded_public_key = to_string(&public_key).unwrap();
let decoded_public_key: PublicKey = from_str(&encoded_public_key).unwrap();

assert_eq!(public_key, decoded_public_key);
}

/*
TODO: Actually test serde on real secret key, not just mini one.
fn serialize_deserialize_secret_key() {
Expand All @@ -123,6 +153,23 @@ mod test {
}
}

#[test]
fn serialize_deserialize_mini_secret_key_json() {
let encoded_secret_key = to_value(&ED25519_SECRET_KEY).unwrap();
let decoded_secret_key: MiniSecretKey = from_value(encoded_secret_key).unwrap();

for i in 0..32 {
assert_eq!(ED25519_SECRET_KEY.0[i], decoded_secret_key.0[i]);
}

let encoded_secret_key = to_string(&ED25519_SECRET_KEY).unwrap();
let decoded_secret_key: MiniSecretKey = from_str(&encoded_secret_key).unwrap();

for i in 0..32 {
assert_eq!(ED25519_SECRET_KEY.0[i], decoded_secret_key.0[i]);
}
}

#[test]
fn serialize_public_key_size() {
let public_key = PublicKey::from_compressed(COMPRESSED_PUBLIC_KEY).unwrap();
Expand Down

0 comments on commit 0d2be05

Please sign in to comment.