#![cfg(test)]
use std::sync::Mutex;
use crate::devices::mmio_bus::MmioDevice;
use crate::devices::serial::{set_heartbeat_detection, SerialPl011, SMPARK_STATE_GPA};
static TEST_LOCK: Mutex<()> = Mutex::new(());
fn drive(line: &str) {
let dev = SerialPl011::new();
set_heartbeat_detection(true);
for b in line.bytes() {
dev.write(0x000, b as u64, 1);
}
dev.write(0x000, b'\n' as u64, 1);
}
#[test]
fn captures_well_formed_state_gpa_line() {
let _g = TEST_LOCK.lock().unwrap();
SMPARK_STATE_GPA.store(0, std::sync::atomic::Ordering::SeqCst);
drive("[SUPERMACHINE-SMPARK] state_gpa=0x83018000");
assert_eq!(
SMPARK_STATE_GPA.load(std::sync::atomic::Ordering::SeqCst),
0x83018000,
);
}
#[test]
fn captures_with_surrounding_log_noise() {
let _g = TEST_LOCK.lock().unwrap();
SMPARK_STATE_GPA.store(0, std::sync::atomic::Ordering::SeqCst);
drive(
"[ 0.123] init-oci: foo bar [SUPERMACHINE-SMPARK] state_gpa=0xdeadbeef extra trailing",
);
assert_eq!(
SMPARK_STATE_GPA.load(std::sync::atomic::Ordering::SeqCst),
0xdeadbeef,
);
}
#[test]
fn captures_max_u64_value() {
let _g = TEST_LOCK.lock().unwrap();
SMPARK_STATE_GPA.store(0, std::sync::atomic::Ordering::SeqCst);
drive("[SUPERMACHINE-SMPARK] state_gpa=0xffffffffffffffff");
assert_eq!(
SMPARK_STATE_GPA.load(std::sync::atomic::Ordering::SeqCst),
u64::MAX,
);
}
#[test]
fn ignores_more_than_16_hex_chars_does_not_overflow() {
let _g = TEST_LOCK.lock().unwrap();
SMPARK_STATE_GPA.store(0, std::sync::atomic::Ordering::SeqCst);
drive("[SUPERMACHINE-SMPARK] state_gpa=0x1234567890abcdef0");
assert_eq!(
SMPARK_STATE_GPA.load(std::sync::atomic::Ordering::SeqCst),
0x1234567890abcdef,
);
}
#[test]
fn stops_at_first_non_hex_byte() {
let _g = TEST_LOCK.lock().unwrap();
SMPARK_STATE_GPA.store(0, std::sync::atomic::Ordering::SeqCst);
drive("[SUPERMACHINE-SMPARK] state_gpa=0xabc trailing-junk");
assert_eq!(
SMPARK_STATE_GPA.load(std::sync::atomic::Ordering::SeqCst),
0xabc,
);
}
#[test]
fn ignores_lines_without_the_prefix() {
let _g = TEST_LOCK.lock().unwrap();
SMPARK_STATE_GPA.store(0xabcd1234, std::sync::atomic::Ordering::SeqCst);
drive("[NOT-SMPARK] state_gpa=0xdead");
drive("init-oci: workload-pre-exec");
drive("smpark: loaded; n_cpus=4 state_gpa=0xbeef");
assert_eq!(
SMPARK_STATE_GPA.load(std::sync::atomic::Ordering::SeqCst),
0xabcd1234,
);
}
#[test]
fn case_sensitive_hex_a_through_f_supported() {
let _g = TEST_LOCK.lock().unwrap();
SMPARK_STATE_GPA.store(0, std::sync::atomic::Ordering::SeqCst);
drive("[SUPERMACHINE-SMPARK] state_gpa=0xABCDEF");
assert_eq!(
SMPARK_STATE_GPA.load(std::sync::atomic::Ordering::SeqCst),
0xabcdef,
);
}
#[test]
fn second_announcement_overwrites_first() {
let _g = TEST_LOCK.lock().unwrap();
SMPARK_STATE_GPA.store(0, std::sync::atomic::Ordering::SeqCst);
drive("[SUPERMACHINE-SMPARK] state_gpa=0x1000");
assert_eq!(
SMPARK_STATE_GPA.load(std::sync::atomic::Ordering::SeqCst),
0x1000,
);
drive("[SUPERMACHINE-SMPARK] state_gpa=0x2000");
assert_eq!(
SMPARK_STATE_GPA.load(std::sync::atomic::Ordering::SeqCst),
0x2000,
);
}