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.0 KB
Raw Blame
"""Recover real encrypted backups after losing the complete source directory.

This also runs natively on Windows. Existing salts are seeded with binary
file I/O so backup qualification is independent of the salt-creation fix.
No filesystem, SQLCipher, export, verification or recovery operation is mocked.
"""

from contextlib import closing
import os
from pathlib import Path
import shutil
import tempfile
import unittest
from unittest.mock import patch
from uuid import uuid4

from local_deep_research.database.backup.backup_service import BackupService
from local_deep_research.database.sqlcipher_compat import get_sqlcipher_module
from local_deep_research.database.sqlcipher_utils import (
    create_sqlcipher_connection,
    get_salt_file_path,
    get_sqlcipher_settings,
)


class NativeBackupTests(unittest.TestCase):
    def test_backup_pair_survives_source_loss_and_accepts_durable_writes(self):
        salts = (
            b"\x00\x1a\xff\r\n\r\r\n" + bytes(range(24)),
            b"\n" * 32,
            b"\r\n" * 16,
        )
        sqlcipher = get_sqlcipher_module()
        for index, salt in enumerate(salts):
            with self.subTest(salt=index):
                with tempfile.TemporaryDirectory(
                    prefix="ldr-native-backup-"
                ) as temporary:
                    root = Path(temporary)
                    source = root / "original's café data"
                    with patch.dict(
                        os.environ,
                        {
                            "LDR_DATA_DIR": str(source),
                            "LDR_TEST_MODE": "false",
                            "LDR_DB_CONFIG_KDF_ITERATIONS": "256000",
                        },
                    ):
                        self.assertEqual(
                            get_sqlcipher_settings()["kdf_iterations"], 256000
                        )
                        service = BackupService(
                            f"native-backup-{uuid4().hex}",
                            "native-backup-test-password",
                        )
                        service.db_path.parent.mkdir(
                            parents=True, exist_ok=True
                        )
                        source_salt = get_salt_file_path(service.db_path)
                        source_salt.write_bytes(salt)
                        with closing(
                            create_sqlcipher_connection(
                                service.db_path,
                                service.password,
                                creation_mode=True,
                            )
                        ) as connection:
                            self.assertTrue(
                                connection.execute(
                                    "PRAGMA cipher_version"
                                ).fetchone()[0]
                            )
                            self.assertEqual(
                                connection.execute(
                                    "PRAGMA journal_mode=WAL"
                                ).fetchone()[0],
                                "wal",
                            )
                            connection.execute("PRAGMA wal_autocheckpoint=0")
                            connection.execute(
                                "CREATE TABLE canary (value TEXT NOT NULL)"
                            )
                            connection.execute(
                                "INSERT INTO canary VALUES (?)", ("WAL data",)
                            )
                            connection.commit()
                            self.assertGreater(
                                Path(str(service.db_path) + "-wal")
                                .stat()
                                .st_size,
                                0,
                            )
                            backup = service.create_backup(force=True)
                            self.assertTrue(backup.success, backup.error)
                            self.assertEqual(source_salt.read_bytes(), salt)
                            self.assertEqual(
                                get_salt_file_path(
                                    backup.backup_path
                                ).read_bytes(),
                                salt,
                            )

                        archive = root / "archive's café"
                        archive.mkdir()
                        recovered = archive / "recovered.db"
                        shutil.copy2(backup.backup_path, recovered)
                        shutil.copy2(
                            get_salt_file_path(backup.backup_path),
                            get_salt_file_path(recovered),
                        )
                        shutil.rmtree(source)
                        self.assertFalse(source.exists())
                        self.assertTrue(service._verify_backup(recovered))
                        self.assertNotEqual(
                            recovered.read_bytes()[:16], b"SQLite format 3\0"
                        )
                        with closing(
                            create_sqlcipher_connection(
                                recovered, service.password
                            )
                        ) as connection:
                            self.assertEqual(
                                connection.execute(
                                    "PRAGMA quick_check"
                                ).fetchall(),
                                [("ok",)],
                            )
                            self.assertEqual(
                                connection.execute(
                                    "SELECT value FROM canary"
                                ).fetchall(),
                                [("WAL data",)],
                            )
                            connection.execute(
                                "INSERT INTO canary VALUES (?)",
                                ("durable write",),
                            )
                            connection.commit()
                        with closing(
                            create_sqlcipher_connection(
                                recovered, service.password
                            )
                        ) as connection:
                            self.assertEqual(
                                connection.execute(
                                    "SELECT COUNT(*) FROM canary"
                                ).fetchone(),
                                (2,),
                            )
                        with self.assertRaises(
                            (ValueError, sqlcipher.DatabaseError)
                        ):
                            with closing(
                                create_sqlcipher_connection(
                                    recovered, "wrong-password"
                                )
                            ):
                                pass
                        self.assertFalse(source.exists())


if __name__ == "__main__":
    unittest.main()