i2pd-launch 0.5.0-beta.12

Launches i2pd with clean state
// src/wipe_tests.rs

#![allow(unused_variables)]
#![allow(unused_imports)]
#![allow(dead_code)]

// Důležité: Importujeme všechny funkce a konstanty z nadřazeného modulu.
// Pro testy makra v odděleném souboru je potřeba `#[macro_use] extern crate`.
// Zde předpokládáme, že název tvého crate je `your_crate_name`.
// #[macro_use]
// extern crate i2pd-launch; // Důležité: Zde použij název tvého crate z Cargo.toml

use super::{wipe_with_zeros, BATCH_SIZE, BUFFER_SIZE}; // delete_file už není potřeba importovat
use std::fs;
use std::io;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

/// Pomocná funkce pro vytvoření dočasného souboru s daným obsahem.
/// Vrací cestu k vytvořenému souboru.
fn create_temp_file(name: &str, content: &[u8]) -> PathBuf {
   let mut path = std::env::temp_dir();
   path.push(name);
   // Zajistíme, že soubor neexistuje z předchozích testů
   if path.exists() {
      let _ = fs::remove_file(&path);
   }
   {
      let mut file = fs::File::create(&path).expect("Failed to create temp file for test");
      file.write_all(content).expect("Failed to write to temp file for test");
   }
   path
}

/// Pomocná funkce pro ověření, zda je soubor naplněn nulami.
fn check_file_for_zeros(path: &Path, expected_original_size: u64) -> bool {
   if !path.exists() {
      return false;
   }
   let mut file = fs::File::open(path).expect("Failed to open file for zero check");
   let metadata = file.metadata().expect("Failed to get metadata for zero check");

   let actual_size = metadata.len();
   if actual_size < expected_original_size {
      return false;
   }

   let mut buffer = vec![0u8; 1024];
   let mut bytes_read_total = 0u64;

   while bytes_read_total < expected_original_size {
      let bytes_to_read = (expected_original_size - bytes_read_total).min(buffer.len() as u64) as usize;
      let bytes_read = file
         .read(&mut buffer[..bytes_to_read])
         .expect("Failed to read from file for zero check");
      if bytes_read == 0 {
         return false;
      }

      for i in 0..bytes_read {
         if buffer[i] != 0 {
            println!("Found non-zero byte at offset {}", bytes_read_total + i as u64);
            return false;
         }
      }
      bytes_read_total += bytes_read as u64;
   }
   true
}

// --- Testy pro wipe_with_zeros (pouze přepsání nulami) ---
#[test]
fn test_wipe_with_zeros_small_file() {
   let content = b"Original small data.";
   let path = create_temp_file("wipe_zeros_small.txt", content);
   let original_size = content.len() as u64;

   assert!(wipe_with_zeros(&path).is_ok());
   assert!(path.exists()); // Soubor by po přepsání měl stále existovat
   assert!(check_file_for_zeros(&path, original_size)); // Ověření obsahu nulami
   let _ = fs::remove_file(&path); // Úklid po testu
}

#[test]
fn test_wipe_with_zeros_larger_file() {
   let content = vec![0xAA; BATCH_SIZE * 3 + 500]; // Větší soubor
   let path = create_temp_file("wipe_zeros_large.bin", &content);
   let original_size = content.len() as u64;

   assert!(wipe_with_zeros(&path).is_ok());
   assert!(path.exists());
   assert!(check_file_for_zeros(&path, original_size));
   let _ = fs::remove_file(&path);
}

#[test]
fn test_wipe_with_zeros_non_existent_file_error() {
   let path = PathBuf::from("non_existent_wipe_zeros.txt");
   let result = wipe_with_zeros(&path);
   assert!(result.is_err());
   assert_eq!(result.unwrap_err().kind(), io::ErrorKind::NotFound);
}

// --- Testy pro makro wipe_and_delete! ---
#[test]
fn test_wipe_and_delete_small_file() {
   let content = b"This data should be wiped and deleted by macro.";
   let path = create_temp_file("macro_wipe_small.txt", content);

   assert!(path.exists());
   // Voláme makro
   assert!(wipe_and_delete!(&path).is_ok());
   assert!(!path.exists()); // Soubor by neměl existovat
}

#[test]
fn test_wipe_and_delete_large_file() {
   let content = vec![0xDE; BUFFER_SIZE + BATCH_SIZE]; // Větší soubor
   let path = create_temp_file("macro_wipe_large.bin", &content);

   assert!(path.exists());
   // Voláme makro
   assert!(wipe_and_delete!(&path).is_ok());
   assert!(!path.exists());
}

#[test]
fn test_wipe_and_delete_non_existent_file_error() {
   let path = PathBuf::from("non_existent_macro_wipe.txt");
   // Voláme makro
   let result = wipe_and_delete!(&path);
   assert!(result.is_err());
   assert_eq!(result.unwrap_err().kind(), io::ErrorKind::NotFound);
}

#[test]
#[ignore = "Might fail on some systems due to permission issues"]
fn test_wipe_and_delete_read_only_file() {
   let path = create_temp_file("macro_wipe_readonly.txt", b"secret data");
   let mut perms = fs::metadata(&path).unwrap().permissions();
   perms.set_readonly(true);
   fs::set_permissions(&path, perms).unwrap();

   // Voláme makro
   let result = wipe_and_delete!(&path);
   assert!(
      result.is_ok(),
      "Failed to wipe read-only file with macro: {:?}",
      result.unwrap_err()
   );
   assert!(!path.exists());

   // Úklid: Zkusíme nastavit oprávnění zpět a smazat soubor pro případ selhání testu
   if path.exists() {
      let mut perms = fs::metadata(&path).unwrap().permissions();
      perms.set_readonly(false);
      fs::set_permissions(&path, perms).unwrap();
      let _ = fs::remove_file(&path);
   }
}