Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

test: add 8 SQLCipher 4.7+ behavior and thread safety tests

LearningCircuit6c30c5a8dd16
diff --git a/tests/database/test_sqlcipher_integration.py b/tests/database/test_sqlcipher_integration.py
index 5a39eb771e..f3b1ca7583 100644
--- a/tests/database/test_sqlcipher_integration.py
+++ b/tests/database/test_sqlcipher_integration.py
@@ -4080,3 +4080,575 @@ class TestSQLCipherIntegration:
or "error" in error_msg
or "decrypt" in error_msg
), f"Expected HMAC/decryption error with wrong salt, got: {e}"
+
+ # =========================================================================
+ # SQLCipher 4.7+ Behavior & 2025 Features
+ # =========================================================================
+
+ def test_select_before_key_fails_4_7(self, tmp_path):
+ """Verify SELECT 1 fails before PRAGMA key is set (SQLCipher 4.7+ behavior).
+
+ Breaking change in 4.7.0: Schema-less statements no longer work
+ before keying. Previously, SELECT 1 would succeed before PRAGMA key;
+ now it fails.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "select_before_key_test.db"
+ password = "test_password_123"
+
+ # Create an encrypted database first
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+ cursor.execute("CREATE TABLE pre_key_test (id INTEGER)")
+ cursor.execute("INSERT INTO pre_key_test VALUES (1)")
+ conn.commit()
+ conn.close()
+
+ # Reopen without setting key first
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+
+ # Try SELECT 1 WITHOUT setting key first
+ # In SQLCipher 4.7+, this should fail
+ pre_key_select_failed = False
+ try:
+ cursor.execute("SELECT 1")
+ cursor.fetchone()
+ # If we get here, either:
+ # 1. SQLCipher version < 4.7.0 (older behavior)
+ # 2. The database is somehow readable without key
+ except Exception as e:
+ pre_key_select_failed = True
+ error_msg = str(e).lower()
+ # Should get an error about the database being encrypted
+ assert (
+ "not a database" in error_msg
+ or "file is encrypted" in error_msg
+ or "encrypted" in error_msg
+ or "error" in error_msg
+ ), f"Unexpected error type: {e}"
+
+ # Now set key and verify SELECT 1 works
+ cursor.execute(f"PRAGMA key = '{password}'")
+ cursor.execute("SELECT 1")
+ result = cursor.fetchone()
+ assert result[0] == 1, "SELECT 1 should work after key is set"
+
+ conn.close()
+
+ # Document the observed behavior (may vary by SQLCipher version)
+ if not pre_key_select_failed:
+ # Older SQLCipher version - document this
+ pytest.skip(
+ "SELECT before key succeeded - SQLCipher version may be < 4.7.0"
+ )
+
+ def test_select_sqlite_master_before_key_fails(self, tmp_path):
+ """Verify schema query fails before PRAGMA key is set.
+
+ Tests that SELECT * FROM sqlite_master fails without key.
+ This is a fundamental security requirement for encrypted databases.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "schema_before_key_test.db"
+ password = "test_password_123"
+
+ # Create an encrypted database with a table
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+ cursor.execute("CREATE TABLE schema_test (id INTEGER, value TEXT)")
+ cursor.execute("INSERT INTO schema_test VALUES (1, 'secret_data')")
+ conn.commit()
+ conn.close()
+
+ # Reopen without setting key
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+
+ # Try to query schema WITHOUT key
+ schema_query_failed = False
+ try:
+ cursor.execute("SELECT * FROM sqlite_master")
+ result = cursor.fetchall()
+ # If we can read schema without key, that's a security concern
+ if result:
+ # We got schema data without key - this shouldn't happen
+ pass
+ except Exception as e:
+ schema_query_failed = True
+ error_msg = str(e).lower()
+ # Expected errors for encrypted databases
+ assert (
+ "not a database" in error_msg
+ or "file is encrypted" in error_msg
+ or "encrypted" in error_msg
+ or "error" in error_msg
+ ), f"Unexpected error: {e}"
+
+ # Now set key and verify query works
+ cursor.execute(f"PRAGMA key = '{password}'")
+ cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
+ tables = [row[0] for row in cursor.fetchall()]
+ assert "schema_test" in tables, (
+ "Should find schema_test table after key is set"
+ )
+
+ conn.close()
+
+ # Schema should not be readable without key
+ assert schema_query_failed, (
+ "Schema query should fail without key on encrypted database"
+ )
+
+ def test_cipher_log_level_settable(self, tmp_path):
+ """Test PRAGMA cipher_log_level can be set and queried.
+
+ New in SQLCipher 4.6.0 - default log output to stderr/logcat.
+ Log levels: NONE=0, ERROR=1, WARN=2, INFO=3, DEBUG=4, TRACE=5
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "cipher_log_level_test.db"
+ password = "test_password_123"
+
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+
+ # Test setting different log levels
+ log_levels = {
+ "NONE": 0,
+ "ERROR": 1,
+ "WARN": 2,
+ "INFO": 3,
+ "DEBUG": 4,
+ "TRACE": 5,
+ }
+
+ pragma_supported = True
+ for level_name, level_value in log_levels.items():
+ try:
+ cursor.execute(f"PRAGMA cipher_log_level = {level_value}")
+ cursor.execute("PRAGMA cipher_log_level")
+ result = cursor.fetchone()
+
+ if result is not None:
+ # Verify the level was set (value may be returned differently)
+ current_level = result[0]
+ # Accept either numeric or string representation
+ assert (
+ current_level == level_value
+ or str(current_level).upper() == level_name
+ or (
+ isinstance(current_level, int)
+ and 0 <= current_level <= 5
+ )
+ ), f"Log level setting failed for {level_name}"
+ except Exception as e:
+ error_msg = str(e).lower()
+ if "undefined" in error_msg or "no such" in error_msg:
+ pragma_supported = False
+ break
+ # Other errors might be version-specific
+
+ # Verify database still functions after log level changes
+ cursor.execute("CREATE TABLE log_test (id INTEGER)")
+ cursor.execute("INSERT INTO log_test VALUES (1)")
+ cursor.execute("SELECT * FROM log_test")
+ assert cursor.fetchone()[0] == 1, "Database should function normally"
+
+ conn.close()
+
+ if not pragma_supported:
+ pytest.skip(
+ "cipher_log_level pragma not supported (version < 4.6.0)"
+ )
+
+ def test_cipher_status_returns_keyed_state(self, tmp_path):
+ """Test PRAGMA cipher_status returns correct keyed state.
+
+ cipher_status returns 1 if database is keyed and not in error state.
+ Useful for verifying encryption is properly configured.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "cipher_status_test.db"
+ password = "test_password_123"
+
+ # Create encrypted database
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+ cursor.execute("CREATE TABLE status_test (id INTEGER)")
+ conn.commit()
+ conn.close()
+
+ # Reopen and check cipher_status
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+
+ # Check status BEFORE key - should indicate not ready
+ try:
+ cursor.execute("PRAGMA cipher_status")
+ _before_key_status = cursor.fetchone()
+ # Status before key may be 0 or None
+ except Exception:
+ _before_key_status = None # noqa: F841
+
+ # Set key
+ cursor.execute(f"PRAGMA key = '{password}'")
+
+ # Check status AFTER key - should be 1 (ready)
+ try:
+ cursor.execute("PRAGMA cipher_status")
+ after_key_status = cursor.fetchone()
+
+ if after_key_status is not None:
+ # After keying successfully, status should be 1
+ # (or non-zero indicating ready state)
+ status_value = after_key_status[0]
+ assert status_value in (1, "ok", True), (
+ f"Expected keyed status, got: {status_value}"
+ )
+ except Exception as e:
+ error_msg = str(e).lower()
+ if "undefined" in error_msg or "no such" in error_msg:
+ pytest.skip("cipher_status pragma not supported")
+ raise
+
+ # Verify database operations work
+ cursor.execute("SELECT COUNT(*) FROM status_test")
+ conn.close()
+
+ def test_cipher_migrate_version_4_database_returns_error(self, tmp_path):
+ """Test cipher_migrate on already-v4 database returns appropriate error.
+
+ Bug fix in SQLCipher 4.8.0 improved error handling for this case.
+ cipher_migrate should fail gracefully when database is already v4.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "cipher_migrate_v4_test.db"
+ password = "test_password_123"
+
+ # Create database with SQLCipher 4 defaults
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+
+ # Verify we're using SQLCipher 4 settings
+ cursor.execute("PRAGMA cipher_version")
+ _version = cursor.fetchone()[0] # noqa: F841
+
+ cursor.execute("CREATE TABLE migrate_v4_test (id INTEGER, value TEXT)")
+ cursor.execute("INSERT INTO migrate_v4_test VALUES (1, 'v4_data')")
+ conn.commit()
+ conn.close()
+
+ # Reopen and attempt cipher_migrate
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+
+ # cipher_migrate on already-v4 database should return failure (non-zero)
+ # or indicate that no migration is needed
+ try:
+ cursor.execute("PRAGMA cipher_migrate")
+ result = cursor.fetchone()
+
+ if result is not None:
+ migrate_result = result[0]
+ # cipher_migrate returns:
+ # 0 = success (migration performed)
+ # 1 = failure (no migration needed or error)
+ # Already on v4 should return non-zero (no migration needed)
+ assert migrate_result != 0, (
+ f"cipher_migrate on v4 DB should return non-zero, "
+ f"got: {migrate_result}"
+ )
+ except Exception as e:
+ error_msg = str(e).lower()
+ if "undefined" in error_msg or "no such" in error_msg:
+ pytest.skip("cipher_migrate pragma not supported")
+ # Other errors are acceptable - migration not needed
+ pass
+
+ # Verify database is not corrupted after failed migration attempt
+ cursor.execute("SELECT value FROM migrate_v4_test WHERE id = 1")
+ result = cursor.fetchone()
+ assert result[0] == "v4_data", (
+ "Data should be intact after failed cipher_migrate"
+ )
+
+ conn.close()
+
+ def test_hmac_check_failure_clear_error(self, tmp_path):
+ """Test HMAC check failure provides clear error message.
+
+ Common issue: users get confusing "file is not a database" errors.
+ This test documents expected error format and verifies error messages
+ contain useful diagnostic information.
+ """
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "hmac_error_test.db"
+ correct_password = "correct_password_123"
+ wrong_password = "wrong_password_456"
+
+ # Create encrypted database
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{correct_password}'")
+ cursor.execute("CREATE TABLE hmac_test (id INTEGER, value TEXT)")
+ cursor.execute("INSERT INTO hmac_test VALUES (1, 'secret_data')")
+ conn.commit()
+ conn.close()
+
+ # Reopen with WRONG key
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{wrong_password}'")
+
+ # Attempt query - should fail with HMAC error
+ hmac_failure_detected = False
+ error_message = ""
+ try:
+ cursor.execute("SELECT * FROM hmac_test")
+ cursor.fetchall()
+ # If we get here, wrong key somehow worked (shouldn't happen)
+ pytest.fail("Query with wrong password should fail")
+ except Exception as e:
+ hmac_failure_detected = True
+ error_message = str(e).lower()
+
+ conn.close()
+
+ # Verify error message contains useful diagnostic info
+ # Common error messages include:
+ # - "file is not a database"
+ # - "file is encrypted or is not a database"
+ # - "HMAC validation failed"
+ # - "decrypt failed"
+ assert hmac_failure_detected, "Wrong password should cause failure"
+
+ # Check for common error patterns
+ error_patterns = [
+ "not a database",
+ "encrypted",
+ "hmac",
+ "decrypt",
+ "sqlite_master",
+ "error",
+ ]
+ has_useful_error = any(
+ pattern in error_message for pattern in error_patterns
+ )
+ assert has_useful_error, (
+ f"Error should contain useful diagnostic info. Got: {error_message}"
+ )
+
+ def test_concurrent_open_close_no_deadlock(self, tmp_path):
+ """Test multiple threads can open/close same database without deadlock.
+
+ Thread safety improvements in SQLCipher 4.9.0 address potential
+ deadlocks during concurrent access.
+ """
+ import threading
+ import time
+
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "concurrent_test.db"
+ password = "test_password_123"
+
+ # Create encrypted database first
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+ cursor.execute("CREATE TABLE concurrent_test (id INTEGER, value TEXT)")
+ for i in range(10):
+ cursor.execute(
+ f"INSERT INTO concurrent_test VALUES ({i}, 'data_{i}')"
+ )
+ conn.commit()
+ conn.close()
+
+ # Track results from threads
+ results = []
+ errors = []
+ lock = threading.Lock()
+
+ def thread_operation(thread_id):
+ """Open database, execute query, close."""
+ try:
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+ cursor.execute("SELECT COUNT(*) FROM concurrent_test")
+ count = cursor.fetchone()[0]
+ conn.close()
+
+ with lock:
+ results.append((thread_id, count))
+ except Exception as e:
+ with lock:
+ errors.append((thread_id, str(e)))
+
+ # Spawn 5 threads to do concurrent open/close
+ threads = []
+ for i in range(5):
+ t = threading.Thread(target=thread_operation, args=(i,))
+ threads.append(t)
+
+ # Start all threads
+ for t in threads:
+ t.start()
+
+ # Wait for completion with timeout (deadlock detection)
+ timeout = 30 # seconds
+ start_time = time.time()
+ for t in threads:
+ remaining = timeout - (time.time() - start_time)
+ if remaining > 0:
+ t.join(timeout=remaining)
+ if t.is_alive():
+ pytest.fail(
+ "Thread deadlock detected - operation did not complete"
+ )
+
+ # Check results
+ if errors:
+ # Some errors may be acceptable (e.g., database locked briefly)
+ acceptable_errors = ["locked", "busy"]
+ for thread_id, error in errors:
+ error_lower = error.lower()
+ if not any(ae in error_lower for ae in acceptable_errors):
+ pytest.fail(f"Thread {thread_id} failed with: {error}")
+
+ # At least some threads should have succeeded
+ assert len(results) >= 1, (
+ "At least one thread should complete successfully"
+ )
+
+ # All successful threads should see correct count
+ for thread_id, count in results:
+ assert count == 10, f"Thread {thread_id} got wrong count: {count}"
+
+ def test_library_shutdown_thread_safety(self, tmp_path):
+ """Test library handles connection close while operations may be active.
+
+ Thread safety improvements in SQLCipher 4.9.0 for shutdown sequences.
+ Tests graceful handling when closing connection from another thread.
+ """
+ import threading
+ import time
+
+ from local_deep_research.database.sqlcipher_compat import (
+ get_sqlcipher_module,
+ )
+
+ pysqlcipher3 = get_sqlcipher_module()
+ db_path = tmp_path / "shutdown_safety_test.db"
+ password = "test_password_123"
+
+ # Create database with substantial data
+ conn = pysqlcipher3.connect(str(db_path))
+ cursor = conn.cursor()
+ cursor.execute(f"PRAGMA key = '{password}'")
+ cursor.execute("CREATE TABLE shutdown_test (id INTEGER, value TEXT)")
+ for i in range(100):
+ cursor.execute(
+ f"INSERT INTO shutdown_test VALUES ({i}, 'data_{i}')"
+ )
+ conn.commit()
+ conn.close()
+
+ # Use separate connections for reader and closer
+ read_errors = []
+ read_count = [0]
+ stop_reading = threading.Event()
+
+ def continuous_reader():
+ """Continuously read from database until stopped."""
+ try:
+ reader_conn = pysqlcipher3.connect(str(db_path))
+ reader_cursor = reader_conn.cursor()
+ reader_cursor.execute(f"PRAGMA key = '{password}'")
+
+ while not stop_reading.is_set():
+ try:
+ reader_cursor.execute(
+ "SELECT COUNT(*) FROM shutdown_test"
+ )
+ reader_cursor.fetchone()
+ read_count[0] += 1
+ except Exception as e:
+ error_msg = str(e).lower()
+ # Database closed/locked errors are expected
+ if not any(
+ x in error_msg
+ for x in ["closed", "locked", "cannot operate"]
+ ):
+ read_errors.append(str(e))
+ break
+ reader_conn.close()
+ except Exception as e:
+ error_msg = str(e).lower()
+ if not any(x in error_msg for x in ["closed", "locked"]):
+ read_errors.append(str(e))
+
+ # Start reader thread
+ reader_thread = threading.Thread(target=continuous_reader)
+ reader_thread.start()
+
+ # Let reader run briefly
+ time.sleep(0.1)
+
+ # Signal stop and wait for reader
+ stop_reading.set()
+ reader_thread.join(timeout=5)
+
+ if reader_thread.is_alive():
+ pytest.fail("Reader thread did not stop gracefully")
+
+ # Check that some reads completed
+ assert read_count[0] >= 1, (
+ "Reader should have completed at least one read"
+ )
+
+ # Check for unexpected errors (not related to normal shutdown)
+ critical_errors = [
+ e
+ for e in read_errors
+ if "crash" in e.lower()
+ or "segfault" in e.lower()
+ or "abort" in e.lower()
+ ]
+ assert not critical_errors, (
+ f"Critical errors during shutdown: {critical_errors}"
+ )