From 941524e92faf2e9a62e02a2f9fd351c84d50b96e Mon Sep 17 00:00:00 2001 From: specCon18 Date: Wed, 20 Sep 2023 23:43:49 -0400 Subject: [PATCH] working checksummung --- src/lib/lib.rs | 94 +++++++++++++++++++++++++++++++------- src/test_data/checksum_01 | 1 + src/test_data/checksum_02 | 1 + src/test_data/checksum_03 | 1 + src/test_data/testfile_01 | Bin 0 -> 1024 bytes src/test_data/testfile_02 | Bin 0 -> 1024 bytes src/test_data/testfile_03 | Bin 0 -> 1024 bytes 7 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 src/test_data/checksum_01 create mode 100644 src/test_data/checksum_02 create mode 100644 src/test_data/checksum_03 create mode 100644 src/test_data/testfile_01 create mode 100644 src/test_data/testfile_02 create mode 100644 src/test_data/testfile_03 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 0000000000000000000000000000000000000000..a30dcc5c4a14aed9b49ab2c049b5ed744ad0e9e5 GIT binary patch literal 1024 zcmV+b1poUje3J*R7iDxF#@a!eyQQ*H_PaxkSh~tW=+lE9%{Q01OUT@RhR_o!I6E;3^ zEl`uK=i(S|pX(8!5)_zhTykF;27yuLN^67g_zEtEc9{Z1-dxp0h1(`b#lpJ0MV%$* zRFkovBro0x6MtE~=v?xxH+H4g68;hqEnw{zVoSP*+;uJsrs>9HJy7&F3|p&yl7|^C zkc18p`k=kM$sz@rbT&P~q9vqb#6|T@JimML?84SD(MMwM z9{+++LsY${jCbaZ$~!6J_| zLDb61DVE#sq1zJv&Q`Rp^TQg;d3G;tbzzyE@%Vm-J%x_hG5gSxZ4+Tfi*ieD09)3m zBlkwuPLj%h#4$f4OcnQS(+^bsru~Z>?y+24N!FSzRv@n8Nq4c(ZJ0J`%Rvdkx^Os4 zOghG27E|ll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