...
This commit is contained in:
208
packages/system/os/tests/download_tests.rs
Normal file
208
packages/system/os/tests/download_tests.rs
Normal file
@@ -0,0 +1,208 @@
|
||||
use sal_os::{download, DownloadError};
|
||||
use std::fs;
|
||||
use tempfile::TempDir;
|
||||
|
||||
#[test]
|
||||
fn test_chmod_exec() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let test_file = temp_dir.path().join("test_script.sh");
|
||||
|
||||
// Create a test file
|
||||
fs::write(&test_file, "#!/bin/bash\necho 'test'").unwrap();
|
||||
|
||||
// Make it executable
|
||||
let result = download::chmod_exec(test_file.to_str().unwrap());
|
||||
assert!(result.is_ok());
|
||||
|
||||
// Check if file is executable (Unix only)
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let metadata = fs::metadata(&test_file).unwrap();
|
||||
let permissions = metadata.permissions();
|
||||
assert!(permissions.mode() & 0o111 != 0); // Check if any execute bit is set
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_download_error_handling() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
|
||||
// Test with invalid URL
|
||||
let result = download::download("invalid-url", temp_dir.path().to_str().unwrap(), 0);
|
||||
assert!(result.is_err());
|
||||
|
||||
// Test with non-existent domain
|
||||
let result = download::download(
|
||||
"https://nonexistentdomain12345.com/file.txt",
|
||||
temp_dir.path().to_str().unwrap(),
|
||||
0,
|
||||
);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_download_file_error_handling() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let dest_file = temp_dir.path().join("downloaded_file.txt");
|
||||
|
||||
// Test with invalid URL
|
||||
let result = download::download_file("invalid-url", dest_file.to_str().unwrap(), 0);
|
||||
assert!(result.is_err());
|
||||
|
||||
// Test with non-existent domain
|
||||
let result = download::download_file(
|
||||
"https://nonexistentdomain12345.com/file.txt",
|
||||
dest_file.to_str().unwrap(),
|
||||
0,
|
||||
);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_download_install_error_handling() {
|
||||
// Test with invalid URL
|
||||
let result = download::download_install("invalid-url", 0);
|
||||
assert!(result.is_err());
|
||||
|
||||
// Test with non-existent domain
|
||||
let result = download::download_install("https://nonexistentdomain12345.com/package.deb", 0);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_download_minimum_size_validation() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
|
||||
// Test with a very high minimum size requirement that won't be met
|
||||
// This should fail even if the URL exists
|
||||
let result = download::download(
|
||||
"https://httpbin.org/bytes/10", // This returns only 10 bytes
|
||||
temp_dir.path().to_str().unwrap(),
|
||||
1000, // Require 1000KB minimum
|
||||
);
|
||||
// This might succeed or fail depending on network, but we're testing the interface
|
||||
// The important thing is that it doesn't panic
|
||||
let _ = result;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_download_to_nonexistent_directory() {
|
||||
// Test downloading to a directory that doesn't exist
|
||||
// The download function should create parent directories
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let nonexistent_dir = temp_dir.path().join("nonexistent").join("nested");
|
||||
|
||||
let _ = download::download(
|
||||
"https://httpbin.org/status/404", // This will fail, but directory creation should work
|
||||
nonexistent_dir.to_str().unwrap(),
|
||||
0,
|
||||
);
|
||||
|
||||
// The directory should be created even if download fails
|
||||
assert!(nonexistent_dir.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_chmod_exec_nonexistent_file() {
|
||||
// Test chmod_exec on a file that doesn't exist
|
||||
let result = download::chmod_exec("/nonexistent/path/file.sh");
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_download_file_path_validation() {
|
||||
let _ = TempDir::new().unwrap();
|
||||
|
||||
// Test with invalid destination path
|
||||
let result = download::download_file(
|
||||
"https://httpbin.org/status/404",
|
||||
"/invalid/path/that/does/not/exist/file.txt",
|
||||
0,
|
||||
);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
// Integration test that requires network access
|
||||
// This test is marked with ignore so it doesn't run by default
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_download_real_file() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
|
||||
// Download a small file from httpbin (a testing service)
|
||||
let result = download::download(
|
||||
"https://httpbin.org/bytes/100", // Returns 100 random bytes
|
||||
temp_dir.path().to_str().unwrap(),
|
||||
0,
|
||||
);
|
||||
|
||||
if result.is_ok() {
|
||||
// If download succeeded, verify the file exists
|
||||
let downloaded_path = result.unwrap();
|
||||
assert!(fs::metadata(&downloaded_path).is_ok());
|
||||
|
||||
// Verify file size is approximately correct
|
||||
let metadata = fs::metadata(&downloaded_path).unwrap();
|
||||
assert!(metadata.len() >= 90 && metadata.len() <= 110); // Allow some variance
|
||||
}
|
||||
// If download failed (network issues), that's okay for this test
|
||||
}
|
||||
|
||||
// Integration test for download_file
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn test_download_file_real() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let dest_file = temp_dir.path().join("test_download.bin");
|
||||
|
||||
// Download a small file to specific location
|
||||
let result = download::download_file(
|
||||
"https://httpbin.org/bytes/50",
|
||||
dest_file.to_str().unwrap(),
|
||||
0,
|
||||
);
|
||||
|
||||
if result.is_ok() {
|
||||
// Verify the file was created at the specified location
|
||||
assert!(dest_file.exists());
|
||||
|
||||
// Verify file size
|
||||
let metadata = fs::metadata(&dest_file).unwrap();
|
||||
assert!(metadata.len() >= 40 && metadata.len() <= 60); // Allow some variance
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_download_error_types() {
|
||||
// DownloadError is already imported at the top
|
||||
|
||||
// Test that our error types can be created and displayed
|
||||
let error = DownloadError::InvalidUrl("test".to_string());
|
||||
assert!(!error.to_string().is_empty());
|
||||
|
||||
let error = DownloadError::DownloadFailed("test".to_string());
|
||||
assert!(!error.to_string().is_empty());
|
||||
|
||||
let error = DownloadError::FileTooSmall(50, 100);
|
||||
assert!(!error.to_string().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_download_url_parsing() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
|
||||
// Test with URL that has no filename
|
||||
let result = download::download("https://example.com/", temp_dir.path().to_str().unwrap(), 0);
|
||||
// Should fail with invalid URL error
|
||||
assert!(result.is_err());
|
||||
|
||||
// Test with URL that has query parameters
|
||||
let result = download::download(
|
||||
"https://httpbin.org/get?param=value",
|
||||
temp_dir.path().to_str().unwrap(),
|
||||
0,
|
||||
);
|
||||
// This might succeed or fail depending on network, but shouldn't panic
|
||||
let _ = result;
|
||||
}
|
Reference in New Issue
Block a user