| 1 | use super::*; |
| 2 | |
| 3 | fn finish_preview_catalog(model: &mut Model, allow: bool) -> usize { |
| 4 | let deadline = Instant::now() + Duration::from_secs(30); |
| 5 | let mut questions = 0; |
| 6 | while model.busy() { |
| 7 | model.poll(); |
| 8 | if let Some(question) = model.resource.clone() { |
| 9 | assert!(question.scope.entries); |
| 10 | assert!(!question.scope.ratio); |
| 11 | assert_eq!(question.action, ActionKind::Preview); |
| 12 | assert_eq!(model.estimated_usage().estimated_entries, Some(10_001)); |
| 13 | assert!(model.resource_reply(model.job_id().unwrap(), question.id, allow)); |
| 14 | questions += 1; |
| 15 | } |
| 16 | assert!(Instant::now() < deadline, "{:?}", model.error); |
| 17 | std::thread::sleep(Duration::from_millis(1)); |
| 18 | } |
| 19 | assert!(model.error.is_none(), "{:?}", model.error); |
| 20 | questions |
| 21 | } |
| 22 | |
| 23 | fn preview_catalog(count: usize, bytes: usize, allow: bool, image: bool) { |
| 24 | let name = if image { "small.png" } else { "small.txt" }; |
| 25 | let scratch = Scratch::new(); |
| 26 | let archive = scratch.path("many.zip"); |
| 27 | let generated = std::process::Command::new("python3") |
| 28 | .arg("-c") |
| 29 | .arg("import sys,zipfile; z=zipfile.ZipFile(sys.argv[1], 'w'); z.writestr(sys.argv[4], b'x'*int(sys.argv[3])); [z.writestr(f'{i}.bin', b'') for i in range(int(sys.argv[2])-1)]; z.close()") |
| 30 | .arg(&archive) |
| 31 | .arg(count.to_string()) |
| 32 | .arg(bytes.to_string()) |
| 33 | .arg(name) |
| 34 | .output() |
| 35 | .unwrap(); |
| 36 | assert!(generated.status.success()); |
| 37 | let mut factory = ProcessFactory::new(binary("zipelo-archive-worker")); |
| 38 | let mut model = Model::default(); |
| 39 | assert!(model.begin(Start::List { archive }, &mut factory)); |
| 40 | wait(&mut model); |
| 41 | if count > 10_000 { |
| 42 | assert_eq!( |
| 43 | model.terminal.as_ref().unwrap().error, |
| 44 | Some(ErrorCode::Limit) |
| 45 | ); |
| 46 | assert!(model.retry_expanded(&mut factory)); |
| 47 | let deadline = Instant::now() + Duration::from_secs(30); |
| 48 | while model.busy() { |
| 49 | model.poll(); |
| 50 | if let Some(question) = model.resource.clone() { |
| 51 | assert!(model.resource_reply(model.job_id().unwrap(), question.id, true)); |
| 52 | } |
| 53 | assert!(Instant::now() < deadline); |
| 54 | std::thread::sleep(Duration::from_millis(1)); |
| 55 | } |
| 56 | } |
| 57 | completed(&model); |
| 58 | assert_eq!(model.entries.len(), count); |
| 59 | let id = model |
| 60 | .entries |
| 61 | .iter() |
| 62 | .find(|entry| entry.name == name) |
| 63 | .unwrap() |
| 64 | .id; |
| 65 | model.selected.insert(id); |
| 66 | assert!(model.begin_preview(&mut factory)); |
| 67 | let questions = finish_preview_catalog(&mut model, allow); |
| 68 | assert_eq!(questions, usize::from(count > 10_000)); |
| 69 | if !allow { |
| 70 | assert!(model.preview.is_none()); |
| 71 | assert_ne!(model.terminal.as_ref().unwrap().outcome, Outcome::Completed); |
| 72 | } else if bytes > if image { 8 * 1024 * 1024 } else { 1024 * 1024 } { |
| 73 | assert_eq!( |
| 74 | model.terminal.as_ref().unwrap().error, |
| 75 | Some(ErrorCode::Limit) |
| 76 | ); |
| 77 | assert!(model.preview.is_none()); |
| 78 | assert!(!model.can_retry_expanded()); |
| 79 | } else { |
| 80 | completed(&model); |
| 81 | assert_eq!(model.preview.as_ref().unwrap().data, vec![b'x'; bytes]); |
| 82 | } |
| 83 | assert!(model.outputs.is_empty()); |
| 84 | assert_eq!(model.selected, [id].into_iter().collect()); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn preview_catalog_at_default_entry_limit_needs_no_approval() { |
| 89 | preview_catalog(10_000, 7, true, false); |
| 90 | } |
| 91 | |
| 92 | #[test] |
| 93 | fn preview_catalog_over_default_entry_limit_requires_new_approval() { |
| 94 | preview_catalog(10_001, 7, true, false); |
| 95 | } |
| 96 | |
| 97 | #[test] |
| 98 | fn preview_catalog_approval_does_not_raise_text_size_limit() { |
| 99 | preview_catalog(10_001, 1024 * 1024 + 1, true, false); |
| 100 | } |
| 101 | |
| 102 | #[test] |
| 103 | fn preview_catalog_denial_does_not_show_data() { |
| 104 | preview_catalog(10_001, 7, false, false); |
| 105 | } |
| 106 | |
| 107 | #[test] |
| 108 | fn preview_catalog_approval_does_not_raise_image_size_limit() { |
| 109 | preview_catalog(10_001, 8 * 1024 * 1024 + 1, true, true); |
| 110 | } |
| 111 | |
| 112 | fn preview_catalog_bytes(total: u64, allow: bool) { |
| 113 | let scratch = Scratch::new(); |
| 114 | let archive = scratch.path("catalog-bytes.zip"); |
| 115 | // 선택하지 않은 큰 항목은 해제하지 않고 ZIP64 목록 예산만 검증함. |
| 116 | let generated = std::process::Command::new("python3") |
| 117 | .arg("-c") |
| 118 | .arg( |
| 119 | r#" |
| 120 | import struct, sys, zlib |
| 121 | central = [] |
| 122 | with open(sys.argv[1], 'wb') as f: |
| 123 | for name, size, compressed, payload in [ |
| 124 | (b'small.txt', 7, 7, b'preview'), |
| 125 | (b'large.bin', int(sys.argv[2]) - 7, 75 * 1024 * 1024, None), |
| 126 | ]: |
| 127 | offset = f.tell() |
| 128 | crc = zlib.crc32(payload) if payload else 0 |
| 129 | method = 0 if payload else 8 |
| 130 | extra = struct.pack('<HHQQ', 1, 16, size, compressed) |
| 131 | f.write(struct.pack('<IHHHHHIIIHH', 0x04034b50, 45, 0, method, 0, 0, |
| 132 | crc, 0xffffffff, 0xffffffff, len(name), len(extra))) |
| 133 | f.write(name + extra) |
| 134 | if payload: |
| 135 | f.write(payload) |
| 136 | else: |
| 137 | f.seek(compressed, 1) |
| 138 | central.append(struct.pack('<IHHHHHHIIIHHHHHII', 0x02014b50, 45, 45, |
| 139 | 0, method, 0, 0, crc, 0xffffffff, 0xffffffff, |
| 140 | len(name), len(extra), 0, 0, 0, 0, offset) |
| 141 | + name + extra) |
| 142 | offset = f.tell() |
| 143 | data = b''.join(central) |
| 144 | f.write(data) |
| 145 | f.write(struct.pack('<IHHHHIIH', 0x06054b50, 0, 0, 2, 2, len(data), offset, 0)) |
| 146 | "#, |
| 147 | ) |
| 148 | .arg(&archive) |
| 149 | .arg(total.to_string()) |
| 150 | .output() |
| 151 | .unwrap(); |
| 152 | assert!(generated.status.success(), "{:?}", generated); |
| 153 | let mut factory = ProcessFactory::new(binary("zipelo-archive-worker")); |
| 154 | let mut model = Model::default(); |
| 155 | assert!(model.begin(Start::List { archive }, &mut factory)); |
| 156 | wait(&mut model); |
| 157 | if total > Limits::default().output { |
| 158 | assert_eq!( |
| 159 | model.terminal.as_ref().unwrap().error, |
| 160 | Some(ErrorCode::Limit) |
| 161 | ); |
| 162 | assert!(model.retry_expanded(&mut factory)); |
| 163 | finish_catalog_bytes(&mut model, true, ActionKind::List); |
| 164 | } |
| 165 | completed(&model); |
| 166 | assert_eq!(model.entries.len(), 2); |
| 167 | assert_eq!( |
| 168 | model.entries.iter().map(|e| e.bytes.unwrap()).sum::<u64>(), |
| 169 | total |
| 170 | ); |
| 171 | let id = model |
| 172 | .entries |
| 173 | .iter() |
| 174 | .find(|e| e.name == "small.txt") |
| 175 | .unwrap() |
| 176 | .id; |
| 177 | model.selected.insert(id); |
| 178 | assert!(model.begin_preview(&mut factory)); |
| 179 | assert_eq!(model.estimated_usage().estimated_bytes, Some(total)); |
| 180 | let questions = finish_catalog_bytes(&mut model, allow, ActionKind::Preview); |
| 181 | assert_eq!(questions, usize::from(total > Limits::default().output)); |
| 182 | if allow { |
| 183 | completed(&model); |
| 184 | assert_eq!(model.preview.as_ref().unwrap().data, b"preview"); |
| 185 | } else { |
| 186 | assert!(model.preview.is_none()); |
| 187 | assert_ne!(model.terminal.as_ref().unwrap().outcome, Outcome::Completed); |
| 188 | } |
| 189 | assert!(model.outputs.is_empty()); |
| 190 | assert_eq!(model.selected, [id].into_iter().collect()); |
| 191 | } |
| 192 | |
| 193 | fn finish_catalog_bytes(model: &mut Model, allow: bool, action: ActionKind) -> usize { |
| 194 | let deadline = Instant::now() + Duration::from_secs(30); |
| 195 | let mut questions = 0; |
| 196 | while model.busy() { |
| 197 | model.poll(); |
| 198 | if let Some(question) = model.resource.clone() { |
| 199 | assert!(question.scope.output); |
| 200 | assert!(!question.scope.ratio); |
| 201 | assert_eq!(question.action, action); |
| 202 | assert!(model.resource_reply(model.job_id().unwrap(), question.id, allow)); |
| 203 | questions += 1; |
| 204 | } |
| 205 | assert!(Instant::now() < deadline, "{:?}", model.error); |
| 206 | std::thread::sleep(Duration::from_millis(1)); |
| 207 | } |
| 208 | assert!(model.error.is_none(), "{:?}", model.error); |
| 209 | questions |
| 210 | } |
| 211 | |
| 212 | #[test] |
| 213 | fn preview_catalog_at_default_byte_limit_needs_no_approval() { |
| 214 | preview_catalog_bytes(Limits::default().output, true); |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn preview_catalog_over_default_byte_limit_requires_new_approval() { |
| 219 | preview_catalog_bytes(Limits::default().output + 1, true); |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | fn preview_catalog_byte_approval_denial_does_not_show_data() { |
| 224 | preview_catalog_bytes(Limits::default().output + 1, false); |
| 225 | } |