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.

4.8 KB
Raw Blame
"""Passwordless accessor surface: post-auth-only, side-effect-free.

Incident context (PR #5596): the fix's safety argument for methods that
take NO password (``get_session``, ``check_database_integrity``,
``get_connected_usernames``, ``get_memory_usage``) is that they are purely
POST-AUTH accessors over already-verified state -- they must never open,
create, or revive anything (the same never-open principle round 1 pinned
for ``get_session``). This file pins the remaining passwordless surface:

* J1 ``check_database_integrity``: for a closed-but-on-disk user it
  returns False WITHOUT opening (code: the ``username not in
  self.connections`` early-return happens before any engine work) and
  with zero side effects -- no file mutation, no cache/verifier entries,
  not even a new init lock; for a warm encrypted user it returns True via
  a real quick_check + cipher_integrity_check; for an unknown user,
  False with zero side effects.
(A J2 companion covering ``get_memory_usage`` /
``get_connected_usernames`` liveness was dropped: it duplicated
``test_login_cached_connection_password_lifecycle.py``'s
``test_is_user_connected_reflects_liveness_only`` and
``test_close_all_databases_clears_connections_and_verifiers``.)
"""

import hashlib
import uuid

import pytest

from local_deep_research.database.encrypted_db import DatabaseManager

# Module-level gate. Without a working SQLCipher build every test below
# is a no-op, and the dozens of in-body ``pytest.skip`` calls let a CI
# lane report this whole battery green having executed nothing. Skipping
# at import time makes the missing dependency one visible, uniform signal
# per module instead.
pytest.importorskip("sqlcipher3", reason="requires SQLCipher (encrypted mode)")


@pytest.fixture
def manager(tmp_path, monkeypatch):
    """A DatabaseManager writing to an isolated data directory.

    Same self-contained pattern as the round-1 files, with the sanctioned
    test-mode KDF knob.
    """
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")
    mgr = DatabaseManager()
    mgr.data_dir = tmp_path
    yield mgr
    mgr.close_all_databases()


def _dir_fingerprint(root) -> dict:
    """name -> (size, sha256, mode) for every file under root."""
    fingerprint = {}
    for path in sorted(root.rglob("*")):
        if path.is_file():
            data = path.read_bytes()
            fingerprint[path.name] = (
                len(data),
                hashlib.sha256(data).hexdigest(),
                path.stat().st_mode,
            )
    return fingerprint


def test_check_database_integrity_is_post_auth_only(manager):
    """J1: integrity checking must be a read over LIVE state, never an
    open-on-miss."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    def maps_snapshot():
        return (
            dict(manager.connections),
            dict(manager._password_verifiers),
            dict(manager._init_locks),
        )

    # --- Unknown user: False, zero side effects of any kind.
    unknown = f"never_existed_{uuid.uuid4().hex[:8]}"
    files_before = _dir_fingerprint(manager.data_dir)
    maps_before = maps_snapshot()
    assert manager.check_database_integrity(unknown) is False
    assert _dir_fingerprint(manager.data_dir) == files_before, (
        "integrity check created or mutated files for an unknown user"
    )
    assert maps_snapshot() == maps_before, (
        "integrity check added cache/verifier/init-lock state for an "
        "unknown user"
    )

    # --- Closed-but-on-disk user: False WITHOUT opening the database
    # (the never-open principle for this accessor), zero side effects.
    username = f"integrity_{uuid.uuid4().hex[:8]}"
    password = "IntegrityAccessorPw1!"  # noqa: S105
    manager.create_user_database(username, password)
    manager.close_user_database(username)
    assert manager._get_user_db_path(username).exists()

    files_before = _dir_fingerprint(manager.data_dir)
    maps_before = maps_snapshot()
    for _ in range(3):
        assert manager.check_database_integrity(username) is False, (
            "integrity check returned True for a CLOSED database -- it must "
            "not have opened it"
        )
    assert _dir_fingerprint(manager.data_dir) == files_before, (
        "integrity check touched the on-disk database of a closed user"
    )
    assert maps_snapshot() == maps_before, (
        "integrity check changed manager state for a closed user"
    )

    # --- Warm user: a real quick_check + cipher_integrity_check over the
    # live engine returns True.
    engine = manager.open_user_database(username, password)
    assert engine is not None
    assert manager.check_database_integrity(username) is True
    assert manager.connections[username] is engine