Skip to content

Commit 09b5b34

Browse files
haowqsjyao1
authored andcommitted
tests/test-td-exception: Run td-exception unit test cases inside a VM
Signed-off-by: Jiewen Yao <jiewen.yao@intel.com> Signed-off-by: Liu Jiang <gerry@linux.alibaba.com> Signed-off-by: haowei <WeiX.Hao@intel.com>
1 parent e6490b3 commit 09b5b34

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

tests/test-td-exception/Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "test-td-exception"
3+
version = "0.1.0"
4+
description = "Run td-exception unit test cases inside a VM"
5+
repository = "https://github.com/confidential-containers/td-shim"
6+
homepage = "https://github.com/confidential-containers"
7+
license = "BSD-2-Clause-Patent"
8+
edition = "2018"
9+
10+
[dependencies]
11+
# Keep it as `dependencies` to satisfy bootloader-locator's requirement, though it should be `dev-dependencies`
12+
bootloader = "=0.10.9"
13+
14+
[dev-dependencies]
15+
td-exception = { path = "../../td-exception", features = ["integration-test"] }
16+
test-runner-client = { path = "../../devtools/test-runner-client" }
17+
18+
[package.metadata.bootloader]
19+
map-physical-memory = true

tests/test-td-exception/src/lib.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2021 Intel Corporation
2+
//
3+
// SPDX-License-Identifier: BSD-2-Clause-Patent
4+
5+
#![no_std]
6+
#![cfg_attr(test, no_main)]
7+
// The `custom_test_frameworks` feature allows the use of `#[test_case]` and `#![test_runner]`.
8+
// Any function, const, or static can be annotated with `#[test_case]` causing it to be aggregated
9+
// (like #[test]) and be passed to the test runner determined by the `#![test_runner]` crate
10+
// attribute.
11+
#![feature(custom_test_frameworks)]
12+
#![test_runner(test_runner)]
13+
// Reexport the test harness main function under a different symbol.
14+
#![reexport_test_harness_main = "test_main"]
15+
16+
#[cfg(test)]
17+
use bootloader::{boot_info, entry_point, BootInfo};
18+
#[cfg(test)]
19+
use core::ops::Deref;
20+
#[cfg(test)]
21+
use test_runner_client::{init_heap, serial_println, test_runner};
22+
23+
#[cfg(test)]
24+
entry_point!(kernel_main);
25+
26+
#[cfg(test)]
27+
fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
28+
// turn the screen gray
29+
if let Some(framebuffer) = boot_info.framebuffer.as_mut() {
30+
for byte in framebuffer.buffer_mut() {
31+
*byte = 0x90;
32+
}
33+
}
34+
35+
let memoryregions = boot_info.memory_regions.deref();
36+
let offset = boot_info.physical_memory_offset.into_option().unwrap();
37+
38+
for usable in memoryregions.iter() {
39+
if usable.kind == boot_info::MemoryRegionKind::Usable {
40+
init_heap((usable.start + offset) as usize, 0x100000);
41+
break;
42+
}
43+
}
44+
45+
serial_println!("Start to execute test cases...");
46+
test_main();
47+
panic!("Unexpected return from test_main()!!!");
48+
}
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use td_exception::{setup_exception_handlers, DIVIDED_BY_ZERO_EVENT_COUNT};
53+
54+
#[test_case]
55+
fn test_divided_by_zero() {
56+
use core::sync::atomic::Ordering::Acquire;
57+
setup_exception_handlers();
58+
59+
assert_eq!(DIVIDED_BY_ZERO_EVENT_COUNT.load(Acquire), 0);
60+
// TODO: make this work:)
61+
//let _ = 1 / DIVIDED_BY_ZERO_EVENT_COUNT.load(Acquire);
62+
//assert_eq!(DIVIDED_BY_ZERO_EVENT_COUNT.load(Acquire), 1);
63+
}
64+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../devtools/rustc-targets/x86_64-custom.json

0 commit comments

Comments
 (0)