Move HKDF functional tests to tests/.

This commit is contained in:
Brian Smith 2017-06-08 11:51:49 -10:00
parent c035ea67e1
commit 5884ed3149
4 changed files with 45 additions and 33 deletions

View File

@ -95,7 +95,6 @@ include = [
"src/error.rs",
"src/limb.rs",
"src/hkdf.rs",
"src/hkdf_tests.txt",
"src/hmac.rs",
"src/hmac_generate_serializable_tests.txt",
"src/hmac_tests.txt",
@ -243,6 +242,8 @@ include = [
"tests/ed25519_tests.txt",
"tests/ed25519_from_pkcs8_tests.txt",
"tests/ed25519_from_pkcs8_unchecked_tests.txt",
"tests/hkdf_tests.rs",
"tests/hkdf_tests.txt",
"tests/rsa_from_pkcs8_tests.txt",
"tests/rsa_pkcs1_sign_tests.txt",
"tests/rsa_pkcs1_verify_tests.txt",

View File

@ -129,35 +129,3 @@ pub fn expand(prk: &hmac::SigningKey, info: &[u8], out: &mut [u8]) {
n += 1;
}
}
#[cfg(test)]
mod tests {
use super::*;
use {error, hmac, test};
#[test]
pub fn hkdf_tests() {
test::from_file("src/hkdf_tests.txt", |section, test_case| {
assert_eq!(section, "");
let digest_alg =
test_case.consume_digest_alg("Hash").ok_or(error::Unspecified)?;
let secret = test_case.consume_bytes("IKM");
let salt = test_case.consume_bytes("salt");
let info = test_case.consume_bytes("info");
// The PRK is an intermediate value that we can't test, but we
// have to consume it to make test::from_file happy.
let _ = test_case.consume_bytes("PRK");
let expected_out = test_case.consume_bytes("OKM");
let salt = hmac::SigningKey::new(digest_alg, &salt);
let mut out = vec![0u8; expected_out.len()];
extract_and_expand(&salt, &secret, &info, &mut out);
assert_eq!(out, expected_out);
Ok(())
});
}
}

43
tests/hkdf_tests.rs Normal file
View File

@ -0,0 +1,43 @@
// Copyright 2015 Brian Smith.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
extern crate ring;
use ring::{error, hkdf, hmac, test};
#[test]
fn hkdf_tests() {
test::from_file("tests/hkdf_tests.txt", |section, test_case| {
assert_eq!(section, "");
let digest_alg =
test_case.consume_digest_alg("Hash").ok_or(error::Unspecified)?;
let secret = test_case.consume_bytes("IKM");
let salt = test_case.consume_bytes("salt");
let info = test_case.consume_bytes("info");
// The PRK is an intermediate value that we can't test, but we
// have to consume it to make test::from_file happy.
let _ = test_case.consume_bytes("PRK");
let expected_out = test_case.consume_bytes("OKM");
let salt = hmac::SigningKey::new(digest_alg, &salt);
let mut out = vec![0u8; expected_out.len()];
hkdf::extract_and_expand(&salt, &secret, &info, &mut out);
assert_eq!(out, expected_out);
Ok(())
});
}