Refactor application structure and simplify implementation
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
use proxywarden_lib::proxifyre_ownership::verify_managed_proxifyre_install;
|
||||
use serde_json::json;
|
||||
use std::{fs, path::PathBuf};
|
||||
|
||||
#[test]
|
||||
fn accepts_matching_managed_install_and_returns_packet_filter_ownership() {
|
||||
let fixture = ManagedInstallFixture::new("owned");
|
||||
fixture.write_marker(true, &fixture.install_dir);
|
||||
|
||||
let ownership = verify_managed_proxifyre_install(
|
||||
&fixture.install_dir,
|
||||
&fixture.executable_path,
|
||||
&fixture.install_dir,
|
||||
)
|
||||
.expect("matching marker should prove ownership");
|
||||
|
||||
assert_eq!(ownership.service_name, "ProxiFyreService");
|
||||
assert!(ownership.remove_packet_filter);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_install_outside_expected_managed_directory() {
|
||||
let fixture = ManagedInstallFixture::new("unexpected-root");
|
||||
fixture.write_marker(true, &fixture.install_dir);
|
||||
let other_root = fixture
|
||||
.root
|
||||
.join("other")
|
||||
.join("components")
|
||||
.join("ProxiFyre");
|
||||
fs::create_dir_all(&other_root).expect("other root should be created");
|
||||
|
||||
let error = verify_managed_proxifyre_install(
|
||||
&fixture.install_dir,
|
||||
&fixture.executable_path,
|
||||
&other_root,
|
||||
)
|
||||
.expect_err("a detected portable install must not be recursively removed");
|
||||
|
||||
assert!(error.contains("не является управляемой папкой"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_marker_with_mismatched_install_root() {
|
||||
let fixture = ManagedInstallFixture::new("mismatched-marker");
|
||||
fixture.write_marker(false, &fixture.root);
|
||||
|
||||
let error = verify_managed_proxifyre_install(
|
||||
&fixture.install_dir,
|
||||
&fixture.executable_path,
|
||||
&fixture.install_dir,
|
||||
)
|
||||
.expect_err("marker installRoot must match the managed directory");
|
||||
|
||||
assert!(error.contains("installRoot из marker"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_marker_with_foreign_service_name() {
|
||||
let fixture = ManagedInstallFixture::new("foreign-service");
|
||||
fixture.write_custom_marker(json!({
|
||||
"manager": "ProxyWarden",
|
||||
"component": "proxifyre",
|
||||
"serviceName": "ForeignProxyService",
|
||||
"installRoot": fixture.install_dir,
|
||||
"packetFilterInstalledByProxyWarden": true
|
||||
}));
|
||||
|
||||
let error = verify_managed_proxifyre_install(
|
||||
&fixture.install_dir,
|
||||
&fixture.executable_path,
|
||||
&fixture.install_dir,
|
||||
)
|
||||
.expect_err("foreign service name must not be trusted");
|
||||
|
||||
assert!(error.contains("неподдерживаемое имя службы"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_packet_filter_flag_defaults_to_not_owned() {
|
||||
let fixture = ManagedInstallFixture::new("shared-driver");
|
||||
fixture.write_custom_marker(json!({
|
||||
"manager": "ProxyWarden",
|
||||
"component": "proxifyre",
|
||||
"serviceName": "ProxiFyreService",
|
||||
"installRoot": fixture.install_dir
|
||||
}));
|
||||
|
||||
let ownership = verify_managed_proxifyre_install(
|
||||
&fixture.install_dir,
|
||||
&fixture.executable_path,
|
||||
&fixture.install_dir,
|
||||
)
|
||||
.expect("valid marker without ownership flag should remain safe");
|
||||
|
||||
assert!(!ownership.remove_packet_filter);
|
||||
}
|
||||
|
||||
struct ManagedInstallFixture {
|
||||
root: PathBuf,
|
||||
install_dir: PathBuf,
|
||||
executable_path: PathBuf,
|
||||
}
|
||||
|
||||
impl ManagedInstallFixture {
|
||||
fn new(label: &str) -> Self {
|
||||
let root = std::env::temp_dir().join(format!(
|
||||
"proxywarden-ownership-{label}-{}",
|
||||
uuid::Uuid::new_v4().hyphenated()
|
||||
));
|
||||
let install_dir = root.join("components").join("ProxiFyre");
|
||||
let executable_path = install_dir.join("ProxiFyre.exe");
|
||||
fs::create_dir_all(&install_dir).expect("managed install directory should be created");
|
||||
fs::write(&executable_path, b"fixture").expect("fixture executable should be written");
|
||||
|
||||
Self {
|
||||
root,
|
||||
install_dir,
|
||||
executable_path,
|
||||
}
|
||||
}
|
||||
|
||||
fn write_marker(&self, packet_filter_owned: bool, install_root: &std::path::Path) {
|
||||
self.write_custom_marker(json!({
|
||||
"manager": "ProxyWarden",
|
||||
"component": "proxifyre",
|
||||
"serviceName": "ProxiFyreService",
|
||||
"installRoot": install_root,
|
||||
"packetFilterInstalledByProxyWarden": packet_filter_owned
|
||||
}));
|
||||
}
|
||||
|
||||
fn write_custom_marker(&self, marker: serde_json::Value) {
|
||||
fs::write(
|
||||
self.install_dir.join("proxywarden-component.json"),
|
||||
serde_json::to_vec_pretty(&marker).expect("marker should serialize"),
|
||||
)
|
||||
.expect("marker should be written");
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ManagedInstallFixture {
|
||||
fn drop(&mut self) {
|
||||
let _ = fs::remove_dir_all(&self.root);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user