Skip to main content

sqlive/
test_hooks.rs

1//! Hooks a test uses to make a connection fail, panic or end the process
2//! where it chooses.
3
4use super::{HostSql, Policy};
5
6impl<P: Policy> HostSql<P> {
7    /// Fail each statement that fires `event` with `message`, through a
8    /// trigger named `name`.
9    pub fn fail_at(&self, name: &str, event: &str, message: &str) {
10        self.batch(&format!(
11            "CREATE TRIGGER {} {event} BEGIN SELECT RAISE(ABORT,'{}'); END",
12            crate::sql::identifier(name),
13            message.replace('\'', "''")
14        ))
15        .unwrap();
16    }
17
18    /// Set the most bytes a string or BLOB can hold on this connection,
19    /// returning the limit it had.
20    pub fn set_value_limit(&self, bytes: i32) -> i32 {
21        self.connection.set_limit(rusqlite::limits::Limit::SQLITE_LIMIT_LENGTH, bytes).unwrap()
22    }
23
24    /// End the process with status 86 as the next commit writes the WAL.
25    pub fn crash_after_next_commit(&mut self) {
26        self.connection
27            .wal_hook(Some(|_: &mut rusqlite::hooks::Wal, _| std::process::exit(86)))
28            .unwrap();
29    }
30
31    /// Panic in the next call of one of the host's SQL functions on this
32    /// connection.
33    pub fn panic_next_function(&self) {
34        self.authorization.panic_next_function();
35    }
36
37    /// Panic in the next judgment of this connection's authorizer.
38    pub fn panic_next_authorization(&self) {
39        self.authorization.panic_next();
40    }
41
42    /// Panic in the next call of a write transaction's hook on this
43    /// connection.
44    pub fn panic_in_next_hook(&self) {
45        self.hook_panics.store(true, std::sync::atomic::Ordering::SeqCst);
46    }
47
48    /// Have the authorizer refuse every COMMIT and ROLLBACK when `refuse` is
49    /// true.
50    pub fn refuse_transaction_end(&mut self, refuse: bool) {
51        use rusqlite::hooks::{AuthAction, AuthContext, Authorization, TransactionOperation};
52        let mut authorize = self.authorization.callback();
53        self.connection
54            .authorizer(Some(move |context: AuthContext<'_>| {
55                if refuse
56                    && matches!(
57                        context.action,
58                        AuthAction::Transaction { operation }
59                            if !matches!(operation, TransactionOperation::Begin)
60                    )
61                {
62                    Authorization::Deny
63                } else {
64                    authorize(context)
65                }
66            }))
67            .unwrap();
68    }
69
70    /// End the process with status 86 after each `action` on `table`.
71    pub fn crash_after(&self, table: &str, action: &str) {
72        self.connection
73            .create_scalar_function(
74                "test_crash",
75                0,
76                rusqlite::functions::FunctionFlags::SQLITE_UTF8
77                    | rusqlite::functions::FunctionFlags::SQLITE_INNOCUOUS,
78                |_| -> rusqlite::Result<i64> { std::process::exit(86) },
79            )
80            .unwrap();
81        self.batch(&format!(
82            "CREATE TRIGGER _coven_crash AFTER {action} ON {table} BEGIN SELECT test_crash(); END"
83        ))
84        .unwrap();
85    }
86}