Skip to content

Commit

Permalink
chain-crypto: cover happy path and corner cases for serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-babichenko committed Sep 16, 2021
1 parent 08cf415 commit 613f286
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
18 changes: 15 additions & 3 deletions chain-crypto/src/algorithms/ed25519_extended.rs
Expand Up @@ -88,15 +88,27 @@ mod test {
prop_assert!(keypair_signing_ko(input))
}

#[test]
fn secret_from_binary_correct_size() {
Ed25519Extended::secret_from_binary(&vec![0; EXTENDED_KEY_SIZE]).unwrap();
}

#[test]
fn secret_from_binary_empty_slice() {
assert!(matches!(
Ed25519Extended::secret_from_binary(&[]),
Err(SecretKeyError::SizeInvalid)
))
}

// `secret_from_binary` should fail if the provided byte array does not match the public key size
// the size is limited to 1 MiB to avoid segfaults during testing
#[proptest]
fn secret_from_binary_size_check(#[strategy(..1_048_576usize)] n: usize) {
fn secret_from_binary_size_check(#[strategy(..EXTENDED_KEY_SIZE * 10)] n: usize) {
let secret_key = Ed25519Extended::secret_from_binary(&vec![0; n]);

prop_assert_eq!(
n != EXTENDED_KEY_SIZE,
matches!(secret_key, Err(SecretKeyError::SizeInvalid { .. }))
matches!(secret_key, Err(SecretKeyError::SizeInvalid))
);
}
}
48 changes: 36 additions & 12 deletions chain-crypto/src/algorithms/sumed25519/mod.rs
Expand Up @@ -122,30 +122,54 @@ mod tests {

use proptest::prelude::*;
use test_strategy::proptest;
// `public_from_binary`should fail if the provided byte array does not match the public key size
// the size is limited to 1 MiB to avoid segfaults during testing

#[test]
fn public_from_binary_correct_size() {
SumEd25519_12::public_from_binary(&vec![0; SumEd25519_12::PUBLIC_KEY_SIZE]).unwrap();
}

#[test]
fn public_from_binary_empty_slice() {
assert!(matches!(
SumEd25519_12::public_from_binary(&[]),
Err(PublicKeyError::SizeInvalid)
))
}

// `secret_from_binary` should fail if the provided byte array does not match the public key size
#[proptest]
fn public_from_binary_size_check(#[strategy(..1_048_576usize)] n: usize) {
fn public_from_binary_size_check(#[strategy(..SumEd25519_12::PUBLIC_KEY_SIZE * 10)] n: usize) {
let public_key = SumEd25519_12::public_from_binary(&vec![0; n]);

prop_assert_eq!(
n != SumEd25519_12::PUBLIC_KEY_SIZE,
public_key == Err(PublicKeyError::SizeInvalid)
matches!(public_key, Err(PublicKeyError::SizeInvalid))
);
}

// `signature_from_bytes` should fail if the provided byte array does not match the public key size
// the size is limited to 1 MiB to avoid segfaults during testing
#[test]
fn signature_from_binary_correct_size() {
SumEd25519_12::signature_from_bytes(&vec![0; SumEd25519_12::SIGNATURE_SIZE]).unwrap();
}

#[test]
fn signature_from_binary_empty_slice() {
assert!(matches!(
SumEd25519_12::signature_from_bytes(&[]),
Err(SignatureError::SizeInvalid { .. })
))
}

// `secret_from_binary` should fail if the provided byte array does not match the public key size
#[proptest]
fn signature_from_bytes_size_check(#[strategy(..1_048_576usize)] n: usize) {
let verification_algorithm = SumEd25519_12::signature_from_bytes(&vec![0; n]);
fn signature_from_binary_size_check(
#[strategy(..SumEd25519_12::SIGNATURE_SIZE * 10)] n: usize,
) {
let signature = SumEd25519_12::signature_from_bytes(&vec![0; n]);

prop_assert_eq!(
n != SumEd25519_12::SIGNATURE_SIZE,
matches!(
verification_algorithm,
Err(SignatureError::SizeInvalid { .. })
)
matches!(signature, Err(SignatureError::SizeInvalid { .. }))
);
}
}
18 changes: 15 additions & 3 deletions chain-crypto/src/algorithms/vrf/mod.rs
Expand Up @@ -92,15 +92,27 @@ mod tests {
use proptest::prelude::*;
use test_strategy::proptest;

#[test]
fn secret_from_binary_correct_size() {
RistrettoGroup2HashDh::secret_from_binary(&vec![0; vrf::SecretKey::BYTES_LEN]).unwrap();
}

#[test]
fn secret_from_binary_empty_slice() {
assert!(matches!(
RistrettoGroup2HashDh::secret_from_binary(&[]),
Err(SecretKeyError::SizeInvalid)
))
}

// `secret_from_binary` should fail if the provided byte array does not match the public key size
// the size is limited to 1 MiB to avoid segfaults during testing
#[proptest]
fn secret_from_binary_size_check(#[strategy(..1_048_576usize)] n: usize) {
fn secret_from_binary_size_check(#[strategy(..vrf::SecretKey::BYTES_LEN * 10)] n: usize) {
let secret_key = RistrettoGroup2HashDh::secret_from_binary(&vec![0; n]);

prop_assert_eq!(
n != vrf::SecretKey::BYTES_LEN,
secret_key == Err(SecretKeyError::SizeInvalid)
matches!(secret_key, Err(SecretKeyError::SizeInvalid))
);
}
}

0 comments on commit 613f286

Please sign in to comment.