Файли
openidea/crates/ide-host/tests/trust.rs
T
Mr0Bread b60b743843 refactor(ide-host): shared test HostConfig constructors replace ~100 copied literals (#5501)
`HostConfig` (9 fields) was written out as a full struct literal in about 100 tests of `ide-host`, almost all of them the same boilerplate, so a new field meant editing ~100 places. This adds one base constructor for tests and migrates every literal that is a plain variant of it. Tests that differ keep struct-update overrides (`HostConfig { sink_for: .., ..HostConfig::for_test(&base) }`). No builder.
- Unit tests (`src/`): `#[cfg(test)] pub(crate) fn HostConfig::for_test(base: &Path)` in `lib.rs`.
- Integration tests (`tests/`): `tests/common/mod.rs` with `host_config(base: &Path)`, pulled in with `mod common;`. It builds the same value as `for_test`.
Both build: `config`, `data`, and `data` again as the machine-local dir (the layout most tests used), `LanguageRegistry::load(None)`, empty syntax, a sink that drops events, identity owner, `Handle::current()`, `project_environment: false`.
Why not one constructor for both: integration tests compile against the crate without `cfg(test)`, so a shared one needs a new `testing` cargo feature plus a dev-dependency on the crate itself (nothing in `ide-host` does that today). Two small definitions need no feature, no `Cargo.toml` change and no public item.

Co-authored-by: Mr0Bread <[email protected]>
2026-11-24 07:02:21 +08:00

420 рядки
19 KiB
Rust

mod common;
#[cfg(unix)]
use ide_host::ops;
use ide_host::{Host, HostConfig, catalogue::TrustClass, trust::TrustStore};
use std::path::Path;
use std::sync::Arc;
#[cfg(unix)]
use std::time::{Duration, Instant};
fn host(base: &Path, environment: bool) -> Arc<Host> {
Arc::new(Host::new(HostConfig { project_environment: environment, ..common::host_config(base) }))
}
/// Each class against each window state: a trusted project, a Restricted one, and no project.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_commands_trust_class_decides_where_it_runs() {
let tmp = tempfile::tempdir().unwrap();
let restricted = tmp.path().join("restricted");
let trusted = tmp.path().join("trusted");
std::fs::create_dir(&restricted).unwrap();
std::fs::create_dir(&trusted).unwrap();
let host = host(&tmp.path().join("app"), false);
assert!(host.project("welcome").is_err());
assert!(ide_host::ops::project::project_roots(&host, "welcome").is_err());
assert!(!host.open_project("main", &restricted).unwrap().is_trusted());
host.set_project_trust(&trusted, true, false).await.unwrap();
assert!(host.open_project("other", &trusted).unwrap().is_trusted());
let allowed = |window, class| host.authorize(window, class).is_ok();
for (class, in_trusted, in_restricted, without_project) in [
(TrustClass::Always, true, true, true),
(TrustClass::OutsideRestricted, true, false, true),
(TrustClass::Trusted, true, false, false),
] {
assert_eq!(allowed("other", class), in_trusted, "{class:?} in a trusted project");
assert_eq!(allowed("main", class), in_restricted, "{class:?} in Restricted Mode");
assert_eq!(allowed("welcome", class), without_project, "{class:?} without a project");
}
// Toolchain detection runs from Settings opened on the Welcome screen, but not in Restricted Mode.
assert!(ide_host::ops::sdk::sdk_detect(&host, "welcome", ide_sdk::SdkKind::Zig).await.is_ok());
assert!(ide_host::ops::sdk::sdk_detect(&host, "main", ide_sdk::SdkKind::Zig).await.is_err());
// These requests validate the vendor before any network access, independently of project trust.
let error = ide_host::ops::tools::jdk_download_releases(&host, "unknown-vendor".into(), ide_core::RequestId(901))
.await
.unwrap_err();
assert_eq!(error.kind, ide_core::error::ErrorKind::InvalidInput);
let error = ide_host::ops::tools::jdk_download(
&host,
"unknown-vendor".into(),
25,
None,
ide_core::RequestId(902),
ide_host::ops::Channel::new(|_| {}),
)
.await
.unwrap_err();
assert_eq!(error.kind, ide_core::error::ErrorKind::InvalidInput);
host.shutdown().await;
}
#[tokio::test]
async fn welcome_window_browses_selected_ssh_host_without_executing_commands() {
use ide_core::remote_settings::{SshAuth, SshConfiguration};
use ide_ssh::{FsConnection, SshFiles, SshManager};
let tmp = tempfile::tempdir().unwrap();
let server_root = tmp.path().join("server");
std::fs::create_dir_all(server_root.join("projects/demo")).unwrap();
std::fs::write(server_root.join("projects/README.md"), "remote file").unwrap();
let port = ide_ssh::testing::sshd(server_root, Arc::new(|cmd| panic!("unexpected SSH exec: {cmd}"))).await;
let ssh = Arc::new(SshManager::with_files(
tmp.path().join("secrets.json"),
SshFiles { known_hosts: tmp.path().join("known_hosts"), config: None },
));
ssh.set_configs(vec![SshConfiguration {
id: "selected-host".into(),
host: "127.0.0.1".into(),
port: Some(port),
user: "dev".into(),
auth: SshAuth::Password,
..Default::default()
}]);
ssh.set_secret("selected-host", Some("secret".into()), false);
assert!(ssh.session("selected-host").await.is_err());
ssh.trust_host("selected-host").await.unwrap();
let mut host = host(&tmp.path().join("app"), false);
let state = Arc::get_mut(&mut host).unwrap();
state.ssh = ssh;
state.remote = Arc::new(ide_remote::RemoteService::new(state.ssh.clone(), tmp.path().join("cache")));
assert!(host.project("welcome").is_err());
let connection = FsConnection::Sftp { ssh: "selected-host".into() };
let entries = host.remote.list(&connection, "/projects").await.unwrap();
assert!(entries.iter().any(|e| e.path == "/projects/demo" && e.is_dir));
assert!(entries.iter().any(|e| e.name == "README.md" && !e.is_dir));
assert!(host.remote.list(&connection, "/projects/demo").await.unwrap().is_empty());
host.remote.test_connection(&connection, "/projects").await.unwrap();
let local = tmp.path().join("local-deployment");
std::fs::create_dir(&local).unwrap();
ide_host::ops::deploy::deploy_test_connection(&host, FsConnection::Local, local.to_str().unwrap().into(), None)
.await
.unwrap();
assert_eq!(std::fs::read_dir(local).unwrap().count(), 0, "write probe is cleaned up");
assert!(host.project("welcome").is_err());
host.shutdown().await;
}
#[test]
fn persists_user_decisions_locations_revocation_and_one_time_migration() {
let tmp = tempfile::tempdir().unwrap();
let config = tmp.path().join("config");
let data = tmp.path().join("data");
let a = tmp.path().join("projects/a");
let b = tmp.path().join("projects/b");
std::fs::create_dir_all(a.join(".openidea")).unwrap();
std::fs::create_dir_all(b.join(".openidea")).unwrap();
std::fs::write(a.join(".openidea/project-trust.json"), r#"{"trusted":true}"#).unwrap();
std::fs::write(b.join(".openidea/project-trust.json"), r#"{"trusted":true}"#).unwrap();
let recent = ide_workspace::RecentProjects::load(&config);
recent.touch(&a, None).unwrap();
let trust = TrustStore::load(&config, &data);
assert!(trust.trusted(&a).unwrap());
assert!(!trust.status(&b).unwrap().decided);
recent.touch(&b, None).unwrap();
let trust = TrustStore::load(&config, &data);
assert!(!trust.trusted(&b).unwrap(), "migration is not repeated");
trust.decide(&a, true, true).unwrap();
assert!(trust.trusted(&b).unwrap());
trust.decide(&b, false, false).unwrap();
assert!(!trust.trusted(&b).unwrap(), "revocation overrides a trusted location");
let trust = TrustStore::load(&config, &data);
assert!(!trust.trusted(&b).unwrap());
trust.remove_location(&dunce::canonicalize(a.parent().unwrap()).unwrap()).unwrap();
assert!(trust.trusted(&a).unwrap());
trust.decide(&a, false, false).unwrap();
assert!(!TrustStore::load(&config, &data).trusted(&a).unwrap());
std::fs::write(data.join("project-trust.json"), "broken").unwrap();
assert!(TrustStore::load(&config, &data).trusted(&a).is_err(), "corruption fails closed");
}
#[test]
fn an_inherited_folder_is_trusted_for_this_process_only() {
let tmp = tempfile::tempdir().unwrap();
let (config, data) = (tmp.path().join("config"), tmp.path().join("data"));
let root = tmp.path().join("workspaces/app");
std::fs::create_dir_all(&root).unwrap();
let trust = TrustStore::load(&config, &data);
assert!(!trust.trusted(&root).unwrap());
trust.inherit(&root).unwrap();
assert!(trust.trusted(&root).unwrap());
assert!(!trust.trusted(root.parent().unwrap()).unwrap(), "only that folder");
assert!(trust.status(&root).unwrap().decided, "the trust prompt doesn't ask again");
assert!(trust.decisions().unwrap().projects.is_empty(), "no decision is recorded");
assert!(!TrustStore::load(&config, &data).trusted(&root).unwrap(), "not persisted");
trust.decide(&root, false, false).unwrap();
assert!(!trust.trusted(&root).unwrap(), "a decision to restrict it still wins");
}
#[cfg(unix)]
fn script(root: &Path, name: &str, marker: &str) {
use std::os::unix::fs::PermissionsExt;
let path = root.join(name);
std::fs::write(&path, format!("#!/bin/sh\nprintf executed > '{marker}'\n")).unwrap();
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
}
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn malicious_project_stays_editable_then_starts_in_place_after_trust() {
let tmp = tempfile::tempdir().unwrap();
let root = dunce::canonicalize(tmp.path()).unwrap().join("malicious");
std::fs::create_dir_all(root.join(".openidea")).unwrap();
script(&root, "hook", "hook.marker");
script(&root, "server", "lsp.marker");
script(&root, "gradlew", "build.marker");
std::fs::write(root.join("build.gradle"), "plugins { id 'java' }").unwrap();
std::fs::write(root.join("main.py"), "print('safe')\n").unwrap();
std::fs::write(root.join(".devcontainer.json"), r#"{"postStartCommand":["./hook"]}"#).unwrap();
std::fs::write(root.join(".openidea/settings.toml"), format!(
"[languageServers.trust-witness]\nlanguages = ['python']\ncommand = {:?}\n[languageServers.pyright]\nenabled = false\n", root.join("server").to_str().unwrap()
)).unwrap();
let host = host(&tmp.path().join("app"), true);
let project = host.open_project("main", &root).unwrap();
assert!(!project.is_trusted());
let doc = ops::doc::doc_open(&host, "main", root.join("main.py")).await.unwrap();
ops::doc::doc_save(&host, "main", doc.id).await.unwrap();
assert!(project.index.wait_idle(Duration::from_secs(10)));
assert!(project.index.status().files > 0);
let sync = || {
ops::build::build_sync(
&host,
"main",
ide_build::BuildTool::Gradle,
ide_core::RequestId(472),
false,
vec![],
None,
ops::Channel::new(|_| {}),
)
};
assert!(sync().await.is_err(), "restricted build marker: {}", root.join("build.marker").exists());
assert!(
ops::sdk::sdk_locate_tools(&host, "missing-window", vec!["server".into()], Some(root.clone())).await.is_err()
);
assert!(host.authorize("main", TrustClass::Trusted).is_err());
tokio::time::sleep(Duration::from_millis(200)).await;
for marker in ["hook.marker", "lsp.marker", "build.marker"] {
assert!(!root.join(marker).exists(), "{marker}");
}
host.set_project_trust(&root, true, false).await.unwrap();
assert!(Arc::ptr_eq(&project, &host.project("main").unwrap()));
assert_eq!(project.documents.snapshot(doc.id).unwrap().text, doc.text);
sync().await.unwrap();
let deadline = Instant::now() + Duration::from_secs(10);
while !root.join("hook.marker").exists() || !root.join("lsp.marker").exists() {
assert!(Instant::now() < deadline, "trusted services must start");
tokio::time::sleep(Duration::from_millis(20)).await;
}
assert!(root.join("build.marker").exists());
host.set_project_trust(&root, false, false).await.unwrap();
assert!(!project.is_trusted());
assert!(sync().await.is_err());
ops::doc::doc_save(&host, "main", doc.id).await.unwrap();
host.shutdown().await;
}
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn restricted_terminal_keeps_user_input_but_ignores_project_startup_commands() {
let tmp = tempfile::tempdir().unwrap();
let root = dunce::canonicalize(tmp.path()).unwrap().join("project");
std::fs::create_dir_all(&root).unwrap();
let host = host(&tmp.path().join("app"), false);
host.global_settings
.set(
ide_workspace::SettingsScope::Global,
"terminal.env",
serde_json::json!({"ZDOTDIR": root, "ENV": "/dev/null", "BASH_ENV": "/dev/null"}),
)
.unwrap();
host.open_project("main", &root).unwrap();
let terminal = ops::term::term_spawn(
&host,
"main",
ide_terminal::TerminalOptions {
cwd: root.clone().into(),
shell: Some("/bin/sh".into()),
args: vec!["-c".into(), "touch automatic.marker".into()],
env: Default::default(),
cols: 80,
rows: 24,
shell_integration: false,
},
ops::Channel::new(|_| {}),
)
.await
.unwrap();
host.terminals.write(terminal.id, "printf user > terminal.marker; exit\n").unwrap();
let deadline = Instant::now() + Duration::from_secs(10);
while !root.join("terminal.marker").exists() {
assert!(Instant::now() < deadline, "user terminal input must run");
tokio::time::sleep(Duration::from_millis(20)).await;
}
assert!(!root.join("automatic.marker").exists());
host.shutdown().await;
}
#[cfg(unix)]
#[test]
fn trusted_locations_use_canonical_directory_boundaries() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("trusted/project");
let other = tmp.path().join("trusted-other/project");
std::fs::create_dir_all(&root).unwrap();
std::fs::create_dir_all(&other).unwrap();
std::os::unix::fs::symlink(other.parent().unwrap(), root.parent().unwrap().join("escape")).unwrap();
let trust = TrustStore::load(&tmp.path().join("config"), &tmp.path().join("data"));
trust.decide(&root, true, true).unwrap();
assert!(!trust.trusted(&other).unwrap());
let alias = root.parent().unwrap().join("escape/project");
assert!(!trust.trusted(&alias).unwrap());
assert_eq!(trust.status(&alias).unwrap().root.as_ref(), dunce::canonicalize(&other).unwrap());
}
#[test]
fn add_location_trusts_every_project_under_it() {
let tmp = tempfile::tempdir().unwrap();
let location = tmp.path().join("projects");
let inside = location.join("nested/deep/app");
let outside = tmp.path().join("projects-other/app");
std::fs::create_dir_all(&inside).unwrap();
std::fs::create_dir_all(&outside).unwrap();
let file = tmp.path().join("not-a-dir");
std::fs::write(&file, "x").unwrap();
let trust = TrustStore::load(&tmp.path().join("config"), &tmp.path().join("data"));
trust.add_location(&location).unwrap();
assert!(trust.trusted(&inside).unwrap());
assert!(!trust.trusted(&outside).unwrap());
assert_eq!(trust.decisions().unwrap().locations.len(), 1);
trust.add_location(&location).unwrap();
assert_eq!(trust.decisions().unwrap().locations.len(), 1, "re-adding is a no-op");
assert!(trust.add_location(&file).is_err(), "a file is not a location");
assert!(trust.add_location(&tmp.path().join("missing")).is_err(), "a missing path is not a location");
let reloaded = TrustStore::load(&tmp.path().join("config"), &tmp.path().join("data"));
assert!(reloaded.trusted(&inside).unwrap());
}
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn restricted_git_views_ignore_project_executables_and_mutations_cannot_run_hooks() {
use std::os::unix::fs::PermissionsExt;
let tmp = tempfile::tempdir().unwrap();
let root = dunce::canonicalize(tmp.path()).unwrap().join("project");
std::fs::create_dir_all(root.join(".openidea")).unwrap();
let git = |args: &[&str]| {
let output = std::process::Command::new("git").current_dir(&root).args(args).output().unwrap();
assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr));
};
git(&["init", "-q"]);
git(&["config", "user.name", "Test"]);
git(&["config", "user.email", "[email protected]"]);
git(&["config", "commit.gpgsign", "false"]);
git(&["config", "core.hooksPath", ".git/hooks"]);
let file = root.join("safe.txt");
std::fs::write(&file, "safe\n").unwrap();
script(&root, ".git/hooks/pre-commit", "git-hook.marker");
let executable = root.join(".openidea/git-witness");
std::fs::write(&executable, "#!/bin/sh\nprintf executed > git-executable.marker\nexec git \"$@\"\n").unwrap();
std::fs::set_permissions(&executable, std::fs::Permissions::from_mode(0o755)).unwrap();
std::fs::write(
root.join(".openidea/settings.toml"),
format!("[vcs.git]\nexecutable = {:?}\n", executable.to_str().unwrap()),
)
.unwrap();
let host = host(&tmp.path().join("app"), false);
let project = host.open_project("main", &root).unwrap();
assert!(!project.with_repo(|repo| repo.status()).unwrap().is_empty());
assert!(ops::vcs::vcs_stage(&host, "main", vec![file.clone()]).await.is_err());
assert!(!root.join("git-hook.marker").exists());
assert!(!root.join("git-executable.marker").exists());
host.set_project_trust(&root, true, false).await.unwrap();
ops::vcs::vcs_stage(&host, "main", vec![file]).await.unwrap();
project.with_repo(|repo| repo.commit("trusted", false, false)).unwrap();
assert!(root.join("git-executable.marker").exists());
assert!(root.join("git-hook.marker").exists());
host.shutdown().await;
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn revocation_still_applies_when_a_project_folder_was_removed() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("project");
std::fs::create_dir(&root).unwrap();
let host = host(&tmp.path().join("app"), false);
host.trust.decide(&root, true, true).unwrap();
let project = host.open_project("main", &root).unwrap();
std::fs::remove_dir_all(&root).unwrap();
host.set_project_trust(&project.root, false, false).await.unwrap();
assert!(!project.is_trusted());
host.shutdown().await;
}
#[test]
fn attached_member_trust_is_required_after_restart_and_revocation() {
let tmp = tempfile::tempdir().unwrap();
let (config, data) = (tmp.path().join("config"), tmp.path().join("data"));
let base = dunce::canonicalize(tmp.path()).unwrap();
let root = base.join("primary");
let other = base.join("other");
for path in [&root, &other] {
std::fs::create_dir(path).unwrap();
}
ide_workspace::workspace::Workspace::attach(&root, "Primary", vec![root.clone(), other.clone()]).unwrap();
let trust = TrustStore::load(&config, &data);
trust.decide(&root, true, false).unwrap();
trust.inherit(&other).unwrap();
assert!(trust.window_trusted(&root).unwrap());
let trust = TrustStore::load(&config, &data);
assert!(trust.status(&root).unwrap().trusted);
assert!(!trust.window_trusted(&root).unwrap());
assert!(!trust.status(&other).unwrap().decided);
trust.decide(&root, true, false).unwrap();
assert!(!trust.window_trusted(&root).unwrap(), "trusting only the root does not recover the window");
trust.decide(&other, true, false).unwrap();
assert!(trust.window_trusted(&root).unwrap());
trust.decide(&other, false, false).unwrap();
assert!(trust.status(&other).unwrap().decided);
assert!(!trust.window_trusted(&root).unwrap());
trust.decide(&other, true, false).unwrap();
assert!(trust.window_trusted(&root).unwrap());
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_project_created_with_new_project_is_trusted() {
let tmp = tempfile::tempdir().unwrap();
let host = host(&tmp.path().join("app"), false);
let req: ide_scaffold::CreateRequest = serde_json::from_value(serde_json::json!({
"generator": "empty",
"location": tmp.path().join("projects").to_string_lossy(),
"name": "fresh",
"git": false,
"values": {},
}))
.unwrap();
let dir = ide_host::ops::scaffold::scaffold_create(
&host,
req,
ide_core::RequestId(902),
ide_host::ops::Channel::new(|_| {}),
)
.await
.unwrap();
let dir = std::path::PathBuf::from(dir);
assert!(host.trust.status(&dir).unwrap().trusted);
// Its siblings are not.
let sibling = tmp.path().join("projects/other");
std::fs::create_dir(&sibling).unwrap();
assert!(!host.trust.status(&sibling).unwrap().trusted);
}