use super::Signer;
#[test]
fn hmac_sha256_rfc4231_test_case_1() {
let key = [0x0bu8; 20];
let data = b"Hi There";
let result = Signer::hmac_sha256(&key, data);
assert_eq!(result.len(), 64);
assert_eq!(
result,
"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"
);
}
#[test]
fn hmac_sha256_rfc4231_test_case_2() {
let result = Signer::hmac_sha256(b"Jefe", b"what do ya want for nothing?");
assert_eq!(
result,
"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"
);
}
#[test]
fn hmac_sha256_deterministic() {
let a = Signer::hmac_sha256(b"key", b"data");
let b = Signer::hmac_sha256(b"key", b"data");
assert_eq!(a, b);
}
#[test]
fn hmac_sha256_different_secret_different_result() {
let a = Signer::hmac_sha256(b"secret1", b"data");
let b = Signer::hmac_sha256(b"secret2", b"data");
assert_ne!(a, b);
}
#[test]
fn hmac_sha512_rfc4231_test_case_1() {
let key = [0x0bu8; 20];
let data = b"Hi There";
let result = Signer::hmac_sha512(&key, data);
assert_eq!(result.len(), 128);
assert_eq!(
result,
"87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854"
);
}
#[test]
fn base64_encode_decode_roundtrip() {
let original = b"Hello, World!";
let encoded = Signer::base64_encode(original);
let decoded = Signer::base64_decode(&encoded).unwrap();
assert_eq!(decoded, original);
}
#[test]
fn base64_encode_known_values() {
assert_eq!(Signer::base64_encode(b"hello"), "aGVsbG8=");
assert_eq!(Signer::base64_encode(b""), "");
}
#[test]
fn base64_decode_invalid_input_errors() {
let result = Signer::base64_decode("!!!not-base64!!!");
assert!(result.is_err());
}
#[test]
fn verify_hmac_sha256_valid_signature_returns_true() {
let secret = b"my-secret-key";
let data = b"request-body";
let sig = Signer::hmac_sha256(secret, data);
assert!(Signer::verify_hmac_sha256(secret, data, &sig));
}
#[test]
fn verify_hmac_sha256_invalid_signature_returns_false() {
let secret = b"my-secret-key";
let data = b"request-body";
let tampered = "0".repeat(64);
assert!(!Signer::verify_hmac_sha256(secret, data, &tampered));
}
#[test]
fn verify_hmac_sha256_wrong_length_signature_returns_false() {
let secret = b"my-secret-key";
let data = b"request-body";
assert!(!Signer::verify_hmac_sha256(secret, data, "tooshort"));
assert!(!Signer::verify_hmac_sha256(secret, data, ""));
}
#[test]
fn verify_hmac_sha256_wrong_secret_returns_false() {
let sig = Signer::hmac_sha256(b"secret-a", b"data");
assert!(!Signer::verify_hmac_sha256(b"secret-b", b"data", &sig));
}
#[test]
fn verify_hmac_sha256_wrong_data_returns_false() {
let sig = Signer::hmac_sha256(b"secret", b"data-a");
assert!(!Signer::verify_hmac_sha256(b"secret", b"data-b", &sig));
}
#[test]
fn verify_hmac_sha256_case_sensitive() {
let sig = Signer::hmac_sha256(b"secret", b"data");
let upper = sig.to_uppercase();
assert!(!Signer::verify_hmac_sha256(b"secret", b"data", &upper));
}
#[test]
fn verify_hmac_sha256_empty_inputs_boundary() {
let sig = Signer::hmac_sha256(b"", b"");
assert!(Signer::verify_hmac_sha256(b"", b"", &sig));
}
#[test]
fn verify_hmac_sha256_constant_time_no_early_return() {
use std::time::Instant;
let secret = b"timing-test-secret";
let data = b"timing-test-data";
let valid_sig = Signer::hmac_sha256(secret, data);
let mut invalid_sig = valid_sig.clone();
invalid_sig.replace_range(0..1, if &valid_sig[0..1] == "0" { "1" } else { "0" });
const BLOCKS: usize = 20;
const BLOCK_SIZE: usize = 1000;
for _ in 0..100 {
let _ = Signer::verify_hmac_sha256(secret, data, &valid_sig);
let _ = Signer::verify_hmac_sha256(secret, data, &invalid_sig);
}
let mut valid_nanos: u128 = 0;
let mut invalid_nanos: u128 = 0;
for _ in 0..BLOCKS {
let start = Instant::now();
for _ in 0..BLOCK_SIZE {
let _ = Signer::verify_hmac_sha256(secret, data, &valid_sig);
}
valid_nanos += start.elapsed().as_nanos();
let start = Instant::now();
for _ in 0..BLOCK_SIZE {
let _ = Signer::verify_hmac_sha256(secret, data, &invalid_sig);
}
invalid_nanos += start.elapsed().as_nanos();
}
let ratio = if invalid_nanos < valid_nanos {
valid_nanos as f64 / invalid_nanos.max(1) as f64
} else {
invalid_nanos as f64 / valid_nanos.max(1) as f64
};
assert!(
ratio < 4.0,
"时序差异过大 ratio={:.2}, valid_ns={}, invalid_ns={}(常量时间比较失败)",
ratio,
valid_nanos,
invalid_nanos
);
}
#[test]
fn signer_implements_default() {
let _signer: Signer = Default::default();
}
#[test]
fn deep02_signer_has_no_manual_constant_time_fallback() {
let source = include_str!("signer.rs");
let code_only: String = source
.lines()
.filter(|line| {
let trimmed = line.trim_start();
!(trimmed.starts_with("//!") || trimmed.starts_with("///") || trimmed.starts_with("//"))
})
.collect::<Vec<_>>()
.join("\n");
assert!(
!code_only.contains("constant_time_eq_manual"),
"DEEP-02: signer.rs 不应再包含 constant_time_eq_manual 手动 fallback(死代码)"
);
assert!(
!code_only.contains("#[cfg(not(feature = \"subtle\"))]"),
"DEEP-02: signer.rs 不应再有 not(subtle) 分支(secure-sign 恒启用 subtle)"
);
}