From afee02024ec4ce2c463c5eadf38f1f44f286037c Mon Sep 17 00:00:00 2001 From: mdecimus Date: Sun, 4 Aug 2024 10:50:03 +0200 Subject: [PATCH] Fixed is_local_domain Sieve function (closes #622) --- crates/common/src/scripts/plugins/lookup.rs | 2 +- crates/common/src/tracing/mod.rs | 2 +- crates/trc/src/bitset.rs | 9 ++++ crates/trc/src/collector.rs | 4 ++ crates/trc/src/fmt.rs | 59 --------------------- tests/resources/smtp/sieve/stage_mail.sieve | 5 ++ tests/src/lib.rs | 13 +++++ tests/src/smtp/inbound/scripts.rs | 37 ++++++++----- tests/src/smtp/session.rs | 1 + 9 files changed, 59 insertions(+), 73 deletions(-) diff --git a/crates/common/src/scripts/plugins/lookup.rs b/crates/common/src/scripts/plugins/lookup.rs index a3d07889..404961ed 100644 --- a/crates/common/src/scripts/plugins/lookup.rs +++ b/crates/common/src/scripts/plugins/lookup.rs @@ -335,7 +335,7 @@ async fn exec_remote_(ctx: &PluginContext<'_>) -> trc::Result { } pub async fn exec_local_domain(ctx: PluginContext<'_>) -> trc::Result { - let domain = ctx.arguments[0].to_string(); + let domain = ctx.arguments[1].to_string(); if !domain.is_empty() { return match &ctx.arguments[0] { diff --git a/crates/common/src/tracing/mod.rs b/crates/common/src/tracing/mod.rs index 2ff2444a..3ec26f9f 100644 --- a/crates/common/src/tracing/mod.rs +++ b/crates/common/src/tracing/mod.rs @@ -88,7 +88,7 @@ impl Tracers { crate::config::tracers::ConsoleTracer { ansi: true, multiline: false, - buffered: true, + buffered: false, }, ); diff --git a/crates/trc/src/bitset.rs b/crates/trc/src/bitset.rs index 30890277..d94dd61c 100644 --- a/crates/trc/src/bitset.rs +++ b/crates/trc/src/bitset.rs @@ -66,6 +66,15 @@ impl AtomicBitset { self.0[i].store(0, Ordering::Relaxed); } } + + pub fn is_empty(&self) -> bool { + for i in 0..N { + if self.0[i].load(Ordering::Relaxed) != 0 { + return false; + } + } + true + } } impl Bitset { diff --git a/crates/trc/src/collector.rs b/crates/trc/src/collector.rs index 1cd5ae2e..7ecd833f 100644 --- a/crates/trc/src/collector.rs +++ b/crates/trc/src/collector.rs @@ -291,6 +291,10 @@ impl Collector { Collector::reload(); } + pub fn is_enabled() -> bool { + !INTERESTS.is_empty() + } + pub fn reload() { Event::new(EventType::Tracing(TracingEvent::Update)).send() } diff --git a/crates/trc/src/fmt.rs b/crates/trc/src/fmt.rs index fec702eb..e1bd4d9b 100644 --- a/crates/trc/src/fmt.rs +++ b/crates/trc/src/fmt.rs @@ -325,65 +325,6 @@ impl Color { mod tests { use crate::{EventType, Level}; - /*fn pepe() -> crate::Pop3Event { - crate::Pop3Event::Fetch - } - - #[test] - fn macro_test() { - let c = 1; - - crate::collector::Collector::collect_metrics( - EventType::Pop3(crate::Pop3Event::Quit), - &[(Key::AccountId, 1u32.into()), (Key::SpanId, 2u32.into())], - ); - - event_macro::event!( - Pop3(crate::Pop3Event::Quit), - SpanId = "const", - Size = 1234, - Contents = { - if true { - 1 - } else { - 0 - } - }, - ); - - event_macro::event!( - Pop3(if c == 1 { - crate::Pop3Event::RawInput - } else { - crate::Pop3Event::RawOutput - }), - SpanId = "abc", - Size = 1234, - Contents = { - if true { - 1 - } else { - 0 - } - }, - ); - - let tutu = pepe(); - - event_macro::event!( - Pop3(tutu), - SpanId = "tutu", - Size = 1234, - Contents = { - if true { - 1 - } else { - 0 - } - }, - ); - }*/ - #[test] fn print_all_events() { assert!(!Level::Disable.is_contained(Level::Warn)); diff --git a/tests/resources/smtp/sieve/stage_mail.sieve b/tests/resources/smtp/sieve/stage_mail.sieve index b2d40510..473a7631 100644 --- a/tests/resources/smtp/sieve/stage_mail.sieve +++ b/tests/resources/smtp/sieve/stage_mail.sieve @@ -10,3 +10,8 @@ eval "query('sql', 'INSERT OR IGNORE INTO blocked_senders (addr) VALUES (?)', 'm if eval "query('sql', 'SELECT 1 FROM blocked_senders WHERE addr=? LIMIT 1', [envelope.from])" { reject "Your address has been blocked."; } + +if eval "!is_local_domain('', 'localdomain.org') || is_local_domain('', 'other.org')" { + let "reason" "'result: ' + is_local_domain('', 'localdomain.org') + ' ' + is_local_domain('', 'other.org')"; + reject "is_local_domain function failed: ${reason}"; +} diff --git a/tests/src/lib.rs b/tests/src/lib.rs index 96d3b3bf..4c08038f 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -8,6 +8,8 @@ use std::path::PathBuf; #[cfg(not(target_env = "msvc"))] use jemallocator::Jemalloc; +#[cfg(test)] +use trc::collector::Collector; #[cfg(not(target_env = "msvc"))] #[global_allocator] @@ -59,3 +61,14 @@ impl AssertConfig for utils::config::Config { self } } + +#[cfg(test)] +pub fn enable_logging() { + use common::config::tracers::Tracers; + + if let Ok(level) = std::env::var("LOG") { + if !Collector::is_enabled() { + Tracers::test_tracer(level.parse().expect("Invalid log level")); + } + } +} diff --git a/tests/src/smtp/inbound/scripts.rs b/tests/src/smtp/inbound/scripts.rs index a35683ae..d3757c8c 100644 --- a/tests/src/smtp/inbound/scripts.rs +++ b/tests/src/smtp/inbound/scripts.rs @@ -7,11 +7,15 @@ use core::panic; use std::{fmt::Write, fs, path::PathBuf}; -use crate::smtp::{ - build_smtp, - inbound::{sign::SIGNATURES, TestMessage, TestQueueEvent}, - session::{TestSession, VerifyResponse}, - TempDir, TestSMTP, +use crate::{ + enable_logging, + smtp::{ + build_smtp, + inbound::{sign::SIGNATURES, TestMessage, TestQueueEvent}, + session::{TestSession, VerifyResponse}, + TempDir, TestSMTP, + }, + AssertConfig, }; use common::Core; @@ -28,6 +32,7 @@ data = "sql" lookup = "sql" blob = "sql" fts = "sql" +directory = "local" [store."sql"] type = "sqlite" @@ -84,17 +89,24 @@ message-id = true date = true return-path = false +[directory."local"] +type = "memory" + +[[directory."local".principals]] +name = "john" +description = "John Doe" +secret = "secret" +email = ["john@localdomain.org", "jdoe@localdomain.org", "john.doe@localdomain.org"] +email-list = ["info@localdomain.org"] +member-of = ["sales"] + + "#; #[tokio::test] async fn sieve_scripts() { - /*let disable = 1; - tracing::subscriber::set_global_default( - tracing_subscriber::FmtSubscriber::builder() - .with_max_level(tracing::Level::TRACE) - .finish(), - ) - .unwrap();*/ + // Enable logging + enable_logging(); // Add test scripts let mut config = CONFIG.to_string() + SIGNATURES; @@ -144,6 +156,7 @@ async fn sieve_scripts() { let stores = Stores::parse_all(&mut config).await; let core = Core::parse(&mut config, stores, Default::default()).await; let mut qr = inner.init_test_queue(&core); + config.assert_no_errors(); // Build session let core = build_smtp(core, inner); diff --git a/tests/src/smtp/session.rs b/tests/src/smtp/session.rs index fd1173c0..62a39f20 100644 --- a/tests/src/smtp/session.rs +++ b/tests/src/smtp/session.rs @@ -175,6 +175,7 @@ impl TestSession for Session { ) .await .unwrap(); + tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; self.response().assert_code(expected_code); }