~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
test: add 10 SQLCipher 4.x compatibility and bug regression tests
LearningCircuit3c67ec0498b4
diff --git a/tests/database/test_sqlcipher_integration.py b/tests/database/test_sqlcipher_integration.pyindex f3b1ca7583..721ad514de 100644--- a/tests/database/test_sqlcipher_integration.py+++ b/tests/database/test_sqlcipher_integration.py@@ -4652,3 +4652,652 @@ class TestSQLCipherIntegration:assert not critical_errors, (f"Critical errors during shutdown: {critical_errors}")++ # ========================================================================+ # SQLCipher 4.x Compatibility Tests (January 2026)+ # Based on GitHub issues #98, #4025, #16482 and iOS compatibility requirements+ # ========================================================================++ def test_rekey_preserves_row_count(self, tmp_path):+ """Verify rekey doesn't delete data (addresses GitHub issue #98).++ Issue #98 reported that rekey operation could result in empty database.+ This test verifies exact row count before and after rekey matches.+ """+ from local_deep_research.database.sqlcipher_compat import (+ get_sqlcipher_module,+ )++ pysqlcipher3 = get_sqlcipher_module()+ db_path = tmp_path / "rekey_row_count_test.db"+ original_password = "original_password_123" # DevSkim: ignore DS117838+ new_password = "new_password_456" # DevSkim: ignore DS117838+ num_rows = 1000++ # Create database with substantial data+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{original_password}'")+ cursor.execute(+ "CREATE TABLE rekey_test (id INTEGER PRIMARY KEY, value TEXT)"+ )++ # Insert 1000 rows+ for i in range(num_rows):+ cursor.execute(+ f"INSERT INTO rekey_test (value) VALUES ('data_{i}')"+ )+ conn.commit()++ # Get row count before rekey+ cursor.execute("SELECT COUNT(*) FROM rekey_test")+ count_before = cursor.fetchone()[0]+ assert count_before == num_rows, (+ f"Setup failed: expected {num_rows}, got {count_before}"+ )++ # Perform rekey+ cursor.execute(f"PRAGMA rekey = '{new_password}'")+ conn.commit()++ # Get row count after rekey (same connection)+ cursor.execute("SELECT COUNT(*) FROM rekey_test")+ count_after_same_conn = cursor.fetchone()[0]+ conn.close()++ # Reopen with new password and verify count+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{new_password}'")+ cursor.execute("SELECT COUNT(*) FROM rekey_test")+ count_after_reopen = cursor.fetchone()[0]+ conn.close()++ # Verify row counts match+ assert count_after_same_conn == num_rows, (+ f"Row count changed after rekey (same conn): "+ f"expected {num_rows}, got {count_after_same_conn}"+ )+ assert count_after_reopen == num_rows, (+ f"Row count changed after rekey (reopen): "+ f"expected {num_rows}, got {count_after_reopen}"+ )++ def test_rekey_preserves_data_content(self, tmp_path):+ """Verify specific data values survive rekey operation.++ Addresses GitHub issue #98 - ensures not just row count but actual+ data content is preserved including Unicode and various data types.+ """+ from local_deep_research.database.sqlcipher_compat import (+ get_sqlcipher_module,+ )++ pysqlcipher3 = get_sqlcipher_module()+ db_path = tmp_path / "rekey_data_content_test.db"+ original_password = "original_pass" # DevSkim: ignore DS117838+ new_password = "new_pass" # DevSkim: ignore DS117838++ # Test data with various types+ test_data = [+ (1, "simple text", 42, 3.14159),+ (2, "Unicode: こんにちは 🎉 émojis", 100, 2.71828),+ (3, "Special chars: <>&\"'", -999, 0.0),+ (4, "Long text" * 100, 2147483647, 1e308),+ (5, "", 0, -1e308),+ ]++ # Create database with test data+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{original_password}'")+ cursor.execute("""+ CREATE TABLE content_test (+ id INTEGER PRIMARY KEY,+ text_col TEXT,+ int_col INTEGER,+ real_col REAL+ )+ """)++ for row in test_data:+ cursor.execute("INSERT INTO content_test VALUES (?, ?, ?, ?)", row)+ conn.commit()++ # Perform rekey+ cursor.execute(f"PRAGMA rekey = '{new_password}'")+ conn.commit()+ conn.close()++ # Reopen with new password and verify data+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{new_password}'")+ cursor.execute(+ "SELECT id, text_col, int_col, real_col FROM content_test "+ "ORDER BY id"+ )+ results = cursor.fetchall()+ conn.close()++ # Verify each row matches original data+ assert len(results) == len(test_data), (+ f"Row count mismatch: expected {len(test_data)}, got {len(results)}"+ )++ for original, retrieved in zip(test_data, results):+ assert original[0] == retrieved[0], (+ f"ID mismatch: expected {original[0]}, got {retrieved[0]}"+ )+ assert original[1] == retrieved[1], (+ f"Text mismatch for id {original[0]}"+ )+ assert original[2] == retrieved[2], (+ f"Integer mismatch for id {original[0]}"+ )+ # Float comparison with tolerance+ assert abs(original[3] - retrieved[3]) < 1e-10 or (+ original[3] == retrieved[3]+ ), f"Float mismatch for id {original[0]}"++ def test_rekey_on_plaintext_db_fails(self, tmp_path):+ """PRAGMA rekey cannot encrypt a plaintext database.++ Addresses DBeaver issue #16482 - rekey on plaintext DB should fail+ gracefully, not corrupt the database. SQLCipher requires the DB to+ already be encrypted before rekey can be used.+ """+ import sqlite3++ from local_deep_research.database.sqlcipher_compat import (+ get_sqlcipher_module,+ )++ pysqlcipher3 = get_sqlcipher_module()+ db_path = tmp_path / "plaintext_rekey_test.db"+ new_password = "new_password" # DevSkim: ignore DS117838++ # Create plaintext SQLite database using standard sqlite3+ conn = sqlite3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute("CREATE TABLE test (id INTEGER, value TEXT)")+ cursor.execute("INSERT INTO test VALUES (1, 'test_data')")+ conn.commit()+ conn.close()++ # Verify file is plaintext (has SQLite magic)+ with open(db_path, "rb") as f:+ header = f.read(16)+ assert header[:6] == b"SQLite", (+ "Test setup failed: database should be plaintext"+ )++ # Try to rekey plaintext database using sqlcipher+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()++ # Don't set key (it's plaintext) - just try rekey+ # We expect this to either fail or be a no-op (not actually encrypt)+ try:+ cursor.execute(f"PRAGMA rekey = '{new_password}'")+ conn.commit()+ except Exception:+ pass # Expected - rekey on plaintext may fail+ conn.close()++ # Verify database is still accessible (not corrupted)+ conn = sqlite3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute("SELECT * FROM test")+ result = cursor.fetchall()+ conn.close()++ assert len(result) == 1, (+ "Database should still be accessible after failed rekey"+ )+ assert result[0] == (1, "test_data"), (+ "Data should be preserved after failed rekey"+ )++ # Verify file is still plaintext+ with open(db_path, "rb") as f:+ header = f.read(16)+ assert header[:6] == b"SQLite", (+ "Database should remain plaintext (rekey should not encrypt it)"+ )++ def test_hmac_disabled_database_opens(self, tmp_path):+ """Test cipher_use_hmac=OFF mode works correctly.++ Addresses SQLiteBrowser issue #4025 - databases created with+ cipher_use_hmac=OFF should still be openable and functional.+ """+ from local_deep_research.database.sqlcipher_compat import (+ get_sqlcipher_module,+ )++ pysqlcipher3 = get_sqlcipher_module()+ db_path = tmp_path / "hmac_off_test.db"+ password = "test_password" # DevSkim: ignore DS117838++ # Create database with HMAC disabled+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{password}'")+ cursor.execute("PRAGMA cipher_use_hmac = OFF")++ # Insert test data+ cursor.execute("CREATE TABLE hmac_test (id INTEGER, value TEXT)")+ cursor.execute("INSERT INTO hmac_test VALUES (1, 'hmac_off_data')")+ cursor.execute("INSERT INTO hmac_test VALUES (2, 'more_data')")+ conn.commit()+ conn.close()++ # Reopen with same settings+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{password}'")+ cursor.execute("PRAGMA cipher_use_hmac = OFF")++ # Verify data is accessible+ cursor.execute("SELECT * FROM hmac_test ORDER BY id")+ results = cursor.fetchall()+ conn.close()++ assert len(results) == 2, "Should retrieve both rows"+ assert results[0] == (1, "hmac_off_data")+ assert results[1] == (2, "more_data")++ # Verify wrong password still fails (encryption still works)+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute("PRAGMA key = 'wrong_password'")+ cursor.execute("PRAGMA cipher_use_hmac = OFF")++ wrong_password_error = None+ try:+ cursor.execute("SELECT * FROM hmac_test")+ cursor.fetchall()+ except Exception as e:+ wrong_password_error = str(e)+ conn.close()++ assert wrong_password_error is not None, (+ "Wrong password should still fail even with HMAC disabled"+ )++ def test_16kb_page_size(self, tmp_path):+ """Test with 16KB page size (Google Play Store requirement).++ Addresses android-database-sqlcipher issue #664 - Google Play+ requires 16KB page alignment for Android 15+ apps.+ """+ from local_deep_research.database.sqlcipher_compat import (+ get_sqlcipher_module,+ )++ pysqlcipher3 = get_sqlcipher_module()+ db_path = tmp_path / "page_16kb_test.db"+ password = "test_password" # DevSkim: ignore DS117838+ page_size = 16384 # 16KB++ # Create database with 16KB page size+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{password}'")+ cursor.execute(f"PRAGMA cipher_page_size = {page_size}")++ # Create table and insert data+ cursor.execute(+ "CREATE TABLE large_page_test (id INTEGER PRIMARY KEY, data TEXT)"+ )++ # Insert enough data to span multiple pages+ for i in range(100):+ cursor.execute(+ f"INSERT INTO large_page_test (data) VALUES "+ f"('{('x' * 1000)}_{i}')"+ )+ conn.commit()++ # Verify page size was applied+ cursor.execute("PRAGMA cipher_page_size")+ actual_page_size = int(cursor.fetchone()[0])+ conn.close()++ # Reopen and verify data persistence+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{password}'")+ cursor.execute(f"PRAGMA cipher_page_size = {page_size}")++ cursor.execute("SELECT COUNT(*) FROM large_page_test")+ count = cursor.fetchone()[0]+ conn.close()++ assert count == 100, f"All rows should persist, got {count}"+ assert actual_page_size == page_size, (+ f"Page size should be {page_size}, got {actual_page_size}"+ )++ # Verify file size is aligned to page size (rough check)+ file_size = db_path.stat().st_size+ assert file_size > page_size, (+ "Database file should be larger than one page"+ )++ def test_plaintext_header_with_external_salt(self, tmp_path):+ """Create DB with plaintext header and external salt management.++ For iOS compatibility (Mozilla issue #2100, SQLCipher #352).+ iOS requires plaintext header to prevent 0xdead10cc termination.+ """+ import secrets++ from local_deep_research.database.sqlcipher_compat import (+ get_sqlcipher_module,+ )++ pysqlcipher3 = get_sqlcipher_module()+ db_path = tmp_path / "external_salt_test.db"+ password = "test_password" # DevSkim: ignore DS117838++ # Generate random 16-byte salt (32 hex chars)+ external_salt = secrets.token_hex(16)++ # Create database with plaintext header and external salt+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()++ # IMPORTANT: cipher_plaintext_header_size and cipher_salt must be+ # set BEFORE PRAGMA key+ cursor.execute("PRAGMA cipher_plaintext_header_size = 32")+ cursor.execute(f"PRAGMA cipher_salt = \"x'{external_salt}'\"")+ cursor.execute(f"PRAGMA key = '{password}'")++ # Create table and insert data+ cursor.execute("CREATE TABLE salt_test (id INTEGER, value TEXT)")+ cursor.execute("INSERT INTO salt_test VALUES (1, 'external_salt_data')")+ conn.commit()+ conn.close()++ # Verify file has SQLite magic bytes (plaintext header)+ with open(db_path, "rb") as f:+ header = f.read(32)++ # First 16 bytes should contain SQLite header string for plaintext+ # header mode+ has_sqlite_magic = b"SQLite" in header++ if has_sqlite_magic:+ # Plaintext header applied - verify we can reopen+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute("PRAGMA cipher_plaintext_header_size = 32")+ cursor.execute(f"PRAGMA cipher_salt = \"x'{external_salt}'\"")+ cursor.execute(f"PRAGMA key = '{password}'")++ cursor.execute("SELECT * FROM salt_test")+ result = cursor.fetchall()+ conn.close()++ assert len(result) == 1+ assert result[0] == (1, "external_salt_data")+ else:+ # Some SQLCipher versions may not support this feature+ pytest.skip(+ "Plaintext header with external salt not supported in "+ "this SQLCipher version"+ )++ def test_extract_salt_for_external_storage(self, tmp_path):+ """Extract salt via PRAGMA cipher_salt for external storage.++ Tests the ability to extract the salt from an encrypted database+ for external storage (needed for iOS plaintext header mode).+ """+ from local_deep_research.database.sqlcipher_compat import (+ get_sqlcipher_module,+ )++ pysqlcipher3 = get_sqlcipher_module()+ db_path = tmp_path / "extract_salt_test.db"+ password = "test_password" # DevSkim: ignore DS117838++ # Create standard encrypted database+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{password}'")+ cursor.execute("CREATE TABLE salt_extract_test (id INTEGER)")+ cursor.execute("INSERT INTO salt_extract_test VALUES (1)")+ conn.commit()++ # Extract salt+ cursor.execute("PRAGMA cipher_salt")+ salt_result = cursor.fetchone()+ conn.close()++ if salt_result and salt_result[0]:+ salt = salt_result[0]++ # Salt should be a hex string (with or without x'' wrapper)+ salt_str = str(salt)++ # Remove x'' wrapper if present+ if salt_str.startswith("x'") and salt_str.endswith("'"):+ salt_hex = salt_str[2:-1]+ else:+ salt_hex = salt_str++ # Salt should be 32 hex characters (16 bytes)+ # Note: some versions may return different formats+ assert len(salt_hex) >= 16, (+ f"Salt should be at least 16 chars, got {len(salt_hex)}"+ )++ # Verify salt contains only hex characters+ try:+ int(salt_hex[:32], 16)+ is_valid_hex = True+ except ValueError:+ is_valid_hex = False++ assert is_valid_hex, f"Salt should be hex string, got: {salt_hex}"+ else:+ pytest.skip("PRAGMA cipher_salt not supported in this version")++ def test_reopen_with_extracted_salt(self, tmp_path):+ """Reopen plaintext header DB using stored salt.++ Tests the full workflow of creating a plaintext header database,+ extracting the salt, and reopening with that salt.+ """+ import secrets++ from local_deep_research.database.sqlcipher_compat import (+ get_sqlcipher_module,+ )++ pysqlcipher3 = get_sqlcipher_module()+ db_path = tmp_path / "reopen_salt_test.db"+ password = "test_password" # DevSkim: ignore DS117838++ # Generate external salt+ external_salt = secrets.token_hex(16)++ # Create database with plaintext header+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()++ try:+ cursor.execute("PRAGMA cipher_plaintext_header_size = 32")+ cursor.execute(f"PRAGMA cipher_salt = \"x'{external_salt}'\"")+ cursor.execute(f"PRAGMA key = '{password}'")++ cursor.execute("CREATE TABLE reopen_test (id INTEGER, data TEXT)")+ cursor.execute("INSERT INTO reopen_test VALUES (1, 'test_value')")+ cursor.execute(+ "INSERT INTO reopen_test VALUES (2, 'another_value')"+ )+ conn.commit()+ conn.close()++ # Simulate storing salt externally (like iOS Keychain)+ stored_salt = external_salt++ # Reopen using stored salt+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute("PRAGMA cipher_plaintext_header_size = 32")+ cursor.execute(f"PRAGMA cipher_salt = \"x'{stored_salt}'\"")+ cursor.execute(f"PRAGMA key = '{password}'")++ # Verify data accessible+ cursor.execute("SELECT * FROM reopen_test ORDER BY id")+ results = cursor.fetchall()+ conn.close()++ assert len(results) == 2, f"Expected 2 rows, got {len(results)}"+ assert results[0] == (1, "test_value")+ assert results[1] == (2, "another_value")++ except Exception as e:+ conn.close()+ error_msg = str(e).lower()+ if (+ "cipher_plaintext_header" in error_msg+ or "not supported" in error_msg+ ):+ pytest.skip(+ "Plaintext header mode not supported in this version"+ )+ raise++ def test_cipher_compatibility_3_after_key(self, tmp_path):+ """cipher_compatibility must be set AFTER key pragma.++ Based on pysqlcipher3 documentation - when opening v3 databases,+ cipher_compatibility = 3 must be set AFTER PRAGMA key.+ """+ from local_deep_research.database.sqlcipher_compat import (+ get_sqlcipher_module,+ )++ pysqlcipher3 = get_sqlcipher_module()+ db_path = tmp_path / "compat_order_test.db"+ password = "test_password" # DevSkim: ignore DS117838++ # Create database with v3 compatibility settings+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{password}'")+ cursor.execute("PRAGMA cipher_compatibility = 3")++ cursor.execute("CREATE TABLE compat_test (id INTEGER, value TEXT)")+ cursor.execute("INSERT INTO compat_test VALUES (1, 'v3_compat_data')")+ conn.commit()+ conn.close()++ # Reopen with CORRECT order: key THEN compatibility+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{password}'")+ cursor.execute("PRAGMA cipher_compatibility = 3")++ cursor.execute("SELECT * FROM compat_test")+ result = cursor.fetchall()+ conn.close()++ assert len(result) == 1, "Data should be accessible"+ assert result[0] == (1, "v3_compat_data")++ # Test that setting compatibility BEFORE key may cause issues+ # (This documents the correct order requirement)+ conn = pysqlcipher3.connect(str(db_path))+ cursor = conn.cursor()++ # Wrong order: compatibility before key+ cursor.execute("PRAGMA cipher_compatibility = 3")+ cursor.execute(f"PRAGMA key = '{password}'")++ # This might work or fail depending on SQLCipher version+ try:+ cursor.execute("SELECT * FROM compat_test")+ result = cursor.fetchall()+ # If it works, that's fine - we're just documenting behavior+ except Exception:+ # If it fails, that demonstrates why order matters+ pass+ conn.close()++ def test_add_encryption_to_plaintext_db(self, tmp_path):+ """Use sqlcipher_export to add encryption to plaintext DB.++ Based on pysqlcipher3 test coverage - tests the recommended method+ for encrypting an existing plaintext database using ATTACH and+ sqlcipher_export().+ """+ import sqlite3++ from local_deep_research.database.sqlcipher_compat import (+ get_sqlcipher_module,+ )++ pysqlcipher3 = get_sqlcipher_module()+ plaintext_path = tmp_path / "plaintext_source.db"+ encrypted_path = tmp_path / "encrypted_dest.db"+ password = "encryption_password" # DevSkim: ignore DS117838++ # Create plaintext database with standard sqlite3+ conn = sqlite3.connect(str(plaintext_path))+ cursor = conn.cursor()+ cursor.execute(+ "CREATE TABLE export_test (id INTEGER PRIMARY KEY, name TEXT)"+ )+ cursor.execute("INSERT INTO export_test VALUES (1, 'Alice')")+ cursor.execute("INSERT INTO export_test VALUES (2, 'Bob')")+ cursor.execute("INSERT INTO export_test VALUES (3, 'Charlie')")+ conn.commit()+ conn.close()++ # Verify source is plaintext+ with open(plaintext_path, "rb") as f:+ header = f.read(16)+ assert header[:6] == b"SQLite", "Source should be plaintext"++ # Open plaintext database with sqlcipher (no key)+ conn = pysqlcipher3.connect(str(plaintext_path))+ cursor = conn.cursor()++ # Attach encrypted destination+ cursor.execute(+ f"ATTACH DATABASE '{encrypted_path}' AS encrypted KEY '{password}'"+ )++ # Export to encrypted database+ cursor.execute("SELECT sqlcipher_export('encrypted')")++ # Detach+ cursor.execute("DETACH DATABASE encrypted")+ conn.close()++ # Verify encrypted database is actually encrypted+ with open(encrypted_path, "rb") as f:+ header = f.read(16)+ assert header[:6] != b"SQLite", (+ "Encrypted database should not have plaintext header"+ )++ # Open encrypted database and verify data+ conn = pysqlcipher3.connect(str(encrypted_path))+ cursor = conn.cursor()+ cursor.execute(f"PRAGMA key = '{password}'")++ cursor.execute("SELECT * FROM export_test ORDER BY id")+ results = cursor.fetchall()+ conn.close()++ assert len(results) == 3, f"Expected 3 rows, got {len(results)}"+ assert results[0] == (1, "Alice")+ assert results[1] == (2, "Bob")+ assert results[2] == (3, "Charlie")