this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(bzip2): restrict compression levels of bzip2

Only allow 1-9, not 0-9.

+44 -4
+44 -4
src/bzip2.rs
··· 18 18 #[clap(flatten)] 19 19 pub progress_args: ProgressArgs, 20 20 21 - #[clap(flatten)] 22 - pub level_args: LevelArgs, 21 + /// Level of compression. 22 + /// This is an int 1-9, with 1 being minimal compression and 9 being highest compression. 23 + /// Also supports 'fast', and 'best'. 24 + #[arg(long, default_value = "9")] 25 + pub level: CompressionLevel, 23 26 } 24 27 25 28 pub struct Bzip2 { 26 - pub level: u32, // 0-9 29 + pub level: u32, // 1-9 27 30 pub progress_args: ProgressArgs, 28 31 } 29 32 ··· 39 42 impl Bzip2 { 40 43 pub fn new(args: &Bzip2Args) -> Self { 41 44 Bzip2 { 42 - level: args.level_args.level.level, 45 + level: args.level.level, 43 46 progress_args: args.progress_args, 44 47 } 45 48 } ··· 58 61 59 62 /// Compress an input file or pipe to a bz2 archive 60 63 fn compress(&self, input: CmprssInput, output: CmprssOutput) -> Result<(), io::Error> { 64 + if self.level < 1 || self.level > 9 { 65 + return cmprss_error("Invalid compression level. Must be 1-9."); 66 + } 61 67 let mut file_size = None; 62 68 let mut input_stream = match input { 63 69 CmprssInput::Path(paths) => { ··· 178 184 .assert(predicate::path::eq_file(file.path())); 179 185 180 186 Ok(()) 187 + } 188 + 189 + // Fail with a compression level of 0 190 + #[test] 191 + fn invalid_compression_level_0() { 192 + let compressor = Bzip2 { 193 + level: 0, 194 + ..Bzip2::default() 195 + }; 196 + let file = assert_fs::NamedTempFile::new("test.txt").unwrap(); 197 + let working_dir = assert_fs::TempDir::new().unwrap(); 198 + let archive = working_dir.child("archive.".to_owned() + compressor.extension()); 199 + let result = compressor.compress( 200 + CmprssInput::Path(vec![file.path().to_path_buf()]), 201 + CmprssOutput::Path(archive.path().to_path_buf()), 202 + ); 203 + assert!(result.is_err()); 204 + } 205 + 206 + // Fail with a compression level of 10 207 + #[test] 208 + fn invalid_compression_level_10() { 209 + let compressor = Bzip2 { 210 + level: 10, 211 + ..Bzip2::default() 212 + }; 213 + let file = assert_fs::NamedTempFile::new("test.txt").unwrap(); 214 + let working_dir = assert_fs::TempDir::new().unwrap(); 215 + let archive = working_dir.child("archive.".to_owned() + compressor.extension()); 216 + let result = compressor.compress( 217 + CmprssInput::Path(vec![file.path().to_path_buf()]), 218 + CmprssOutput::Path(archive.path().to_path_buf()), 219 + ); 220 + assert!(result.is_err()); 181 221 } 182 222 }