diff --git a/Cargo.lock b/Cargo.lock index e9d1af1..cbd7792 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -474,6 +474,7 @@ dependencies = [ "libsystemd", "serde_cbor", "tarpc", + "tempfile", "tokio", "tokio-serde", "tokio-stream", diff --git a/event-broker/Cargo.toml b/event-broker/Cargo.toml index 8378081..a667605 100644 --- a/event-broker/Cargo.toml +++ b/event-broker/Cargo.toml @@ -26,3 +26,6 @@ tokio-stream = "0.1" toml = "0.6" tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } + +[dev-dependencies] +tempfile = "3" diff --git a/event-broker/tests/test.rs b/event-broker/tests/test.rs new file mode 100644 index 0000000..e46a987 --- /dev/null +++ b/event-broker/tests/test.rs @@ -0,0 +1,107 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +// Copyright (C) 2023 The crypto-auditing developers. + +use crypto_auditing::event_broker::Client; +use futures::stream::StreamExt; +use std::env; +use std::fs; +use std::io::{Read, Write}; +use std::path::PathBuf; +use std::process::{Child, Command}; +use std::thread; +use std::time::Duration; +use tempfile::tempdir; +use tracing_subscriber::{fmt, prelude::*, EnvFilter}; + +fn fixture_dir() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .join("fixtures") +} + +fn target_dir() -> PathBuf { + env::current_exe() + .ok() + .map(|mut path| { + path.pop(); + if path.ends_with("deps") { + path.pop(); + } + path + }) + .unwrap() +} + +struct EventBrokerProcess(Child); + +impl Drop for EventBrokerProcess { + fn drop(&mut self) { + self.0.kill().expect("unable to kill event-broker"); + } +} + +#[tokio::test] +async fn test_event_broker() { + tracing_subscriber::registry() + .with(fmt::layer()) + .with(EnvFilter::from_default_env()) + .try_init() + .expect("unable to initialize subscriber"); + + let event_broker_path = target_dir().join("crypto-auditing-event-broker"); + let test_dir = tempdir().expect("unable to create temporary directory"); + + let log_path = test_dir.path().join("agent.log"); + let mut log_file = fs::OpenOptions::new() + .write(true) + .create(true) + .append(true) + .open(&log_path) + .expect("unable to write log file"); + + let socket_path = test_dir.path().join("audit.sock"); + + let process = Command::new(&event_broker_path) + .arg("-c") + .arg(fixture_dir().join("conf").join("event-broker.conf")) + .arg("--log-file") + .arg(&log_path) + .arg("--socket-path") + .arg(&socket_path) + .spawn() + .expect("unable to spawn event-broker"); + + let _process = EventBrokerProcess(process); + + // Wait until the agent starts up + for _ in 0..5 { + if socket_path.exists() { + break; + } + thread::sleep(Duration::from_millis(100)); + } + assert!(socket_path.exists()); + + let client = Client::new() + .scopes(&vec!["tls".to_string()]) + .address(&socket_path); + + let (_handle, mut reader) = client.start().await.expect("unable to start client"); + + // Append more data to log file + let mut fixture_file = fs::OpenOptions::new() + .read(true) + .open(&fixture_dir().join("normal").join("output.cborseq")) + .expect("unable to open fixture"); + let mut buffer = Vec::new(); + fixture_file + .read_to_end(&mut buffer) + .expect("unable to read fixture"); + log_file + .write_all(&buffer) + .expect("unable to append fixture"); + log_file.flush().expect("unable to flush fixture"); + + assert!(reader.next().await.is_some()); +} diff --git a/fixtures/conf/event-broker.conf b/fixtures/conf/event-broker.conf new file mode 100644 index 0000000..2eeeb73 --- /dev/null +++ b/fixtures/conf/event-broker.conf @@ -0,0 +1,2 @@ +# log_file = "/var/log/crypto-auditing/audit.cborseq" +# socket_path = "/run/crypto-auditing/audit.sock"