| 1 | //! Real pinned harness/Execution broker and local fake parser. Native framework |
| 2 | //! is replaced only at its private test port; OS Vision acceptance remains separate. |
| 3 | use super::*; |
| 4 | use crate::extension_host::test_support::node_for_tests; |
| 5 | use crate::extension_host::{ExtensionHostManager, ExtensionHostOptions, TestManagerGuard}; |
| 6 | use codewhale_plugins::activation::TestPolicyGuard; |
| 7 | use codewhale_runtime::features::{Feature, Features}; |
| 8 | use std::os::unix::fs::PermissionsExt; |
| 9 | use std::sync::Arc; |
| 10 | |
| 11 | struct Overrides(Option<TestOverrides>); |
| 12 | impl Drop for Overrides { |
| 13 | fn drop(&mut self) { |
| 14 | TEST_OVERRIDES.with(|slot| *slot.borrow_mut() = self.0.take()); |
| 15 | } |
| 16 | } |
| 17 | fn overrides(native: Result<Option<String>, ToolError>, binary: Option<OsString>) -> Overrides { |
| 18 | let value = TestOverrides { |
| 19 | native: Arc::new(move |_| native.clone()), |
| 20 | tesseract: binary, |
| 21 | }; |
| 22 | Overrides(TEST_OVERRIDES.with(|slot| slot.borrow_mut().replace(value))) |
| 23 | } |
| 24 | fn context(path: &Path, host: bool) -> ToolContext { |
| 25 | let mut flags = Features::with_defaults(); |
| 26 | if host { |
| 27 | flags.enable(Feature::OcrHost); |
| 28 | } |
| 29 | ToolContext::new(path).with_features(flags) |
| 30 | } |
| 31 | fn new_manager(node: PathBuf, path: &Path) -> Arc<ExtensionHostManager> { |
| 32 | Arc::new(ExtensionHostManager::new(ExtensionHostOptions { |
| 33 | runtime: codewhale_config::extension_host::ExtensionHostRuntime::Node, |
| 34 | node_override: Some(node), |
| 35 | root: Some(path.join("host")), |
| 36 | ..ExtensionHostOptions::default() |
| 37 | })) |
| 38 | } |
| 39 | #[cfg(any(test, feature = "test-support"))] |
| 40 | fn binary(root: &Path, body: &str) -> PathBuf { |
| 41 | let path = root.join("fake-tesseract"); |
| 42 | std::fs::write(&path, format!("#!/bin/sh\n{body}\n")).unwrap(); |
| 43 | std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o700)).unwrap(); |
| 44 | path |
| 45 | } |
| 46 | fn decision(code: &str, trim_end: bool, message: Option<&str>) -> ToolResult { |
| 47 | ToolResult::success("").with_metadata( |
| 48 | json!({"kind":"ocr_decision","code":code,"trim_end":trim_end,"message":message}), |
| 49 | ) |
| 50 | } |
| 51 | #[test] |
| 52 | fn ocr_host_is_independent_and_defaults_to_rust() { |
| 53 | let mut flags = Features::with_defaults(); |
| 54 | assert!(!flags.enabled(Feature::OcrHost)); |
| 55 | assert_eq!( |
| 56 | codewhale_runtime::features::feature_from_key("ocr_host"), |
| 57 | Some(Feature::OcrHost) |
| 58 | ); |
| 59 | flags.enable(Feature::OcrHost); |
| 60 | assert!(!flags.enabled(Feature::ExtensionHost)); |
| 61 | assert!(!flags.enabled(Feature::PdfHost)); |
| 62 | } |
| 63 | #[tokio::test(flavor = "current_thread")] |
| 64 | async fn real_host_ocr_and_read_file_preserve_native_first_and_tesseract_results() { |
| 65 | let _home = crate::test_support::SealedHome::new(); |
| 66 | let _policy = TestPolicyGuard::extension_host(false); |
| 67 | let Some(node) = node_for_tests("real_host_ocr_consumers") else { |
| 68 | return; |
| 69 | }; |
| 70 | let root = tempfile::tempdir().unwrap(); |
| 71 | let manager = new_manager(node, root.path()); |
| 72 | let _manager = TestManagerGuard::install(Arc::clone(&manager)); |
| 73 | let image = root.path().canonicalize().unwrap().join("image.png"); |
| 74 | std::fs::write(&image, b"GIF89a private-image-fixture").unwrap(); |
| 75 | let marker = root.path().join("tesseract-launched"); |
| 76 | let tess = binary( |
| 77 | root.path(), |
| 78 | &format!( |
| 79 | "printf x >> '{}'; printf ' page one\\npage two\\f\\n\\t\\302\\205'", |
| 80 | marker.display() |
| 81 | ), |
| 82 | ); |
| 83 | let cases = [ |
| 84 | Ok(Some(" Native output stays exact \n".into())), |
| 85 | Ok(None), |
| 86 | Err(ToolError::execution_failed("Native framework refusal")), |
| 87 | ]; |
| 88 | for native in cases { |
| 89 | let native_ok = matches!(&native, Ok(Some(_))); |
| 90 | let _override = overrides(native, Some(tess.clone().into_os_string())); |
| 91 | for host in [false, true] { |
| 92 | let ctx = context(root.path(), host); |
| 93 | let result = ImageOcrTool |
| 94 | .execute(json!({"path":"image.png"}), &ctx) |
| 95 | .await |
| 96 | .unwrap(); |
| 97 | let expected = if native_ok { |
| 98 | " Native output stays exact \n" |
| 99 | } else { |
| 100 | " page one\npage two" |
| 101 | }; |
| 102 | assert_eq!(result.content, expected); |
| 103 | let read = crate::tools::file::ReadFileTool |
| 104 | .execute(json!({"path":"image.png"}), &ctx) |
| 105 | .await |
| 106 | .unwrap(); |
| 107 | assert_eq!( |
| 108 | read.content, |
| 109 | format!("<image_ocr path=\"image.png\">\n{expected}\n</image_ocr>") |
| 110 | ); |
| 111 | } |
| 112 | if native_ok { |
| 113 | assert!( |
| 114 | !marker.exists(), |
| 115 | "successful Native must not launch/probe fallback" |
| 116 | ); |
| 117 | } |
| 118 | } |
| 119 | assert_eq!(std::fs::read(&marker).unwrap(), b"xxxxxxxx"); |
| 120 | assert!(!codewhale_plugins::activation::extension_host_policy_enabled()); |
| 121 | manager.shutdown().await; |
| 122 | } |
| 123 | #[tokio::test(flavor = "current_thread")] |
| 124 | async fn real_host_ocr_no_backend_native_failure_and_parser_errors_match_default() { |
| 125 | let _home = crate::test_support::SealedHome::new(); |
| 126 | let _policy = TestPolicyGuard::extension_host(false); |
| 127 | let Some(node) = node_for_tests("real_host_ocr_faults") else { |
| 128 | return; |
| 129 | }; |
| 130 | let root = tempfile::tempdir().unwrap(); |
| 131 | let manager = new_manager(node, root.path()); |
| 132 | let _manager = TestManagerGuard::install(Arc::clone(&manager)); |
| 133 | let image = root.path().canonicalize().unwrap().join("image.png"); |
| 134 | std::fs::write(&image, b"GIF89a fixture").unwrap(); |
| 135 | for native in [ |
| 136 | Ok(None), |
| 137 | Err(ToolError::execution_failed("Native framework refusal")), |
| 138 | ] { |
| 139 | let _override = overrides(native, None); |
| 140 | let rust = ocr_image_path(&image, &context(root.path(), false)) |
| 141 | .await |
| 142 | .unwrap_err(); |
| 143 | let host = ocr_image_path(&image, &context(root.path(), true)) |
| 144 | .await |
| 145 | .unwrap_err(); |
| 146 | assert_eq!(host.to_string(), rust.to_string()); |
| 147 | } |
| 148 | for body in ["printf ' parser diagnostic ' >&2; exit 2", "exit 3"] { |
| 149 | let tess = binary(root.path(), body); |
| 150 | let _override = overrides(Ok(None), Some(tess.into_os_string())); |
| 151 | let rust = ocr_image_path(&image, &context(root.path(), false)) |
| 152 | .await |
| 153 | .unwrap_err(); |
| 154 | let host = ocr_image_path(&image, &context(root.path(), true)) |
| 155 | .await |
| 156 | .unwrap_err(); |
| 157 | assert_eq!(host.to_string(), rust.to_string()); |
| 158 | } |
| 159 | manager.shutdown().await; |
| 160 | } |
| 161 | #[tokio::test(flavor = "current_thread")] |
| 162 | async fn ocr_guards_run_before_host_start_and_host_refusal_has_no_fallback() { |
| 163 | let _home = crate::test_support::SealedHome::new(); |
| 164 | let _policy = TestPolicyGuard::extension_host(false); |
| 165 | let root = tempfile::tempdir().unwrap(); |
| 166 | let manager = new_manager(root.path().join("missing-runtime"), root.path()); |
| 167 | let _manager = TestManagerGuard::install(manager); |
| 168 | let ran = Arc::new(std::sync::atomic::AtomicBool::new(false)); |
| 169 | let mark = Arc::clone(&ran); |
| 170 | let value = TestOverrides { |
| 171 | native: Arc::new(move |_| { |
| 172 | mark.store(true, std::sync::atomic::Ordering::SeqCst); |
| 173 | Ok(Some("must not fallback".into())) |
| 174 | }), |
| 175 | tesseract: None, |
| 176 | }; |
| 177 | let _override = Overrides(TEST_OVERRIDES.with(|slot| slot.borrow_mut().replace(value))); |
| 178 | let ctx = context(root.path(), true); |
| 179 | let missing = ImageOcrTool |
| 180 | .execute(json!({"path":"missing.png"}), &ctx) |
| 181 | .await |
| 182 | .unwrap_err(); |
| 183 | assert!(missing.to_string().contains("does not exist")); |
| 184 | std::fs::write(root.path().join(".env"), b"guarded").unwrap(); |
| 185 | assert!(matches!( |
| 186 | ImageOcrTool |
| 187 | .execute(json!({"path":".env"}), &ctx) |
| 188 | .await |
| 189 | .unwrap_err(), |
| 190 | ToolError::PermissionDenied { .. } |
| 191 | )); |
| 192 | std::fs::write( |
| 193 | root.path().canonicalize().unwrap().join("image.png"), |
| 194 | b"GIF89a fixture", |
| 195 | ) |
| 196 | .unwrap(); |
| 197 | let refused = ImageOcrTool |
| 198 | .execute(json!({"path":"image.png"}), &ctx) |
| 199 | .await |
| 200 | .unwrap_err(); |
| 201 | assert!(refused.to_string().contains("Builtin") || refused.to_string().contains("runtime")); |
| 202 | assert!(!ran.load(std::sync::atomic::Ordering::SeqCst)); |
| 203 | } |
| 204 | #[test] |
| 205 | fn ocr_private_text_and_terminal_guards_refuse_forged_host_decisions() { |
| 206 | use std::os::unix::process::ExitStatusExt; |
| 207 | let native = || OcrOutcome::Native(Ok(Some("private image text\n".into()))); |
| 208 | assert!( |
| 209 | native() |
| 210 | .into_host_text(decision("tesseract_success", true, None)) |
| 211 | .is_err() |
| 212 | ); |
| 213 | assert_eq!( |
| 214 | native() |
| 215 | .into_host_text(decision("native_success", false, None)) |
| 216 | .unwrap(), |
| 217 | "private image text\n" |
| 218 | ); |
| 219 | let tess = |success| { |
| 220 | OcrOutcome::Tesseract(Ok(std::process::Output { |
| 221 | status: std::process::ExitStatus::from_raw(if success { 0 } else { 2 << 8 }), |
| 222 | stdout: vec![b'x'; MAX_OCR_TEXT], |
| 223 | stderr: b"private input path /private/customer/receipt.png\n".to_vec(), |
| 224 | })) |
| 225 | }; |
| 226 | let projection = tess(true).projection(); |
| 227 | assert!(serde_json::to_vec(&projection).unwrap().len() < 512); |
| 228 | assert!(projection.get("stdout").is_none()); |
| 229 | assert!(projection.get("stderr").is_none()); |
| 230 | assert!(!projection.to_string().contains("receipt.png")); |
| 231 | let expected = |
| 232 | "tesseract failed (exit Some(2)): private input path /private/customer/receipt.png"; |
| 233 | assert_eq!( |
| 234 | tess(false) |
| 235 | .into_host_text(decision( |
| 236 | "execution", |
| 237 | false, |
| 238 | Some("tesseract failed (exit Some(2)): ") |
| 239 | )) |
| 240 | .unwrap_err() |
| 241 | .to_string(), |
| 242 | ToolError::execution_failed(expected).to_string() |
| 243 | ); |
| 244 | assert!( |
| 245 | tess(false) |
| 246 | .into_host_text(decision( |
| 247 | "execution", |
| 248 | false, |
| 249 | Some("forged private diagnostic") |
| 250 | )) |
| 251 | .is_err() |
| 252 | ); |
| 253 | assert_eq!( |
| 254 | tess(true) |
| 255 | .into_host_text(decision("tesseract_success", true, None)) |
| 256 | .unwrap() |
| 257 | .len(), |
| 258 | MAX_OCR_TEXT |
| 259 | ); |
| 260 | assert!( |
| 261 | tess(false) |
| 262 | .into_host_text(decision("tesseract_success", true, None)) |
| 263 | .is_err() |
| 264 | ); |
| 265 | assert!( |
| 266 | OcrOutcome::Native(Ok(None)) |
| 267 | .into_host_text(decision("native_success", false, None)) |
| 268 | .is_err() |
| 269 | ); |
| 270 | let cancel = OcrOutcome::Tesseract(Err(ToolError::cancelled("cancelled"))) |
| 271 | .into_host_text(decision("fault", false, None)) |
| 272 | .unwrap_err(); |
| 273 | assert!(matches!(cancel, ToolError::Cancelled { .. })); |
| 274 | } |
| 275 | #[tokio::test(flavor = "current_thread")] |
| 276 | async fn real_host_ocr_cancellation_bounds_parser_tree_and_never_replays() { |
| 277 | let _home = crate::test_support::SealedHome::new(); |
| 278 | let _policy = TestPolicyGuard::extension_host(false); |
| 279 | let Some(node) = node_for_tests("real_host_ocr_cancel") else { |
| 280 | return; |
| 281 | }; |
| 282 | let root = tempfile::tempdir().unwrap(); |
| 283 | let manager = new_manager(node, root.path()); |
| 284 | let _manager = TestManagerGuard::install(Arc::clone(&manager)); |
| 285 | let image = root.path().canonicalize().unwrap().join("image.png"); |
| 286 | std::fs::write(&image, b"GIF89a fixture").unwrap(); |
| 287 | let warm = overrides(Ok(Some("warm".into())), None); |
| 288 | ocr_image_path(&image, &context(root.path(), true)) |
| 289 | .await |
| 290 | .unwrap(); |
| 291 | drop(warm); |
| 292 | let marker = root.path().join("launched"); |
| 293 | let child = root.path().join("descendant-survived"); |
| 294 | let parser_pid = root.path().join("parser.pid"); |
| 295 | let descendant_pid = root.path().join("descendant.pid"); |
| 296 | let ready = root.path().join("descendant-ready"); |
| 297 | let release = root.path().join("release-descendant"); |
| 298 | let tess = binary( |
| 299 | root.path(), |
| 300 | &format!( |
| 301 | concat!( |
| 302 | "printf '%s\\n' \"$$\" > '{}'\n", |
| 303 | "printf x >> '{}'\n", |
| 304 | "(printf ready > '{}'; while [ ! -e '{}' ]; do /bin/sleep 0.02; done; printf alive > '{}') &\n", |
| 305 | "printf '%s\\n' \"$!\" > '{}'\nwait", |
| 306 | ), |
| 307 | parser_pid.display(), |
| 308 | marker.display(), |
| 309 | ready.display(), |
| 310 | release.display(), |
| 311 | child.display(), |
| 312 | descendant_pid.display(), |
| 313 | ), |
| 314 | ); |
| 315 | let _override = overrides(Ok(None), Some(tess.into_os_string())); |
| 316 | let cancel = CancellationToken::new(); |
| 317 | // Backstop assertion/timeout failures without leaving a latched parser tree. |
| 318 | struct ParserCleanup { |
| 319 | cancel: CancellationToken, |
| 320 | pid_file: PathBuf, |
| 321 | armed: bool, |
| 322 | } |
| 323 | impl Drop for ParserCleanup { |
| 324 | fn drop(&mut self) { |
| 325 | self.cancel.cancel(); |
| 326 | if self.armed |
| 327 | && let Some(pid) = std::fs::read_to_string(&self.pid_file) |
| 328 | .ok() |
| 329 | .and_then(|text| text.trim().parse::<libc::pid_t>().ok()) |
| 330 | .filter(|pid| *pid > 0) |
| 331 | { |
| 332 | // SAFETY: this private fixture records the contained parser's |
| 333 | // process-group leader; a negative PID targets only that group. |
| 334 | unsafe { libc::kill(-pid, libc::SIGKILL) }; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | let mut cleanup = ParserCleanup { |
| 339 | cancel: cancel.clone(), |
| 340 | pid_file: parser_pid.clone(), |
| 341 | armed: true, |
| 342 | }; |
| 343 | let stop = cancel.clone(); |
| 344 | let waiter = tokio::spawn(async move { |
| 345 | tokio::time::timeout(Duration::from_secs(5), async { |
| 346 | while !ready.exists() || !descendant_pid.exists() { |
| 347 | tokio::time::sleep(Duration::from_millis(5)).await; |
| 348 | } |
| 349 | }) |
| 350 | .await |
| 351 | .expect("OCR descendant never became ready"); |
| 352 | let pids = tokio::task::spawn_blocking(move || { |
| 353 | ( |
| 354 | codewhale_runtime::process_tree::read_pid_file(&parser_pid, Duration::from_secs(5)), |
| 355 | codewhale_runtime::process_tree::read_pid_file( |
| 356 | &descendant_pid, |
| 357 | Duration::from_secs(5), |
| 358 | ), |
| 359 | ) |
| 360 | }) |
| 361 | .await |
| 362 | .unwrap(); |
| 363 | stop.cancel(); |
| 364 | pids |
| 365 | }); |
| 366 | let ctx = context(root.path(), true).with_cancel_token(cancel); |
| 367 | let err = tokio::time::timeout(Duration::from_secs(20), ocr_image_path(&image, &ctx)) |
| 368 | .await |
| 369 | .expect("OCR cancellation did not return") |
| 370 | .unwrap_err(); |
| 371 | assert!(matches!(err, ToolError::Cancelled { .. })); |
| 372 | let (parser, descendant) = waiter.await.unwrap(); |
| 373 | // Host cancellation returns before its retained worker has cleaned up. |
| 374 | // Observe that cleanup while allowing the current-thread runtime to run it. |
| 375 | let stopped = tokio::task::spawn_blocking(move || { |
| 376 | let descendant_stopped = |
| 377 | codewhale_runtime::process_tree::wait_for_pid_exit(descendant, Duration::from_secs(5)); |
| 378 | let parser_stopped = |
| 379 | codewhale_runtime::process_tree::wait_for_pid_exit(parser, Duration::from_secs(5)); |
| 380 | descendant_stopped && parser_stopped |
| 381 | }) |
| 382 | .await |
| 383 | .unwrap(); |
| 384 | if stopped { |
| 385 | cleanup.armed = false; |
| 386 | } |
| 387 | assert!(stopped, "cancelled OCR parser tree is still running"); |
| 388 | // A delayed cancellation observer can no longer race a one-second writer: |
| 389 | // the descendant could not write before this explicit release. |
| 390 | std::fs::write(release, b"release").unwrap(); |
| 391 | assert_eq!(std::fs::read(marker).unwrap(), b"x"); |
| 392 | assert!(!child.exists()); |
| 393 | manager.shutdown().await; |
| 394 | } |
| 395 | |
| 396 | #[tokio::test(flavor = "current_thread")] |
| 397 | async fn real_host_ocr_retains_captured_bytes_after_original_path_replacement() { |
| 398 | let _home = crate::test_support::SealedHome::new(); |
| 399 | let _policy = TestPolicyGuard::extension_host(false); |
| 400 | let Some(node) = node_for_tests("ocr_captured_image") else { |
| 401 | return; |
| 402 | }; |
| 403 | let root = tempfile::tempdir().unwrap(); |
| 404 | let manager = new_manager(node, root.path()); |
| 405 | let _manager = TestManagerGuard::install(Arc::clone(&manager)); |
| 406 | let image = root.path().canonicalize().unwrap().join("receipt.png"); |
| 407 | let tess = binary(root.path(), "/bin/cat \"$1\""); |
| 408 | for host in [false, true] { |
| 409 | std::fs::write(&image, b"GIF89a captured original image").unwrap(); |
| 410 | let original = image.clone(); |
| 411 | let observed = Arc::new(std::sync::Mutex::new(None)); |
| 412 | let record = Arc::clone(&observed); |
| 413 | let value = TestOverrides { |
| 414 | native: Arc::new(move |staged| { |
| 415 | assert_ne!(staged, original.as_path()); |
| 416 | assert_eq!( |
| 417 | std::fs::read(staged).unwrap(), |
| 418 | b"GIF89a captured original image" |
| 419 | ); |
| 420 | *record.lock().unwrap() = Some(staged.to_path_buf()); |
| 421 | std::fs::remove_file(&original).unwrap(); |
| 422 | std::fs::write(&original, b"replacement image must not be parsed").unwrap(); |
| 423 | Ok(None) |
| 424 | }), |
| 425 | tesseract: Some(tess.clone().into_os_string()), |
| 426 | }; |
| 427 | let _override = Overrides(TEST_OVERRIDES.with(|slot| slot.borrow_mut().replace(value))); |
| 428 | let result = ImageOcrTool |
| 429 | .execute(json!({"path":"receipt.png"}), &context(root.path(), host)) |
| 430 | .await |
| 431 | .unwrap(); |
| 432 | assert_eq!(result.content, "GIF89a captured original image"); |
| 433 | assert_eq!( |
| 434 | std::fs::read(&image).unwrap(), |
| 435 | b"replacement image must not be parsed" |
| 436 | ); |
| 437 | assert!( |
| 438 | !observed.lock().unwrap().as_ref().unwrap().exists(), |
| 439 | "captured temporary survives work and is retired afterwards" |
| 440 | ); |
| 441 | } |
| 442 | manager.shutdown().await; |
| 443 | } |
| 444 | #[tokio::test(flavor = "current_thread")] |
| 445 | async fn ocr_capture_reuses_bounded_anchored_reader_and_content_digest() { |
| 446 | use std::os::unix::fs::symlink; |
| 447 | let _home = crate::test_support::SealedHome::new(); |
| 448 | let root = tempfile::tempdir().unwrap(); |
| 449 | let root_path = root.path().canonicalize().unwrap(); |
| 450 | let image = root_path.join("receipt.png"); |
| 451 | std::fs::write(&image, b"GIF89a captured original image").unwrap(); |
| 452 | let ctx = context(&root_path, false); |
| 453 | let deadline = tokio::time::Instant::now() + Duration::from_secs(10); |
| 454 | let first = CapturedOcr::capture(&image, &ctx, deadline).await.unwrap(); |
| 455 | let second = CapturedOcr::capture(&image, &ctx, deadline).await.unwrap(); |
| 456 | assert_ne!(first.path, second.path); |
| 457 | assert_eq!( |
| 458 | first.digest(), |
| 459 | codewhale_runtime::hashing::sha256_hex(b"GIF89a captured original image") |
| 460 | ); |
| 461 | assert_eq!(first.digest(), second.digest()); |
| 462 | let staged = first.path.clone(); |
| 463 | drop(first); |
| 464 | assert!(!staged.exists()); |
| 465 | let link = root_path.join("linked.png"); |
| 466 | symlink(&image, &link).unwrap(); |
| 467 | assert!(CapturedOcr::capture(&link, &ctx, deadline).await.is_err()); |
| 468 | let hard = root_path.join("hard.png"); |
| 469 | std::fs::hard_link(&image, &hard).unwrap(); |
| 470 | assert!(CapturedOcr::capture(&image, &ctx, deadline).await.is_err()); |
| 471 | std::fs::remove_file(hard).unwrap(); |
| 472 | let big = root_path.join("big.png"); |
| 473 | std::fs::File::create(&big) |
| 474 | .unwrap() |
| 475 | .set_len((16 * 1024 * 1024 + 1) as u64) |
| 476 | .unwrap(); |
| 477 | assert!( |
| 478 | CapturedOcr::capture(&big, &ctx, deadline) |
| 479 | .await |
| 480 | .err() |
| 481 | .unwrap() |
| 482 | .to_string() |
| 483 | .contains("16 MiB") |
| 484 | ); |
| 485 | } |
| 486 | #[tokio::test(flavor = "current_thread")] |
| 487 | async fn ocr_backend_runs_in_an_empty_private_directory_and_never_sees_non_images() { |
| 488 | let _home = crate::test_support::SealedHome::new(); |
| 489 | let root = tempfile::tempdir().unwrap(); |
| 490 | let root_path = root.path().canonicalize().unwrap(); |
| 491 | let fixture = |
| 492 | std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/ocr_hello.png"); |
| 493 | std::fs::copy(&fixture, root_path.join("image.png")).unwrap(); |
| 494 | let report = root_path.join("report"); |
| 495 | std::fs::create_dir(&report).unwrap(); |
| 496 | let tess = binary( |
| 497 | &root_path, |
| 498 | &format!( |
| 499 | "pwd -P > '{0}/cwd'; ls -A > '{0}/listing'; ls -A \"$(dirname \"$1\")\" >> '{0}/listing'; printf done", |
| 500 | report.display() |
| 501 | ), |
| 502 | ); |
| 503 | let _override = overrides(Ok(None), Some(tess.into_os_string())); |
| 504 | let ctx = context(&root_path, false); |
| 505 | let result = ImageOcrTool |
| 506 | .execute(json!({"path":"image.png"}), &ctx) |
| 507 | .await |
| 508 | .unwrap(); |
| 509 | assert_eq!(result.content, "done"); |
| 510 | let cwd = std::fs::read_to_string(report.join("cwd")).unwrap(); |
| 511 | assert!(!cwd.trim().is_empty()); |
| 512 | assert!(!std::path::Path::new(cwd.trim()).starts_with(&root_path)); |
| 513 | let listing = std::fs::read_to_string(report.join("listing")).unwrap(); |
| 514 | let mut lines = listing.lines().collect::<Vec<_>>(); |
| 515 | lines.sort_unstable(); |
| 516 | assert_eq!(lines, ["input", "work"], "{listing}"); |
| 517 | |
| 518 | let started = report.join("started"); |
| 519 | let tess = binary( |
| 520 | &root_path, |
| 521 | &format!("touch '{}'; printf ran", started.display()), |
| 522 | ); |
| 523 | let _override = overrides(Ok(None), Some(tess.into_os_string())); |
| 524 | let list = format!("{}\n", root_path.join("image.png").display()); |
| 525 | for (name, bytes) in [ |
| 526 | ("list.png", list.into_bytes()), |
| 527 | ("short.png", b"BMabcdefgh".to_vec()), |
| 528 | ] { |
| 529 | std::fs::write(root_path.join(name), bytes).unwrap(); |
| 530 | ImageOcrTool |
| 531 | .execute(json!({"path":name}), &ctx) |
| 532 | .await |
| 533 | .unwrap_err(); |
| 534 | crate::tools::file::ReadFileTool |
| 535 | .execute(json!({"path":name}), &ctx) |
| 536 | .await |
| 537 | .unwrap_err(); |
| 538 | } |
| 539 | assert!(!started.exists()); |
| 540 | } |
| 541 |