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.

7.4 KB
Raw Blame
"""Passwordless accessors and hostile data-dir paths.

Incident context (PR #5596): the never-open / no-side-effect discipline
(round-1 A, round-2 J) extends to every passwordless surface, and the
data-dir path itself is deployer-controlled input. This file pins:

* AC1 ``user_exists`` -- a pure AUTH-database lookup
  (encrypted_db.py::user_exists: it queries ``auth_db``, never the
  user's encrypted DB): unknown user -> False with NO ``ldr_user_*``
  file created and no cache state; a known-on-disk-but-never-opened
  user -> False as well (no DB open
  happens as a side effect). NOTE the one real side effect, pinned as
  actual: the FIRST call bootstraps ``ldr_auth.db`` inside the data dir
  (the auth store itself) -- expected infrastructure, distinct from any
  user-database artifact;
* AG1 a regular FILE where the data dir should be: assignment through
  the ``data_dir`` setter raises FileExistsError cleanly (the setter's
  ``mkdir(exist_ok=True)`` only tolerates an existing DIRECTORY), the
  file is undamaged, and a valid dir assignment still works afterwards;
* AG2 spaces + unicode in the data-dir path: full create/canary/warm/
  cold/close_all cycle works, and the DB artifacts land inside the
  unicode directory verbatim.
"""

import uuid

import pytest
from sqlalchemy import text

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..5 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 test_user_exists_is_a_pure_auth_lookup(manager):
    """AC1: unknown user -> False with no ldr_user_* artifacts and no
    cache touch; a user whose DB file exists but was never opened ->
    False too (the user DB is never opened by this call). The auth-store
    bootstrap (ldr_auth.db) is the one allowed side effect, pinned."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    unknown = f"no_such_user_{uuid.uuid4().hex[:8]}"
    assert manager.user_exists(unknown) is False

    # No USER-database artifact appeared (ldr_auth.db may -- that is the
    # auth store bootstrapping, not a user DB).
    user_artifacts = [
        p.name
        for p in manager.data_dir.iterdir()
        if p.name.startswith("ldr_user_")
    ]
    assert user_artifacts == [], (
        f"user_exists created user-database artifacts: {user_artifacts}"
    )
    assert manager.connections == {}
    assert manager._password_verifiers == {}
    assert manager.get_connected_usernames() == set()

    # A real user DB that exists on disk but was never opened: still
    # False (registration lives in the auth DB, not the file system),
    # and the call must not have OPENED the file either.
    username = f"on_disk_never_opened_{uuid.uuid4().hex[:8]}"
    password = "OnDiskPw1!"  # noqa: S105
    manager.create_user_database(username, password)
    manager.close_user_database(username)
    assert manager._get_user_db_path(username).exists()

    assert manager.user_exists(username) is False, (
        "a filesystem-only user (created directly through the manager, "
        "never registered in the auth DB) must not count as existing"
    )
    assert username not in manager.connections, (
        "user_exists opened the user's database as a side effect"
    )
    assert username not in manager._password_verifiers


def test_regular_file_as_data_dir_fails_cleanly(manager, tmp_path):
    """AG1: assigning a regular file as the data dir raises
    FileExistsError from the setter's mkdir; the file's content is
    undamaged; a subsequent valid assignment works."""
    if not manager.has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    impostor = tmp_path / "iamfile_not_a_dir"
    impostor.write_text("precious-content")

    with pytest.raises(FileExistsError):
        manager.data_dir = impostor

    assert impostor.read_text() == "precious-content", (
        "the failed data_dir assignment damaged the impostor file"
    )

    # The manager still accepts a real directory afterwards.
    good_dir = tmp_path / "real_dir"
    manager.data_dir = good_dir
    username = f"after impostor fix, user_{uuid.uuid4().hex[:8]}"[:40].replace(
        " ", "_"
    )
    password = "RecoverPw1!"  # noqa: S105
    engine = manager.create_user_database(username, password)
    assert engine is not None
    with engine.connect() as conn:
        assert conn.execute(text("SELECT 1")).scalar() == 1


def test_unicode_and_spaces_data_dir_full_cycle(tmp_path, monkeypatch):
    """AG2: a data dir whose name carries spaces, CJK, and an em-dash:
    create, canary, warm reuse, cold reopen, close_all -- all clean, with
    the artifacts landing inside the verbatim unicode path."""
    # Set the sanctioned test-mode KDF knob BEFORE any DatabaseManager is
    # constructed: `has_encryption` is decided by a probe that derives a key,
    # so building one first makes that probe run at the 256,000-iteration
    # production default instead of 1,000 -- out of line with the other
    # modules in this battery, and hundreds of milliseconds per test.
    monkeypatch.setenv("LDR_DATA_DIR", str(tmp_path))
    monkeypatch.setenv("LDR_DB_CONFIG_KDF_ITERATIONS", "1000")

    if not DatabaseManager().has_encryption:
        pytest.skip("requires SQLCipher (encrypted mode) to be meaningful")

    fancy = tmp_path / "with spaces and 漢字 and —émphasis—"
    mgr = DatabaseManager()
    mgr.data_dir = fancy
    try:
        username = f"unicode_dir_{uuid.uuid4().hex[:8]}"
        password = "UnicodeDirPw1!"  # noqa: S105
        engine = mgr.create_user_database(username, password)
        with engine.begin() as conn:
            conn.execute(
                text(
                    "CREATE TABLE gap_ag_canary "
                    "(id INTEGER PRIMARY KEY, note TEXT)"
                )
            )
            conn.execute(
                text("INSERT INTO gap_ag_canary (id, note) VALUES (1, :n)"),
                {"n": "gap-AG2"},
            )

        # Warm reuse, then cold reopen, inside the verbatim path.
        assert mgr.open_user_database(username, password) is engine
        mgr.close_user_database(username)
        reopened = mgr.open_user_database(username, password)
        assert reopened is not None
        with reopened.connect() as conn:
            assert (
                conn.execute(
                    text("SELECT note FROM gap_ag_canary WHERE id = 1")
                ).scalar()
                == "gap-AG2"
            )

        # The artifacts live inside the unicode directory itself.
        db_path = mgr._get_user_db_path(username)
        assert db_path.exists()
        assert "漢字" in str(db_path.parent.name)
        assert db_path.parent.parent == tmp_path
    finally:
        mgr.close_all_databases()