返回 CodeWhale
sandbox_stay_tests.rs
根目录 / crates / tui / src / sandbox_stay_tests.rs
1 // Unix only: the fixture redirects the home directory through `HOME`,
2 // and Windows resolves the profile through the known-folder API instead.
3 #[cfg(unix)]
4 #[test]
5 fn sandbox_read_guard_denies_active_and_known_agent_credentials_in_real_file_tool() {
6 use codewhale_engine::tools::spec::{ToolContext, ToolSpec};
7 let _env = crate::test_support::lock_test_env();
8 let temp = tempfile::tempdir().expect("private fixture");
9 let home = temp.path().join("home");
10 let active_home = temp.path().join("active");
11 let codex = temp.path().join("codex");
12 let _home = crate::test_support::EnvVarGuard::set("HOME", home.to_str().unwrap());
13 let _active =
14 crate::test_support::EnvVarGuard::set("CODEWHALE_HOME", active_home.to_str().unwrap());
15 let _codex = crate::test_support::EnvVarGuard::set("CODEX_HOME", codex.to_str().unwrap());
16 let _auth = crate::test_support::EnvVarGuard::remove("OPENAI_CODEX_AUTH_FILE");
17 assert_eq!(dirs::home_dir().as_deref(), Some(home.as_path()));
18 let denied = [
19 active_home.join("credentials/fixture.json"),
20 codex.join("auth.json"),
21 home.join(".kimi/credentials/fixture.json"),
22 home.join(".claude/.credentials.json"),
23 home.join(".codewhale-cu/recordings/trajectories/fixture.jsonl"),
24 ];
25 for path in &denied {
26 std::fs::create_dir_all(path.parent().unwrap()).unwrap();
27 std::fs::write(path, "read-s10-synthetic-value").unwrap();
28 }
29 codewhale_sandbox::read_guard::with_test_active_denylist(
30 codewhale_sandbox::read_guard::ReadDenylist::build(true, &[], &[]),
31 || {
32 let runtime = tokio::runtime::Builder::new_current_thread()
33 .enable_all()
34 .build()
35 .unwrap();
36 let context = ToolContext::new(temp.path());
37 for path in denied {
38 let result = runtime.block_on(
39 codewhale_engine::tools::file::ReadFileTool
40 .execute(serde_json::json!({"path": path}), &context),
41 );
42 assert!(result.is_err(), "file tool read a credential source");
43 }
44 let safe = home.join(".claude/settings.json");
45 std::fs::write(&safe, "ordinary-readable-setting").unwrap();
46 let allowed = runtime
47 .block_on(
48 codewhale_engine::tools::file::ReadFileTool
49 .execute(serde_json::json!({"path": safe}), &context),
50 )
51 .expect("ordinary settings remain readable");
52 assert!(allowed.content.contains("ordinary-readable-setting"));
53 },
54 );
55 }
56
57 #[cfg(unix)]
58 #[test]
59 fn sandbox_wait_preserves_the_leader_until_output_is_drained() {
60 use codewhale_runtime::process_tree::{leader_exited_unreaped, spawn_contained_std};
61 use std::process::Command;
62 use std::sync::mpsc::sync_channel;
63 use std::time::Duration;
64
65 let (child, tree) = spawn_contained_std(Command::new("sh").args(["-c", "exit 7"])).unwrap();
66 let pid = child.id();
67 let (done_tx, done_rx) = sync_channel(0);
68 let observer = std::thread::spawn(move || {
69 done_tx.send(()).unwrap();
70 let leader_still_waitable = leader_exited_unreaped(pid);
71 done_tx.send(()).unwrap();
72 leader_still_waitable
73 });
74 let status = crate::wait_for_sandbox_output(child, tree, done_rx, Duration::from_secs(5))
75 .expect("wait for the command and both streams");
76 assert!(observer.join().unwrap().unwrap());
77 assert_eq!(status.code(), Some(7));
78 }
79
80 #[cfg(unix)]
81 #[test]
82 fn sandbox_wait_collects_pending_output_after_cleanup() {
83 use codewhale_runtime::process_tree::{
84 leader_exited_unreaped, spawn_contained_std, wait_leader_exit_until,
85 };
86 use std::process::Command;
87 use std::sync::mpsc::sync_channel;
88 use std::time::{Duration, Instant};
89
90 let (child, tree) = spawn_contained_std(Command::new("sh").args(["-c", "exit 0"])).unwrap();
91 assert!(wait_leader_exit_until(&child, Instant::now() + Duration::from_secs(5)).unwrap());
92 let pid = child.id();
93 let (done_tx, done_rx) = sync_channel(0);
94 let last_output = std::thread::spawn(move || {
95 done_tx.send(()).unwrap();
96 let deadline = Instant::now() + Duration::from_secs(5);
97 let error = loop {
98 if let Err(error) = leader_exited_unreaped(pid) {
99 break error;
100 }
101 assert!(Instant::now() < deadline, "command was not reaped");
102 std::thread::sleep(Duration::from_millis(5));
103 };
104 assert_eq!(error.raw_os_error(), Some(libc::ECHILD));
105 done_tx.send(()).expect("collect the final output");
106 });
107 let status = crate::wait_for_sandbox_output(child, tree, done_rx, Duration::from_millis(350))
108 .expect("the command exited before the deadline");
109 last_output.join().unwrap();
110 assert!(status.success());
111 }
112
112 lines RUST