Skip to content

Commit

Permalink
rewrote serde_json tests to use serde_test
Browse files Browse the repository at this point in the history
  • Loading branch information
LWEdslev committed Mar 22, 2024
1 parent 80b8eff commit d3ca866
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 82 deletions.
24 changes: 0 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ base64ct = { version = "1", features = ["alloc"] }
hex-literal = "0.4.1"
proptest = "1"
serde_test = "1.0.89"
serde_json = "1.0.114"
rand_xorshift = "0.3"
rand_chacha = "0.3"
rand = "0.8"
Expand Down
35 changes: 25 additions & 10 deletions src/oaep/decrypting_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,43 @@ where
{
}

impl<D, MGD> PartialEq for DecryptingKey<D, MGD>
where
D: Digest,
MGD: Digest + FixedOutputReset,
{
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner && self.label == other.label
}
}

#[cfg(test)]
mod tests {
#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use super::*;
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use serde_test::{assert_tokens, Configure, Token};
use sha2::Sha256;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let decrypting_key = DecryptingKey::<Sha256>::new_with_label(
let decrypting_key = DecryptingKey::<Sha256>::new(
RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key"),
"label",
);

let ser_decrypting_key =
serde_json::to_string(&decrypting_key).expect("unable to serialize decrypting key");
let deser_decrypting_key =
serde_json::from_str::<DecryptingKey<Sha256>>(&ser_decrypting_key)
.expect("unable to serialize decrypting key");

assert_eq!(decrypting_key.label, deser_decrypting_key.label);
assert_eq!(decrypting_key.inner, deser_decrypting_key.inner);
let tokens = [
Token::Struct { name: "DecryptingKey", len: 4 },
Token::Str("inner"),
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc"),
Token::Str("label"),
Token::None,
Token::Str("phantom"),
Token::UnitStruct { name: "PhantomData", },
Token::Str("mg_phantom"),
Token::UnitStruct { name: "PhantomData", },
Token::StructEnd,
];
assert_tokens(&decrypting_key.readable(), &tokens);
}
}
50 changes: 34 additions & 16 deletions src/oaep/encrypting_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use alloc::{
string::{String, ToString},
vec::Vec,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use core::marker::PhantomData;
use digest::{Digest, FixedOutputReset};
use rand_core::CryptoRngCore;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Encryption key for PKCS#1 v1.5 encryption as described in [RFC8017 § 7.1].
///
Expand Down Expand Up @@ -66,29 +66,47 @@ where
}
}

impl<D, MGD> PartialEq for EncryptingKey<D, MGD>
where
D: Digest,
MGD: Digest + FixedOutputReset,
{
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner && self.label == other.label
}
}

#[cfg(test)]
mod tests {

#[test]
#[cfg(feature = "serde")]
fn test_serde() {
use super::*;
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use sha2::Sha256;

use crate::RsaPrivateKey;
use serde_test::{assert_tokens, Configure, Token};

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let encrypting_key = EncryptingKey::<Sha256>::new_with_label(
RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key").to_public_key(),
"label",
);

let ser_encrypting_key =
serde_json::to_string(&encrypting_key).expect("unable to serialize encrypting key");
let deser_encrypting_key = serde_json::from_str::<EncryptingKey<Sha256>>(&ser_encrypting_key)
.expect("unable to serialize encrypting key");
let priv_key = crate::RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key");
let encrypting_key = EncryptingKey::<sha2::Sha256>::new(priv_key.to_public_key());

assert_eq!(encrypting_key.label, deser_encrypting_key.label);
assert_eq!(encrypting_key.inner, deser_encrypting_key.inner);
let tokens = [
Token::Struct {
name: "EncryptingKey",
len: 4,
},
Token::Str("inner"),
Token::Str(
"3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001",
),
Token::Str("label"),
Token::None,
Token::Str("phantom"),
Token::UnitStruct { name: "PhantomData", },
Token::Str("mg_phantom"),
Token::UnitStruct { name: "PhantomData", },
Token::StructEnd,
];
assert_tokens(&encrypting_key.readable(), &tokens);
}
}
2 changes: 1 addition & 1 deletion src/pkcs1v15/decrypting_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ mod tests {
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc"),
Token::StructEnd,
];
assert_tokens(&decrypting_key.clone().readable(), &tokens);
assert_tokens(&decrypting_key.readable(), &tokens);
}
}
17 changes: 12 additions & 5 deletions src/pkcs1v15/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ where

impl<D> ZeroizeOnDrop for SigningKey<D> where D: Digest {}

impl<D> PartialEq for SigningKey<D> where D: Digest {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner && self.prefix == other.prefix
}
}

#[cfg(feature = "serde")]
impl<D> Serialize for SigningKey<D>
where
Expand Down Expand Up @@ -297,16 +303,17 @@ mod tests {
fn test_serde() {
use super::*;
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use serde_test::{assert_tokens, Configure, Token};
use sha2::Sha256;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let priv_key = crate::RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key");
let signing_key = SigningKey::<Sha256>::new(priv_key);

let ser_signing_key = serde_json::to_string(&signing_key).expect("unable to serialize signing key");
let deser_signing_key = serde_json::from_str::<SigningKey<Sha256>>(&ser_signing_key).expect("unable to deserialize signing key");

assert_eq!(signing_key.inner, deser_signing_key.inner);
assert_eq!(signing_key.prefix, deser_signing_key.prefix);
let tokens = [
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc")
];

assert_tokens(&signing_key.readable(), &tokens);
}
}
20 changes: 14 additions & 6 deletions src/pkcs1v15/verifying_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ where
}
}

impl<D> PartialEq for VerifyingKey<D>
where
D: Digest,
{
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner && self.prefix == other.prefix
}
}

#[cfg(feature = "serde")]
impl<D> Serialize for VerifyingKey<D>
where
Expand Down Expand Up @@ -247,19 +256,18 @@ mod tests {
fn test_serde() {
use super::*;
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use serde_test::{assert_tokens, Configure, Token};
use sha2::Sha256;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let priv_key = crate::RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key");
let pub_key = priv_key.to_public_key();
let verifying_key = VerifyingKey::<Sha256>::new(pub_key);

let ser_verifying_key =
serde_json::to_string(&verifying_key).expect("unable to serialize verifying key");
let deser_verifying_key = serde_json::from_str::<VerifyingKey<Sha256>>(&ser_verifying_key)
.expect("unable to deserialize verifying key");
let tokens = [
Token::Str("3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001")
];

assert_eq!(verifying_key.inner, deser_verifying_key.inner);
assert_eq!(verifying_key.prefix, deser_verifying_key.prefix);
assert_tokens(&verifying_key.readable(), &tokens);
}
}
25 changes: 18 additions & 7 deletions src/pss/blinded_signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ where

impl<D> ZeroizeOnDrop for BlindedSigningKey<D> where D: Digest {}

impl<D> PartialEq for BlindedSigningKey<D>
where
D: Digest,
{
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner && self.salt_len == other.salt_len
}
}

#[cfg(feature = "serde")]
impl<D> Serialize for BlindedSigningKey<D>
where
Expand Down Expand Up @@ -242,15 +251,17 @@ mod tests {
fn test_serde() {
use super::*;
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use serde_test::{assert_tokens, Configure, Token};
use sha2::Sha256;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let signing_key = BlindedSigningKey::<Sha256>::new(RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key"));
let signing_key = BlindedSigningKey::<Sha256>::new(
RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key"),
);

let ser_signing_key = serde_json::to_string(&signing_key).expect("unable to serialize signing key");
let deser_signing_key = serde_json::from_str::<BlindedSigningKey<Sha256>>(&ser_signing_key).expect("unable to deserialize signing key");

assert_eq!(signing_key.inner, deser_signing_key.inner);
assert_eq!(signing_key.salt_len, deser_signing_key.salt_len);
let tokens = [
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc")
];
assert_tokens(&signing_key.readable(), &tokens);
}
}
}
22 changes: 16 additions & 6 deletions src/pss/signing_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ where

impl<D> ZeroizeOnDrop for SigningKey<D> where D: Digest {}

impl<D> PartialEq for SigningKey<D>
where
D: Digest,
{
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner && self.salt_len == other.salt_len
}
}

#[cfg(feature = "serde")]
impl<D> Serialize for SigningKey<D>
where
Expand Down Expand Up @@ -265,16 +274,17 @@ mod tests {
fn test_serde() {
use super::*;
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use serde_test::{assert_tokens, Configure, Token};
use sha2::Sha256;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let priv_key = crate::RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key");
let signing_key = SigningKey::<Sha256>::new(priv_key);

let ser_signing_key = serde_json::to_string(&signing_key).expect("unable to serialize signing key");
let deser_signing_key = serde_json::from_str::<SigningKey<Sha256>>(&ser_signing_key).expect("unable to deserialize signing key");

assert_eq!(signing_key.inner, deser_signing_key.inner);
assert_eq!(signing_key.salt_len, deser_signing_key.salt_len);
let tokens = [
Token::Str("3054020100300d06092a864886f70d01010105000440303e020100020900cc6c6130e35b46bf0203010001020863de1ac858580019020500f65cff5d020500d46b68cb02046d9a09f102047b4e3a4f020500f45065cc")
];

assert_tokens(&signing_key.readable(), &tokens);
}
}
}
20 changes: 14 additions & 6 deletions src/pss/verifying_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ where
}
}

impl<D> PartialEq for VerifyingKey<D>
where
D: Digest,
{
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner && self.salt_len == other.salt_len
}
}

#[cfg(feature = "serde")]
impl<D> Serialize for VerifyingKey<D>
where
Expand Down Expand Up @@ -201,19 +210,18 @@ mod tests {
fn test_serde() {
use super::*;
use rand_chacha::{rand_core::SeedableRng, ChaCha8Rng};
use serde_test::{assert_tokens, Configure, Token};
use sha2::Sha256;

let mut rng = ChaCha8Rng::from_seed([42; 32]);
let priv_key = crate::RsaPrivateKey::new(&mut rng, 64).expect("failed to generate key");
let pub_key = priv_key.to_public_key();
let verifying_key = VerifyingKey::<Sha256>::new(pub_key);

let ser_verifying_key =
serde_json::to_string(&verifying_key).expect("unable to serialize verifying key");
let deser_verifying_key = serde_json::from_str::<VerifyingKey<Sha256>>(&ser_verifying_key)
.expect("unable to deserialize verifying key");
let tokens = [
Token::Str("3024300d06092a864886f70d01010105000313003010020900cc6c6130e35b46bf0203010001")
];

assert_eq!(verifying_key.inner, deser_verifying_key.inner);
assert_eq!(verifying_key.salt_len, deser_verifying_key.salt_len);
assert_tokens(&verifying_key.readable(), &tokens);
}
}

0 comments on commit d3ca866

Please sign in to comment.