Push Tracker
ria-ran--sim-drop/tests/test_utils.py
A ashkan@beigi.net 0be02bd1f4 Harden: fix bare-filename db path crash, configurable APN
- Fix FileNotFoundError when database.path has no directory component
  (os.makedirs(os.path.dirname(path)) with path="subscribers.db" raised).
  Add ensure_parent_dir helper; use it in app.py and scripts/init_db.py.
- Open5GS/free5gc adapters now use profile.apn for the core session/DNN name
  instead of a hardcoded "internet"; apn is threaded through get_adapter.

Tests: +4 (ensure_parent_dir, adapter APN); 82 pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-01 06:13:26 +00:00

98 lines
2.8 KiB
Python

import pytest
from server.utils import (
ensure_parent_dir,
load_config,
normalize_mac,
validate_imsi,
validate_mac,
validate_op_key,
)
class TestEnsureParentDir:
def test_bare_filename_does_not_crash(self):
# No directory component -> must be a no-op, not FileNotFoundError.
ensure_parent_dir("subscribers.db")
def test_creates_nested_dirs(self, tmp_path):
target = tmp_path / "a" / "b" / "x.db"
ensure_parent_dir(str(target))
assert (tmp_path / "a" / "b").is_dir()
class TestMac:
def test_valid(self):
assert validate_mac("aa:bb:cc:dd:ee:ff")
def test_invalid(self):
assert not validate_mac("nope")
assert not validate_mac("")
def test_normalize_dashes_and_case(self):
assert normalize_mac("AA-BB-CC-DD-EE-FF") == "aa:bb:cc:dd:ee:ff"
def test_normalize_no_separator(self):
assert normalize_mac("aabbccddeeff") == "aa:bb:cc:dd:ee:ff"
class TestImsi:
def test_valid(self):
assert validate_imsi("302720000000001")
def test_wrong_length(self):
assert not validate_imsi("30272000000000")
def test_non_digit(self):
assert not validate_imsi("30272000000000x")
class TestOpKey:
def test_valid(self):
assert validate_op_key("63bfa50ee6523365ff14c1f45f88737d")
def test_wrong_length(self):
assert not validate_op_key("abcd")
def test_non_hex(self):
assert not validate_op_key("zz" * 16)
class TestLoadConfig:
def _base(self):
return (
"network: {mcc: '302', mnc: '720', op_key: '63bfa50ee6523365ff14c1f45f88737d'}\n"
"server: {host: '0.0.0.0', port: 5000}\n"
"database: {path: '/tmp/x.db'}\n"
"rate_limiting: {window_hours: 1}\n"
"profile: {profile_name: 'P', carrier_name: 'C'}\n"
)
def test_valid(self, tmp_path):
p = tmp_path / "c.yaml"
p.write_text(self._base())
cfg = load_config(str(p))
assert cfg["network"]["mcc"] == "302"
def test_missing_file(self):
with pytest.raises(FileNotFoundError):
load_config("/no/such/config.yaml")
def test_empty_file(self, tmp_path):
p = tmp_path / "empty.yaml"
p.write_text("")
with pytest.raises(ValueError, match="empty or not a mapping"):
load_config(str(p))
def test_missing_section(self, tmp_path):
p = tmp_path / "c.yaml"
p.write_text("network: {op_key: '63bfa50ee6523365ff14c1f45f88737d'}\n")
with pytest.raises(ValueError, match="Missing required config section"):
load_config(str(p))
def test_bad_op_key(self, tmp_path):
p = tmp_path / "c.yaml"
p.write_text(self._base().replace("63bfa50ee6523365ff14c1f45f88737d", "abcd"))
with pytest.raises(ValueError, match="op_key"):
load_config(str(p))