Skip to content

Commit b288516

Browse files
committed
Add error context to assert!(matches!()) and remove debug println in tests
Adds descriptive error messages to pattern-matching assertions so test failures show the actual value. Removes println!("{:?}", res) calls that were only useful for manual debugging. Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 28ac106 commit b288516

File tree

2 files changed

+77
-56
lines changed

2 files changed

+77
-56
lines changed

src/hyperlight_host/tests/integration_test.rs

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ fn interrupt_host_call() {
6161
});
6262

6363
let result = sandbox.call::<i32>("CallHostSpin", ()).unwrap_err();
64-
assert!(matches!(result, HyperlightError::ExecutionCanceledByHost()));
64+
assert!(
65+
matches!(&result, HyperlightError::ExecutionCanceledByHost()),
66+
"unexpected error: {result:?}"
67+
);
6568
assert!(sandbox.poisoned());
6669

6770
// Restore from snapshot to clear poison
@@ -92,7 +95,10 @@ fn interrupt_in_progress_guest_call() {
9295
});
9396

9497
let res = sbox1.call::<i32>("Spin", ()).unwrap_err();
95-
assert!(matches!(res, HyperlightError::ExecutionCanceledByHost()));
98+
assert!(
99+
matches!(&res, HyperlightError::ExecutionCanceledByHost()),
100+
"unexpected error: {res:?}"
101+
);
96102
assert!(sbox1.poisoned());
97103

98104
// Restore from snapshot to clear poison
@@ -265,7 +271,10 @@ fn interrupt_moved_sandbox() {
265271
let thread = thread::spawn(move || {
266272
barrier2.wait();
267273
let res = sbox1.call::<i32>("Spin", ()).unwrap_err();
268-
assert!(matches!(res, HyperlightError::ExecutionCanceledByHost()));
274+
assert!(
275+
matches!(&res, HyperlightError::ExecutionCanceledByHost()),
276+
"unexpected error: {res:?}"
277+
);
269278
assert!(sbox1.poisoned());
270279
sbox1.restore(snapshot1.clone()).unwrap();
271280
assert!(!sbox1.poisoned());
@@ -281,7 +290,10 @@ fn interrupt_moved_sandbox() {
281290
});
282291

283292
let res = sbox2.call::<i32>("Spin", ()).unwrap_err();
284-
assert!(matches!(res, HyperlightError::ExecutionCanceledByHost()));
293+
assert!(
294+
matches!(&res, HyperlightError::ExecutionCanceledByHost()),
295+
"unexpected error: {res:?}"
296+
);
285297

286298
thread.join().expect("Thread should finish");
287299
thread2.join().expect("Thread should finish");
@@ -317,7 +329,10 @@ fn interrupt_custom_signal_no_and_retry_delay() {
317329

318330
for _ in 0..NUM_ITERS {
319331
let res = sbox1.call::<i32>("Spin", ()).unwrap_err();
320-
assert!(matches!(res, HyperlightError::ExecutionCanceledByHost()));
332+
assert!(
333+
matches!(&res, HyperlightError::ExecutionCanceledByHost()),
334+
"unexpected error: {res:?}"
335+
);
321336
assert!(sbox1.poisoned());
322337
// immediately reenter another guest function call after having being cancelled,
323338
// so that the vcpu is running again before the interruptor-thread has a chance to see that the vcpu is not running
@@ -355,7 +370,10 @@ fn interrupt_spamming_host_call() {
355370
.call::<i32>("HostCallLoop", "HostFunc1".to_string())
356371
.unwrap_err();
357372

358-
assert!(matches!(res, HyperlightError::ExecutionCanceledByHost()));
373+
assert!(
374+
matches!(&res, HyperlightError::ExecutionCanceledByHost()),
375+
"unexpected error: {res:?}"
376+
);
359377

360378
thread.join().expect("Thread should finish");
361379
});
@@ -368,8 +386,7 @@ fn print_four_args_c_guest() {
368386
"PrintFourArgs",
369387
("Test4".to_string(), 3_i32, 4_i64, "Tested".to_string()),
370388
);
371-
println!("{:?}", res);
372-
assert!(matches!(res, Ok(46)));
389+
assert!(matches!(&res, Ok(46)), "unexpected result: {res:?}");
373390
});
374391
}
375392

@@ -381,9 +398,9 @@ fn guest_abort() {
381398
let res = sbox1
382399
.call::<()>("GuestAbortWithCode", error_code as i32)
383400
.unwrap_err();
384-
println!("{:?}", res);
385401
assert!(
386-
matches!(res, HyperlightError::GuestAborted(code, message) if (code == error_code && message.is_empty()))
402+
matches!(&res, HyperlightError::GuestAborted(code, message) if (*code == error_code && message.is_empty())),
403+
"unexpected error: {res:?}"
387404
);
388405
});
389406
}
@@ -394,9 +411,9 @@ fn guest_abort_with_context1() {
394411
let res = sbox1
395412
.call::<()>("GuestAbortWithMessage", (25_i32, "Oh no".to_string()))
396413
.unwrap_err();
397-
println!("{:?}", res);
398414
assert!(
399-
matches!(res, HyperlightError::GuestAborted(code, context) if (code == 25 && context == "Oh no"))
415+
matches!(&res, HyperlightError::GuestAborted(code, context) if (*code == 25 && context == "Oh no")),
416+
"unexpected error: {res:?}"
400417
);
401418
});
402419
}
@@ -439,9 +456,9 @@ fn guest_abort_with_context2() {
439456
let res = sbox1
440457
.call::<()>("GuestAbortWithMessage", (60_i32, abort_message.to_string()))
441458
.unwrap_err();
442-
println!("{:?}", res);
443459
assert!(
444-
matches!(res, HyperlightError::GuestAborted(_, context) if context.contains("Guest abort buffer overflowed"))
460+
matches!(&res, HyperlightError::GuestAborted(_, context) if context.contains("Guest abort buffer overflowed")),
461+
"unexpected error: {res:?}"
445462
);
446463
});
447464
}
@@ -458,9 +475,9 @@ fn guest_abort_c_guest() {
458475
(75_i32, "This is a test error message".to_string()),
459476
)
460477
.unwrap_err();
461-
println!("{:?}", res);
462478
assert!(
463-
matches!(res, HyperlightError::GuestAborted(code, message) if (code == 75 && message == "This is a test error message") )
479+
matches!(&res, HyperlightError::GuestAborted(code, message) if (*code == 75 && message == "This is a test error message")),
480+
"unexpected error: {res:?}"
464481
);
465482
});
466483
}
@@ -472,9 +489,9 @@ fn guest_panic() {
472489
let res = sbox1
473490
.call::<()>("guest_panic", "Error... error...".to_string())
474491
.unwrap_err();
475-
println!("{:?}", res);
476492
assert!(
477-
matches!(res, HyperlightError::GuestAborted(code, context) if code == ErrorCode::UnknownError as u8 && context.contains("\nError... error..."))
493+
matches!(&res, HyperlightError::GuestAborted(code, context) if *code == ErrorCode::UnknownError as u8 && context.contains("\nError... error...")),
494+
"unexpected error: {res:?}"
478495
);
479496
});
480497
}
@@ -511,30 +528,37 @@ fn guest_malloc_abort() {
511528
let size = 20000000_i32; // some big number that should fail when allocated
512529

513530
let res = sbox1.call::<i32>("TestMalloc", size).unwrap_err();
514-
println!("{:?}", res);
515531
assert!(
516-
matches!(res, HyperlightError::GuestAborted(code, _) if code == ErrorCode::MallocFailed as u8)
532+
matches!(&res, HyperlightError::GuestAborted(code, _) if *code == ErrorCode::MallocFailed as u8),
533+
"unexpected error: {res:?}"
517534
);
518535
});
519536

520537
// allocate a vector (on heap) that is bigger than the heap
521538
let heap_size = 0x4000;
522539
let size_to_allocate = 0x10000;
523-
assert!(size_to_allocate > heap_size);
540+
assert!(
541+
size_to_allocate > heap_size,
542+
"precondition: size_to_allocate ({size_to_allocate}) must be > heap_size ({heap_size})"
543+
);
524544

525545
let mut cfg = SandboxConfiguration::default();
526546
cfg.set_heap_size(heap_size);
527547
with_rust_sandbox_cfg(cfg, |mut sbox2| {
528-
let res = sbox2.call::<i32>(
529-
"CallMalloc", // uses the rust allocator to allocate a vector on heap
530-
size_to_allocate as i32,
548+
let err = sbox2
549+
.call::<i32>(
550+
"CallMalloc", // uses the rust allocator to allocate a vector on heap
551+
size_to_allocate as i32,
552+
)
553+
.unwrap_err();
554+
assert!(
555+
matches!(
556+
&err,
557+
// OOM memory errors in rust allocator are panics. Our panic handler returns ErrorCode::UnknownError on panic
558+
HyperlightError::GuestAborted(code, msg) if *code == ErrorCode::UnknownError as u8 && msg.contains("memory allocation of ")
559+
),
560+
"unexpected error: {err:?}"
531561
);
532-
println!("{:?}", res);
533-
assert!(matches!(
534-
res.unwrap_err(),
535-
// OOM memory errors in rust allocator are panics. Our panic handler returns ErrorCode::UnknownError on panic
536-
HyperlightError::GuestAborted(code, msg) if code == ErrorCode::UnknownError as u8 && msg.contains("memory allocation of ")
537-
));
538562
});
539563
}
540564

@@ -572,10 +596,13 @@ fn guest_panic_no_alloc() {
572596
panic!("panic on OOM caused stack overflow, this implies allocation in panic handler");
573597
}
574598

575-
assert!(matches!(
576-
res,
577-
HyperlightError::GuestAborted(code, msg) if code == ErrorCode::UnknownError as u8 && msg.contains("memory allocation of ") && msg.contains("bytes failed")
578-
));
599+
assert!(
600+
matches!(
601+
&res,
602+
HyperlightError::GuestAborted(code, msg) if *code == ErrorCode::UnknownError as u8 && msg.contains("memory allocation of ") && msg.contains("bytes failed")
603+
),
604+
"unexpected error: {res:?}"
605+
);
579606
});
580607
}
581608

@@ -590,7 +617,8 @@ fn dynamic_stack_allocate_c_guest() {
590617
.call::<i32>("StackAllocate", 0x800_0000_i32)
591618
.unwrap_err();
592619
assert!(
593-
matches!(res, HyperlightError::GuestAborted(code, _) if code == ErrorCode::MallocFailed as u8)
620+
matches!(&res, HyperlightError::GuestAborted(code, _) if *code == ErrorCode::MallocFailed as u8),
621+
"unexpected error: {res:?}"
594622
);
595623
});
596624
}
@@ -610,7 +638,8 @@ fn static_stack_allocate_overflow() {
610638
with_all_sandboxes(|mut sbox1| {
611639
let res = sbox1.call::<i32>("LargeVar", ()).unwrap_err();
612640
assert!(
613-
matches!(res, HyperlightError::GuestAborted(code, _) if code == ErrorCode::MallocFailed as u8)
641+
matches!(&res, HyperlightError::GuestAborted(code, _) if *code == ErrorCode::MallocFailed as u8),
642+
"unexpected error: {res:?}"
614643
);
615644
});
616645
}
@@ -630,7 +659,8 @@ fn guard_page_check_2() {
630659
with_rust_sandbox(|mut sbox1| {
631660
let res = sbox1.call::<()>("InfiniteRecursion", ()).unwrap_err();
632661
assert!(
633-
matches!(res, HyperlightError::GuestAborted(code, _) if code == ErrorCode::MallocFailed as u8)
662+
matches!(&res, HyperlightError::GuestAborted(code, _) if *code == ErrorCode::MallocFailed as u8),
663+
"unexpected error: {res:?}"
634664
);
635665
});
636666
}
@@ -663,15 +693,12 @@ fn recursive_stack_allocate_overflow() {
663693

664694
let res = sbox1.call::<()>("StackOverflow", iterations).unwrap_err();
665695
assert!(
666-
matches!(res, HyperlightError::GuestAborted(code, _) if code == ErrorCode::MallocFailed as u8)
696+
matches!(&res, HyperlightError::GuestAborted(code, _) if *code == ErrorCode::MallocFailed as u8),
697+
"unexpected error: {res:?}"
667698
);
668699
});
669700
}
670701

671-
// assert!(
672-
// matches!(res, HyperlightError::GuestAborted(code, _) if code == ErrorCode::MallocFailed as u8)
673-
// );
674-
675702
// Check that log messages are emitted correctly from the guest
676703
// This test is ignored as it sets a logger and therefore maybe impacted by other tests running concurrently
677704
// or it may impact other tests.
@@ -774,13 +801,11 @@ fn test_if_guest_is_able_to_get_bool_return_values_from_host() {
774801
let res = sbox3
775802
.call::<bool>("GuestRetrievesBoolValue", (i, i))
776803
.unwrap();
777-
println!("{:?}", res);
778804
assert!(!res);
779805
} else {
780806
let res = sbox3
781807
.call::<bool>("GuestRetrievesBoolValue", (i, i))
782808
.unwrap();
783-
println!("{:?}", res);
784809
assert!(res);
785810
}
786811
}
@@ -799,7 +824,6 @@ fn test_if_guest_is_able_to_get_float_return_values_from_host() {
799824
let res = sbox3
800825
.call::<f32>("GuestRetrievesFloatValue", (1.34_f32, 1.34_f32))
801826
.unwrap();
802-
println!("{:?}", res);
803827
assert_eq!(res, 2.68_f32);
804828
});
805829
}
@@ -816,7 +840,6 @@ fn test_if_guest_is_able_to_get_double_return_values_from_host() {
816840
let res = sbox3
817841
.call::<f64>("GuestRetrievesDoubleValue", (1.34_f64, 1.34_f64))
818842
.unwrap();
819-
println!("{:?}", res);
820843
assert_eq!(res, 2.68_f64);
821844
});
822845
}
@@ -835,7 +858,6 @@ fn test_if_guest_is_able_to_get_string_return_values_from_host() {
835858
let res = sbox3
836859
.call::<String>("GuestRetrievesStringValue", ())
837860
.unwrap();
838-
println!("{:?}", res);
839861
assert_eq!(
840862
res,
841863
"Guest Function, string added by Host Function".to_string()
@@ -1550,10 +1572,13 @@ fn interrupt_infinite_moving_loop_stress_test() {
15501572

15511573
// If the guest entered before calling kill, then we know for a fact the call should have been canceled since kill() was NOT premature.
15521574
if entered_before_kill {
1553-
assert!(matches!(
1554-
sandbox_res,
1555-
Err(HyperlightError::ExecutionCanceledByHost())
1556-
));
1575+
assert!(
1576+
matches!(
1577+
&sandbox_res,
1578+
Err(HyperlightError::ExecutionCanceledByHost())
1579+
),
1580+
"unexpected result: {sandbox_res:?}"
1581+
);
15571582
}
15581583

15591584
// If we did NOT enter the guest before calling kill, then the call may or may not have been canceled depending on timing.

src/hyperlight_host/tests/sandbox_host_tests.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ fn invalid_guest_function_name() {
110110
with_all_sandboxes(|mut sandbox| {
111111
let fn_name = "FunctionDoesntExist";
112112
let res = sandbox.call::<i32>(fn_name, ());
113-
println!("{:?}", res);
114113
assert!(
115114
matches!(res.unwrap_err(), HyperlightError::GuestError(hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode::GuestFunctionNotFound, error_name) if error_name == fn_name)
116115
);
@@ -122,7 +121,6 @@ fn set_static() {
122121
with_all_sandboxes(|mut sandbox| {
123122
let fn_name = "SetStatic";
124123
let res = sandbox.call::<i32>(fn_name, ());
125-
println!("{:?}", res);
126124
assert!(res.is_ok());
127125
// the result is the size of the static array in the guest
128126
assert_eq!(res.unwrap(), 1024 * 1024);
@@ -154,10 +152,8 @@ fn multiple_parameters() {
154152
macro_rules! test_case {
155153
($sandbox:ident, $rx:ident, $name:literal, ($($p:ident),+)) => {{
156154
let ($($p),+, ..) = args.clone();
157-
let res: i32 = $sandbox.call($name, ($($p.0,)+)).unwrap();
158-
println!("{res:?}");
155+
let _res: i32 = $sandbox.call($name, ($($p.0,)+)).unwrap();
159156
let output = $rx.try_recv().unwrap();
160-
println!("{output:?}");
161157
assert_eq!(output, format!("Message: {}.", [$($p.1),+].join(" ")));
162158
}};
163159
}

0 commit comments

Comments
 (0)