174 lines
7.6 KiB
Rust
174 lines
7.6 KiB
Rust
// file: crates/ksp-logging-lib/tests/ownership.rs
|
|
// version: 3
|
|
|
|
//! Integration audit ensuring KSP crates do not bypass the logging facade.
|
|
|
|
fn manifest_declares_direct_dependency(manifest: &str, dependency_name: &str) -> bool {
|
|
let mut section = "";
|
|
for raw_line in manifest.lines() {
|
|
let line = raw_line.trim();
|
|
if line.starts_with('[') && line.ends_with(']') {
|
|
section = &line[1..line.len() - 1];
|
|
let direct_table_suffix = std::format!(".dependencies.{dependency_name}");
|
|
if section == std::format!("dependencies.{dependency_name}")
|
|
|| section == std::format!("dev-dependencies.{dependency_name}")
|
|
|| section == std::format!("build-dependencies.{dependency_name}")
|
|
|| section.ends_with(direct_table_suffix.as_str())
|
|
{
|
|
return true;
|
|
}
|
|
continue;
|
|
}
|
|
if line.is_empty() || line.starts_with('#') {
|
|
continue;
|
|
}
|
|
let dependency_section = section == "dependencies"
|
|
|| section == "dev-dependencies"
|
|
|| section == "build-dependencies"
|
|
|| section.ends_with(".dependencies")
|
|
|| section.ends_with(".dev-dependencies")
|
|
|| section.ends_with(".build-dependencies");
|
|
if !dependency_section {
|
|
continue;
|
|
}
|
|
let assignment = line.split_once('=');
|
|
let name = match assignment {
|
|
std::option::Option::Some((name, _)) => name.trim(),
|
|
std::option::Option::None => continue,
|
|
};
|
|
if name == dependency_name || name == std::format!("{dependency_name}.workspace") {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
fn source_uses_direct_path(source: &str, path: &str) -> bool {
|
|
let mut search_start = 0usize;
|
|
while search_start < source.len() {
|
|
let remaining = &source[search_start..];
|
|
let relative_index = match remaining.find(path) {
|
|
std::option::Option::Some(index) => index,
|
|
std::option::Option::None => return false,
|
|
};
|
|
let index = search_start + relative_index;
|
|
if index == 0 {
|
|
return true;
|
|
}
|
|
let previous = source[..index].chars().next_back();
|
|
let embedded_in_identifier = match previous {
|
|
std::option::Option::Some(character) => character.is_ascii_alphanumeric() || character == '_',
|
|
std::option::Option::None => false,
|
|
};
|
|
if !embedded_in_identifier {
|
|
return true;
|
|
}
|
|
search_start = index + path.len();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
fn collect_rust_files(directory: &std::path::Path, files: &mut std::vec::Vec<std::path::PathBuf>) {
|
|
let entries_result = std::fs::read_dir(directory);
|
|
let entries = match entries_result {
|
|
std::result::Result::Ok(entries) => entries,
|
|
std::result::Result::Err(_) => return,
|
|
};
|
|
for entry_result in entries {
|
|
let entry = match entry_result {
|
|
std::result::Result::Ok(entry) => entry,
|
|
std::result::Result::Err(_) => continue,
|
|
};
|
|
let path = entry.path();
|
|
if path.is_dir() {
|
|
collect_rust_files(path.as_path(), files);
|
|
continue;
|
|
}
|
|
if path.extension().and_then(std::ffi::OsStr::to_str) == std::option::Option::Some("rs") {
|
|
files.push(path);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn dependency_scanner_distinguishes_tauri_plugin_tracing_from_tracing() {
|
|
let plugin_only = "[dependencies]\ntauri-plugin-tracing.workspace = true\n";
|
|
let direct_workspace = "[dependencies]\ntracing.workspace = true\n";
|
|
let direct_inline = "[dependencies]\ntracing = { workspace = true }\n";
|
|
let direct_table = "[dependencies.tracing]\nworkspace = true\n";
|
|
assert!(!manifest_declares_direct_dependency(plugin_only, "tracing"));
|
|
assert!(manifest_declares_direct_dependency(direct_workspace, "tracing"));
|
|
assert!(manifest_declares_direct_dependency(direct_inline, "tracing"));
|
|
assert!(manifest_declares_direct_dependency(direct_table, "tracing"));
|
|
assert!(!source_uses_direct_path("let plugin = tauri_plugin_tracing::Builder::new();", "tracing::"));
|
|
assert!(source_uses_direct_path("let span = tracing::info_span!(\"audit\");", "tracing::"));
|
|
}
|
|
|
|
#[test]
|
|
fn workspace_crates_do_not_bypass_ksp_logging_facade() {
|
|
let logging_manifest_directory = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
|
let workspace_root = match logging_manifest_directory.parent().and_then(std::path::Path::parent) {
|
|
std::option::Option::Some(root) => root,
|
|
std::option::Option::None => return,
|
|
};
|
|
let crates_directory = workspace_root.join("crates");
|
|
let entries_result = std::fs::read_dir(crates_directory.as_path());
|
|
assert!(entries_result.is_ok(), "unable to inspect workspace crates at {}", crates_directory.display());
|
|
let entries = match entries_result {
|
|
std::result::Result::Ok(entries) => entries,
|
|
std::result::Result::Err(_) => return,
|
|
};
|
|
for entry_result in entries {
|
|
let entry = match entry_result {
|
|
std::result::Result::Ok(entry) => entry,
|
|
std::result::Result::Err(_) => continue,
|
|
};
|
|
let crate_path = entry.path();
|
|
if !crate_path.is_dir() || entry.file_name() == std::ffi::OsStr::new("ksp-logging-lib") {
|
|
continue;
|
|
}
|
|
let manifest_path = crate_path.join("Cargo.toml");
|
|
if manifest_path.exists() {
|
|
let manifest_result = std::fs::read_to_string(manifest_path.as_path());
|
|
assert!(manifest_result.is_ok(), "unable to read {}", manifest_path.display());
|
|
let manifest = match manifest_result {
|
|
std::result::Result::Ok(manifest) => manifest,
|
|
std::result::Result::Err(_) => continue,
|
|
};
|
|
assert!(!manifest_declares_direct_dependency(manifest.as_str(), "tracing"), "{} depends directly on tracing", manifest_path.display(),);
|
|
assert!(
|
|
!manifest_declares_direct_dependency(manifest.as_str(), "tracing-subscriber"),
|
|
"{} depends directly on tracing-subscriber",
|
|
manifest_path.display(),
|
|
);
|
|
assert!(
|
|
!manifest_declares_direct_dependency(manifest.as_str(), "tracing-appender"),
|
|
"{} depends directly on tracing-appender",
|
|
manifest_path.display(),
|
|
);
|
|
}
|
|
let mut rust_files = std::vec::Vec::new();
|
|
collect_rust_files(crate_path.as_path(), &mut rust_files);
|
|
for rust_file in rust_files {
|
|
let source_result = std::fs::read_to_string(rust_file.as_path());
|
|
assert!(source_result.is_ok(), "unable to read {}", rust_file.display());
|
|
let source = match source_result {
|
|
std::result::Result::Ok(source) => source,
|
|
std::result::Result::Err(_) => continue,
|
|
};
|
|
assert!(!source_uses_direct_path(source.as_str(), "tracing::"), "{} bypasses ksp-logging-lib via tracing", rust_file.display());
|
|
assert!(
|
|
!source_uses_direct_path(source.as_str(), "ksp_logging_lib::tracing::"),
|
|
"{} bypasses ksp-logging-lib via its hidden tracing bridge",
|
|
rust_file.display(),
|
|
);
|
|
assert!(
|
|
!source_uses_direct_path(source.as_str(), "tracing_subscriber::"),
|
|
"{} bypasses ksp-logging-lib via tracing-subscriber",
|
|
rust_file.display(),
|
|
);
|
|
assert!(!source_uses_direct_path(source.as_str(), "tracing_appender::"), "{} bypasses ksp-logging-lib via tracing-appender", rust_file.display(),);
|
|
}
|
|
}
|
|
}
|