team-ljyzipelo

공개

미리보기 공용 타입과 전송 코덱을 추가한다

작성자
시로사키 하나
작성 시각
커밋
68e13d1ae94df1b522efb1b17885446e17a0ea80
부모
9ba6e05
이 시점의 파일 보기

변경된 파일 6개

+606-4
수정experiments/rust-archive-worker/README.md+1-1
@@ -132,7 +132,7 @@ Python 표준 bz2, lzma와 tarfile로 독립 생성 및 해제한 결과의 SHA2
132132
133133## 미리보기 계약
134134
135삭제: 상태: frontend의 32 KiB 청크, 64개 수신 큐와 버퍼 및 취소 기준에 backend가 동의했다. 아래 요청과 결과 타입 명세를 공유하며 아직 protocol.rs, wire.rs와 제품 동작에 적용하지 않았다. 확정된 범위는 [PLANS 8.7](../../docs/PLANS.md)을 따른다. 8.8 분할 7z는 이 구현에서 제외한다.
135추가: 상태: frontend와 backend의 계약 합의가 완료되었다. protocol.rs와 wire.rs에 공용 타입, 전송 규칙과 상한 검증을 반영했다. 작업자 디코더와 앱 연동 전이므로 미리보기 실행은 UnsupportedFeature로 끝나며 결과를 생성하지 않는다. 확정된 범위는 [PLANS 8.7](../../docs/PLANS.md)을 따른다. 8.8 분할 7z는 이 구현에서 제외한다.
136136
137137### 요청과 수명
138138
수정experiments/rust-archive-worker/src/protocol.rs+64-1
@@ -1,5 +1,12 @@
11use std::path::{Path, PathBuf};
22
3추가: pub const MAX_PREVIEW_TEXT_BYTES: u64 = 1024 * 1024;
4추가: pub const MAX_PREVIEW_IMAGE_BYTES: u64 = 8 * 1024 * 1024;
5추가: pub const MAX_PREVIEW_RGBA_BYTES: u64 = 64 * 1024 * 1024;
6추가: pub const MAX_PREVIEW_DIMENSION: u32 = 8192;
7추가: pub const MAX_PREVIEW_PIXELS: u64 = 16_000_000;
8추가: pub const MAX_PREVIEW_CHUNK_BYTES: usize = 32 * 1024;
9추가:
310#[derive(Clone, Debug, PartialEq, Eq)]
411pub enum NativePath {
512 Unix(Vec<u8>),
@@ -96,7 +103,12 @@ impl GuardedStart {
96103 && self.ceiling.output <= 64 * 1024 * 1024 * 1024
97104 && self.ceiling.entries >= self.start.limits.entries
98105 && self.ceiling.entries <= 200_000
99삭제: && (!self.interactive || matches!(self.start.action, Action::Extract { .. }))
106추가: && (!self.interactive
107추가: || matches!(
108추가: self.start.action,
109추가: Action::Extract { .. } | Action::Preview { .. }
110추가: ))
111추가: && (!matches!(self.start.action, Action::Preview { .. }) || self.interactive)
100112 }
101113}
102114#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@@ -185,12 +197,18 @@ pub enum Action {
185197 method: Method,
186198 encryption: Encryption,
187199 },
200추가: Preview {
201추가: archive: NativePath,
202추가: entry_id: u32,
203추가: list_id: ListId,
204추가: },
188205}
189206#[derive(Clone, Copy, Debug, PartialEq, Eq)]
190207pub enum ActionKind {
191208 List,
192209 Extract,
193210 CreateZip,
211추가: Preview,
194212}
195213impl Action {
196214 pub fn kind(&self) -> ActionKind {
@@ -198,6 +216,7 @@ impl Action {
198216 Self::List { .. } => ActionKind::List,
199217 Self::Extract { .. } => ActionKind::Extract,
200218 Self::CreateZip { .. } => ActionKind::CreateZip,
219추가: Self::Preview { .. } => ActionKind::Preview,
201220 }
202221 }
203222}
@@ -223,6 +242,39 @@ pub struct Entry {
223242 pub encrypted: bool,
224243}
225244#[derive(Clone, Copy, Debug, PartialEq, Eq)]
245추가: pub enum PreviewFormat {
246추가: Utf8Text,
247추가: Rgba8,
248추가: }
249추가: #[derive(Clone, Copy, Debug, PartialEq, Eq)]
250추가: pub struct PreviewInfo {
251추가: pub entry_id: u32,
252추가: pub format: PreviewFormat,
253추가: pub total_bytes: u64,
254추가: pub width: u32,
255추가: pub height: u32,
256추가: }
257추가: impl PreviewInfo {
258추가: pub fn valid(&self) -> bool {
259추가: match self.format {
260추가: PreviewFormat::Utf8Text => {
261추가: self.width == 0 && self.height == 0 && self.total_bytes <= MAX_PREVIEW_TEXT_BYTES
262추가: }
263추가: PreviewFormat::Rgba8 => {
264추가: if !(1..=MAX_PREVIEW_DIMENSION).contains(&self.width)
265추가: || !(1..=MAX_PREVIEW_DIMENSION).contains(&self.height)
266추가: {
267추가: return false;
268추가: }
269추가: let pixels = u64::from(self.width) * u64::from(self.height);
270추가: pixels <= MAX_PREVIEW_PIXELS
271추가: && pixels.checked_mul(4) == Some(self.total_bytes)
272추가: && self.total_bytes <= MAX_PREVIEW_RGBA_BYTES
273추가: }
274추가: }
275추가: }
276추가: }
277추가: #[derive(Clone, Copy, Debug, PartialEq, Eq)]
226278pub enum Stage {
227279 Scan,
228280 Read,
@@ -374,6 +426,17 @@ pub enum Message {
374426 relative_name: String,
375427 disposition: Disposition,
376428 },
429추가: PreviewBegin(PreviewInfo),
430추가: PreviewChunk {
431추가: entry_id: u32,
432추가: offset: u64,
433추가: data: Vec<u8>,
434추가: },
435추가: PreviewEnd {
436추가: entry_id: u32,
437추가: total_bytes: u64,
438추가: sha256: [u8; 32],
439추가: },
377440 Terminal(Terminal),
378441 CancelAck {
379442 request_sequence: u64,
수정experiments/rust-archive-worker/src/runtime.rs+2-0
@@ -649,6 +649,8 @@ impl<W: Write> Job<W> {
649649 }
650650 }
651651 }
652추가: // 미리보기 실행은 디코더 연결 전까지 지원하지 않음.
653추가: Action::Preview { .. } => return Err(ErrorCode::UnsupportedFeature),
652654 Action::CreateZip {
653655 sources,
654656 output_file,
수정experiments/rust-archive-worker/src/wire.rs+119-2
@@ -138,7 +138,8 @@ macro_rules! enumeration { ($t:ident {$($v:ident=$n:expr),*$(,)?}) => { impl Wir
138138enumeration!(Format{Zip=0,Tar=1,Gzip=2,TarGzip=3,SevenZip=4});
139139enumeration!(Method{Store=0,Deflate=1});
140140enumeration!(Encryption{None=0,Aes256=1});
141삭제: enumeration!(ActionKind{List=0,Extract=1,CreateZip=2});
141추가: enumeration!(ActionKind{List=0,Extract=1,CreateZip=2,Preview=3});
142추가: enumeration!(PreviewFormat{Utf8Text=0,Rgba8=1});
142143enumeration!(EntryKind{File=0,Directory=1,Link=2,Other=3});
143144enumeration!(Stage{Scan=0,Read=1,Decode=2,Write=3,Verify=4,Commit=5});
144145enumeration!(Disposition{Created=0,PartialRetained=1,Removed=2,CommitUncertain=3});
@@ -166,6 +167,31 @@ structure!(ListId {
166167 archive_bytes,
167168 archive_sha256
168169});
170추가: impl Wire for PreviewInfo {
171추가: fn enc(&self, e: &mut Encoder) -> io::Result<()> {
172추가: if !self.valid() {
173추가: return Err(invalid());
174추가: }
175추가: self.entry_id.enc(e)?;
176추가: self.format.enc(e)?;
177추가: self.total_bytes.enc(e)?;
178추가: self.width.enc(e)?;
179추가: self.height.enc(e)
180추가: }
181추가: fn dec(d: &mut Decoder<'_>) -> io::Result<Self> {
182추가: let info = Self {
183추가: entry_id: Wire::dec(d)?,
184추가: format: Wire::dec(d)?,
185추가: total_bytes: Wire::dec(d)?,
186추가: width: Wire::dec(d)?,
187추가: height: Wire::dec(d)?,
188추가: };
189추가: if !info.valid() {
190추가: return Err(invalid());
191추가: }
192추가: Ok(info)
193추가: }
194추가: }
169195structure!(Source { path, archive_name });
170196structure!(Start { action, limits });
171197structure!(GuardedStart {
@@ -309,6 +335,16 @@ impl Wire for Action {
309335 method.enc(e)?;
310336 encryption.enc(e)
311337 }
338추가: Self::Preview {
339추가: archive,
340추가: entry_id,
341추가: list_id,
342추가: } => {
343추가: 3u8.enc(e)?;
344추가: archive.enc(e)?;
345추가: entry_id.enc(e)?;
346추가: list_id.enc(e)
347추가: }
312348 }
313349 }
314350 fn dec(d: &mut Decoder<'_>) -> io::Result<Self> {
@@ -329,10 +365,21 @@ impl Wire for Action {
329365 method: Wire::dec(d)?,
330366 encryption: Wire::dec(d)?,
331367 }),
368추가: 3 => Ok(Self::Preview {
369추가: archive: Wire::dec(d)?,
370추가: entry_id: Wire::dec(d)?,
371추가: list_id: Wire::dec(d)?,
372추가: }),
332373 _ => Err(invalid()),
333374 }
334375 }
335376}
377추가: fn valid_preview_chunk(offset: u64, length: usize) -> bool {
378추가: (1..=MAX_PREVIEW_CHUNK_BYTES).contains(&length)
379추가: && offset
380추가: .checked_add(length as u64)
381추가: .is_some_and(|end| end <= MAX_PREVIEW_RGBA_BYTES)
382추가: }
336383impl Message {
337384 fn encode(&self) -> io::Result<(u16, Vec<u8>)> {
338385 let mut e = Encoder {
@@ -341,6 +388,9 @@ impl Message {
341388 };
342389 let kind = match self {
343390 Self::Start(v) => {
391추가: if matches!(v.action, Action::Preview { .. }) {
392추가: return Err(invalid());
393추가: }
344394 v.enc(&mut e)?;
345395 1
346396 }
@@ -456,6 +506,37 @@ impl Message {
456506 disposition.enc(&mut e)?;
457507 105
458508 }
509추가: Self::PreviewBegin(info) => {
510추가: info.enc(&mut e)?;
511추가: 111
512추가: }
513추가: Self::PreviewChunk {
514추가: entry_id,
515추가: offset,
516추가: data,
517추가: } => {
518추가: if !valid_preview_chunk(*offset, data.len()) {
519추가: return Err(invalid());
520추가: }
521추가: entry_id.enc(&mut e)?;
522추가: offset.enc(&mut e)?;
523추가: (data.len() as u32).enc(&mut e)?;
524추가: e.put(data)?;
525추가: 112
526추가: }
527추가: Self::PreviewEnd {
528추가: entry_id,
529추가: total_bytes,
530추가: sha256,
531추가: } => {
532추가: if *total_bytes > MAX_PREVIEW_RGBA_BYTES {
533추가: return Err(invalid());
534추가: }
535추가: entry_id.enc(&mut e)?;
536추가: total_bytes.enc(&mut e)?;
537추가: sha256.enc(&mut e)?;
538추가: 113
539추가: }
459540 Self::Terminal(v) => {
460541 if v.detail.len() > 4096 {
461542 return Err(invalid());
@@ -509,7 +590,13 @@ impl Message {
509590 },
510591 };
511592 let m = match kind {
512삭제: 1 => Self::Start(Wire::dec(&mut d)?),
593추가: 1 => {
594추가: let start = Start::dec(&mut d)?;
595추가: if matches!(start.action, Action::Preview { .. }) {
596추가: return Err(invalid());
597추가: }
598추가: Self::Start(start)
599추가: }
513600 2 => {
514601 let challenge_id = Wire::dec(&mut d)?;
515602 let secret = Secret(Vec::<u8>::dec(&mut d)?);
@@ -585,6 +672,36 @@ impl Message {
585672 relative_name: Wire::dec(&mut d)?,
586673 disposition: Wire::dec(&mut d)?,
587674 },
675추가: 111 => Self::PreviewBegin(Wire::dec(&mut d)?),
676추가: 112 => {
677추가: let entry_id = Wire::dec(&mut d)?;
678추가: let offset = Wire::dec(&mut d)?;
679추가: let length = u32::dec(&mut d)? as usize;
680추가: if !valid_preview_chunk(offset, length) {
681추가: return Err(invalid());
682추가: }
683추가: let bytes = d.take(length)?;
684추가: let mut data = Vec::new();
685추가: data.try_reserve_exact(length).map_err(|_| invalid())?;
686추가: data.extend_from_slice(bytes);
687추가: Self::PreviewChunk {
688추가: entry_id,
689추가: offset,
690추가: data,
691추가: }
692추가: }
693추가: 113 => {
694추가: let entry_id = Wire::dec(&mut d)?;
695추가: let total_bytes = u64::dec(&mut d)?;
696추가: if total_bytes > MAX_PREVIEW_RGBA_BYTES {
697추가: return Err(invalid());
698추가: }
699추가: Self::PreviewEnd {
700추가: entry_id,
701추가: total_bytes,
702추가: sha256: Wire::dec(&mut d)?,
703추가: }
704추가: }
588705 106 => {
589706 let t = Terminal::dec(&mut d)?;
590707 if t.detail.len() > 4096 {
수정tests/rust-archive-worker/main.rs+2-0
@@ -1612,6 +1612,8 @@ mod zip_names;
16121612
16131613mod conflict_wire;
16141614
1615추가: mod preview_wire;
1616추가:
16151617mod conflicts;
16161618
16171619mod compressed_tar;
추가tests/rust-archive-worker/preview_wire.rs+418-0
@@ -0,0 +1,418 @@
1추가: use super::*;
2추가: use sha2::{Digest, Sha256};
3추가:
4추가: fn list_id(windows: bool) -> ListId {
5추가: ListId {
6추가: catalog_version: 1,
7추가: format: Format::Zip,
8추가: source_object: if windows {
9추가: SourceObject::Windows {
10추가: volume_serial: 19,
11추가: file_id: [23; 16],
12추가: }
13추가: } else {
14추가: SourceObject::Unix {
15추가: device: 19,
16추가: inode: 23,
17추가: }
18추가: },
19추가: archive_bytes: 37,
20추가: archive_sha256: [41; 32],
21추가: }
22추가: }
23추가:
24추가: fn guarded(windows: bool) -> GuardedStart {
25추가: GuardedStart {
26추가: start: Start {
27추가: action: Action::Preview {
28추가: archive: if windows {
29추가: NativePath::Windows("C:\\archive.zip".encode_utf16().collect())
30추가: } else {
31추가: NativePath::Unix(b"/archive.zip".to_vec())
32추가: },
33추가: entry_id: 13,
34추가: list_id: list_id(windows),
35추가: },
36추가: limits: Limits::default(),
37추가: },
38추가: ceiling: Limits::default(),
39추가: interactive: true,
40추가: }
41추가: }
42추가:
43추가: fn encoded(message: &Message) -> Vec<u8> {
44추가: let mut bytes = Vec::new();
45추가: Writer::new(&mut bytes, 17).send(message).unwrap();
46추가: bytes
47추가: }
48추가:
49추가: fn decoded(bytes: &[u8]) -> Message {
50추가: let mut reader = Reader::new(bytes);
51추가: let (job, sequence, message) = reader.read().unwrap().unwrap();
52추가: assert_eq!((job, sequence), (17, 0));
53추가: assert!(reader.read().unwrap().is_none());
54추가: message
55추가: }
56추가:
57추가: fn raw(kind: u16, body: &[u8]) -> Vec<u8> {
58추가: let mut bytes = b"ZAW1".to_vec();
59추가: bytes.extend(1u16.to_le_bytes());
60추가: bytes.extend(kind.to_le_bytes());
61추가: bytes.extend((body.len() as u32).to_le_bytes());
62추가: bytes.extend([0; 4]);
63추가: bytes.extend(17u64.to_le_bytes());
64추가: bytes.extend(0u64.to_le_bytes());
65추가: bytes.extend(body);
66추가: bytes
67추가: }
68추가:
69추가: fn metadata(info: PreviewInfo) -> Vec<u8> {
70추가: let mut body = info.entry_id.to_le_bytes().to_vec();
71추가: body.push(match info.format {
72추가: PreviewFormat::Utf8Text => 0,
73추가: PreviewFormat::Rgba8 => 1,
74추가: });
75추가: body.extend(info.total_bytes.to_le_bytes());
76추가: body.extend(info.width.to_le_bytes());
77추가: body.extend(info.height.to_le_bytes());
78추가: raw(111, &body)
79추가: }
80추가:
81추가: fn reject_encode(message: Message) {
82추가: let mut bytes = Vec::new();
83추가: assert_eq!(
84추가: Writer::new(&mut bytes, 17)
85추가: .send(&message)
86추가: .unwrap_err()
87추가: .kind(),
88추가: std::io::ErrorKind::InvalidData
89추가: );
90추가: assert!(bytes.is_empty());
91추가: }
92추가:
93추가: fn reject_decode(bytes: &[u8]) {
94추가: assert!(Reader::new(bytes).read().is_err());
95추가: }
96추가:
97추가: #[test]
98추가: fn preview_guarded_request_preserves_identity_and_field_order() {
99추가: for windows in [false, true] {
100추가: let start = guarded(windows);
101추가: assert!(start.valid());
102추가: assert_eq!(start.start.action.kind(), ActionKind::Preview);
103추가: let bytes = encoded(&Message::StartGuarded(start.clone()));
104추가: assert_eq!(&bytes[..8], b"ZAW1\x01\x00\x06\x00");
105추가: assert_eq!(bytes[32], 3);
106추가: let (path_bytes, object_bytes) = if windows {
107추가: ("C:\\archive.zip".encode_utf16().count() * 2, 1 + 8 + 16)
108추가: } else {
109추가: (b"/archive.zip".len(), 1 + 8 + 8)
110추가: };
111추가: let id_at = 32 + 1 + 1 + 4 + path_bytes;
112추가: assert_eq!(&bytes[id_at..id_at + 4], &13u32.to_le_bytes());
113추가: assert_eq!(&bytes[id_at + 4..id_at + 7], &[1, 0, 0]);
114추가: let hash_at = id_at + 4 + 2 + 1 + object_bytes + 8;
115추가: assert_eq!(&bytes[hash_at..hash_at + 32], &[41; 32]);
116추가: match decoded(&bytes) {
117추가: Message::StartGuarded(actual) => assert_eq!(actual, start),
118추가: other => panic!("{other:?}"),
119추가: }
120추가: }
121추가: }
122추가:
123추가: #[test]
124추가: fn preview_requires_guarded_interactive_requests_in_both_directions() {
125추가: let start = guarded(false);
126추가: reject_encode(Message::Start(start.start.clone()));
127추가: reject_encode(Message::StartInteractive(start.start.clone()));
128추가: let bytes = encoded(&Message::StartGuarded(start.clone()));
129추가: let tail = 8 + 8 + 4 + 1;
130추가: for kind in [1, 4] {
131추가: reject_decode(&raw(kind, &bytes[32..bytes.len() - tail]));
132추가: }
133추가: let mut noninteractive = start.clone();
134추가: noninteractive.interactive = false;
135추가: assert!(!noninteractive.valid());
136추가: reject_encode(Message::StartGuarded(noninteractive));
137추가: let mut bytes = bytes;
138추가: *bytes.last_mut().unwrap() = 0;
139추가: reject_decode(&bytes);
140추가: }
141추가:
142추가: #[test]
143추가: fn preview_request_rejects_missing_or_malformed_list_identity() {
144추가: for windows in [false, true] {
145추가: let bytes = encoded(&Message::StartGuarded(guarded(windows)));
146추가: for length in 33..bytes.len() {
147추가: reject_decode(&raw(6, &bytes[32..length]));
148추가: }
149추가: let mut trailing = bytes.clone();
150추가: trailing.push(0);
151추가: let length = (trailing.len() - 32) as u32;
152추가: trailing[8..12].copy_from_slice(&length.to_le_bytes());
153추가: reject_decode(&trailing);
154추가: }
155추가: }
156추가:
157추가: #[test]
158추가: fn preview_metadata_enforces_text_dimension_and_pixel_boundaries() {
159추가: for (format, width, height, total_bytes, valid) in [
160추가: (PreviewFormat::Utf8Text, 0, 0, 0, true),
161추가: (PreviewFormat::Utf8Text, 0, 0, MAX_PREVIEW_TEXT_BYTES, true),
162추가: (
163추가: PreviewFormat::Utf8Text,
164추가: 0,
165추가: 0,
166추가: MAX_PREVIEW_TEXT_BYTES + 1,
167추가: false,
168추가: ),
169추가: (PreviewFormat::Utf8Text, 1, 0, 0, false),
170추가: (PreviewFormat::Utf8Text, 0, 1, 0, false),
171추가: (PreviewFormat::Rgba8, 1, 1, 4, true),
172추가: (PreviewFormat::Rgba8, 8192, 1, 8192 * 4, true),
173추가: (PreviewFormat::Rgba8, 1, 8192, 8192 * 4, true),
174추가: (PreviewFormat::Rgba8, 8193, 1, 8193 * 4, false),
175추가: (PreviewFormat::Rgba8, 1, 8193, 8193 * 4, false),
176추가: (PreviewFormat::Rgba8, 4000, 4000, 64_000_000, true),
177추가: (PreviewFormat::Rgba8, 4001, 4000, 64_016_000, false),
178추가: (PreviewFormat::Rgba8, 8192, 8192, 268_435_456, false),
179추가: (PreviewFormat::Rgba8, 0, 1, 0, false),
180추가: (PreviewFormat::Rgba8, 1, 0, 0, false),
181추가: (PreviewFormat::Rgba8, 1, 1, 3, false),
182추가: (PreviewFormat::Rgba8, 1, 1, 5, false),
183추가: (PreviewFormat::Rgba8, u32::MAX, u32::MAX, u64::MAX, false),
184추가: ] {
185추가: let info = PreviewInfo {
186추가: entry_id: 13,
187추가: format,
188추가: total_bytes,
189추가: width,
190추가: height,
191추가: };
192추가: assert_eq!(info.valid(), valid, "{info:?}");
193추가: let raw = metadata(info);
194추가: if valid {
195추가: assert_eq!(encoded(&Message::PreviewBegin(info)), raw);
196추가: match decoded(&raw) {
197추가: Message::PreviewBegin(actual) => assert_eq!(actual, info),
198추가: other => panic!("{other:?}"),
199추가: }
200추가: } else {
201추가: reject_encode(Message::PreviewBegin(info));
202추가: reject_decode(&raw);
203추가: }
204추가: }
205추가: }
206추가:
207추가: #[test]
208추가: fn preview_metadata_rejects_unknown_format_and_truncated_frames() {
209추가: let bytes = metadata(PreviewInfo {
210추가: entry_id: 13,
211추가: format: PreviewFormat::Rgba8,
212추가: total_bytes: 4,
213추가: width: 1,
214추가: height: 1,
215추가: });
216추가: let mut unknown = bytes.clone();
217추가: unknown[36] = 2;
218추가: reject_decode(&unknown);
219추가: for length in 1..bytes.len() {
220추가: reject_decode(&bytes[..length]);
221추가: }
222추가: let mut oversized = bytes;
223추가: oversized[8..12]
224추가: .copy_from_slice(&((zipelo_archive_worker::wire::MAX_BODY + 1) as u32).to_le_bytes());
225추가: reject_decode(&oversized);
226추가: }
227추가:
228추가: #[test]
229추가: fn preview_chunks_have_bounded_raw_bytes_and_exact_offsets() {
230추가: for length in [1, MAX_PREVIEW_CHUNK_BYTES] {
231추가: for offset in [0, MAX_PREVIEW_RGBA_BYTES - length as u64] {
232추가: let data = (0..length).map(|i| (i % 256) as u8).collect::<Vec<_>>();
233추가: let bytes = encoded(&Message::PreviewChunk {
234추가: entry_id: 13,
235추가: offset,
236추가: data: data.clone(),
237추가: });
238추가: let mut body = 13u32.to_le_bytes().to_vec();
239추가: body.extend(offset.to_le_bytes());
240추가: body.extend((length as u32).to_le_bytes());
241추가: body.extend(&data);
242추가: assert_eq!(bytes, raw(112, &body));
243추가: assert!(bytes.len() - 32 < zipelo_archive_worker::wire::MAX_BODY);
244추가: match decoded(&bytes) {
245추가: Message::PreviewChunk {
246추가: entry_id,
247추가: offset: actual,
248추가: data: result,
249추가: } => {
250추가: assert_eq!(entry_id, 13);
251추가: assert_eq!(actual, offset);
252추가: assert_eq!(result, data);
253추가: }
254추가: other => panic!("{other:?}"),
255추가: }
256추가: }
257추가: }
258추가: }
259추가:
260추가: #[test]
261추가: fn preview_chunks_reject_empty_oversized_overflow_and_short_payloads() {
262추가: for (offset, length) in [
263추가: (0, 0),
264추가: (0, MAX_PREVIEW_CHUNK_BYTES + 1),
265추가: (MAX_PREVIEW_RGBA_BYTES, 1),
266추가: (u64::MAX, 1),
267추가: ] {
268추가: reject_encode(Message::PreviewChunk {
269추가: entry_id: 13,
270추가: offset,
271추가: data: vec![0; length],
272추가: });
273추가: let mut body = 13u32.to_le_bytes().to_vec();
274추가: body.extend(offset.to_le_bytes());
275추가: body.extend((length as u32).to_le_bytes());
276추가: body.extend(vec![0; length]);
277추가: reject_decode(&raw(112, &body));
278추가: }
279추가: for declared in [1, MAX_PREVIEW_CHUNK_BYTES as u32, u32::MAX] {
280추가: let mut body = 13u32.to_le_bytes().to_vec();
281추가: body.extend(0u64.to_le_bytes());
282추가: body.extend(declared.to_le_bytes());
283추가: reject_decode(&raw(112, &body));
284추가: }
285추가: let mut body = 13u32.to_le_bytes().to_vec();
286추가: body.extend(0u64.to_le_bytes());
287추가: body.extend(1u32.to_le_bytes());
288추가: body.extend([0, 0]);
289추가: reject_decode(&raw(112, &body));
290추가: }
291추가:
292추가: #[test]
293추가: fn preview_end_preserves_total_hash_and_global_result_bound() {
294추가: for total_bytes in [0, MAX_PREVIEW_TEXT_BYTES, MAX_PREVIEW_RGBA_BYTES] {
295추가: let bytes = encoded(&Message::PreviewEnd {
296추가: entry_id: 13,
297추가: total_bytes,
298추가: sha256: [47; 32],
299추가: });
300추가: let mut body = 13u32.to_le_bytes().to_vec();
301추가: body.extend(total_bytes.to_le_bytes());
302추가: body.extend([47; 32]);
303추가: assert_eq!(bytes, raw(113, &body));
304추가: match decoded(&bytes) {
305추가: Message::PreviewEnd {
306추가: entry_id,
307추가: total_bytes: actual,
308추가: sha256,
309추가: } => {
310추가: assert_eq!(entry_id, 13);
311추가: assert_eq!(actual, total_bytes);
312추가: assert_eq!(sha256, [47; 32]);
313추가: }
314추가: other => panic!("{other:?}"),
315추가: }
316추가: for length in 32..bytes.len() {
317추가: reject_decode(&raw(113, &bytes[32..length]));
318추가: }
319추가: }
320추가: reject_encode(Message::PreviewEnd {
321추가: entry_id: 13,
322추가: total_bytes: MAX_PREVIEW_RGBA_BYTES + 1,
323추가: sha256: [0; 32],
324추가: });
325추가: let mut body = 13u32.to_le_bytes().to_vec();
326추가: body.extend((MAX_PREVIEW_RGBA_BYTES + 1).to_le_bytes());
327추가: body.extend([0; 32]);
328추가: reject_decode(&raw(113, &body));
329추가: }
330추가:
331추가: #[test]
332추가: fn preview_stream_carries_utf8_split_inside_a_code_point() {
333추가: let text = "a".repeat(MAX_PREVIEW_CHUNK_BYTES - 1) + "가";
334추가: let info = PreviewInfo {
335추가: entry_id: 13,
336추가: format: PreviewFormat::Utf8Text,
337추가: total_bytes: text.len() as u64,
338추가: width: 0,
339추가: height: 0,
340추가: };
341추가: let mut bytes = Vec::new();
342추가: let mut writer = Writer::new(&mut bytes, 17);
343추가: writer.send(&Message::PreviewBegin(info)).unwrap();
344추가: for (index, chunk) in text.as_bytes().chunks(MAX_PREVIEW_CHUNK_BYTES).enumerate() {
345추가: writer
346추가: .send(&Message::PreviewChunk {
347추가: entry_id: 13,
348추가: offset: (index * MAX_PREVIEW_CHUNK_BYTES) as u64,
349추가: data: chunk.to_vec(),
350추가: })
351추가: .unwrap();
352추가: }
353추가: let expected: [u8; 32] = Sha256::digest(text.as_bytes()).into();
354추가: writer
355추가: .send(&Message::PreviewEnd {
356추가: entry_id: 13,
357추가: total_bytes: info.total_bytes,
358추가: sha256: expected,
359추가: })
360추가: .unwrap();
361추가: let mut reader = Reader::new(bytes.as_slice());
362추가: assert!(
363추가: matches!(reader.read().unwrap().unwrap().2, Message::PreviewBegin(actual) if actual == info)
364추가: );
365추가: let mut result = Vec::new();
366추가: for sequence in 1..=2 {
367추가: let (job, actual_sequence, message) = reader.read().unwrap().unwrap();
368추가: assert_eq!((job, actual_sequence), (17, sequence));
369추가: let Message::PreviewChunk {
370추가: entry_id,
371추가: offset,
372추가: data,
373추가: } = message
374추가: else {
375추가: panic!("{message:?}");
376추가: };
377추가: assert_eq!(entry_id, 13);
378추가: assert_eq!(offset, result.len() as u64);
379추가: assert!(std::str::from_utf8(&data).is_err());
380추가: result.extend(data);
381추가: }
382추가: assert_eq!(String::from_utf8(result).unwrap(), text);
383추가: match reader.read().unwrap().unwrap().2 {
384추가: Message::PreviewEnd {
385추가: entry_id,
386추가: total_bytes,
387추가: sha256,
388추가: } => {
389추가: assert_eq!(entry_id, 13);
390추가: assert_eq!(total_bytes, info.total_bytes);
391추가: assert_eq!(sha256, expected);
392추가: }
393추가: other => panic!("{other:?}"),
394추가: }
395추가: assert!(reader.read().unwrap().is_none());
396추가: }
397추가:
398추가: #[test]
399추가: fn preview_action_kind_keeps_all_existing_tags() {
400추가: for (action, tag) in [
401추가: (ActionKind::List, 0),
402추가: (ActionKind::Extract, 1),
403추가: (ActionKind::CreateZip, 2),
404추가: (ActionKind::Preview, 3),
405추가: ] {
406추가: let bytes = encoded(&Message::Accepted {
407추가: action,
408추가: encryption: true,
409추가: });
410추가: assert_eq!(bytes, raw(100, &[tag, 1]));
411추가: assert!(
412추가: matches!(decoded(&bytes), Message::Accepted { action: actual, encryption: true } if actual == action)
413추가: );
414추가: }
415추가: reject_decode(&raw(100, &[4, 1]));
416추가: reject_decode(&raw(8, &[]));
417추가: reject_decode(&raw(114, &[]));
418추가: }