diff options
| author | Tolmachev Igor <me@igorek.dev> | 2024-09-01 20:15:08 +0300 |
|---|---|---|
| committer | Tolmachev Igor <me@igorek.dev> | 2024-09-01 20:15:08 +0300 |
| commit | dafe3b01d7dfe5f314dea37c312beae20e017f4e (patch) | |
| tree | cfea2d919280bf07be4d3110434d5c579a65e70f | |
| parent | 27da50f9d157927ec56dae8316d0edc34eaa244d (diff) | |
| download | archivator-dafe3b01d7dfe5f314dea37c312beae20e017f4e.tar.gz archivator-dafe3b01d7dfe5f314dea37c312beae20e017f4e.zip | |
Add test for InvalidSignature for struct CDR
| -rw-r--r-- | src/zip/driver.rs | 6 | ||||
| -rw-r--r-- | tests/zip.rs | 188 |
2 files changed, 129 insertions, 65 deletions
diff --git a/src/zip/driver.rs b/src/zip/driver.rs index 0502c85..b44c453 100644 --- a/src/zip/driver.rs +++ b/src/zip/driver.rs | |||
| @@ -145,12 +145,10 @@ impl<Io: Read + Seek> ArchiveRead for Zip<Io> { | |||
| 145 | ); | 145 | ); |
| 146 | 146 | ||
| 147 | for i in 0..cd_records as usize { | 147 | for i in 0..cd_records as usize { |
| 148 | let buf = cd_reader.read_arr::<46>()?; | 148 | if cd_reader.read_arr()? != CDR_SIGNATURE { |
| 149 | |||
| 150 | if buf[..4] != CDR_SIGNATURE { | ||
| 151 | return Err(ZipError::InvalidSignature("Cdr")); | 149 | return Err(ZipError::InvalidSignature("Cdr")); |
| 152 | } | 150 | } |
| 153 | let cdr: Cdr = deserialize(&buf[4..46]).unwrap(); | 151 | let cdr: Cdr = deserialize(&cd_reader.read_arr::<42>()?).unwrap(); |
| 154 | let bit_flag = BitFlag::new(cdr.bit_flag); | 152 | let bit_flag = BitFlag::new(cdr.bit_flag); |
| 155 | 153 | ||
| 156 | let name = cd_reader.read_vec(cdr.name_len as usize)?; | 154 | let name = cd_reader.read_vec(cdr.name_len as usize)?; |
diff --git a/tests/zip.rs b/tests/zip.rs index e00789d..f0451cb 100644 --- a/tests/zip.rs +++ b/tests/zip.rs | |||
| @@ -30,12 +30,20 @@ fn test_zip_aes() { | |||
| 30 | ); | 30 | ); |
| 31 | 31 | ||
| 32 | for encryption in ["aes128", "aes192", "aes256"] { | 32 | for encryption in ["aes128", "aes192", "aes256"] { |
| 33 | assert!(archive | 33 | assert_eq!( |
| 34 | .get_file_reader_by_name(&format!("{encryption}/store")) | 34 | archive |
| 35 | .is_err_and(|e| e == ZipError::PasswordIsNotSpecified)); | 35 | .get_file_reader_by_name(&format!("{encryption}/store")) |
| 36 | assert!(archive | 36 | .err() |
| 37 | .get_file_reader_by_name_with_password("aes128/store", b"wrong_passwd") | 37 | .unwrap(), |
| 38 | .is_err_and(|e| e == ZipError::WrongPassword)); | 38 | ZipError::PasswordIsNotSpecified |
| 39 | ); | ||
| 40 | assert_eq!( | ||
| 41 | archive | ||
| 42 | .get_file_reader_by_name_with_password("aes128/store", b"wrong_passwd") | ||
| 43 | .err() | ||
| 44 | .unwrap(), | ||
| 45 | ZipError::WrongPassword | ||
| 46 | ); | ||
| 39 | 47 | ||
| 40 | for (name, check) in [ | 48 | for (name, check) in [ |
| 41 | ("store", "98f64f03b3d168875ffa778f7fb4"), | 49 | ("store", "98f64f03b3d168875ffa778f7fb4"), |
| @@ -68,12 +76,17 @@ fn test_zip_weak() { | |||
| 68 | vec!["store", "deflate", "bzip"] | 76 | vec!["store", "deflate", "bzip"] |
| 69 | ); | 77 | ); |
| 70 | 78 | ||
| 71 | assert!(archive | 79 | assert_eq!( |
| 72 | .get_file_reader_by_name("store") | 80 | archive.get_file_reader_by_name("store").err().unwrap(), |
| 73 | .is_err_and(|e| e == ZipError::PasswordIsNotSpecified)); | 81 | ZipError::PasswordIsNotSpecified |
| 74 | assert!(archive | 82 | ); |
| 75 | .get_file_reader_by_name_with_password("store", b"wrong_passwd") | 83 | assert_eq!( |
| 76 | .is_err_and(|e| e == ZipError::WrongPassword)); | 84 | archive |
| 85 | .get_file_reader_by_name_with_password("store", b"wrong_passwd") | ||
| 86 | .err() | ||
| 87 | .unwrap(), | ||
| 88 | ZipError::WrongPassword | ||
| 89 | ); | ||
| 77 | 90 | ||
| 78 | for (name, check) in [ | 91 | for (name, check) in [ |
| 79 | ("store", "1e643774f40510e37c6f3c451d9d"), | 92 | ("store", "1e643774f40510e37c6f3c451d9d"), |
| @@ -147,66 +160,119 @@ fn test_zip() { | |||
| 147 | 160 | ||
| 148 | f.seek(SeekFrom::Start(7)).unwrap(); | 161 | f.seek(SeekFrom::Start(7)).unwrap(); |
| 149 | assert_eq!(f.seek(SeekFrom::Current(0)).unwrap(), 7); | 162 | assert_eq!(f.seek(SeekFrom::Current(0)).unwrap(), 7); |
| 150 | assert!(f | 163 | assert_eq!( |
| 151 | .seek(SeekFrom::End(-100)) | 164 | f.seek(SeekFrom::End(-100)) |
| 152 | .is_err_and(|e| e.get_ref().unwrap().to_string() | 165 | .err() |
| 153 | == "Invalid seek to a negative or overflowing position")); | 166 | .unwrap() |
| 167 | .get_ref() | ||
| 168 | .unwrap() | ||
| 169 | .to_string(), | ||
| 170 | "Invalid seek to a negative or overflowing position" | ||
| 171 | ); | ||
| 154 | assert_eq!(f.seek(SeekFrom::Current(0)).unwrap(), 7); | 172 | assert_eq!(f.seek(SeekFrom::Current(0)).unwrap(), 7); |
| 155 | 173 | ||
| 156 | assert_eq!(f.seek(SeekFrom::Start(100)).unwrap(), 14); | 174 | assert_eq!(f.seek(SeekFrom::Start(100)).unwrap(), 14); |
| 157 | 175 | ||
| 158 | for index in 0..archive.len() { | 176 | for index in 1..archive.len() { |
| 159 | let mut f = archive.get_file_reader_by_index(index).unwrap(); | 177 | let mut f = archive.get_file_reader_by_index(index).unwrap(); |
| 160 | let mut data = String::new(); | 178 | let mut data = String::new(); |
| 161 | f.read_to_string(&mut data).unwrap(); | 179 | f.read_to_string(&mut data).unwrap(); |
| 162 | assert_eq!(data, "test file data"); | 180 | assert_eq!(data, "test file data"); |
| 163 | assert!(!f.is_seekable() || f.info().name == "store") | 181 | assert!(!f.is_seekable()) |
| 164 | } | 182 | } |
| 165 | } | 183 | } |
| 166 | 184 | ||
| 167 | #[test] | 185 | #[test] |
| 168 | fn test_bad_zip() { | 186 | fn test_bad_zip() { |
| 169 | assert!(Archive::<Zip<_>>::read(Cursor::new(&[])) | 187 | assert_eq!( |
| 170 | .is_err_and(|e| e == ZipError::StructNotFound("Eocdr"))); | 188 | Archive::<Zip<_>>::read(Cursor::new(&[])).err().unwrap(), |
| 171 | 189 | ZipError::StructNotFound("Eocdr") | |
| 172 | assert!(Archive::<Zip<_>>::read(Cursor::new(&[ | 190 | ); |
| 173 | // No Eocdr64 | 191 | |
| 174 | 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // | 192 | assert_eq!( |
| 175 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator | 193 | Archive::<Zip<_>>::read(Cursor::new(&[ |
| 176 | // | 194 | 0, 0, 0, 0, // Eocdr64 |
| 177 | 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // | 195 | // |
| 178 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr | 196 | 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // |
| 179 | ])) | 197 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator |
| 180 | .is_err_and(|e| e == ZipError::InvalidSignature("Eocdr64"))); | 198 | // |
| 181 | 199 | 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // | |
| 182 | assert!(Archive::<Zip<_>>::read(Cursor::new(&[ | 200 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr |
| 183 | 0x50, 0x4b, 0x06, 0x06, // Eocdr64 | 201 | ])) |
| 184 | // | 202 | .err() |
| 185 | 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // | 203 | .unwrap(), |
| 186 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator | 204 | ZipError::InvalidSignature("Eocdr64") |
| 187 | // | 205 | ); |
| 188 | 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // | 206 | |
| 189 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr | 207 | assert_eq!( |
| 190 | ])) | 208 | Archive::<Zip<_>>::read(Cursor::new(&[ |
| 191 | .is_err_and(|e| e == ZipError::Overlapping("Eocdr64", "Eocdr64Locator"))); | 209 | 0x50, 0x4b, 0x06, 0x06, // Eocdr64 |
| 192 | 210 | // | |
| 193 | assert!(Archive::<Zip<_>>::read(Cursor::new(&[ | 211 | 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // |
| 194 | // No records | 212 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator |
| 195 | 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // | 213 | // |
| 196 | 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr | 214 | 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // |
| 197 | ])) | 215 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr |
| 198 | .is_err_and(|e| e == ZipError::Overlapping("Cdr", "Eocdr"))); | 216 | ])) |
| 199 | 217 | .err() | |
| 200 | assert!(Archive::<Zip<_>>::read(Cursor::new(&[ | 218 | .unwrap(), |
| 201 | 0x50, 0x4b, 0x06, 0x06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // | 219 | ZipError::Overlapping("Eocdr64", "Eocdr64Locator") |
| 202 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // | 220 | ); |
| 203 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64 | 221 | |
| 204 | // | 222 | assert_eq!( |
| 205 | 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // | 223 | Archive::<Zip<_>>::read(Cursor::new(&[ |
| 206 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator | 224 | 0x50, 0x4b, 0x06, 0x06, 45, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 207 | // | 225 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // |
| 208 | 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // | 226 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64 |
| 209 | 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr | 227 | // |
| 210 | ])) | 228 | 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // |
| 211 | .is_err_and(|e| e == ZipError::Overlapping("Cdr", "Eocdr64"))); | 229 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator |
| 230 | // | ||
| 231 | 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // | ||
| 232 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr | ||
| 233 | ])) | ||
| 234 | .err() | ||
| 235 | .unwrap(), | ||
| 236 | ZipError::Overlapping("Eocdr64", "Eocdr64Locator") | ||
| 237 | ); | ||
| 238 | |||
| 239 | assert_eq!( | ||
| 240 | Archive::<Zip<_>>::read(Cursor::new(&[ | ||
| 241 | // No records | ||
| 242 | 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // | ||
| 243 | 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr | ||
| 244 | ])) | ||
| 245 | .err() | ||
| 246 | .unwrap(), | ||
| 247 | ZipError::Overlapping("Cdr", "Eocdr") | ||
| 248 | ); | ||
| 249 | |||
| 250 | assert_eq!( | ||
| 251 | Archive::<Zip<_>>::read(Cursor::new(&[ | ||
| 252 | 0x50, 0x4b, 0x06, 0x06, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // | ||
| 253 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // | ||
| 254 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64 | ||
| 255 | // | ||
| 256 | 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // | ||
| 257 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator | ||
| 258 | // | ||
| 259 | 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // | ||
| 260 | 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr | ||
| 261 | ])) | ||
| 262 | .err() | ||
| 263 | .unwrap(), | ||
| 264 | ZipError::Overlapping("Cdr", "Eocdr64") | ||
| 265 | ); | ||
| 266 | |||
| 267 | assert_eq!( | ||
| 268 | Archive::<Zip<_>>::read(Cursor::new(&[ | ||
| 269 | 0, 0, 0, 0, // Cdr | ||
| 270 | // | ||
| 271 | 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // | ||
| 272 | 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr | ||
| 273 | ])) | ||
| 274 | .err() | ||
| 275 | .unwrap(), | ||
| 276 | ZipError::InvalidSignature("Cdr") | ||
| 277 | ); | ||
| 212 | } | 278 | } |
