From ed52ff1a5165007776d92d8ce64c7b920c89a3ef Mon Sep 17 00:00:00 2001 From: Demilade Sonuga Date: Wed, 27 Nov 2024 17:06:42 +0100 Subject: [PATCH] Added tests for the serde implementations --- tests/keys.rs | 30 +++++++++++++++++++++++------- tests/signature.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/tests/keys.rs b/tests/keys.rs index 9f7ab81..74febf6 100644 --- a/tests/keys.rs +++ b/tests/keys.rs @@ -61,11 +61,27 @@ fn apk_identity_fails() { } #[cfg(feature = "serde")] -#[test] -fn pk_serde() { - let mut rng = StdRng::seed_from_u64(42); - let pk = PublicKey::from(&SecretKey::random(&mut rng)); - let ser = serde_json::to_string(&pk); - let deser = serde_json::from_str(&ser.unwrap()); - assert_eq!(pk, deser.unwrap()); +mod serde_support { + use super::*; + + #[test] + fn pk() { + let mut rng = StdRng::seed_from_u64(42); + let pk = PublicKey::from(&SecretKey::random(&mut rng)); + let ser = serde_json::to_string(&pk); + let deser = serde_json::from_str(&ser.unwrap()); + assert_eq!(pk, deser.unwrap()); + } + + #[test] + fn multisig_pk() { + let mut rng = StdRng::seed_from_u64(42); + let pk = MultisigPublicKey::aggregate(&[PublicKey::from( + &SecretKey::random(&mut rng), + )]) + .unwrap(); + let ser = serde_json::to_string(&pk); + let deser = serde_json::from_str(&ser.unwrap()); + assert_eq!(pk, deser.unwrap()); + } } diff --git a/tests/signature.rs b/tests/signature.rs index f56a255..0c36490 100644 --- a/tests/signature.rs +++ b/tests/signature.rs @@ -194,3 +194,29 @@ fn random_message(rng: &mut StdRng) -> [u8; 100] { msg } + +#[cfg(feature = "serde")] +mod serde_support { + use super::*; + + #[test] + fn signature() { + let mut rng = StdRng::seed_from_u64(42); + let sk = SecretKey::random(&mut rng); + let signature = sk.sign(b"a message"); + let ser = serde_json::to_string(&signature); + let deser = serde_json::from_str(&ser.unwrap()); + assert_eq!(signature, deser.unwrap()); + } + + #[test] + fn multisig_signature() { + let mut rng = StdRng::seed_from_u64(42); + let sk = SecretKey::random(&mut rng); + let pk = PublicKey::from(&sk); + let signature = sk.sign_multisig(&pk, b"a message"); + let ser = serde_json::to_string(&signature); + let deser = serde_json::from_str(&ser.unwrap()); + assert_eq!(signature, deser.unwrap()); + } +}