From db88376971fe6ddda569004c426dc8a022e04629 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Fri, 17 Sep 2021 08:22:24 -0600 Subject: [PATCH] sha-crypt: test cleanup (#241) Simplify test vectors --- sha-crypt/tests/{mod.rs => lib.rs} | 53 ++++++++++++++---------------- 1 file changed, 25 insertions(+), 28 deletions(-) rename sha-crypt/tests/{mod.rs => lib.rs} (69%) diff --git a/sha-crypt/tests/mod.rs b/sha-crypt/tests/lib.rs similarity index 69% rename from sha-crypt/tests/mod.rs rename to sha-crypt/tests/lib.rs index 24924139..c2873455 100644 --- a/sha-crypt/tests/mod.rs +++ b/sha-crypt/tests/lib.rs @@ -3,43 +3,40 @@ use sha_crypt::{sha512_crypt_b64, Sha512Params, ROUNDS_MAX, ROUNDS_MIN}; #[cfg(feature = "simple")] use sha_crypt::{sha512_check, sha512_simple}; -struct TestCrypt { +struct TestVector { input: &'static str, salt: &'static str, result: &'static str, rounds: usize, } -fn tests_sha512_crypt() -> Vec { - vec![ - TestCrypt { - input: "Hello world!", - salt: "saltstring", - result: - "svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1", - rounds: 5_000, - }, - TestCrypt { - input: "Hello world!", - salt: "saltstringsaltstring", - result: - "OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v.", - rounds: 10_000, - }, - TestCrypt { - input: "This is just a test", - salt: "toolongsaltstring", - result: - "lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0", - rounds: 5_000, - }, - ] -} +const TEST_VECTORS: &[TestVector] = &[ + TestVector { + input: "Hello world!", + salt: "saltstring", + result: + "svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1", + rounds: 5_000, + }, + TestVector { + input: "Hello world!", + salt: "saltstringsaltstring", + result: + "OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sbHbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v.", + rounds: 10_000, + }, + TestVector { + input: "This is just a test", + salt: "toolongsaltstring", + result: + "lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0", + rounds: 5_000, + }, +]; #[test] fn test_sha512_crypt() { - let tests = tests_sha512_crypt(); - for t in tests.iter() { + for t in TEST_VECTORS { let params = Sha512Params::new(t.rounds).expect("Rounds error"); let result = sha512_crypt_b64(t.input.as_bytes(), t.salt.as_bytes(), ¶ms).unwrap(); assert!(result == t.result);