myph-v2.git


myph-v2

~ rust shellcode loader with evasive capabilities
git clone https://github.com/djnnvx/myph-v2.git
log | files | refs | archive | README

file_ops.rs (2999B)

      1 //! File Operations Module
      2 //!
      3 //! Provides simple file I/O utilities for reading shellcode files and other
      4 //! binary data. This module is kept minimal and focused on the specific needs
      5 //! of the shellcode loader generator.
      6 //!
      7 //! # Functions
      8 //!
      9 //! - `read_file_bytes`: Reads a file and returns its contents as a byte vector
     10 //!
     11 //! # Example
     12 //!
     13 //! ```no_run
     14 //! use myph_v2::file_ops::read_file_bytes;
     15 //!
     16 //! let shellcode = read_file_bytes("payload.raw").expect("Failed to read file");
     17 //! println!("Read {} bytes", shellcode.len());
     18 //! ```
     19 
     20 use std::fs;
     21 use std::io;
     22 use std::path::Path;
     23 
     24 /// Reads a file and returns its contents as bytes
     25 pub fn read_file_bytes<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
     26     fs::read(path)
     27 }
     28 
     29 #[cfg(test)]
     30 mod tests {
     31     use super::*;
     32     use std::io::Write;
     33 
     34     #[test]
     35     fn test_read_file_bytes_success() {
     36         let temp_dir = std::env::temp_dir();
     37         let test_file = temp_dir.join(format!("test_read_{}.bin", rand::random::<u32>()));
     38 
     39         let test_data = b"Hello, World!";
     40         let mut file = fs::File::create(&test_file).unwrap();
     41         file.write_all(test_data).unwrap();
     42         drop(file);
     43 
     44         let result = read_file_bytes(&test_file);
     45         assert!(result.is_ok());
     46         assert_eq!(result.unwrap(), test_data);
     47 
     48         let _ = fs::remove_file(&test_file);
     49     }
     50 
     51     #[test]
     52     fn test_read_file_bytes_binary_data() {
     53         let temp_dir = std::env::temp_dir();
     54         let test_file = temp_dir.join(format!("test_binary_{}.bin", rand::random::<u32>()));
     55 
     56         let test_data: Vec<u8> = vec![0x00, 0xFF, 0xAA, 0x55, 0x01, 0x02];
     57         fs::write(&test_file, &test_data).unwrap();
     58 
     59         let result = read_file_bytes(&test_file);
     60         assert!(result.is_ok());
     61         assert_eq!(result.unwrap(), test_data);
     62 
     63         let _ = fs::remove_file(&test_file);
     64     }
     65 
     66     #[test]
     67     fn test_read_file_bytes_empty_file() {
     68         let temp_dir = std::env::temp_dir();
     69         let test_file = temp_dir.join(format!("test_empty_{}.bin", rand::random::<u32>()));
     70 
     71         fs::write(&test_file, b"").unwrap();
     72 
     73         let result = read_file_bytes(&test_file);
     74         assert!(result.is_ok());
     75         assert_eq!(result.unwrap(), b"");
     76 
     77         let _ = fs::remove_file(&test_file);
     78     }
     79 
     80     #[test]
     81     fn test_read_file_bytes_nonexistent() {
     82         let nonexistent = "/nonexistent/path/to/file.bin";
     83         let result = read_file_bytes(nonexistent);
     84         assert!(result.is_err());
     85     }
     86 
     87     #[test]
     88     fn test_read_file_bytes_large_file() {
     89         let temp_dir = std::env::temp_dir();
     90         let test_file = temp_dir.join(format!("test_large_{}.bin", rand::random::<u32>()));
     91 
     92         let test_data: Vec<u8> = (0..10000).map(|i| (i % 256) as u8).collect();
     93         fs::write(&test_file, &test_data).unwrap();
     94 
     95         let result = read_file_bytes(&test_file);
     96         assert!(result.is_ok());
     97         assert_eq!(result.unwrap(), test_data);
     98 
     99         let _ = fs::remove_file(&test_file);
    100     }
    101 }