diff --git a/src/lib/lib.rs b/src/lib/lib.rs index d3bbf33..ba67ccb 100644 --- a/src/lib/lib.rs +++ b/src/lib/lib.rs @@ -36,20 +36,38 @@ use extractors::{ // extract_exe }; - #[test] - fn test_extract_zip() { - let input_path = Path::new("src/test_data/test.zip"); - let output_directory = create_temp_dir(); +#[test] +fn test_extract_zip() { + let input_path = Path::new("src/test_data/test.zip"); + let output_directory = create_temp_dir(); + + // Extract the zip file + let result = extract_zip(input_path, &output_directory); + assert!(result.is_ok()); + + // Check checksums and assert equality + let checksum_01 = verify_checksum("src/test_data/checksum_01", "src/test_data/testfile_01").unwrap(); + let checksum_02 = verify_checksum("src/test_data/checksum_02", "src/test_data/testfile_02").unwrap(); + let checksum_03 = verify_checksum("src/test_data/checksum_03", "src/test_data/testfile_03").unwrap(); + + println!("Checksum 01: {:?}", checksum_01); + println!("Checksum 02: {:?}", checksum_02); + println!("Checksum 03: {:?}", checksum_03); + + assert_eq!(checksum_01, true); + assert_eq!(checksum_02, true); + assert_eq!(checksum_03, true); +} + + - let result = extract_zip(input_path, &output_directory); - assert!(result.is_ok()); - } #[test] fn test_extract_rar() { let input_path = Path::new("src/test_data/test.rar"); let output_directory = create_temp_dir(); - + //get the text in checksum_01,checksum_02,checksum_03 and compare to the hashes of testfile_01 testfile_02 and testfile_03 + //assert equality let result = extract_rar(input_path, &output_directory); assert!(result.is_ok()); } @@ -58,7 +76,8 @@ use extractors::{ fn test_extract_tar() { let input_path = Path::new("src/test_data/test.tar"); let output_directory = create_temp_dir(); - + //get the text in checksum_01,checksum_02,checksum_03 and compare to the hashes of testfile_01 testfile_02 and testfile_03 + //assert equality let result = extract_tar(input_path, &output_directory); assert!(result.is_ok()); } @@ -67,7 +86,8 @@ use extractors::{ fn test_extract_lzma() { let input_path = Path::new("src/test_data/test.lzma"); let output_directory = create_temp_dir(); - + //get the text in checksum_01 and compare to the hashes of testfile_01 + //assert equality let result = extract_lzma(input_path, &output_directory); assert!(result.is_ok()); } @@ -76,7 +96,8 @@ use extractors::{ fn test_extract_gz() { let input_path = Path::new("src/test_data/test.gz"); let output_directory = create_temp_dir(); - + //get the text in checksum_01 and compare to the hashes of testfile_01 + //assert equality let result = extract_gz(input_path, &output_directory); assert!(result.is_ok()); } @@ -85,7 +106,8 @@ use extractors::{ fn test_extract_bz2() { let input_path = Path::new("src/test_data/test.bz2"); let output_directory = create_temp_dir(); - + //get the text in checksum_01 and compare to the hashes of testfile_01 + //assert equality let result = extract_bz2(input_path, &output_directory); assert!(result.is_ok()); } @@ -94,7 +116,8 @@ use extractors::{ fn test_extract_7z() { let input_path = Path::new("src/test_data/test.7z"); let output_directory = create_temp_dir(); - + //get the text in checksum_01,checksum_02,checksum_03 and compare to the hashes of testfile_01 testfile_02 and testfile_03 + //assert equality let result = extract_7z(input_path, &output_directory); assert!(result.is_ok()); } @@ -103,7 +126,8 @@ use extractors::{ fn test_extract_tbz2() { let input_path = Path::new("src/test_data/test.tbz2"); let output_directory = create_temp_dir(); - + //get the text in checksum_01,checksum_02,checksum_03 and compare to the hashes of testfile_01 testfile_02 and testfile_03 + //assert equality let result = extract_tbz2(input_path, &output_directory); assert!(result.is_ok()); } @@ -112,7 +136,8 @@ use extractors::{ fn test_extract_tgz() { let input_path = Path::new("src/test_data/test.tgz"); let output_directory = create_temp_dir(); - + //get the text in checksum_01,checksum_02,checksum_03 and compare to the hashes of testfile_01 testfile_02 and testfile_03 + //assert equality let result = extract_tgz(input_path, &output_directory); assert!(result.is_ok()); } @@ -121,11 +146,45 @@ use extractors::{ fn test_extract_txz() { let input_path = Path::new("src/test_data/test.txz"); let output_directory = create_temp_dir(); - + //get the text in checksum_01,checksum_02,checksum_03 and compare to the hashes of testfile_01 testfile_02 and testfile_03 + //assert equality let result = extract_txz(input_path, &output_directory); assert!(result.is_ok()); } + + fn verify_checksum(checksum_path: &str, testfile_path: &str) -> Result { + let mut checksum_file = File::open(checksum_path).expect("Failed to open checksum file"); + let mut checksum_data = String::new(); + checksum_file + .read_to_string(&mut checksum_data) + .expect("Failed to read checksum data"); + let mut checksum_data_uppercase = checksum_data.to_uppercase(); + + if checksum_data_uppercase.len() >= 2 { + checksum_data_uppercase.truncate(checksum_data_uppercase.len() - 1); + } else { + // Handle cases where the string is too short to remove characters + println!("String is too short to remove characters"); + } + println!("Checksum file data: {:?}", checksum_data_uppercase); + let mut testfile = File::open(testfile_path).expect("Failed to open test file"); + let testfile_buffer = BufReader::new(&mut testfile); + let calculated_checksum = HEXUPPER.encode(sha256_digest(testfile_buffer, testfile_path)?.as_ref()); + let tf_path = Path::new(testfile_path); + let mut checksum_with_filename = String::new(); // Initialize the variable + if let Some(testfile_name) = tf_path.file_name() { + if let Some(testfile_name_str) = testfile_name.to_str() { + checksum_with_filename = format!("{} {}", calculated_checksum, testfile_name_str.to_uppercase()); + } + } else { + println!("Invalid path or no file name found."); + } + println!("Calculated Checksum: {:?}", checksum_with_filename); + + Ok(checksum_with_filename == checksum_data_uppercase) + } + fn create_temp_dir() -> PathBuf { let mut temp_dir = std::env::temp_dir(); temp_dir.push("test_dir"); @@ -133,7 +192,7 @@ use extractors::{ temp_dir } - fn sha256_digest(mut reader: R) -> Result { + fn sha256_digest(mut reader: R, filename: &str) -> Result { let mut context = Context::new(&SHA256); let mut buffer = [0; 1024]; @@ -147,6 +206,7 @@ use extractors::{ Ok(context.finish()) } + fn mode_to_chmod(mode: u32) -> u32 { let mut flags:u32 = 0; diff --git a/src/test_data/checksum_01 b/src/test_data/checksum_01 new file mode 100644 index 0000000..205be5f --- /dev/null +++ b/src/test_data/checksum_01 @@ -0,0 +1 @@ +c63a52591e30851e2dc94705902a592dea89c3134166df1c0a8979f90d9ea0a2 testfile_01 diff --git a/src/test_data/checksum_02 b/src/test_data/checksum_02 new file mode 100644 index 0000000..35d7fe9 --- /dev/null +++ b/src/test_data/checksum_02 @@ -0,0 +1 @@ +dd5c253429a3a1a34e382ff0158555d4a573ace73c1a4fb60e5b215053d8b6ab testfile_02 diff --git a/src/test_data/checksum_03 b/src/test_data/checksum_03 new file mode 100644 index 0000000..de13d75 --- /dev/null +++ b/src/test_data/checksum_03 @@ -0,0 +1 @@ +1e103ac2fc72820330a578900880744481726c7752ead976e3a30963ee4b39bc testfile_03 diff --git a/src/test_data/testfile_01 b/src/test_data/testfile_01 new file mode 100644 index 0000000..a30dcc5 Binary files /dev/null and b/src/test_data/testfile_01 differ diff --git a/src/test_data/testfile_02 b/src/test_data/testfile_02 new file mode 100644 index 0000000..da9b2bd Binary files /dev/null and b/src/test_data/testfile_02 differ diff --git a/src/test_data/testfile_03 b/src/test_data/testfile_03 new file mode 100644 index 0000000..4556c15 Binary files /dev/null and b/src/test_data/testfile_03 differ