File size: 9,260 Bytes
ea39c0e | 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 | #[cfg(windows)]
use std::collections::HashMap;
#[cfg(windows)]
use std::path::Path;
use std::process::Stdio;
use std::time::Duration;
#[cfg(windows)]
use codex_protocol::config_types::WindowsSandboxLevel;
#[cfg(windows)]
use codex_protocol::models::PermissionProfile;
#[cfg(windows)]
use codex_sandboxing::SandboxExecRequest;
#[cfg(windows)]
use codex_sandboxing::SandboxType;
#[cfg(windows)]
use codex_utils_path_uri::PathUri;
use pretty_assertions::assert_eq;
#[cfg(windows)]
use tokio::io::AsyncReadExt;
use tokio::io::AsyncWriteExt;
use tokio::process::Command;
use super::drain_helper_stderr;
use super::read_helper_response;
use super::reap_helper_after_response;
#[cfg(windows)]
use super::run_command;
#[cfg(windows)]
use crate::fs_helper::FsHelperPayload;
#[cfg(windows)]
use crate::protocol::FsReadFileResponse;
#[cfg(windows)]
use crate::protocol::FsWriteFileResponse;
#[tokio::test(start_paused = true)]
async fn filesystem_operation_is_not_limited_by_helper_response_deadline() {
let (reader, mut writer) = tokio::io::duplex(/*max_buf_size*/ 256);
let response = tokio::spawn(async move { read_helper_response(reader).await });
tokio::task::yield_now().await;
tokio::time::advance(Duration::from_secs(/*secs*/ 31)).await;
tokio::task::yield_now().await;
assert!(
!response.is_finished(),
"a filesystem operation must not time out before the helper responds"
);
writer
.write_all(b"completed after the old deadline\n")
.await
.expect("helper response");
assert_eq!(
response
.await
.expect("response task")
.expect("unbounded filesystem operation"),
b"completed after the old deadline\n"
);
}
#[tokio::test]
async fn noisy_failing_helper_preserves_exit_status_and_bounded_stderr() {
#[cfg(unix)]
let mut command = {
let mut command = Command::new("sh");
command.arg("-c").arg(
"printf 'expected helper diagnostic' >&2; i=0; while [ \"$i\" -lt 1024 ]; do printf '%0128d' 0 >&2; i=$((i + 1)); done; exit 7",
);
command
};
#[cfg(windows)]
let mut command = {
let system_root = std::env::var("SystemRoot").expect("Windows system root");
let powershell = Path::new(&system_root)
.join("System32")
.join("WindowsPowerShell")
.join("v1.0")
.join("powershell.exe");
let mut command = Command::new(powershell);
command
.arg("-NoProfile")
.arg("-Command")
.arg("[Console]::Error.Write('expected helper diagnostic' + ('x' * 131072)); exit 7");
command
};
command.stdout(Stdio::null());
command.stderr(Stdio::piped());
command.kill_on_drop(true);
let mut child = command.spawn().expect("noisy helper process");
let stderr = drain_helper_stderr(&mut child);
let error = tokio::time::timeout(
Duration::from_secs(/*secs*/ 8),
reap_helper_after_response(child, stderr),
)
.await
.expect("helper stderr must be drained during bounded cleanup")
.expect_err("nonzero helper exit should fail after its stderr pipe fills");
assert!(error.message.contains('7'), "{}", error.message);
assert!(
error.message.contains("expected helper diagnostic"),
"{}",
error.message
);
assert!(error.message.len() < 4400, "{}", error.message.len());
}
#[tokio::test]
async fn helper_stderr_is_drained_before_the_response() {
#[cfg(unix)]
let mut command = {
let mut command = Command::new("sh");
command.arg("-c").arg(
"printf 'expected pre-response diagnostic' >&2; i=0; while [ \"$i\" -lt 1024 ]; do printf '%0128d' 0 >&2; i=$((i + 1)); done; printf 'completed after noisy stderr\\n'",
);
command
};
#[cfg(windows)]
let mut command = {
let system_root = std::env::var("SystemRoot").expect("Windows system root");
let powershell = Path::new(&system_root)
.join("System32")
.join("WindowsPowerShell")
.join("v1.0")
.join("powershell.exe");
let mut command = Command::new(powershell);
command.arg("-NoProfile").arg("-Command").arg(
"[Console]::Error.Write('expected pre-response diagnostic' + ('x' * 131072)); [Console]::Out.WriteLine('completed after noisy stderr')",
);
command
};
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());
command.kill_on_drop(true);
let mut child = command.spawn().expect("noisy helper process");
let stdout = child.stdout.take().expect("helper stdout");
let stderr = drain_helper_stderr(&mut child);
let response = tokio::time::timeout(
Duration::from_secs(/*secs*/ 2),
read_helper_response(stdout),
)
.await
.expect("helper stderr must be drained before awaiting its response")
.expect("helper response");
assert_eq!(response.trim_ascii_end(), b"completed after noisy stderr");
reap_helper_after_response(child, stderr)
.await
.expect("noisy helper should be cleaned up after its response");
}
#[cfg(windows)]
#[tokio::test]
async fn completed_windows_image_read_does_not_wait_for_a_stuck_helper() {
let directory = tempfile::tempdir().expect("temporary directory");
let path = directory.path().join("image.png");
let operations = [
(
r#"[IO.File]::WriteAllText($env:CODEX_FS_HELPER_TEST_PATH, 'image')
[Console]::Out.WriteLine('{"status":"ok","payload":{"operation":"fs/writeFile","response":{}}}')"#,
FsHelperPayload::WriteFile(FsWriteFileResponse {}),
),
(
r#"$data = [Convert]::ToBase64String([IO.File]::ReadAllBytes($env:CODEX_FS_HELPER_TEST_PATH))
[Console]::Out.WriteLine('{"status":"ok","payload":{"operation":"fs/readFile","response":{"dataBase64":"' + $data + '"}}}')"#,
FsHelperPayload::ReadFile(FsReadFileResponse {
data_base64: "aW1hZ2U=".to_string(),
}),
),
];
for (operation, expected) in operations {
let script = format!(
"[Console]::In.ReadLine() | Out-Null\n{operation}\n[Console]::Out.Flush()\n[Threading.Thread]::Sleep(30000)"
);
let command = powershell_command(&script, &path).expect("PowerShell helper command");
let result = tokio::time::timeout(
Duration::from_secs(/*secs*/ 8),
run_command(command, b"{}".to_vec()),
)
.await
.expect("the completed operation must not wait for helper termination")
.expect("helper response");
assert_eq!(result, expected);
}
assert_eq!(std::fs::read(&path).expect("created file"), b"image");
}
#[cfg(windows)]
#[tokio::test]
async fn duplicated_windows_file_handle_survives_bounded_helper_cleanup() {
let directory = tempfile::tempdir().expect("temporary directory");
let path = directory.path().join("image.png");
std::fs::write(&path, b"image").expect("image file");
let command = powershell_command(
r#"[Console]::In.ReadLine() | Out-Null
$file = [IO.File]::OpenRead($env:CODEX_FS_HELPER_TEST_PATH)
$handle = $file.SafeFileHandle.DangerousGetHandle().ToInt64()
[Console]::Out.WriteLine('{"status":"ok","payload":{"operation":"fs/open","response":{"processId":' + $PID + ',"fileHandle":' + $handle + '}}}')
[Console]::Out.Flush()
[Threading.Thread]::Sleep(30000)"#,
&path,
)
.expect("PowerShell helper command");
let mut file = tokio::time::timeout(
Duration::from_secs(/*secs*/ 8),
crate::sandboxed_file_open::open(
command,
PathUri::from_host_native_path(&path).expect("image path URI"),
),
)
.await
.expect("the opened file must not wait for helper termination")
.expect("duplicated file handle");
let mut data = Vec::new();
file.read_to_end(&mut data).await.expect("image contents");
assert_eq!(data, b"image");
}
#[cfg(windows)]
fn powershell_command(script: &str, path: &Path) -> anyhow::Result<SandboxExecRequest> {
let system_root = std::env::var("SystemRoot")?;
let powershell = Path::new(&system_root)
.join("System32")
.join("WindowsPowerShell")
.join("v1.0")
.join("powershell.exe");
let cwd = PathUri::from_host_native_path(std::env::current_dir()?)?;
Ok(SandboxExecRequest {
command: vec![
powershell.to_string_lossy().into_owned(),
"-NoProfile".to_string(),
"-Command".to_string(),
script.to_string(),
],
cwd: cwd.clone(),
sandbox_policy_cwd: cwd,
env: HashMap::from([
("SystemRoot".to_string(), system_root),
(
"CODEX_FS_HELPER_TEST_PATH".to_string(),
path.to_string_lossy().into_owned(),
),
]),
network: None,
network_environment_id: None,
sandbox: SandboxType::None,
windows_sandbox_level: WindowsSandboxLevel::Disabled,
windows_sandbox_private_desktop: false,
permission_profile: PermissionProfile::Disabled,
arg0: None,
})
}
|