| 1 | use super::*; |
| 2 | |
| 3 | // 해제 작업의 원본 사본과 단계 임시 파일은 시스템 임시 폴더가 아니라 출력 폴더에 둠. |
| 4 | // 시스템 임시 폴더를 쓸 수 없게 만들어도(/tmp 할당량 초과와 같은 상황) 해제가 성공해야 함. |
| 5 | fn extract_without_system_temp(bytes: &[u8]) -> (Scratch, Vec<Message>) { |
| 6 | extract_unusable_system_temp(bytes, None, false) |
| 7 | } |
| 8 | fn extract_unusable_system_temp( |
| 9 | bytes: &[u8], |
| 10 | fault: Option<&str>, |
| 11 | file_at_temp_path: bool, |
| 12 | ) -> (Scratch, Vec<Message>) { |
| 13 | let s = Scratch::new(); |
| 14 | let start = encrypted_start(&s, bytes); |
| 15 | let temp = s.0.join("missing-temp"); |
| 16 | if file_at_temp_path { |
| 17 | fs::write(&temp, b"keep").unwrap(); |
| 18 | } |
| 19 | let mut command = s.worker(); |
| 20 | command.env("TMPDIR", &temp); |
| 21 | if let Some(fault) = fault { |
| 22 | let shim = s.0.join("io_fault.so"); |
| 23 | assert!( |
| 24 | Command::new("cc") |
| 25 | .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"]) |
| 26 | .arg( |
| 27 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 28 | .join("../../tests/rust-archive-worker/io_fault.c") |
| 29 | ) |
| 30 | .args(["-ldl", "-o"]) |
| 31 | .arg(&shim) |
| 32 | .status() |
| 33 | .unwrap() |
| 34 | .success() |
| 35 | ); |
| 36 | command |
| 37 | .env("LD_PRELOAD", shim) |
| 38 | .env("ZIPELO_TEST_FAULT", fault); |
| 39 | } |
| 40 | let mut child = command |
| 41 | .stdin(Stdio::piped()) |
| 42 | .stdout(Stdio::piped()) |
| 43 | .stderr(Stdio::piped()) |
| 44 | .spawn() |
| 45 | .unwrap(); |
| 46 | let mut writer = Some(Writer::new(child.stdin.take().unwrap(), 17)); |
| 47 | writer |
| 48 | .as_mut() |
| 49 | .unwrap() |
| 50 | .send(&Message::StartInteractive(start)) |
| 51 | .unwrap(); |
| 52 | let mut reader = Reader::new(child.stdout.take().unwrap()); |
| 53 | let mut events = Vec::new(); |
| 54 | while let Some((job, _, event)) = reader.read().unwrap() { |
| 55 | assert_eq!(job, 17); |
| 56 | if let Message::PasswordRequired { challenge_id, .. } = &event { |
| 57 | for reply in password_reply(*challenge_id, TEST_PASSWORD) { |
| 58 | writer.as_mut().unwrap().send(&reply).unwrap(); |
| 59 | } |
| 60 | } |
| 61 | if matches!(event, Message::Terminal(_)) { |
| 62 | writer = None; |
| 63 | } |
| 64 | events.push(event); |
| 65 | } |
| 66 | drop(writer); |
| 67 | let output = child.wait_with_output().unwrap(); |
| 68 | assert!(output.status.success(), "{:?}", output.stderr); |
| 69 | assert!(output.stderr.is_empty()); |
| 70 | if file_at_temp_path { |
| 71 | assert_eq!(fs::read(&temp).unwrap(), b"keep"); |
| 72 | } else { |
| 73 | assert!(!temp.exists()); |
| 74 | } |
| 75 | (s, events) |
| 76 | } |
| 77 | |
| 78 | fn assert_only_outputs(s: &Scratch, expected: &[&str]) { |
| 79 | let mut names = fs::read_dir(s.0.join("output")) |
| 80 | .unwrap() |
| 81 | .map(|entry| entry.unwrap().file_name().into_string().unwrap()) |
| 82 | .collect::<Vec<_>>(); |
| 83 | names.sort(); |
| 84 | assert_eq!(names, expected, "임시 파일이 출력 폴더에 남으면 안 됨"); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn extract_spools_in_destination_when_system_temp_is_unusable() { |
| 89 | let (s, events) = extract_without_system_temp(include_bytes!("fixtures/deflate.zip")); |
| 90 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 91 | assert_eq!(terminal(&events).publication, Publication::Complete); |
| 92 | assert_eq!( |
| 93 | fs::read(s.0.join("output/folder/hello.txt")).unwrap(), |
| 94 | PAYLOAD |
| 95 | ); |
| 96 | assert_only_outputs(&s, &["empty", "folder"]); |
| 97 | } |
| 98 | |
| 99 | #[test] |
| 100 | fn encrypted_extract_spools_in_destination_when_system_temp_is_unusable() { |
| 101 | let (s, events) = extract_without_system_temp(AES_ZIP); |
| 102 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 103 | assert_eq!( |
| 104 | fs::read(s.0.join("output/payload.bin")).unwrap(), |
| 105 | ENCRYPTED_PAYLOAD |
| 106 | ); |
| 107 | assert_only_outputs(&s, &["payload.bin"]); |
| 108 | } |
| 109 | |
| 110 | #[test] |
| 111 | fn corrupt_archive_error_wins_over_read_only_destination() { |
| 112 | // 출력 폴더에 임시 파일을 만들 수 없으면 시스템 임시 폴더를 써서 기존 오류 우선순위를 유지함. |
| 113 | // 손상된 압축파일은 권한 문제가 아니라 손상으로 안내해야 함. |
| 114 | let bytes = seven_fixture("bad-trailing.7z"); |
| 115 | let writable = Scratch::new(); |
| 116 | let baseline = extract( |
| 117 | &writable, |
| 118 | &bytes, |
| 119 | None, |
| 120 | Selection::All, |
| 121 | None, |
| 122 | Limits::default(), |
| 123 | ); |
| 124 | assert_eq!(terminal(&baseline).error, ErrorCode::Integrity); |
| 125 | |
| 126 | use std::os::unix::fs::PermissionsExt; |
| 127 | let s = Scratch::new(); |
| 128 | let output = s.0.join("output"); |
| 129 | fs::create_dir(&output).unwrap(); |
| 130 | fs::set_permissions(&output, fs::Permissions::from_mode(0o500)).unwrap(); |
| 131 | let events = extract(&s, &bytes, None, Selection::All, None, Limits::default()); |
| 132 | fs::set_permissions(&output, fs::Permissions::from_mode(0o700)).unwrap(); |
| 133 | assert_eq!(terminal(&events).error, ErrorCode::Integrity, "{events:?}"); |
| 134 | assert_eq!(terminal(&events).publication, Publication::None); |
| 135 | assert_eq!(terminal(&events).cleanup, Cleanup::Complete); |
| 136 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn destination_space_or_quota_failure_survives_unusable_system_temp() { |
| 141 | let input = include_bytes!("fixtures/deflate.zip"); |
| 142 | for fault in [ |
| 143 | "destination-enospc", |
| 144 | "destination-edquot", |
| 145 | "destination-named-enospc", |
| 146 | "destination-named-edquot", |
| 147 | ] { |
| 148 | for file in [false, true] { |
| 149 | let (s, events) = extract_unusable_system_temp(input, Some(fault), file); |
| 150 | let terminal = terminal(&events); |
| 151 | assert_eq!( |
| 152 | terminal.outcome, |
| 153 | Outcome::Failed, |
| 154 | "{fault}/{file}: {events:?}" |
| 155 | ); |
| 156 | assert_eq!( |
| 157 | terminal.error, |
| 158 | ErrorCode::DiskFull, |
| 159 | "{fault}/{file}: {events:?}" |
| 160 | ); |
| 161 | assert_eq!(terminal.publication, Publication::None); |
| 162 | assert_eq!(terminal.cleanup, Cleanup::Complete); |
| 163 | assert_eq!(terminal.created_count, 0); |
| 164 | assert_only_outputs(&s, &[]); |
| 165 | assert_eq!(fs::read(s.0.join("input")).unwrap(), input); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn destination_permission_failure_with_unusable_system_temp_stays_io() { |
| 172 | for file in [false, true] { |
| 173 | let (s, events) = extract_unusable_system_temp( |
| 174 | include_bytes!("fixtures/deflate.zip"), |
| 175 | Some("destination-denied"), |
| 176 | file, |
| 177 | ); |
| 178 | assert_eq!(terminal(&events).error, ErrorCode::Io, "{events:?}"); |
| 179 | assert_eq!(terminal(&events).outcome, Outcome::Failed); |
| 180 | assert_only_outputs(&s, &[]); |
| 181 | } |
| 182 | } |