| 1 | use super::*; |
| 2 | use serde_json::{Value, json}; |
| 3 | use std::fs; |
| 4 | use std::path::{Path, PathBuf}; |
| 5 | use std::process::Command; |
| 6 | use tempfile::TempDir; |
| 7 | #[cfg(target_os = "linux")] |
| 8 | use tokio_util::sync::CancellationToken; |
| 9 | |
| 10 | fn git(cwd: &Path, args: &[&str]) -> String { |
| 11 | let output = Command::new("git") |
| 12 | .current_dir(cwd) |
| 13 | .args(args) |
| 14 | .output() |
| 15 | .expect("git command"); |
| 16 | assert!( |
| 17 | output.status.success(), |
| 18 | "git {}: {}", |
| 19 | args.join(" "), |
| 20 | String::from_utf8_lossy(&output.stderr) |
| 21 | ); |
| 22 | String::from_utf8(output.stdout).unwrap().trim().to_owned() |
| 23 | } |
| 24 | |
| 25 | struct Fixture { |
| 26 | temp: TempDir, |
| 27 | repo: PathBuf, |
| 28 | worktree: PathBuf, |
| 29 | manifest: PathBuf, |
| 30 | #[cfg(target_os = "linux")] |
| 31 | public_cases: PathBuf, |
| 32 | } |
| 33 | |
| 34 | impl Fixture { |
| 35 | fn new() -> Self { |
| 36 | let temp = tempfile::tempdir().unwrap(); |
| 37 | let repo = temp.path().join("repo"); |
| 38 | let worktree = temp.path().join("candidate"); |
| 39 | fs::create_dir(&repo).unwrap(); |
| 40 | git(&repo, &["init", "-q"]); |
| 41 | fs::write( |
| 42 | repo.join("candidate.py"), |
| 43 | "import json,sys\nprint(json.dumps(json.load(sys.stdin)))\n", |
| 44 | ) |
| 45 | .unwrap(); |
| 46 | git(&repo, &["add", "."]); |
| 47 | git( |
| 48 | &repo, |
| 49 | &[ |
| 50 | "-c", |
| 51 | "user.name=Evaluator Test", |
| 52 | "-c", |
| 53 | "user.email=evaluator@example.invalid", |
| 54 | "commit", |
| 55 | "-qm", |
| 56 | "baseline", |
| 57 | ], |
| 58 | ); |
| 59 | git( |
| 60 | &repo, |
| 61 | &[ |
| 62 | "worktree", |
| 63 | "add", |
| 64 | "--detach", |
| 65 | worktree.to_str().unwrap(), |
| 66 | "HEAD", |
| 67 | ], |
| 68 | ); |
| 69 | let public_cases = temp.path().join("public.json"); |
| 70 | let hidden_cases = temp.path().join("hidden.json"); |
| 71 | fs::write(&public_cases, r#"[{"input":1,"expected":2}]"#).unwrap(); |
| 72 | fs::write(&hidden_cases, r#"[{"input":2,"expected":3}]"#).unwrap(); |
| 73 | let manifest = temp.path().join("evaluator.json"); |
| 74 | let spec = json!({ |
| 75 | "version": 1, |
| 76 | "objective": "increment an integer", |
| 77 | "baseline_sha": git(&repo, &["rev-parse", "HEAD"]), |
| 78 | "allowed_write_roots": ["candidate.py", "helper.py"], |
| 79 | "protected_paths": ["tests"], |
| 80 | "isolation_backend": "linux-bwrap", |
| 81 | "max_wall_secs": 60, |
| 82 | "hard_gates": [ |
| 83 | {"candidate_argv":["/usr/bin/python3","candidate.py"], |
| 84 | "cases_path": public_cases, "timeout_secs": 5, "min_tests": 1, |
| 85 | "holdout": false}, |
| 86 | {"candidate_argv":["/usr/bin/python3","candidate.py"], |
| 87 | "cases_path": hidden_cases, "timeout_secs": 5, "min_tests": 1, |
| 88 | "holdout": true} |
| 89 | ] |
| 90 | }); |
| 91 | fs::write(&manifest, serde_json::to_vec_pretty(&spec).unwrap()).unwrap(); |
| 92 | Self { |
| 93 | temp, |
| 94 | repo, |
| 95 | worktree, |
| 96 | manifest, |
| 97 | #[cfg(target_os = "linux")] |
| 98 | public_cases, |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | fn edit_spec(&self, edit: impl FnOnce(&mut Value)) { |
| 103 | let mut spec: Value = serde_json::from_slice(&fs::read(&self.manifest).unwrap()).unwrap(); |
| 104 | edit(&mut spec); |
| 105 | fs::write(&self.manifest, serde_json::to_vec_pretty(&spec).unwrap()).unwrap(); |
| 106 | } |
| 107 | |
| 108 | #[cfg(target_os = "linux")] |
| 109 | fn correct_candidate(&self) { |
| 110 | fs::write( |
| 111 | self.worktree.join("candidate.py"), |
| 112 | "import json,sys\nprint(json.dumps(json.load(sys.stdin)+1))\n", |
| 113 | ) |
| 114 | .unwrap(); |
| 115 | } |
| 116 | |
| 117 | #[cfg(target_os = "linux")] |
| 118 | fn freeze(&self, id: &str) -> VerifiedCandidateEvaluator { |
| 119 | VerifiedCandidateEvaluator::freeze(&self.manifest, &self.repo, id, "increment an integer") |
| 120 | .unwrap() |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #[cfg(target_os = "linux")] |
| 125 | fn bwrap_available() -> bool { |
| 126 | let available = Path::new("/usr/bin/bwrap").exists() |
| 127 | && Command::new("/usr/bin/bwrap") |
| 128 | .arg("--version") |
| 129 | .output() |
| 130 | .is_ok_and(|output| output.status.success()); |
| 131 | if !available && std::env::var_os("CI").is_some() { |
| 132 | panic!("Linux CI requires /usr/bin/bwrap so verified_candidate evaluator tests execute"); |
| 133 | } |
| 134 | available |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn checked_in_workflow_identity_is_exact() { |
| 139 | let source = |
| 140 | include_str!("../../../../../../.codewhale/workflows/operate_best_of_n.workflow.js"); |
| 141 | assert_eq!(hash(source.as_bytes()), AUTHORIZED_WORKFLOW_SHA256); |
| 142 | assert!(authorized_workflow_source(source)); |
| 143 | assert!(!authorized_workflow_source(&format!("{source}\n"))); |
| 144 | } |
| 145 | |
| 146 | #[test] |
| 147 | fn declared_baseline_and_case_identity_fail_closed() { |
| 148 | let fixture = Fixture::new(); |
| 149 | fixture.edit_spec(|spec| spec["baseline_sha"] = json!("0".repeat(40))); |
| 150 | assert!( |
| 151 | VerifiedCandidateEvaluator::freeze( |
| 152 | &fixture.manifest, |
| 153 | &fixture.repo, |
| 154 | "baseline_mismatch", |
| 155 | "increment an integer" |
| 156 | ) |
| 157 | .is_err() |
| 158 | ); |
| 159 | fixture |
| 160 | .edit_spec(|spec| spec["baseline_sha"] = json!(git(&fixture.repo, &["rev-parse", "HEAD"]))); |
| 161 | fixture.edit_spec(|spec| spec["hard_gates"][0]["min_tests"] = json!(0)); |
| 162 | fixture.edit_spec(|spec| spec["hard_gates"][1]["min_tests"] = json!(0)); |
| 163 | assert!( |
| 164 | VerifiedCandidateEvaluator::freeze( |
| 165 | &fixture.manifest, |
| 166 | &fixture.repo, |
| 167 | "zero_tests", |
| 168 | "increment an integer" |
| 169 | ) |
| 170 | .is_err() |
| 171 | ); |
| 172 | fixture.edit_spec(|spec| spec["isolation_backend"] = json!("unconfined")); |
| 173 | assert!( |
| 174 | VerifiedCandidateEvaluator::freeze( |
| 175 | &fixture.manifest, |
| 176 | &fixture.repo, |
| 177 | "unsupported", |
| 178 | "increment an integer" |
| 179 | ) |
| 180 | .is_err() |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | #[test] |
| 185 | fn sealed_artifact_survives_deleted_worktree_and_rejects_tampering() { |
| 186 | let fixture = Fixture::new(); |
| 187 | let root = fixture.temp.path().join("store"); |
| 188 | let store = ArtifactStore::freeze(&fixture.repo, &root).unwrap(); |
| 189 | fs::write( |
| 190 | fixture.worktree.join("candidate.py"), |
| 191 | "import helper\nprint(helper.answer())\n", |
| 192 | ) |
| 193 | .unwrap(); |
| 194 | fs::write( |
| 195 | fixture.worktree.join("helper.py"), |
| 196 | "def answer():\n return 2\n", |
| 197 | ) |
| 198 | .unwrap(); |
| 199 | let artifact = store |
| 200 | .capture(&fixture.worktree, "sealed_1", Vec::new(), &[]) |
| 201 | .unwrap(); |
| 202 | let tree = fixture.temp.path().join("materialized"); |
| 203 | git( |
| 204 | &fixture.repo, |
| 205 | &[ |
| 206 | "worktree", |
| 207 | "remove", |
| 208 | "--force", |
| 209 | fixture.worktree.to_str().unwrap(), |
| 210 | ], |
| 211 | ); |
| 212 | store.materialize(&artifact, &tree).unwrap(); |
| 213 | assert_eq!( |
| 214 | fs::read(tree.join("helper.py")).unwrap(), |
| 215 | b"def answer():\n return 2\n" |
| 216 | ); |
| 217 | assert_eq!(artifact.candidate_tree_hash, artifact.artifact_id); |
| 218 | let blob = artifact |
| 219 | .entries |
| 220 | .iter() |
| 221 | .find(|entry| entry.path == "helper.py") |
| 222 | .unwrap() |
| 223 | .blob_hash |
| 224 | .as_ref() |
| 225 | .unwrap(); |
| 226 | fs::write(root.join("blobs").join(blob), b"tampered").unwrap(); |
| 227 | assert!( |
| 228 | store |
| 229 | .materialize( |
| 230 | &store.load("sealed_1").unwrap(), |
| 231 | &fixture.temp.path().join("tampered") |
| 232 | ) |
| 233 | .is_err() |
| 234 | ); |
| 235 | let manifest = root.join("artifacts").join("sealed_1.json"); |
| 236 | fs::write( |
| 237 | &manifest, |
| 238 | b"{\"schema_version\":1,\"candidate_id\":\"other\"}", |
| 239 | ) |
| 240 | .unwrap(); |
| 241 | assert!(store.load("sealed_1").is_err()); |
| 242 | } |
| 243 | |
| 244 | #[test] |
| 245 | fn artifact_rejects_links_traversal_and_protected_paths() { |
| 246 | let fixture = Fixture::new(); |
| 247 | let root = fixture.temp.path().join("store"); |
| 248 | let store = ArtifactStore::freeze(&fixture.repo, &root).unwrap(); |
| 249 | assert!(normalized_path("../escape").is_err()); |
| 250 | assert!(normalized_path("a/../../escape").is_err()); |
| 251 | fs::create_dir(fixture.worktree.join("tests")).unwrap(); |
| 252 | fs::write( |
| 253 | fixture.worktree.join("tests").join("test_hidden.py"), |
| 254 | b"changed", |
| 255 | ) |
| 256 | .unwrap(); |
| 257 | let artifact = store |
| 258 | .capture(&fixture.worktree, "protected", Vec::new(), &[]) |
| 259 | .unwrap(); |
| 260 | assert!( |
| 261 | validate_scope( |
| 262 | &artifact, |
| 263 | &["candidate.py".to_owned(), "helper.py".to_owned()], |
| 264 | &["tests".to_owned()] |
| 265 | ) |
| 266 | .is_err() |
| 267 | ); |
| 268 | fs::remove_dir_all(fixture.worktree.join("tests")).unwrap(); |
| 269 | #[cfg(unix)] |
| 270 | { |
| 271 | std::os::unix::fs::symlink( |
| 272 | fixture.temp.path().join("public.json"), |
| 273 | fixture.worktree.join("helper.py"), |
| 274 | ) |
| 275 | .unwrap(); |
| 276 | assert!( |
| 277 | store |
| 278 | .capture(&fixture.worktree, "link", Vec::new(), &[]) |
| 279 | .is_err() |
| 280 | ); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | #[test] |
| 285 | fn generated_outputs_are_recorded_without_hiding_source_or_protected_changes() { |
| 286 | let fixture = Fixture::new(); |
| 287 | git( |
| 288 | &fixture.repo, |
| 289 | &[ |
| 290 | "worktree", |
| 291 | "remove", |
| 292 | "--force", |
| 293 | fixture.worktree.to_str().unwrap(), |
| 294 | ], |
| 295 | ); |
| 296 | fs::write( |
| 297 | fixture.repo.join(".gitignore"), |
| 298 | "target/\n__pycache__/\noutside/\n", |
| 299 | ) |
| 300 | .unwrap(); |
| 301 | fs::create_dir(fixture.repo.join("target")).unwrap(); |
| 302 | fs::write(fixture.repo.join("target/tracked.rs"), "baseline").unwrap(); |
| 303 | git(&fixture.repo, &["add", ".gitignore"]); |
| 304 | git(&fixture.repo, &["add", "-f", "target/tracked.rs"]); |
| 305 | git( |
| 306 | &fixture.repo, |
| 307 | &[ |
| 308 | "-c", |
| 309 | "user.name=Evaluator Test", |
| 310 | "-c", |
| 311 | "user.email=evaluator@example.invalid", |
| 312 | "commit", |
| 313 | "-qm", |
| 314 | "baseline with ignored tracked file", |
| 315 | ], |
| 316 | ); |
| 317 | git( |
| 318 | &fixture.repo, |
| 319 | &[ |
| 320 | "worktree", |
| 321 | "add", |
| 322 | "--detach", |
| 323 | fixture.worktree.to_str().unwrap(), |
| 324 | "HEAD", |
| 325 | ], |
| 326 | ); |
| 327 | let root = fixture.temp.path().join("store"); |
| 328 | let store = ArtifactStore::freeze(&fixture.repo, &root).unwrap(); |
| 329 | fs::write(fixture.worktree.join("target/tracked.rs"), "changed").unwrap(); |
| 330 | fs::write(fixture.worktree.join("target/build.o"), b"generated").unwrap(); |
| 331 | fs::create_dir(fixture.worktree.join("__pycache__")).unwrap(); |
| 332 | fs::write( |
| 333 | fixture.worktree.join("__pycache__/candidate.pyc"), |
| 334 | b"generated", |
| 335 | ) |
| 336 | .unwrap(); |
| 337 | fs::write(fixture.worktree.join("helper.py"), "def helper(): pass\n").unwrap(); |
| 338 | fs::create_dir_all(fixture.worktree.join("tests/__pycache__")).unwrap(); |
| 339 | fs::write( |
| 340 | fixture.worktree.join("tests/__pycache__/poison.pyc"), |
| 341 | b"protected", |
| 342 | ) |
| 343 | .unwrap(); |
| 344 | fs::create_dir(fixture.worktree.join("outside")).unwrap(); |
| 345 | fs::write( |
| 346 | fixture.worktree.join("outside/ignored.txt"), |
| 347 | b"out of scope", |
| 348 | ) |
| 349 | .unwrap(); |
| 350 | assert_eq!( |
| 351 | git(&fixture.worktree, &["check-ignore", "target/build.o"]), |
| 352 | "target/build.o" |
| 353 | ); |
| 354 | let protected = ["tests".to_owned()]; |
| 355 | let artifact = store |
| 356 | .capture(&fixture.worktree, "generated", Vec::new(), &protected) |
| 357 | .unwrap(); |
| 358 | let changed = artifact |
| 359 | .entries |
| 360 | .iter() |
| 361 | .map(|entry| entry.path.as_str()) |
| 362 | .collect::<Vec<_>>(); |
| 363 | assert!(changed.contains(&"target/tracked.rs")); |
| 364 | assert!(changed.contains(&"helper.py")); |
| 365 | assert!(changed.contains(&"tests/__pycache__/poison.pyc")); |
| 366 | assert!(changed.contains(&"outside/ignored.txt")); |
| 367 | assert!(!changed.contains(&"target/build.o")); |
| 368 | assert!(!changed.contains(&"__pycache__/candidate.pyc")); |
| 369 | assert_eq!( |
| 370 | artifact.excluded_generated_paths, |
| 371 | ["__pycache__/candidate.pyc", "target/build.o"] |
| 372 | ); |
| 373 | assert!( |
| 374 | validate_scope( |
| 375 | &artifact, |
| 376 | &[ |
| 377 | "candidate.py".to_owned(), |
| 378 | "helper.py".to_owned(), |
| 379 | "target/tracked.rs".to_owned() |
| 380 | ], |
| 381 | &protected, |
| 382 | ) |
| 383 | .is_err() |
| 384 | ); |
| 385 | } |
| 386 | |
| 387 | #[cfg(target_os = "linux")] |
| 388 | #[test] |
| 389 | fn trusted_oracle_beats_candidate_claim_and_replay_uses_seal() { |
| 390 | if !bwrap_available() { |
| 391 | return; |
| 392 | } |
| 393 | let fixture = Fixture::new(); |
| 394 | fs::write( |
| 395 | fixture.worktree.join("candidate.py"), |
| 396 | "import json,sys\njson.load(sys.stdin)\nprint(json.dumps({'status':'PASS','test_count':999}))\n", |
| 397 | ) |
| 398 | .unwrap(); |
| 399 | let mut evaluator = fixture.freeze("claim"); |
| 400 | assert_eq!( |
| 401 | evaluator.sandbox_probe["resource_limits"]["processes_rlimit"], |
| 402 | 4096 |
| 403 | ); |
| 404 | let failed = evaluator |
| 405 | .evaluate( |
| 406 | "claim_1", |
| 407 | &fixture.worktree, |
| 408 | true, |
| 409 | &CancellationToken::new(), |
| 410 | ) |
| 411 | .unwrap(); |
| 412 | assert_eq!(failed["status"], "FAIL"); |
| 413 | assert_eq!(failed["test_count"], 1); |
| 414 | fixture.correct_candidate(); |
| 415 | let passed = evaluator |
| 416 | .evaluate( |
| 417 | "correct_1", |
| 418 | &fixture.worktree, |
| 419 | true, |
| 420 | &CancellationToken::new(), |
| 421 | ) |
| 422 | .unwrap(); |
| 423 | assert_eq!(passed["status"], "PASS"); |
| 424 | assert_eq!(passed["test_count"], 1); |
| 425 | git( |
| 426 | &fixture.repo, |
| 427 | &[ |
| 428 | "worktree", |
| 429 | "remove", |
| 430 | "--force", |
| 431 | fixture.worktree.to_str().unwrap(), |
| 432 | ], |
| 433 | ); |
| 434 | let replay = evaluator |
| 435 | .replay("correct_1", &CancellationToken::new()) |
| 436 | .unwrap(); |
| 437 | assert_eq!(replay["status"], "PASS"); |
| 438 | assert_eq!(replay["test_count"], 2); |
| 439 | assert_eq!(replay["artifact_id"], passed["artifact_id"]); |
| 440 | assert_eq!(replay["candidate_tree_hash"], passed["candidate_tree_hash"]); |
| 441 | } |
| 442 | |
| 443 | #[cfg(target_os = "linux")] |
| 444 | #[test] |
| 445 | fn missing_evidence_incomplete_worker_and_changed_oracle_are_rejected() { |
| 446 | if !bwrap_available() { |
| 447 | return; |
| 448 | } |
| 449 | let fixture = Fixture::new(); |
| 450 | fixture.correct_candidate(); |
| 451 | let mut evaluator = fixture.freeze("identity"); |
| 452 | assert!( |
| 453 | evaluator |
| 454 | .evaluate( |
| 455 | "not_done", |
| 456 | &fixture.worktree, |
| 457 | false, |
| 458 | &CancellationToken::new() |
| 459 | ) |
| 460 | .is_err() |
| 461 | ); |
| 462 | fs::write(&fixture.public_cases, r#"[{"input":1,"expected":999}]"#).unwrap(); |
| 463 | assert!( |
| 464 | evaluator |
| 465 | .evaluate( |
| 466 | "changed_oracle", |
| 467 | &fixture.worktree, |
| 468 | true, |
| 469 | &CancellationToken::new() |
| 470 | ) |
| 471 | .is_err() |
| 472 | ); |
| 473 | } |
| 474 | |
| 475 | #[cfg(target_os = "linux")] |
| 476 | #[test] |
| 477 | fn evaluation_deadline_begins_after_generation_delay() { |
| 478 | if !bwrap_available() { |
| 479 | return; |
| 480 | } |
| 481 | let fixture = Fixture::new(); |
| 482 | fixture.correct_candidate(); |
| 483 | fixture.edit_spec(|spec| spec["max_wall_secs"] = json!(4)); |
| 484 | let mut evaluator = fixture.freeze("delayed_generation"); |
| 485 | std::thread::sleep(Duration::from_secs(5)); |
| 486 | let receipt = evaluator |
| 487 | .evaluate( |
| 488 | "delayed_candidate", |
| 489 | &fixture.worktree, |
| 490 | true, |
| 491 | &CancellationToken::new(), |
| 492 | ) |
| 493 | .unwrap(); |
| 494 | assert_eq!(receipt["status"], "PASS", "{receipt}"); |
| 495 | assert!(receipt["evaluation_started_at_ms"].as_u64().is_some()); |
| 496 | assert!(receipt["execution_deadline_at_ms"].as_u64().is_some()); |
| 497 | } |
| 498 | |
| 499 | #[cfg(target_os = "linux")] |
| 500 | #[test] |
| 501 | fn timeout_and_cancellation_reap_descendants_inside_writable_sandbox() { |
| 502 | if !bwrap_available() { |
| 503 | return; |
| 504 | } |
| 505 | let root = tempfile::tempdir().unwrap(); |
| 506 | let source = root.path().join("source"); |
| 507 | fs::create_dir(&source).unwrap(); |
| 508 | let token = uuid::Uuid::new_v4().simple().to_string(); |
| 509 | let code = format!( |
| 510 | "import subprocess,sys,time\nsubprocess.Popen([sys.executable,'-c',\"import time; time.sleep(2); open('marker','w').write('leak')\",{token:?}])\nopen('ready','w').write('spawned')\ntime.sleep(10)\n" |
| 511 | ); |
| 512 | let run = |label: &str, timeout: Duration, cancel: Option<&CancellationToken>| { |
| 513 | let scratch = root.path().join(label); |
| 514 | fs::create_dir(&scratch).unwrap(); |
| 515 | let env = isolated_command( |
| 516 | &source, |
| 517 | &scratch, |
| 518 | "/usr/bin/python3", |
| 519 | vec!["-c".to_owned(), code.clone()], |
| 520 | timeout, |
| 521 | ) |
| 522 | .unwrap(); |
| 523 | let outcome = run_isolated( |
| 524 | &env, |
| 525 | b"", |
| 526 | &root.path().join(format!("{label}.stdout")), |
| 527 | &root.path().join(format!("{label}.stderr")), |
| 528 | timeout, |
| 529 | cancel, |
| 530 | ) |
| 531 | .unwrap(); |
| 532 | (scratch, outcome) |
| 533 | }; |
| 534 | let (control, control_run) = run("control", Duration::from_secs(4), None); |
| 535 | assert_eq!(control_run.stop_reason.as_deref(), Some("timeout")); |
| 536 | assert_eq!(fs::read(control.join("marker")).unwrap(), b"leak"); |
| 537 | let (timed, timed_run) = run("timed", Duration::from_secs(1), None); |
| 538 | assert_eq!(timed_run.stop_reason.as_deref(), Some("timeout")); |
| 539 | assert!(timed.join("ready").exists()); |
| 540 | std::thread::sleep(Duration::from_secs(3)); |
| 541 | assert!(!timed.join("marker").exists()); |
| 542 | let cancelled = CancellationToken::new(); |
| 543 | let scratch = root.path().join("cancelled"); |
| 544 | fs::create_dir(&scratch).unwrap(); |
| 545 | let env = isolated_command( |
| 546 | &source, |
| 547 | &scratch, |
| 548 | "/usr/bin/python3", |
| 549 | vec!["-c".to_owned(), code], |
| 550 | Duration::from_secs(5), |
| 551 | ) |
| 552 | .unwrap(); |
| 553 | std::thread::scope(|scope| { |
| 554 | let handle = scope.spawn(|| { |
| 555 | run_isolated( |
| 556 | &env, |
| 557 | b"", |
| 558 | &root.path().join("cancelled.stdout"), |
| 559 | &root.path().join("cancelled.stderr"), |
| 560 | Duration::from_secs(5), |
| 561 | Some(&cancelled), |
| 562 | ) |
| 563 | .unwrap() |
| 564 | }); |
| 565 | let started = Instant::now(); |
| 566 | while !scratch.join("ready").exists() && started.elapsed() < Duration::from_secs(4) { |
| 567 | std::thread::sleep(Duration::from_millis(20)); |
| 568 | } |
| 569 | assert!(scratch.join("ready").exists()); |
| 570 | cancelled.cancel(); |
| 571 | assert_eq!( |
| 572 | handle.join().unwrap().stop_reason.as_deref(), |
| 573 | Some("cancelled") |
| 574 | ); |
| 575 | }); |
| 576 | std::thread::sleep(Duration::from_secs(3)); |
| 577 | assert!(!scratch.join("marker").exists()); |
| 578 | let survivors = fs::read_dir("/proc") |
| 579 | .unwrap() |
| 580 | .filter_map(Result::ok) |
| 581 | .filter(|entry| entry.file_name().to_string_lossy().parse::<u32>().is_ok()) |
| 582 | .filter_map(|entry| fs::read(entry.path().join("cmdline")).ok()) |
| 583 | .filter(|cmdline| { |
| 584 | cmdline |
| 585 | .windows(token.len()) |
| 586 | .any(|window| window == token.as_bytes()) |
| 587 | }) |
| 588 | .count(); |
| 589 | assert_eq!(survivors, 0); |
| 590 | } |
| 591 |