Skip to content

Commit f360b2b

Browse files
committed
Remove GUEST env var for testing
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 14bbdd0 commit f360b2b

File tree

8 files changed

+685
-686
lines changed

8 files changed

+685
-686
lines changed

Justfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ like-ci config=default-target hypervisor="kvm":
205205
just check-license-headers
206206

207207
# runs all tests
208-
test target=default-target features="": (test-unit target features) (test-isolated target features) (test-integration "rust" target features) (test-integration "c" target features) (test-doc target features)
208+
test target=default-target features="": (test-unit target features) (test-isolated target features) (test-integration target features) (test-doc target features)
209209

210210
# runs unit tests
211211
test-unit target=default-target features="":
@@ -218,13 +218,14 @@ test-isolated target=default-target features="" :
218218
{{ cargo-cmd }} test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} -p hyperlight-host --test integration_test -- log_message --exact --ignored
219219
@# metrics tests
220220
{{ cargo-cmd }} test {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F function_call_metrics,init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} -p hyperlight-host --lib -- metrics::tests::test_metrics_are_emitted --exact
221-
# runs integration tests. Guest can either be "rust" or "c"
222-
test-integration guest target=default-target features="":
221+
222+
# runs integration tests
223+
test-integration target=default-target features="":
223224
@# run execute_on_heap test with feature "executable_heap" on (runs with off during normal tests)
224-
{{if os() == "windows" { "$env:" } else { "" } }}GUEST="{{guest}}"{{if os() == "windows" { ";" } else { "" } }} {{ cargo-cmd }} test --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} --test integration_test execute_on_heap {{ if features =="" {" --features executable_heap"} else {"--features executable_heap," + features} }}
225+
{{ cargo-cmd }} test --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} --test integration_test execute_on_heap {{ if features =="" {" --features executable_heap"} else {"--features executable_heap," + features} }}
225226

226227
@# run the rest of the integration tests
227-
{{if os() == "windows" { "$env:" } else { "" } }}GUEST="{{guest}}"{{if os() == "windows" { ";" } else { "" } }} {{ cargo-cmd }} test -p hyperlight-host {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} --test '*'
228+
{{ cargo-cmd }} test -p hyperlight-host {{ if features =="" {''} else if features=="no-default-features" {"--no-default-features" } else {"--no-default-features -F init-paging," + features } }} --profile={{ if target == "debug" { "dev" } else { target } }} {{ target-triple-flag }} --test '*'
228229

229230
# tests compilation with no default features on different platforms
230231
test-compilation-no-default-features target=default-target:

src/hyperlight_host/src/mem/layout.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,4 +662,13 @@ mod tests {
662662
get_expected_memory_size(&sbox_mem_layout)
663663
);
664664
}
665+
666+
#[test]
667+
fn test_max_memory_sandbox() {
668+
let mut cfg = SandboxConfiguration::default();
669+
cfg.set_input_data_size(0x40000000);
670+
let layout = SandboxMemoryLayout::new(cfg, 4096, 2048, 4096, 4096, None).unwrap();
671+
let result = layout.get_memory_size();
672+
assert!(matches!(result.unwrap_err(), MemoryRequestTooBig(..)));
673+
}
665674
}

src/hyperlight_host/tests/common/mod.rs

Lines changed: 117 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -13,100 +13,136 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
1617
use hyperlight_host::func::HostFunction;
17-
#[cfg(gdb)]
18-
use hyperlight_host::sandbox::config::DebugInfo;
19-
use hyperlight_host::{GuestBinary, MultiUseSandbox, Result, UninitializedSandbox};
18+
use hyperlight_host::sandbox::SandboxConfiguration;
19+
use hyperlight_host::{GuestBinary, MultiUseSandbox, UninitializedSandbox};
2020
use hyperlight_testing::{c_simple_guest_as_string, simple_guest_as_string};
2121

22-
/// Returns a rust/c simpleguest depending on environment variable GUEST.
23-
/// Uses rust guest by default. Run test with environment variable GUEST="c" to use the c version
24-
/// If a test is only applicable to rust, use `new_uninit_rust`` instead
25-
pub fn new_uninit() -> Result<UninitializedSandbox> {
26-
UninitializedSandbox::new(
27-
GuestBinary::FilePath(get_c_or_rust_simpleguest_path()),
28-
None,
29-
)
22+
/// Returns the path to the Rust simple guest binary.
23+
fn rust_guest_path() -> String {
24+
simple_guest_as_string().unwrap()
3025
}
3126

32-
/// Use this instead of the `new_uninit` if you want your test to only run with the rust guest, not the c guest
33-
pub fn new_uninit_rust() -> Result<UninitializedSandbox> {
34-
#[cfg(gdb)]
35-
{
36-
use hyperlight_host::sandbox::SandboxConfiguration;
37-
let mut cfg = SandboxConfiguration::default();
38-
let debug_info = DebugInfo { port: 8080 };
39-
cfg.set_guest_debug_info(debug_info);
40-
41-
UninitializedSandbox::new(
42-
GuestBinary::FilePath(simple_guest_as_string().unwrap()),
43-
Some(cfg),
44-
)
45-
}
27+
/// Returns the path to the C simple guest binary.
28+
fn c_guest_path() -> String {
29+
c_simple_guest_as_string().unwrap()
30+
}
31+
32+
/// Creates a new Rust guest MultiUseSandbox.
33+
pub fn new_rust_sandbox() -> MultiUseSandbox {
34+
UninitializedSandbox::new(GuestBinary::FilePath(rust_guest_path()), None)
35+
.unwrap()
36+
.evolve()
37+
.unwrap()
38+
}
39+
40+
/// Creates a new Rust guest UninitializedSandbox.
41+
pub fn new_rust_uninit_sandbox() -> UninitializedSandbox {
42+
UninitializedSandbox::new(GuestBinary::FilePath(rust_guest_path()), None).unwrap()
43+
}
44+
45+
// =============================================================================
46+
// Rust guest helpers
47+
// =============================================================================
48+
49+
/// Runs a test with a Rust guest MultiUseSandbox.
50+
pub fn with_rust_sandbox<F>(f: F)
51+
where
52+
F: FnOnce(MultiUseSandbox),
53+
{
54+
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(rust_guest_path()), None)
55+
.unwrap()
56+
.evolve()
57+
.unwrap();
58+
f(sandbox);
59+
}
4660

47-
#[cfg(not(gdb))]
48-
UninitializedSandbox::new(
49-
GuestBinary::FilePath(simple_guest_as_string().unwrap()),
50-
None,
51-
)
61+
/// Runs a test with a Rust guest MultiUseSandbox using custom configuration.
62+
pub fn with_rust_sandbox_cfg<F>(cfg: SandboxConfiguration, f: F)
63+
where
64+
F: FnOnce(MultiUseSandbox),
65+
{
66+
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(rust_guest_path()), Some(cfg))
67+
.unwrap()
68+
.evolve()
69+
.unwrap();
70+
f(sandbox);
5271
}
5372

54-
/// Returns a c-language simpleguest.
55-
pub fn new_uninit_c() -> Result<UninitializedSandbox> {
56-
UninitializedSandbox::new(
57-
GuestBinary::FilePath(c_simple_guest_as_string().unwrap()),
58-
None,
59-
)
73+
/// Runs a test with a Rust guest UninitializedSandbox.
74+
pub fn with_rust_uninit_sandbox<F>(f: F)
75+
where
76+
F: FnOnce(UninitializedSandbox),
77+
{
78+
let sandbox =
79+
UninitializedSandbox::new(GuestBinary::FilePath(rust_guest_path()), None).unwrap();
80+
f(sandbox);
6081
}
6182

62-
pub fn get_simpleguest_sandboxes(
63-
writer: Option<HostFunction<i32, (String,)>>, // An optional writer to make sure correct info is passed to the host printer
64-
) -> Vec<MultiUseSandbox> {
65-
let elf_path = get_c_or_rust_simpleguest_path();
66-
67-
let sandboxes = [
68-
// in hypervisor elf
69-
UninitializedSandbox::new(GuestBinary::FilePath(elf_path.clone()), None).unwrap(),
70-
];
71-
72-
sandboxes
73-
.into_iter()
74-
.map(|mut sandbox| {
75-
if let Some(writer) = writer.clone() {
76-
sandbox.register_print(writer).unwrap();
77-
}
78-
sandbox.evolve().unwrap()
79-
})
80-
.collect()
83+
// =============================================================================
84+
// C guest helpers
85+
// =============================================================================
86+
87+
/// Runs a test with a C guest MultiUseSandbox.
88+
pub fn with_c_sandbox<F>(f: F)
89+
where
90+
F: FnOnce(MultiUseSandbox),
91+
{
92+
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(c_guest_path()), None)
93+
.unwrap()
94+
.evolve()
95+
.unwrap();
96+
f(sandbox);
8197
}
8298

83-
pub fn get_uninit_simpleguest_sandboxes(
84-
writer: Option<HostFunction<i32, (String,)>>, // An optional writer to make sure correct info is passed to the host printer
85-
) -> Vec<UninitializedSandbox> {
86-
let elf_path = get_c_or_rust_simpleguest_path();
87-
88-
let sandboxes = [
89-
// in hypervisor elf
90-
UninitializedSandbox::new(GuestBinary::FilePath(elf_path.clone()), None).unwrap(),
91-
];
92-
93-
sandboxes
94-
.into_iter()
95-
.map(|mut sandbox| {
96-
if let Some(writer) = writer.clone() {
97-
sandbox.register_print(writer).unwrap();
98-
}
99-
sandbox
100-
})
101-
.collect()
99+
/// Runs a test with a C guest UninitializedSandbox.
100+
pub fn with_c_uninit_sandbox<F>(f: F)
101+
where
102+
F: FnOnce(UninitializedSandbox),
103+
{
104+
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(c_guest_path()), None).unwrap();
105+
f(sandbox);
106+
}
107+
108+
// =============================================================================
109+
// Both guests helpers (run test with Rust AND C guests)
110+
// =============================================================================
111+
112+
/// Runs a test with both Rust and C guest MultiUseSandboxes.
113+
pub fn with_all_sandboxes<F>(f: F)
114+
where
115+
F: Fn(MultiUseSandbox),
116+
{
117+
for path in [rust_guest_path(), c_guest_path()] {
118+
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None)
119+
.unwrap()
120+
.evolve()
121+
.unwrap();
122+
f(sandbox);
123+
}
124+
}
125+
126+
/// Runs a test with both Rust and C guest UninitializedSandboxes.
127+
pub fn with_all_uninit_sandboxes<F>(f: F)
128+
where
129+
F: Fn(UninitializedSandbox),
130+
{
131+
for path in [rust_guest_path(), c_guest_path()] {
132+
let sandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None).unwrap();
133+
f(sandbox);
134+
}
102135
}
103136

104-
// returns the the path of simpleguest binary. Picks rust/c version depending on environment variable GUEST (or rust by default if unset)
105-
pub(crate) fn get_c_or_rust_simpleguest_path() -> String {
106-
let guest_type = std::env::var("GUEST").unwrap_or("rust".to_string());
107-
match guest_type.as_str() {
108-
"rust" => simple_guest_as_string().unwrap(),
109-
"c" => c_simple_guest_as_string().unwrap(),
110-
_ => panic!("Unknown guest type '{guest_type}', use either 'rust' or 'c'"),
137+
/// Runs a test with both Rust and C guest MultiUseSandboxes, with a print writer.
138+
pub fn with_all_sandboxes_with_writer<F>(writer: HostFunction<i32, (String,)>, f: F)
139+
where
140+
F: Fn(MultiUseSandbox),
141+
{
142+
for path in [rust_guest_path(), c_guest_path()] {
143+
let mut sandbox = UninitializedSandbox::new(GuestBinary::FilePath(path), None).unwrap();
144+
sandbox.register_print(writer.clone()).unwrap();
145+
let sandbox = sandbox.evolve().unwrap();
146+
f(sandbox);
111147
}
112148
}

0 commit comments

Comments
 (0)