| 1 | #![cfg(target_os = "linux")] |
| 2 | |
| 3 | use std::{ |
| 4 | fs, |
| 5 | io::Write, |
| 6 | path::{Path, PathBuf}, |
| 7 | process::{Command, Stdio}, |
| 8 | time::{SystemTime, UNIX_EPOCH}, |
| 9 | }; |
| 10 | use zipelo_archive_worker::{ |
| 11 | protocol::*, |
| 12 | wire::{Reader, Writer}, |
| 13 | }; |
| 14 | |
| 15 | fn execute(action: Action) -> Vec<Message> { |
| 16 | let mut request = Vec::new(); |
| 17 | Writer::new(&mut request, 17) |
| 18 | .send(&Message::Start(Start { |
| 19 | action, |
| 20 | limits: Limits::default(), |
| 21 | })) |
| 22 | .unwrap(); |
| 23 | let worker = std::env::var_os("ZIPELO_ZIP64_WORKER") |
| 24 | .unwrap_or_else(|| env!("CARGO_BIN_EXE_zip64-test-worker").into()); |
| 25 | let mut child = Command::new(worker) |
| 26 | .stdin(Stdio::piped()) |
| 27 | .stdout(Stdio::piped()) |
| 28 | .stderr(Stdio::piped()) |
| 29 | .spawn() |
| 30 | .unwrap(); |
| 31 | child.stdin.take().unwrap().write_all(&request).unwrap(); |
| 32 | let output = child.wait_with_output().unwrap(); |
| 33 | assert!(output.status.success(), "{:?}", output.stderr); |
| 34 | assert!(output.stderr.is_empty()); |
| 35 | let mut reader = Reader::new(output.stdout.as_slice()); |
| 36 | let mut events = Vec::new(); |
| 37 | while let Some((job, _, event)) = reader.read().unwrap() { |
| 38 | assert_eq!(job, 17); |
| 39 | assert!(!matches!(events.last(), Some(Message::Terminal(_)))); |
| 40 | events.push(event); |
| 41 | } |
| 42 | assert!(matches!(events.last(), Some(Message::Terminal(_)))); |
| 43 | events |
| 44 | } |
| 45 | |
| 46 | fn terminal(events: &[Message]) -> &Terminal { |
| 47 | match events.last().unwrap() { |
| 48 | Message::Terminal(value) => value, |
| 49 | _ => unreachable!(), |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fn directory(path: &Path) { |
| 54 | use std::os::unix::fs::DirBuilderExt; |
| 55 | fs::DirBuilder::new().mode(0o700).create(path).unwrap(); |
| 56 | } |
| 57 | |
| 58 | #[test] |
| 59 | fn zip64_tool_and_field_corpus_lists_and_extracts() { |
| 60 | let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 61 | let runs = PathBuf::from(std::env::var_os("ZIPELO_TEST_WORK_DIR").expect("runner 작업 경로 필요")).join("fixtures/rust-zip64-compat"); |
| 62 | fs::create_dir_all(&runs).unwrap(); |
| 63 | let scratch = runs.join(format!( |
| 64 | "{}-{}", |
| 65 | std::process::id(), |
| 66 | SystemTime::now() |
| 67 | .duration_since(UNIX_EPOCH) |
| 68 | .unwrap() |
| 69 | .as_nanos() |
| 70 | )); |
| 71 | directory(&scratch); |
| 72 | let corpus = scratch.join("corpus"); |
| 73 | assert!( |
| 74 | Command::new("python3") |
| 75 | .arg(root.join("generate.py")) |
| 76 | .arg(&corpus) |
| 77 | .status() |
| 78 | .unwrap() |
| 79 | .success() |
| 80 | ); |
| 81 | let mut report = String::from("case\tlist\textract\n"); |
| 82 | let mut failures = Vec::new(); |
| 83 | let manifest = fs::read_to_string(corpus.join("cases.tsv")).unwrap(); |
| 84 | let mut count = 0; |
| 85 | for line in manifest.lines() { |
| 86 | let columns: Vec<_> = line.split('\t').collect(); |
| 87 | assert_eq!(columns.len(), 3); |
| 88 | let (id, name, hex) = (columns[0], columns[1], columns[2]); |
| 89 | let expected: Vec<_> = (0..hex.len()) |
| 90 | .step_by(2) |
| 91 | .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap()) |
| 92 | .collect(); |
| 93 | let archive = NativePath::from_path(&corpus.join(format!("{id}.zip"))); |
| 94 | let listed = execute(Action::List { |
| 95 | archive: archive.clone(), |
| 96 | }); |
| 97 | let list_id = listed.iter().find_map(|event| match event { |
| 98 | Message::ListEnd { |
| 99 | entry_count: 1, |
| 100 | list_id, |
| 101 | } => Some(list_id.clone()), |
| 102 | _ => None, |
| 103 | }); |
| 104 | let entries: Vec<_> = listed |
| 105 | .iter() |
| 106 | .filter_map(|event| match event { |
| 107 | Message::Entry(entry) => Some(entry), |
| 108 | _ => None, |
| 109 | }) |
| 110 | .collect(); |
| 111 | let list_ok = terminal(&listed).outcome == Outcome::Completed |
| 112 | && terminal(&listed).error == ErrorCode::None |
| 113 | && list_id.is_some() |
| 114 | && entries.len() == 1 |
| 115 | && entries[0].name == name |
| 116 | && entries[0].size == expected.len() as u64; |
| 117 | let destination = scratch.join(id); |
| 118 | directory(&destination); |
| 119 | let extracted = execute(Action::Extract { |
| 120 | archive, |
| 121 | destination: NativePath::from_path(&destination), |
| 122 | selection: Selection::All, |
| 123 | list_id, |
| 124 | gzip_output_name: None, |
| 125 | }); |
| 126 | let end = terminal(&extracted); |
| 127 | let extract_ok = end.outcome == Outcome::Completed |
| 128 | && end.error == ErrorCode::None |
| 129 | && end.publication == Publication::Complete |
| 130 | && end.cleanup == Cleanup::Complete |
| 131 | && end.created_count == 1 |
| 132 | && end.retained_partial_count == 0 |
| 133 | && fs::read(destination.join(name)).ok().as_deref() == Some(expected.as_slice()) |
| 134 | && fs::read_dir(&destination).unwrap().count() == 1; |
| 135 | report.push_str(&format!( |
| 136 | "{id}\t{}:{:?}\t{}:{:?}\n", |
| 137 | if list_ok { "PASS" } else { "FAIL" }, |
| 138 | terminal(&listed).error, |
| 139 | if extract_ok { "PASS" } else { "FAIL" }, |
| 140 | end.error |
| 141 | )); |
| 142 | if !list_ok || !extract_ok { |
| 143 | failures.push(id.to_owned()); |
| 144 | } |
| 145 | count += 1; |
| 146 | } |
| 147 | assert_eq!(count, 1066); |
| 148 | fs::write(scratch.join("results.tsv"), report).unwrap(); |
| 149 | println!( |
| 150 | "ZIP64 corpus: {count} cases, {} failed; {}", |
| 151 | failures.len(), |
| 152 | scratch.display() |
| 153 | ); |
| 154 | assert!( |
| 155 | failures.is_empty(), |
| 156 | "실패 {}개, 앞 12개: {:?}; 전체 결과: {}", |
| 157 | failures.len(), |
| 158 | &failures[..failures.len().min(12)], |
| 159 | scratch.join("results.tsv").display() |
| 160 | ); |
| 161 | } |