-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpython_filesystem_demo.rs
More file actions
269 lines (245 loc) · 9.07 KB
/
python_filesystem_demo.rs
File metadata and controls
269 lines (245 loc) · 9.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! Filesystem capabilities demo: exercises all input/output combinations.
//!
//! Run with: `just wasm::sandbox-filesystem-example`
use std::path::Path;
use hyperlight_sandbox::{DirPerms, FilePerms, SandboxBuilder};
use hyperlight_wasm_sandbox::Wasm;
fn python_guest_path() -> String {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("guests/python/python-sandbox.aot")
.display()
.to_string()
}
fn separator(label: &str) {
println!("\n── {label} ──");
}
fn main() {
// ── 1: No filesystem ────────────────────────────────────────────
separator("Test 1: No filesystem");
let mut sandbox = SandboxBuilder::new()
.guest(Wasm)
.module_path(python_guest_path())
.build()
.expect("failed to create sandbox without filesystem");
let result = sandbox.run("print('no filesystem needed')").unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
assert!(
result.stdout.contains("no filesystem needed"),
"stdout: {:?}",
result.stdout,
);
let outputs = sandbox.get_output_files().unwrap();
assert!(outputs.is_empty(), "outputs should be empty");
println!("OK: sandbox runs without any filesystem preopens");
// ── 2: Input only ───────────────────────────────────────────────
separator("Test 2: Input only (read-only preopen, no output)");
let input_tmp = tempfile::tempdir().unwrap();
std::fs::write(input_tmp.path().join("greeting.txt"), b"hello from host").unwrap();
let mut sandbox = SandboxBuilder::new()
.guest(Wasm)
.module_path(python_guest_path())
.input_dir(input_tmp.path())
.build()
.expect("failed to create sandbox with input only");
let result = sandbox
.run(
r#"
with open('/input/greeting.txt') as f:
content = f.read()
print(f"content: {content}")
"#,
)
.unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
assert!(
result.stdout.contains("hello from host"),
"stdout: {:?}",
result.stdout,
);
let outputs = sandbox.get_output_files().unwrap();
assert!(outputs.is_empty(), "no output dir configured");
println!("OK: guest reads host-provided input files");
// ── 3: Temp output only ─────────────────────────────────────────
separator("Test 3: Temp output only (writable preopen, no input)");
let mut sandbox = SandboxBuilder::new()
.guest(Wasm)
.module_path(python_guest_path())
.temp_output()
.build()
.expect("failed to create sandbox with temp output");
let result = sandbox
.run(
r#"
with open('/output/result.txt', 'w') as f:
f.write('computed result')
print('wrote output')
"#,
)
.unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
assert!(
result.stdout.contains("wrote output"),
"stdout: {:?}",
result.stdout,
);
let output_dir = sandbox.output_path().unwrap().unwrap();
let output_files = sandbox.get_output_files().unwrap();
assert!(output_files.contains(&"result.txt".to_string()));
assert_eq!(
std::fs::read(output_dir.join("result.txt")).unwrap(),
b"computed result",
);
println!("OK: guest writes to temp output, host collects files");
// ── 4: Input + temp output ──────────────────────────────────────
separator("Test 4: Input + temp output");
let input_tmp = tempfile::tempdir().unwrap();
std::fs::write(input_tmp.path().join("data.json"), br#"{"value": 42}"#).unwrap();
let mut sandbox = SandboxBuilder::new()
.guest(Wasm)
.module_path(python_guest_path())
.input_dir(input_tmp.path())
.temp_output()
.build()
.expect("failed to create sandbox with input + temp output");
let result = sandbox
.run(
r#"
import json
with open('/input/data.json') as f:
data = json.load(f)
result = data['value'] * 2
with open('/output/doubled.txt', 'w') as f:
f.write(str(result))
print(f"doubled: {result}")
"#,
)
.unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
assert!(
result.stdout.contains("doubled: 84"),
"stdout: {:?}",
result.stdout,
);
let output_dir = sandbox.output_path().unwrap().unwrap();
let output_files = sandbox.get_output_files().unwrap();
assert!(output_files.contains(&"doubled.txt".to_string()));
assert_eq!(
std::fs::read(output_dir.join("doubled.txt")).unwrap(),
b"84",
);
println!("OK: guest reads input, writes output, host collects");
// ── 5: Explicit output dir ──────────────────────────────────────
separator("Test 5: Input + explicit output dir with permissions");
let input_tmp = tempfile::tempdir().unwrap();
let output_tmp = tempfile::tempdir().unwrap();
std::fs::write(input_tmp.path().join("source.txt"), b"transform me").unwrap();
let mut sandbox = SandboxBuilder::new()
.guest(Wasm)
.module_path(python_guest_path())
.input_dir(input_tmp.path())
.output_dir(
output_tmp.path(),
DirPerms::READ | DirPerms::MUTATE,
FilePerms::READ | FilePerms::WRITE,
)
.build()
.expect("failed to create sandbox with explicit output dir");
let result = sandbox
.run(
r#"
with open('/input/source.txt') as f:
text = f.read()
with open('/output/upper.txt', 'w') as f:
f.write(text.upper())
print(f"transformed: {text.upper()}")
"#,
)
.unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
assert!(
result.stdout.contains("TRANSFORM ME"),
"stdout: {:?}",
result.stdout,
);
// Verify via get_output_files
let output_files = sandbox.get_output_files().unwrap();
assert!(output_files.contains(&"upper.txt".to_string()));
// Also verify the file exists on the host filesystem
let host_file = std::fs::read_to_string(output_tmp.path().join("upper.txt")).unwrap();
assert_eq!(host_file, "TRANSFORM ME");
println!("OK: output written to explicit dir, visible on host filesystem");
// ── 6: Output is wiped between runs ─────────────────────────────
separator("Test 6: Output is ephemeral (wiped between runs)");
let mut sandbox = SandboxBuilder::new()
.guest(Wasm)
.module_path(python_guest_path())
.temp_output()
.build()
.expect("failed to create sandbox");
sandbox
.run(
r#"
with open('/output/run1.txt', 'w') as f:
f.write('first run')
"#,
)
.unwrap();
let first = sandbox.get_output_files().unwrap();
assert!(
first.contains(&"run1.txt".to_string()),
"run1.txt should exist after first run"
);
let result = sandbox
.run(
r#"
with open('/output/run2.txt', 'w') as f:
f.write('second run')
print('wrote run2')
"#,
)
.unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
let second = sandbox.get_output_files().unwrap();
assert!(
!second.contains(&"run1.txt".to_string()),
"run1.txt should be wiped"
);
assert!(
second.contains(&"run2.txt".to_string()),
"run2.txt should exist"
);
println!("OK: output files wiped between runs");
// ── 7: Input is read-only (guest cannot write) ──────────────────
separator("Test 7: Input is read-only");
let input_tmp = tempfile::tempdir().unwrap();
std::fs::write(input_tmp.path().join("readonly.txt"), b"do not modify").unwrap();
let mut sandbox = SandboxBuilder::new()
.guest(Wasm)
.module_path(python_guest_path())
.input_dir(input_tmp.path())
.build()
.expect("failed to create sandbox");
let result = sandbox
.run(
r#"
try:
with open('/input/readonly.txt', 'w') as f:
f.write('hacked')
print('FAIL: write succeeded')
except (OSError, PermissionError) as e:
print(f'OK: write blocked: {e}')
"#,
)
.unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
assert!(
result.stdout.contains("OK: write blocked"),
"guest should not be able to write to /input, got: {:?}",
result.stdout,
);
// Verify host file is untouched
let content = std::fs::read_to_string(input_tmp.path().join("readonly.txt")).unwrap();
assert_eq!(content, "do not modify");
println!("OK: guest cannot write to input directory");
println!("\n✅ All filesystem tests passed!");
}