use mesk::cfg::config::Config; use mesk::pkgtoolkit::ArchiveOperations; use mesk::pkgtoolkit::{Archs, Package}; mod shared; use shared::create_test_config; use tempfile::TempDir; use tokio; // Pkg toolkit tests #[cfg(test)] mod package_tests { use super::*; use std::fs; use std::io::Write; #[test] fn test_archs_as_str() { assert_eq!(Archs::X86_64.as_str(), "x86_64"); assert_eq!(Archs::Aarch64.as_str(), "aarch64"); assert_eq!(Archs::X86.as_str(), "x86"); assert_eq!(Archs::ArmV7.as_str(), "armv7"); assert_eq!(Archs::ArmV8.as_str(), "armv8"); } #[tokio::test] async fn test_extract_archive() { let temp_dir = TempDir::new().unwrap(); let _config = create_test_config(temp_dir.path().to_str().unwrap()); let test_file_content = "test content"; let test_file_path = temp_dir.path().join("test_file.txt"); fs::write(&test_file_path, test_file_content).unwrap(); let archive_path = temp_dir.path().join("test_archive.tar.gz"); let file = fs::File::create(&archive_path).unwrap(); let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); let mut tar_builder = tar::Builder::new(gz_encoder); tar_builder .append_path_with_name(&test_file_path, "extracted_test_file.txt") .unwrap(); tar_builder.into_inner().unwrap().finish().unwrap(); match Package::extract_archive(archive_path.to_str().unwrap()) { Ok(()) => { let config = Config::parse().unwrap(); let extracted_file_path = std::path::Path::new(&config.paths.cache_dir).join("extracted_test_file.txt"); assert!(extracted_file_path.exists()); let content = fs::read_to_string(extracted_file_path).unwrap(); assert_eq!(content, test_file_content); } Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => { // допустимый исход для запуска без root return; } Err(e) => panic!("unexpected error in extract_archive: {e}"), } } #[tokio::test] async fn test_package_check_valid() { let temp_dir = TempDir::new().unwrap(); let _config = create_test_config(temp_dir.path().to_str().unwrap()); let install_content = r#" [package] name = "test-pkg" version = "1.0.0" arch = "X86_64" # Используйте архитектуру хоста или измените для теста архитектуры descr = "A test package" license = "MIT" url = "/repo/test-pkg.mesk" [install] path = "/tmp/test_binary" user = "root" group = "root" mode = "755" "#; let install_path = temp_dir.path().join("INSTALL"); let mut install_file = fs::File::create(&install_path).unwrap(); install_file.write_all(install_content.as_bytes()).unwrap(); let archive_path = temp_dir.path().join("test_pkg_with_install.tar.gz"); let file = fs::File::create(&archive_path).unwrap(); let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); let mut tar_builder = tar::Builder::new(gz_encoder); tar_builder .append_path_with_name(&install_path, "INSTALL") .unwrap(); tar_builder.into_inner().unwrap().finish().unwrap(); let result = Package::check(archive_path.to_str().unwrap().to_string()); match result { Ok(v) => { assert!(v); } Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => { return; } Err(e) => panic!("unexpected error in Package::check(valid): {e}"), } } #[tokio::test] async fn test_package_check_missing_install() { let temp_dir = TempDir::new().unwrap(); let _config = create_test_config(temp_dir.path().to_str().unwrap()); let archive_path = temp_dir.path().join("test_pkg_without_install.tar.gz"); let file = fs::File::create(&archive_path).unwrap(); let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); let mut tar_builder = tar::Builder::new(gz_encoder); let dummy_path = temp_dir.path().join("dummy.txt"); fs::write(&dummy_path, "dummy").unwrap(); tar_builder .append_path_with_name(&dummy_path, "dummy.txt") .unwrap(); tar_builder.into_inner().unwrap().finish().unwrap(); let result = Package::check(archive_path.to_str().unwrap().to_string()); match result { Ok(_) => { panic!("expected error, got Ok"); } Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => { // допустимый исход для запуска без root return; } Err(err) => { assert_eq!(err.kind(), std::io::ErrorKind::NotFound); assert!(err.to_string().contains("INSTALL file not found")); } } } #[tokio::test] async fn test_package_check_empty_install() { let temp_dir = TempDir::new().unwrap(); let _config = create_test_config(temp_dir.path().to_str().unwrap()); let install_path = temp_dir.path().join("INSTALL"); fs::write(&install_path, "").unwrap(); let archive_path = temp_dir.path().join("test_pkg_with_empty_install.tar.gz"); let file = fs::File::create(&archive_path).unwrap(); let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); let mut tar_builder = tar::Builder::new(gz_encoder); tar_builder .append_path_with_name(&install_path, "INSTALL") .unwrap(); tar_builder.into_inner().unwrap().finish().unwrap(); let result = Package::check(archive_path.to_str().unwrap().to_string()); match result { Ok(_) => { panic!("expected error, got Ok"); } Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => { // допустимый исход для запуска без root return; } Err(err) => { assert_eq!(err.kind(), std::io::ErrorKind::InvalidData); assert!(err.to_string().contains("INSTALL file is empty")); } } } #[tokio::test] async fn test_package_check_arch_mismatch() { let temp_dir = TempDir::new().unwrap(); let _config = create_test_config(temp_dir.path().to_str().unwrap()); let host_arch = std::env::consts::ARCH; let wrong_arch = if host_arch == "x86" { "x86_64" } else { "x86" }; let install_content = format!( r#" [package] name = "test-pkg" version = "1.0.0" arch = "{}" descr = "A test package for arch mismatch" license = "MIT" url = "/repo/test-pkg.mesk" [install] path = "/tmp/test_binary" user = "root" group = "root" mode = "755" "#, wrong_arch ); let install_path = temp_dir.path().join("INSTALL"); let mut install_file = fs::File::create(&install_path).unwrap(); install_file.write_all(install_content.as_bytes()).unwrap(); let archive_path = temp_dir.path().join("test_pkg_with_wrong_arch.tar.gz"); let file = fs::File::create(&archive_path).unwrap(); let gz_encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default()); let mut tar_builder = tar::Builder::new(gz_encoder); tar_builder .append_path_with_name(&install_path, "INSTALL") .unwrap(); tar_builder.into_inner().unwrap().finish().unwrap(); let result = Package::check(archive_path.to_str().unwrap().to_string()); match result { Ok(_) => { panic!("expected error, got Ok"); } Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => { return; } Err(err) => { assert_eq!(err.kind(), std::io::ErrorKind::InvalidData); assert!(err.to_string().contains("Arch mismatch")); } } } }