@@ -13,100 +13,136 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
16+
1617use 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 } ;
2020use 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