|
| 1 | +use std::env; |
| 2 | +use std::fs; |
| 3 | + |
| 4 | +use exe_zip_maker_lib::{ |
| 5 | + compute_file_sha256_hex, create_exe_zip, replace_bytes, PLACEHOLDER, |
| 6 | +}; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn test_replace_bytes_none() { |
| 10 | + let src = b"hello world"; |
| 11 | + assert_eq!(replace_bytes(src, b"foo", b"bar"), b"hello world"); |
| 12 | +} |
| 13 | + |
| 14 | +#[test] |
| 15 | +fn test_replace_bytes_single() { |
| 16 | + let src = b"EXPECTED_HASH='%ZIP_HASH%'"; |
| 17 | + let replaced = replace_bytes(src, PLACEHOLDER, b"12345678"); |
| 18 | + assert_eq!(replaced, b"EXPECTED_HASH='12345678'"); |
| 19 | +} |
| 20 | + |
| 21 | +#[test] |
| 22 | +fn test_replace_bytes_multiple() { |
| 23 | + let src = b"%ZIP_HASH% and %ZIP_HASH%"; |
| 24 | + let replaced = replace_bytes(src, PLACEHOLDER, b"abc"); |
| 25 | + assert_eq!(replaced, b"abc and abc"); |
| 26 | +} |
| 27 | + |
| 28 | +#[test] |
| 29 | +fn test_replace_bytes_empty_from() { |
| 30 | + let src = b"unchanged"; |
| 31 | + assert_eq!(replace_bytes(src, b"", b"abc"), b"unchanged"); |
| 32 | +} |
| 33 | + |
| 34 | +#[test] |
| 35 | +fn test_compute_file_sha256_hex() { |
| 36 | + let temp_dir = env::temp_dir().join(format!("sha256_test_{}", std::process::id())); |
| 37 | + fs::create_dir_all(&temp_dir).unwrap(); |
| 38 | + let file_path = temp_dir.join("sample.txt"); |
| 39 | + |
| 40 | + fs::write(&file_path, b"hello world\n").unwrap(); |
| 41 | + let hash = compute_file_sha256_hex(&file_path).unwrap(); |
| 42 | + assert_eq!( |
| 43 | + hash, |
| 44 | + "a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447" |
| 45 | + ); |
| 46 | + |
| 47 | + let _ = fs::remove_dir_all(&temp_dir); |
| 48 | +} |
| 49 | + |
| 50 | +#[test] |
| 51 | +fn test_create_exe_zip_successful() { |
| 52 | + let temp_dir = env::temp_dir().join(format!("create_exe_zip_test_{}", std::process::id())); |
| 53 | + fs::create_dir_all(&temp_dir).unwrap(); |
| 54 | + |
| 55 | + let preamble_path = temp_dir.join("preamble.sh"); |
| 56 | + let zip_path = temp_dir.join("data.zip"); |
| 57 | + let output_path = temp_dir.join("output.exe"); |
| 58 | + |
| 59 | + let zip_content = b"PK\x03\x04dummyzipcontent"; |
| 60 | + fs::write(&zip_path, zip_content).unwrap(); |
| 61 | + |
| 62 | + let preamble_text = b"#!/bin/bash\nEXPECTED_HASH='%ZIP_HASH%'\n# ... logic ...\n"; |
| 63 | + fs::write(&preamble_path, preamble_text).unwrap(); |
| 64 | + |
| 65 | + create_exe_zip(&preamble_path, &zip_path, &output_path).unwrap(); |
| 66 | + |
| 67 | + assert!(output_path.exists()); |
| 68 | + |
| 69 | + #[cfg(unix)] |
| 70 | + { |
| 71 | + use std::os::unix::fs::PermissionsExt; |
| 72 | + let st = fs::metadata(&output_path).unwrap(); |
| 73 | + assert_ne!( |
| 74 | + st.permissions().mode() & 0o100, |
| 75 | + 0, |
| 76 | + "Expected executable permission on output file" |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + let content = fs::read(&output_path).unwrap(); |
| 81 | + let expected_hash = "65e39989ca91c49484998aa3f0429f6943c029609bfd2f3c18c77bf9ded72c59"; |
| 82 | + let expected_preamble = replace_bytes(preamble_text, PLACEHOLDER, expected_hash.as_bytes()); |
| 83 | + |
| 84 | + assert!(content.starts_with(&expected_preamble)); |
| 85 | + assert!(content.ends_with(zip_content)); |
| 86 | + assert_eq!(content.len(), expected_preamble.len() + zip_content.len()); |
| 87 | + |
| 88 | + let _ = fs::remove_dir_all(&temp_dir); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn test_create_exe_zip_multiple_placeholders() { |
| 93 | + let temp_dir = env::temp_dir().join(format!("create_exe_zip_multi_{}", std::process::id())); |
| 94 | + fs::create_dir_all(&temp_dir).unwrap(); |
| 95 | + |
| 96 | + let preamble_path = temp_dir.join("preamble.sh"); |
| 97 | + let zip_path = temp_dir.join("data.zip"); |
| 98 | + let output_path = temp_dir.join("output.exe"); |
| 99 | + |
| 100 | + let zip_content = b"PK\x03\x04dummyzipcontent"; |
| 101 | + fs::write(&zip_path, zip_content).unwrap(); |
| 102 | + |
| 103 | + let preamble_text = b"# First: %ZIP_HASH%\n# Second: %ZIP_HASH%\n"; |
| 104 | + fs::write(&preamble_path, preamble_text).unwrap(); |
| 105 | + |
| 106 | + create_exe_zip(&preamble_path, &zip_path, &output_path).unwrap(); |
| 107 | + |
| 108 | + let content = fs::read(&output_path).unwrap(); |
| 109 | + let expected_hash = "65e39989ca91c49484998aa3f0429f6943c029609bfd2f3c18c77bf9ded72c59"; |
| 110 | + let expected_preamble = replace_bytes(preamble_text, PLACEHOLDER, expected_hash.as_bytes()); |
| 111 | + |
| 112 | + assert!(content.starts_with(&expected_preamble)); |
| 113 | + assert!(content.ends_with(zip_content)); |
| 114 | + |
| 115 | + let _ = fs::remove_dir_all(&temp_dir); |
| 116 | +} |
| 117 | + |
| 118 | +#[test] |
| 119 | +fn test_create_exe_zip_creates_parent_dir() { |
| 120 | + let temp_dir = env::temp_dir().join(format!("create_exe_zip_parent_{}", std::process::id())); |
| 121 | + fs::create_dir_all(&temp_dir).unwrap(); |
| 122 | + |
| 123 | + let preamble_path = temp_dir.join("preamble.sh"); |
| 124 | + let zip_path = temp_dir.join("data.zip"); |
| 125 | + let output_path = temp_dir.join("nested").join("sub").join("output.exe"); |
| 126 | + |
| 127 | + fs::write(&zip_path, b"content").unwrap(); |
| 128 | + fs::write(&preamble_path, b"preamble").unwrap(); |
| 129 | + |
| 130 | + create_exe_zip(&preamble_path, &zip_path, &output_path).unwrap(); |
| 131 | + assert!(output_path.exists()); |
| 132 | + |
| 133 | + let _ = fs::remove_dir_all(&temp_dir); |
| 134 | +} |
| 135 | + |
| 136 | +#[test] |
| 137 | +fn test_create_exe_zip_missing_files() { |
| 138 | + let temp_dir = env::temp_dir().join(format!("create_exe_zip_err_{}", std::process::id())); |
| 139 | + fs::create_dir_all(&temp_dir).unwrap(); |
| 140 | + |
| 141 | + let missing_preamble = temp_dir.join("nonexistent_preamble.sh"); |
| 142 | + let zip_path = temp_dir.join("data.zip"); |
| 143 | + let output_path = temp_dir.join("output.exe"); |
| 144 | + fs::write(&zip_path, b"dummy").unwrap(); |
| 145 | + |
| 146 | + assert!(create_exe_zip(&missing_preamble, &zip_path, &output_path).is_err()); |
| 147 | + |
| 148 | + let preamble_path = temp_dir.join("preamble.sh"); |
| 149 | + fs::write(&preamble_path, b"preamble").unwrap(); |
| 150 | + let missing_zip = temp_dir.join("nonexistent_data.zip"); |
| 151 | + |
| 152 | + assert!(create_exe_zip(&preamble_path, &missing_zip, &output_path).is_err()); |
| 153 | + |
| 154 | + let _ = fs::remove_dir_all(&temp_dir); |
| 155 | +} |
0 commit comments