| 1 | use std::convert::Infallible; |
| 2 | |
| 3 | use sha2::{Digest, Sha256}; |
| 4 | use zipelo_memory_ustar_reader::{Completed, Error, Failure, Limits, NamePolicy, Request, read}; |
| 5 | |
| 6 | pub const BLOCK: usize = 512; |
| 7 | |
| 8 | pub struct Fixture<'a> { |
| 9 | pub name: &'a str, |
| 10 | pub prefix: &'a str, |
| 11 | pub data: Vec<u8>, |
| 12 | pub kind: u8, |
| 13 | } |
| 14 | |
| 15 | impl<'a> Fixture<'a> { |
| 16 | pub fn file(name: &'a str, data: &[u8]) -> Self { |
| 17 | Self { |
| 18 | name, |
| 19 | prefix: "", |
| 20 | data: data.to_vec(), |
| 21 | kind: b'0', |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | pub fn directory(name: &'a str) -> Self { |
| 26 | Self { |
| 27 | name, |
| 28 | prefix: "", |
| 29 | data: Vec::new(), |
| 30 | kind: b'5', |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub fn prefix(mut self, prefix: &'a str) -> Self { |
| 35 | self.prefix = prefix; |
| 36 | self |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // 정상 USTAR 표본만 만들며 거부 사례는 각 시험에서 한 조건씩 변경함. |
| 41 | pub fn archive(entries: &[Fixture<'_>]) -> Vec<u8> { |
| 42 | let mut bytes = Vec::new(); |
| 43 | for entry in entries { |
| 44 | assert!(entry.name.len() <= 100 && entry.prefix.len() <= 155); |
| 45 | let mut header = [0_u8; BLOCK]; |
| 46 | header[..entry.name.len()].copy_from_slice(entry.name.as_bytes()); |
| 47 | header[345..345 + entry.prefix.len()].copy_from_slice(entry.prefix.as_bytes()); |
| 48 | number(&mut header, 100, 8, 420); |
| 49 | number(&mut header, 108, 8, 0); |
| 50 | number(&mut header, 116, 8, 0); |
| 51 | number(&mut header, 124, 12, entry.data.len() as u64); |
| 52 | number(&mut header, 136, 12, 0); |
| 53 | header[156] = entry.kind; |
| 54 | header[257..263].copy_from_slice(b"ustar\0"); |
| 55 | header[263..265].copy_from_slice(b"00"); |
| 56 | checksum(&mut header); |
| 57 | bytes.extend_from_slice(&header); |
| 58 | bytes.extend_from_slice(&entry.data); |
| 59 | bytes.resize(bytes.len() + (BLOCK - entry.data.len() % BLOCK) % BLOCK, 0); |
| 60 | } |
| 61 | bytes.resize(bytes.len() + 2 * BLOCK, 0); |
| 62 | bytes |
| 63 | } |
| 64 | |
| 65 | fn number(header: &mut [u8], offset: usize, width: usize, value: u64) { |
| 66 | let text = format!("{value:0digits$o}", digits = width - 1); |
| 67 | assert_eq!(text.len(), width - 1); |
| 68 | header[offset..offset + width - 1].copy_from_slice(text.as_bytes()); |
| 69 | header[offset + width - 1] = 0; |
| 70 | } |
| 71 | |
| 72 | pub fn checksum(header: &mut [u8]) { |
| 73 | assert!(header.len() >= BLOCK); |
| 74 | header[148..156].fill(b' '); |
| 75 | let sum: u64 = header[..BLOCK].iter().map(|v| u64::from(*v)).sum(); |
| 76 | let text = format!("{sum:06o}"); |
| 77 | assert_eq!(text.len(), 6); |
| 78 | header[148..154].copy_from_slice(text.as_bytes()); |
| 79 | header[154] = 0; |
| 80 | header[155] = b' '; |
| 81 | } |
| 82 | |
| 83 | pub fn field(mut bytes: Vec<u8>, offset: usize, width: usize, value: &[u8]) -> Vec<u8> { |
| 84 | assert!(value.len() <= width); |
| 85 | bytes[offset..offset + width].fill(0); |
| 86 | bytes[offset..offset + value.len()].copy_from_slice(value); |
| 87 | checksum(&mut bytes[..BLOCK]); |
| 88 | bytes |
| 89 | } |
| 90 | |
| 91 | pub fn normal() -> Vec<u8> { |
| 92 | archive(&[ |
| 93 | Fixture::directory("folder/"), |
| 94 | Fixture::file("folder/a.txt", b"alpha"), |
| 95 | Fixture::file("empty", &[]), |
| 96 | Fixture::file("b.bin", &payload()), |
| 97 | ]) |
| 98 | } |
| 99 | |
| 100 | pub fn payload() -> Vec<u8> { |
| 101 | (0..140_000).map(|i| i as u8).collect() |
| 102 | } |
| 103 | |
| 104 | pub fn request(bytes: Vec<u8>) -> Request { |
| 105 | Request { |
| 106 | input: bytes.into_boxed_slice(), |
| 107 | policy: NamePolicy::Linux, |
| 108 | limits: Limits::default(), |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | pub fn ids(values: &[&str]) -> Vec<String> { |
| 113 | values.iter().map(|v| (*v).to_owned()).collect() |
| 114 | } |
| 115 | |
| 116 | pub fn run(bytes: Vec<u8>, selection: &[&str]) -> Completed { |
| 117 | read( |
| 118 | request(bytes), |
| 119 | |_| Ok::<_, Infallible>(ids(selection)), |
| 120 | |_| Ok(()), |
| 121 | ) |
| 122 | .expect("정상 입력은 완료되어야 함") |
| 123 | } |
| 124 | |
| 125 | pub fn limited( |
| 126 | bytes: Vec<u8>, |
| 127 | selection: &[&str], |
| 128 | limits: Limits, |
| 129 | ) -> Result<Completed, Error<Infallible>> { |
| 130 | let mut request = request(bytes); |
| 131 | request.limits = limits; |
| 132 | read(request, |_| Ok(ids(selection)), |_| Ok(())) |
| 133 | } |
| 134 | |
| 135 | pub fn failure<E: std::fmt::Debug>(result: Result<Completed, Error<E>>, expected: Failure) { |
| 136 | match result { |
| 137 | Err(Error::Reader(actual)) => assert_eq!(actual, expected), |
| 138 | other => panic!("예상한 오류 {expected:?} 대신 {other:?} 반환됨"), |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | pub fn reject(bytes: Vec<u8>, expected: Failure) { |
| 143 | let mut selected = false; |
| 144 | let result = read( |
| 145 | request(bytes), |
| 146 | |_| { |
| 147 | selected = true; |
| 148 | Ok::<_, Infallible>(Vec::new()) |
| 149 | }, |
| 150 | |_| Ok(()), |
| 151 | ); |
| 152 | failure(result, expected); |
| 153 | assert!(!selected, "전체 검증 실패 전 선택 콜백이 호출됨"); |
| 154 | } |
| 155 | |
| 156 | pub fn digest(bytes: &[u8]) -> [u8; 32] { |
| 157 | Sha256::digest(bytes).into() |
| 158 | } |