summaryrefslogtreecommitdiff
path: root/tests/pkgtoolkit_funcs.rs
blob: 667592d7427980b05789e2686fcc7027e547649c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use mesk::pkgtoolkit::pkgtools::{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() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = TempDir::new()?;
        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)?;

        let archive_path = temp_dir.path().join("test_archive.tar.gz");
        let file = fs::File::create(&archive_path)?;
        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")?;
        tar_builder.into_inner()?.finish()?;

        Package::extract_archive(archive_path.to_str().unwrap())?;

        let extracted_file_path = temp_dir.path().join("extracted_test_file.txt");
        assert!(extracted_file_path.exists());
        let content = fs::read_to_string(extracted_file_path)?;
        assert_eq!(content, test_file_content);

        Ok(())
    }

    #[tokio::test]
    async fn test_package_check_valid() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = TempDir::new()?;
        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)?;
        install_file.write_all(install_content.as_bytes())?;

        let archive_path = temp_dir.path().join("test_pkg_with_install.tar.gz");
        let file = fs::File::create(&archive_path)?;
        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")?;
        tar_builder.into_inner()?.finish()?;

        let result = Package::check(archive_path.to_str().unwrap().to_string());

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), true);

        Ok(())
    }

    #[tokio::test]
    async fn test_package_check_missing_install() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = TempDir::new()?;

        let archive_path = temp_dir.path().join("test_pkg_without_install.tar.gz");
        let file = fs::File::create(&archive_path)?;
        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")?;
        tar_builder.append_path_with_name(&dummy_path, "dummy.txt")?;
        tar_builder.into_inner()?.finish()?;

        let result = Package::check(archive_path.to_str().unwrap().to_string());

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::NotFound);
        assert!(err.to_string().contains("INSTALL file not found"));

        Ok(())
    }

    #[tokio::test]
    async fn test_package_check_empty_install() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = TempDir::new()?;

        let install_path = temp_dir.path().join("INSTALL");
        fs::write(&install_path, "")?;
        let archive_path = temp_dir.path().join("test_pkg_with_empty_install.tar.gz");
        let file = fs::File::create(&archive_path)?;
        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")?;
        tar_builder.into_inner()?.finish()?;

        let result = Package::check(archive_path.to_str().unwrap().to_string());

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
        assert!(err.to_string().contains("INSTALL file is empty"));

        Ok(())
    }

    #[tokio::test]
    async fn test_package_check_arch_mismatch() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = TempDir::new()?;
        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)?;
        install_file.write_all(install_content.as_bytes())?;

        let archive_path = temp_dir.path().join("test_pkg_with_wrong_arch.tar.gz");
        let file = fs::File::create(&archive_path)?;
        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")?;
        tar_builder.into_inner()?.finish()?;

        let result = Package::check(archive_path.to_str().unwrap().to_string());

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.kind(), std::io::ErrorKind::InvalidData);
        assert!(err.to_string().contains("Arch mismatch"));

        Ok(())
    }
}