"""Strict backup manifest validation regression tests.""" import json import tempfile import unittest from pathlib import Path from types import SimpleNamespace from core.backup import BackupEngine, BackupError class BackupManifestValidationTests(unittest.TestCase): def setUp(self): temp = tempfile.TemporaryDirectory() self.addCleanup(temp.cleanup) self.root = Path(temp.name) self.root.chmod(0o700) self.configure("mysql") def configure(self, driver): self.adapter = SimpleNamespace( database={ "driver": driver, "name": "example_db", "username": "example_app", "container": "test-database", }, identity_command=lambda: ["identity"], backup_command=lambda: ["backup"], ) identity = ( b"example_db\texample_app@localhost\tserver-uuid" if driver == "mysql" else b"example_db|example_app" ) def runner(command, output): if command == ["identity"]: output.write(identity) elif command == ["backup"]: output.write(b"synthetic backup data") else: raise AssertionError("Unexpected command") self.engine = BackupEngine( self.adapter, self.root, runner ) self.backup = self.engine.create() self.manifest = self.backup / "manifest.json" def reject(self, key, value): data = json.loads(self.manifest.read_text()) data[key] = value self.manifest.write_text(json.dumps(data)) with self.assertRaises(BackupError): self.engine.verify(self.backup) def test_valid_mysql_manifest(self): self.engine.verify(self.backup) def test_valid_postgresql_manifest(self): self.configure("pgsql") self.engine.verify(self.backup) def test_null_timestamp(self): self.reject("created_at", None) def test_empty_timestamp(self): self.reject("created_at", "") def test_numeric_timestamp(self): self.reject("created_at", 123) def test_invalid_timestamp(self): self.reject("created_at", "not-a-date") def test_date_without_time(self): self.reject("created_at", "2026-11-27") def test_timestamp_without_timezone(self): self.reject( "created_at", "2026-11-27T08:49:03" ) def test_impossible_timestamp(self): self.reject( "created_at", "2026-02-30T08:49:03+00:00" ) def test_null_identity(self): self.reject("identity", None) def test_empty_identity(self): self.reject("identity", "") def test_whitespace_identity(self): self.reject("identity", " ") def test_numeric_identity(self): self.reject("identity", 123) def test_wrong_database_identity(self): self.reject( "identity", "other_db\texample_app@localhost\tserver-uuid" ) def test_wrong_account_identity(self): self.reject( "identity", "example_db\tother_app@localhost\tserver-uuid" ) def test_missing_mysql_server_identity(self): self.reject( "identity", "example_db\texample_app@localhost\t" ) def test_identity_control_character(self): self.reject( "identity", "example_db\texample_app@localhost\tserver\nuuid" ) def test_invalid_postgresql_identity(self): self.configure("pgsql") self.reject("identity", "example_db|wrong_account") def test_boolean_format_version(self): self.reject("format_version", True) def test_duplicate_json_key(self): data = self.manifest.read_text().strip() # Both values are valid. This tests duplicate-key # rejection rather than invalid field values. data = data[:-1] + ', "format_version": 1}' self.manifest.write_text(data) with self.assertRaises(BackupError): self.engine.verify(self.backup)