-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfilesystem_demo.rs
More file actions
200 lines (178 loc) · 7.22 KB
/
filesystem_demo.rs
File metadata and controls
200 lines (178 loc) · 7.22 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
//! Filesystem capabilities demo for the JavaScript sandbox.
use hyperlight_javascript_sandbox::HyperlightJs;
use hyperlight_sandbox::{DirPerms, FilePerms, SandboxBuilder};
fn separator(label: &str) {
println!("\n── {label} ──");
}
fn main() {
// ── 1: No filesystem ────────────────────────────────────────────
separator("Test 1: No filesystem");
let mut sandbox = SandboxBuilder::new()
.guest(HyperlightJs)
.build()
.expect("failed to create sandbox");
let result = sandbox.run("console.log('no filesystem');").unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
assert!(
result.stdout.contains("no filesystem"),
"stdout: {:?}",
result.stdout,
);
let outputs = sandbox.get_output_files().unwrap();
assert!(outputs.is_empty());
println!("OK: runs without filesystem");
// ── 2: Input only ───────────────────────────────────────────────
separator("Test 2: Input only");
let input_tmp = tempfile::tempdir().unwrap();
std::fs::write(input_tmp.path().join("hello.txt"), b"hello from host").unwrap();
let mut sandbox = SandboxBuilder::new()
.guest(HyperlightJs)
.input_dir(input_tmp.path())
.build()
.expect("failed to create sandbox");
let result = sandbox
.run(r#"const text = read_file('/input/hello.txt'); console.log('read: ' + text);"#)
.unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
assert!(
result.stdout.contains("hello from host"),
"stdout: {:?}",
result.stdout,
);
println!("OK: guest reads input");
// ── 3: Temp output only ─────────────────────────────────────────
separator("Test 3: Temp output only");
let mut sandbox = SandboxBuilder::new()
.guest(HyperlightJs)
.temp_output()
.build()
.expect("failed to create sandbox");
let result = sandbox
.run(r#"write_file('/output/result.txt', 'js output'); console.log('wrote');"#)
.unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
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"js output",
);
println!("OK: guest writes to temp output");
// ── 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#"{"n": 21}"#).unwrap();
let mut sandbox = SandboxBuilder::new()
.guest(HyperlightJs)
.input_dir(input_tmp.path())
.temp_output()
.build()
.expect("failed to create sandbox");
let result = sandbox
.run(
r#"
const raw = read_file('/input/data.json');
const data = JSON.parse(raw);
write_file('/output/doubled.txt', String(data.n * 2));
console.log('doubled: ' + data.n * 2);
"#,
)
.unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
assert!(
result.stdout.contains("doubled: 42"),
"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"42",
);
println!("OK: reads input, writes output");
// ── 5: Explicit output dir ──────────────────────────────────────
separator("Test 5: Explicit output dir");
let input_tmp = tempfile::tempdir().unwrap();
let output_tmp = tempfile::tempdir().unwrap();
std::fs::write(input_tmp.path().join("msg.txt"), b"shout").unwrap();
let mut sandbox = SandboxBuilder::new()
.guest(HyperlightJs)
.input_dir(input_tmp.path())
.output_dir(
output_tmp.path(),
DirPerms::READ | DirPerms::MUTATE,
FilePerms::READ | FilePerms::WRITE,
)
.build()
.expect("failed to create sandbox");
let result = sandbox
.run(
r#"
const text = read_file('/input/msg.txt');
write_file('/output/upper.txt', text.toUpperCase());
console.log('done');
"#,
)
.unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
let host_file = std::fs::read_to_string(output_tmp.path().join("upper.txt")).unwrap();
assert_eq!(host_file, "SHOUT");
println!("OK: output visible on host filesystem");
// ── 6: Output wiped between runs ────────────────────────────────
separator("Test 6: Output is ephemeral");
let mut sandbox = SandboxBuilder::new()
.guest(HyperlightJs)
.temp_output()
.build()
.expect("failed to create sandbox");
sandbox
.run(r#"write_file('/output/run1.txt', 'first');"#)
.unwrap();
let first = sandbox.get_output_files().unwrap();
assert!(first.contains(&"run1.txt".to_string()));
let result = sandbox
.run(r#"write_file('/output/run2.txt', 'second'); console.log('ok');"#)
.unwrap();
assert_eq!(result.exit_code, 0);
let second = sandbox.get_output_files().unwrap();
assert!(
!second.contains(&"run1.txt".to_string()),
"run1 should be wiped"
);
assert!(second.contains(&"run2.txt".to_string()));
println!("OK: output wiped between runs");
// ── 7: Input is read-only ───────────────────────────────────────
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(HyperlightJs)
.input_dir(input_tmp.path())
.build()
.expect("failed to create sandbox");
let result = sandbox
.run(
r#"
try {
write_file('/input/readonly.txt', 'hacked');
console.log('FAIL: write succeeded');
} catch (e) {
console.log('OK: write blocked: ' + e);
}
"#,
)
.unwrap();
assert_eq!(result.exit_code, 0, "stderr: {}", result.stderr);
assert!(
result.stdout.contains("OK: write blocked"),
"guest should not write to /input, got: {:?}",
result.stdout,
);
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");
println!("\n✅ All JS filesystem tests passed!");
}