Files
singbox_glue/tests/test_metadata.py
T

56 lines
2.0 KiB
Python

from __future__ import annotations
import hashlib
import json
import tempfile
import unittest
from pathlib import Path
from vpn_egressctl.errors import ValidationError
from vpn_egressctl.metadata import build_config_metadata, load_config_metadata
class MetadataTests(unittest.TestCase):
def write(self, root: Path, value: dict) -> Path:
path = root / "config.meta.json"
path.write_text(json.dumps(value), encoding="utf-8")
return path
def test_current_metadata_is_accepted(self) -> None:
with tempfile.TemporaryDirectory() as directory:
data = b"{}\n"
digest = hashlib.sha256(data).hexdigest()
path = self.write(
Path(directory), build_config_metadata(digest, "1.14.0")
)
value = load_config_metadata(
path, config_sha256=digest, sing_box_version="1.14.0"
)
self.assertEqual(value["controller_version"], "0.2.0")
def test_wrong_engine_is_rejected(self) -> None:
with tempfile.TemporaryDirectory() as directory:
digest = hashlib.sha256(b"{}\n").hexdigest()
path = self.write(
Path(directory), build_config_metadata(digest, "1.13.19")
)
with self.assertRaisesRegex(ValidationError, "another sing-box"):
load_config_metadata(
path, config_sha256=digest, sing_box_version="1.14.0"
)
def test_unknown_keys_are_rejected(self) -> None:
with tempfile.TemporaryDirectory() as directory:
digest = hashlib.sha256(b"{}\n").hexdigest()
metadata = build_config_metadata(digest, "1.14.0")
metadata["future"] = True
path = self.write(Path(directory), metadata)
with self.assertRaisesRegex(ValidationError, "unsupported structure"):
load_config_metadata(
path, config_sha256=digest, sing_box_version="1.14.0"
)
if __name__ == "__main__":
unittest.main()