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.

2.8 KB
Raw Blame
"""Native SQLCipher errors must not be written straight to stderr."""

import json
import os
import subprocess
import sys

import pytest

from local_deep_research.database import sqlcipher_compat


@pytest.mark.parametrize("private", [False, True])
def test_real_native_error_output_is_suppressed(tmp_path, private):
    # Exercise the installed driver in a fresh process so its global native
    # log level cannot affect this worker or another test's connections.
    script = """
import json
import sys
from local_deep_research.database.sqlcipher_compat import connect_sqlcipher, get_sqlcipher_module

driver = get_sqlcipher_module()
path, private = sys.argv[1], sys.argv[2] == 'true'
source = connect_sqlcipher(driver, path)
source.execute('PRAGMA key = "x' + chr(39) + bytes(range(32)).hex() + chr(39) + '"')
source.execute('PRAGMA cipher_compatibility = 4')
source.execute('CREATE TABLE private_data (value TEXT)')
source.execute('INSERT INTO private_data VALUES (?)', ('test-native-event',))
source.commit()
source.close()
connection = connect_sqlcipher(driver, path) if private else driver.connect(path)
if not private:
    connection.execute('PRAGMA cipher_log = stderr')
    connection.execute('PRAGMA cipher_log_level = ERROR')
supported = connection.execute('PRAGMA cipher_log_level').fetchone() is not None
connection.execute('PRAGMA key = "x' + chr(39) + bytes(reversed(range(32))).hex() + chr(39) + '"')
connection.execute('PRAGMA cipher_compatibility = 4')
denied = False
try:
    connection.execute('SELECT * FROM private_data').fetchall()
except driver.DatabaseError:
    denied = True
connection.close()
print(json.dumps({'denied': denied, 'supported': supported}))
"""
    result = subprocess.run(
        [
            sys.executable,
            "-c",
            script,
            str(tmp_path / "private.db"),
            str(private).lower(),
        ],
        capture_output=True,
        text=True,
        timeout=45,
        check=True,
        env={**os.environ, "LDR_DATA_DIR": str(tmp_path)},
    )
    report = json.loads(result.stdout)
    assert report["denied"] is True
    if private:
        assert result.stderr == ""
    else:
        if not report["supported"]:
            pytest.skip("This driver has no configurable native diagnostics")
        assert "sqlcipher" in result.stderr.lower()


def test_failed_private_configuration_does_not_return_a_connection(monkeypatch):
    from unittest.mock import Mock

    connection = Mock()
    connection.cursor.return_value.execute.side_effect = RuntimeError(
        "privacy configuration failed"
    )
    driver = Mock(sqlite_version_info=(3, 53, 1))
    driver.connect.return_value = connection
    with pytest.raises(RuntimeError, match="privacy configuration failed"):
        sqlcipher_compat.connect_sqlcipher(driver, ":memory:")
    connection.close.assert_called_once()