mirror of
https://github.com/kristoferssolo/tls-pq-bench.git
synced 2026-03-22 00:36:21 +00:00
test: add unit tests for config, errors, and records
This commit is contained in:
@@ -5,6 +5,7 @@ authors.workspace = true
|
||||
edition.workspace = true
|
||||
|
||||
[dependencies]
|
||||
claims.workspace = true
|
||||
miette.workspace = true
|
||||
rcgen.workspace = true
|
||||
rustls.workspace = true
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Common types and utilities for the TLS benchmark harness.
|
||||
//! Common types and utilities for the TLS benchmark harness
|
||||
|
||||
pub mod cert;
|
||||
pub mod error;
|
||||
@@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
use strum::{Display, EnumString};
|
||||
|
||||
/// TLS key exchange mode.
|
||||
/// TLS key exchange mode
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, EnumString, Display)]
|
||||
#[strum(serialize_all = "lowercase")]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
@@ -20,18 +20,18 @@ pub enum KeyExchangeMode {
|
||||
X25519Mlkem768,
|
||||
}
|
||||
|
||||
/// A single benchmark measurement record, output as NDJSON.
|
||||
/// A single benchmark measurement record, output as NDJSON
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BenchRecord {
|
||||
/// Iteration number (0-indexed, excludes warmup).
|
||||
/// Iteration number (0-indexed, excludes warmup)
|
||||
pub iteration: u64,
|
||||
/// Key exchange mode used.
|
||||
/// Key exchange mode used
|
||||
pub mode: KeyExchangeMode,
|
||||
/// Payload size in bytes.
|
||||
/// Payload size in bytes
|
||||
pub payload_bytes: u64,
|
||||
/// Handshake latency in nanoseconds.
|
||||
/// Handshake latency in nanoseconds
|
||||
pub handshake_ns: u64,
|
||||
/// Time-to-last-byte in nanoseconds (from connection start).
|
||||
/// Time-to-last-byte in nanoseconds (from connection start)
|
||||
pub ttlb_ns: u64,
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ impl BenchRecord {
|
||||
/// Serialize this record as a single NDJSON line (no trailing newline).
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns an error if serialization fails.
|
||||
/// Returns an error if serialization fails
|
||||
pub fn to_ndjson(&self) -> Result<String, serde_json::Error> {
|
||||
serde_json::to_string(self)
|
||||
}
|
||||
@@ -56,6 +56,9 @@ impl fmt::Display for BenchRecord {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use claims::{assert_err, assert_ok};
|
||||
use serde_json::Value;
|
||||
|
||||
use super::*;
|
||||
use std::str::FromStr;
|
||||
|
||||
@@ -68,21 +71,54 @@ mod tests {
|
||||
handshake_ns: 1_000_000,
|
||||
ttlb_ns: 2_000_000,
|
||||
};
|
||||
let json = record.to_ndjson().expect("serialization should succeed");
|
||||
let json = assert_ok!(record.to_ndjson());
|
||||
assert!(json.contains(r#""iteration":0"#));
|
||||
assert!(json.contains(r#""mode":"x25519""#));
|
||||
assert!(json.contains(r#""payload_bytes":1024"#));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bench_record_roundtrip() {
|
||||
let original = BenchRecord {
|
||||
iteration: 42,
|
||||
mode: KeyExchangeMode::X25519Mlkem768,
|
||||
payload_bytes: 4096,
|
||||
handshake_ns: 5_000_000,
|
||||
ttlb_ns: 10_000_000,
|
||||
};
|
||||
let json = assert_ok!(original.to_ndjson());
|
||||
let deserialized = assert_ok!(serde_json::from_str::<BenchRecord>(&json));
|
||||
|
||||
assert_eq!(original.iteration, deserialized.iteration);
|
||||
assert_eq!(original.mode, deserialized.mode);
|
||||
assert_eq!(original.payload_bytes, deserialized.payload_bytes);
|
||||
assert_eq!(original.handshake_ns, deserialized.handshake_ns);
|
||||
assert_eq!(original.ttlb_ns, deserialized.ttlb_ns);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn key_exchange_mode_from_str() {
|
||||
assert_eq!(
|
||||
KeyExchangeMode::from_str("x25519").expect("should parse"),
|
||||
KeyExchangeMode::X25519
|
||||
);
|
||||
assert_eq!(
|
||||
KeyExchangeMode::from_str("x25519mlkem768").expect("should parse"),
|
||||
KeyExchangeMode::X25519Mlkem768
|
||||
);
|
||||
let mode = assert_ok!(KeyExchangeMode::from_str("x25519"));
|
||||
assert_eq!(mode, KeyExchangeMode::X25519);
|
||||
|
||||
let mode = assert_ok!(KeyExchangeMode::from_str("x25519mlkem768"));
|
||||
assert_eq!(mode, KeyExchangeMode::X25519Mlkem768);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn key_exchange_mode_parse_error() {
|
||||
assert_err!(KeyExchangeMode::from_str("invalid"));
|
||||
assert_err!(KeyExchangeMode::from_str("x25519invalid"));
|
||||
assert_err!(KeyExchangeMode::from_str(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn key_exchange_mode_serde() {
|
||||
let json = r#"{"mode":"x25519mlkem768"}"#;
|
||||
let value = assert_ok!(serde_json::from_str::<Value>(json));
|
||||
let mode = assert_ok!(serde_json::from_value::<KeyExchangeMode>(
|
||||
value["mode"].clone()
|
||||
));
|
||||
assert_eq!(mode, KeyExchangeMode::X25519Mlkem768);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user