Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 1 addition & 59 deletions nvx-rs/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,62 +131,4 @@ pub fn manifest_is_compatible(manifest: &serde_json::Value) -> bool {
}

#[cfg(all(test, feature = "host"))]
mod tests {
use serde_json::json;

use super::*;

#[test]
fn release_manifests_must_match_every_bundled_contract() {
let manifest = json!({
"openvmm": {
"microvm_abi_version": MICROVM_ABI_VERSION,
"time_abi_version": TIME_ABI_VERSION,
"restore_packet_version": RESTORE_PACKET_VERSION,
"control_session_protocol_version": CONTROL_SESSION_PROTOCOL_VERSION,
"control_contract_revision": CONTROL_CONTRACT_REVISION,
}
});
assert!(manifest_is_compatible(&manifest));
for field in [
"microvm_abi_version",
"time_abi_version",
"restore_packet_version",
"control_session_protocol_version",
"control_contract_revision",
] {
let mut incompatible = manifest.clone();
incompatible["openvmm"][field] = serde_json::Value::Null;
assert!(!manifest_is_compatible(&incompatible), "{field}");
}
}

#[test]
fn mismatches_name_each_field_with_its_expected_and_declared_values() {
// A release from before time ABI 2 declares the control revision but neither the time
// ABI nor the restore packet, so naming the revision alone would not tell what differs.
let manifest = json!({
"openvmm": {
"microvm_abi_version": MICROVM_ABI_VERSION,
"control_session_protocol_version": 2,
"control_contract_revision": CONTROL_CONTRACT_REVISION,
}
});
assert_eq!(
manifest_mismatches(&manifest),
[
format!("time_abi_version: expected {TIME_ABI_VERSION}, found none"),
format!("restore_packet_version: expected {RESTORE_PACKET_VERSION}, found none"),
format!(
"control_session_protocol_version: expected \
{CONTROL_SESSION_PROTOCOL_VERSION}, found 2"
),
]
);
let other = json!({"openvmm": {"control_contract_revision": "other"}});
assert!(manifest_mismatches(&other).contains(&format!(
"control_contract_revision: expected \"{CONTROL_CONTRACT_REVISION}\", found \"other\""
)));
assert_eq!(manifest_mismatches(&json!({})).len(), 5);
}
}
mod tests;
57 changes: 57 additions & 0 deletions nvx-rs/src/contract/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use serde_json::json;

use super::*;

#[test]
fn release_manifests_must_match_every_bundled_contract() {
let manifest = json!({
"openvmm": {
"microvm_abi_version": MICROVM_ABI_VERSION,
"time_abi_version": TIME_ABI_VERSION,
"restore_packet_version": RESTORE_PACKET_VERSION,
"control_session_protocol_version": CONTROL_SESSION_PROTOCOL_VERSION,
"control_contract_revision": CONTROL_CONTRACT_REVISION,
}
});
assert!(manifest_is_compatible(&manifest));
for field in [
"microvm_abi_version",
"time_abi_version",
"restore_packet_version",
"control_session_protocol_version",
"control_contract_revision",
] {
let mut incompatible = manifest.clone();
incompatible["openvmm"][field] = serde_json::Value::Null;
assert!(!manifest_is_compatible(&incompatible), "{field}");
}
}

#[test]
fn mismatches_name_each_field_with_its_expected_and_declared_values() {
// A release from before time ABI 2 declares the control revision but neither the time
// ABI nor the restore packet, so naming the revision alone would not tell what differs.
let manifest = json!({
"openvmm": {
"microvm_abi_version": MICROVM_ABI_VERSION,
"control_session_protocol_version": 2,
"control_contract_revision": CONTROL_CONTRACT_REVISION,
}
});
assert_eq!(
manifest_mismatches(&manifest),
[
format!("time_abi_version: expected {TIME_ABI_VERSION}, found none"),
format!("restore_packet_version: expected {RESTORE_PACKET_VERSION}, found none"),
format!(
"control_session_protocol_version: expected \
{CONTROL_SESSION_PROTOCOL_VERSION}, found 2"
),
]
);
let other = json!({"openvmm": {"control_contract_revision": "other"}});
assert!(manifest_mismatches(&other).contains(&format!(
"control_contract_revision: expected \"{CONTROL_CONTRACT_REVISION}\", found \"other\""
)));
assert_eq!(manifest_mismatches(&json!({})).len(), 5);
}
101 changes: 1 addition & 100 deletions nvx-rs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,103 +401,4 @@ impl std::error::Error for Error {
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn errors_keep_their_kind_message_and_cause() {
let cause = std::io::Error::other("disk full");
let error = Error::io("cannot write /state/runtime.json").with_source(cause);
assert_eq!(error.kind(), ErrorKind::Io);
assert_eq!(error.code(), ErrorCode::Io);
assert_eq!(error.to_string(), "cannot write /state/runtime.json");
assert_eq!(
std::error::Error::source(&error).unwrap().to_string(),
"disk full"
);
let (kind, message, source) = error.into_parts();
assert_eq!(kind, ErrorKind::Io);
assert_eq!(message, "cannot write /state/runtime.json");
assert_eq!(source.unwrap().to_string(), "disk full");
assert_eq!(ErrorKind::TimedOut.to_string(), "timed-out");
}

#[test]
fn codes_determine_kinds() {
let error = Error::new(ErrorCode::SandboxExited, "sandbox x stopped unexpectedly");
assert_eq!(error.code(), ErrorCode::SandboxExited);
assert_eq!(error.kind(), ErrorKind::Exited);
assert_eq!(error.into_parts().0, ErrorKind::Exited);
assert_eq!(
Error::new(ErrorKind::TimedOut, "late").code(),
ErrorCode::TimedOut
);
assert_eq!(Error::unsupported("no").code(), ErrorCode::Unsupported);
assert_eq!(
ErrorCode::SandboxNotRunning.to_string(),
"sandbox-not-running"
);
}

#[test]
fn every_kind_has_a_code_of_its_own_spelling() {
for kind in [
ErrorKind::Unavailable,
ErrorKind::Unsupported,
ErrorKind::Exited,
ErrorKind::Io,
ErrorKind::NotFound,
ErrorKind::Protocol,
ErrorKind::TimedOut,
ErrorKind::Other,
] {
let code = ErrorCode::from(kind);
assert_eq!(code.kind(), kind);
assert_eq!(code.as_str(), kind.as_str());
}
}

#[test]
fn codes_are_unique_lowercase_words_joined_by_hyphens() {
let mut seen = std::collections::HashSet::new();
for code in ErrorCode::ALL {
let spelling = code.as_str();
assert!(seen.insert(spelling), "{spelling} repeats");
assert!(
spelling
.split('-')
.all(|word| !word.is_empty() && word.bytes().all(|b| b.is_ascii_lowercase())),
"{spelling}"
);
// A finer code is never spelled as a kind, whose code it would shadow.
if ErrorCode::from(code.kind()) != *code {
assert_ne!(spelling, code.kind().as_str());
}
}
assert_eq!(seen.len(), ErrorCode::ALL.len());
}

#[test]
fn the_readme_documents_every_code_with_its_kind() {
// The table of the README's "Error codes" section lists each code and its kind, in the
// order of `ErrorCode::ALL`.
let readme = include_str!("../README.md");
let documented: Vec<(String, String)> = readme
.lines()
.skip_while(|line| *line != "### Error codes")
.skip(1)
.take_while(|line| !line.starts_with('#'))
.filter_map(|line| {
let cells: Vec<&str> = line.trim_matches('|').split('|').map(str::trim).collect();
let code = cells.first()?.strip_prefix('`')?.strip_suffix('`')?;
let kind = cells.get(1)?.strip_prefix('`')?.strip_suffix('`')?;
Some((code.to_owned(), kind.to_owned()))
})
.collect();
let expected: Vec<(String, String)> = ErrorCode::ALL
.iter()
.map(|code| (code.as_str().to_owned(), format!("{:?}", code.kind())))
.collect();
assert_eq!(documented, expected);
}
}
mod tests;
98 changes: 98 additions & 0 deletions nvx-rs/src/error/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use super::*;

#[test]
fn errors_keep_their_kind_message_and_cause() {
let cause = std::io::Error::other("disk full");
let error = Error::io("cannot write /state/runtime.json").with_source(cause);
assert_eq!(error.kind(), ErrorKind::Io);
assert_eq!(error.code(), ErrorCode::Io);
assert_eq!(error.to_string(), "cannot write /state/runtime.json");
assert_eq!(
std::error::Error::source(&error).unwrap().to_string(),
"disk full"
);
let (kind, message, source) = error.into_parts();
assert_eq!(kind, ErrorKind::Io);
assert_eq!(message, "cannot write /state/runtime.json");
assert_eq!(source.unwrap().to_string(), "disk full");
assert_eq!(ErrorKind::TimedOut.to_string(), "timed-out");
}

#[test]
fn codes_determine_kinds() {
let error = Error::new(ErrorCode::SandboxExited, "sandbox x stopped unexpectedly");
assert_eq!(error.code(), ErrorCode::SandboxExited);
assert_eq!(error.kind(), ErrorKind::Exited);
assert_eq!(error.into_parts().0, ErrorKind::Exited);
assert_eq!(
Error::new(ErrorKind::TimedOut, "late").code(),
ErrorCode::TimedOut
);
assert_eq!(Error::unsupported("no").code(), ErrorCode::Unsupported);
assert_eq!(
ErrorCode::SandboxNotRunning.to_string(),
"sandbox-not-running"
);
}

#[test]
fn every_kind_has_a_code_of_its_own_spelling() {
for kind in [
ErrorKind::Unavailable,
ErrorKind::Unsupported,
ErrorKind::Exited,
ErrorKind::Io,
ErrorKind::NotFound,
ErrorKind::Protocol,
ErrorKind::TimedOut,
ErrorKind::Other,
] {
let code = ErrorCode::from(kind);
assert_eq!(code.kind(), kind);
assert_eq!(code.as_str(), kind.as_str());
}
}

#[test]
fn codes_are_unique_lowercase_words_joined_by_hyphens() {
let mut seen = std::collections::HashSet::new();
for code in ErrorCode::ALL {
let spelling = code.as_str();
assert!(seen.insert(spelling), "{spelling} repeats");
assert!(
spelling
.split('-')
.all(|word| !word.is_empty() && word.bytes().all(|b| b.is_ascii_lowercase())),
"{spelling}"
);
// A finer code is never spelled as a kind, whose code it would shadow.
if ErrorCode::from(code.kind()) != *code {
assert_ne!(spelling, code.kind().as_str());
}
}
assert_eq!(seen.len(), ErrorCode::ALL.len());
}

#[test]
fn the_readme_documents_every_code_with_its_kind() {
// The table of the README's "Error codes" section lists each code and its kind, in the
// order of `ErrorCode::ALL`.
let readme = include_str!("../../README.md");
let documented: Vec<(String, String)> = readme
.lines()
.skip_while(|line| *line != "### Error codes")
.skip(1)
.take_while(|line| !line.starts_with('#'))
.filter_map(|line| {
let cells: Vec<&str> = line.trim_matches('|').split('|').map(str::trim).collect();
let code = cells.first()?.strip_prefix('`')?.strip_suffix('`')?;
let kind = cells.get(1)?.strip_prefix('`')?.strip_suffix('`')?;
Some((code.to_owned(), kind.to_owned()))
})
.collect();
let expected: Vec<(String, String)> = ErrorCode::ALL
.iter()
.map(|code| (code.as_str().to_owned(), format!("{:?}", code.kind())))
.collect();
assert_eq!(documented, expected);
}
28 changes: 1 addition & 27 deletions nvx-rs/src/gpt/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,4 @@ pub(super) fn crc32(bytes: &[u8]) -> u32 {
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn the_crc_matches_the_ieee_check_values() {
// The standard check value of CRC-32/ISO-HDLC, and two values that zlib computes.
assert_eq!(crc32(b"123456789"), 0xCBF4_3926);
assert_eq!(crc32(b""), 0);
assert_eq!(crc32(&[0; 4]), 0x2144_DF1C);
assert_eq!(
crc32(b"The quick brown fox jumps over the lazy dog"),
0x414F_A339
);
}

#[test]
fn a_crc_over_parts_equals_one_over_the_whole() {
let bytes: Vec<u8> = (0..=255).cycle().take(1000).collect();
let parts = Crc32::new()
.update(&bytes[..1])
.update(&bytes[1..500])
.update(&[])
.update(&bytes[500..])
.finish();
assert_eq!(parts, crc32(&bytes));
}
}
mod tests;
25 changes: 25 additions & 0 deletions nvx-rs/src/gpt/crc/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use super::*;

#[test]
fn the_crc_matches_the_ieee_check_values() {
// The standard check value of CRC-32/ISO-HDLC, and two values that zlib computes.
assert_eq!(crc32(b"123456789"), 0xCBF4_3926);
assert_eq!(crc32(b""), 0);
assert_eq!(crc32(&[0; 4]), 0x2144_DF1C);
assert_eq!(
crc32(b"The quick brown fox jumps over the lazy dog"),
0x414F_A339
);
}

#[test]
fn a_crc_over_parts_equals_one_over_the_whole() {
let bytes: Vec<u8> = (0..=255).cycle().take(1000).collect();
let parts = Crc32::new()
.update(&bytes[..1])
.update(&bytes[1..500])
.update(&[])
.update(&bytes[500..])
.finish();
assert_eq!(parts, crc32(&bytes));
}
2 changes: 1 addition & 1 deletion nvx-rs/src/guest/time/decoders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ mod tests {
use proptest::prelude::*;

use super::*;
use crate::protocol::cmdline::model;
use crate::protocol::cmdline::tests::model;

/// A valid packet header without memory ranges, of generation 1.
fn valid_header() -> [u8; PACKET_HEADER_SIZE] {
Expand Down
Loading
Loading