86 lines
2.6 KiB
Python
86 lines
2.6 KiB
Python
|
A
|
import importlib.util
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
import yaml
|
||
|
|
|
||
|
|
from server import saip_profile
|
||
|
|
|
||
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
|
|
|
||
|
|
|
||
|
|
def _load_tool():
|
||
|
|
path = os.path.join(ROOT, "scripts", "make_profile.py")
|
||
|
|
spec = importlib.util.spec_from_file_location("make_profile", path)
|
||
|
|
mod = importlib.util.module_from_spec(spec)
|
||
|
|
spec.loader.exec_module(mod)
|
||
|
|
return mod
|
||
|
|
|
||
|
|
|
||
|
|
mp = _load_tool()
|
||
|
|
|
||
|
|
CFG = {
|
||
|
|
"network": {"mcc": "302", "mnc": "720",
|
||
|
|
"op_key": "63bfa50ee6523365ff14c1f45f88737d"},
|
||
|
|
"profile": {"profile_name": "Emergency 5G", "carrier_name": "C",
|
||
|
|
"apn": "internet", "smdp_address": "esim.local"},
|
||
|
|
}
|
||
|
|
|
||
|
|
saip_required = pytest.mark.skipif(
|
||
|
|
not saip_profile.saip_available(), reason="SAIP unavailable")
|
||
|
|
|
||
|
|
|
||
|
|
def _full_config(tmp_path):
|
||
|
|
cfg = dict(CFG)
|
||
|
|
cfg.update({
|
||
|
|
"server": {"host": "0.0.0.0", "port": 5000},
|
||
|
|
"database": {"path": str(tmp_path / "s.db")},
|
||
|
|
"rate_limiting": {"window_hours": 1},
|
||
|
|
})
|
||
|
|
path = tmp_path / "config.yaml"
|
||
|
|
path.write_text(yaml.safe_dump(cfg))
|
||
|
|
return str(path)
|
||
|
|
|
||
|
|
|
||
|
|
class TestBuildProfile:
|
||
|
|
def test_v2(self):
|
||
|
|
imsi, iccid, ki, opc, data = mp.build_profile(CFG, "v2")
|
||
|
|
assert len(imsi) == 15 and len(ki) == 16
|
||
|
|
obj = json.loads(data)
|
||
|
|
assert obj["type"] == "profile-package-v2" and obj["imsi"] == imsi
|
||
|
|
|
||
|
|
def test_v2_custom_ids(self):
|
||
|
|
imsi, iccid, *_rest = mp.build_profile(
|
||
|
|
CFG, "v2", imsi="302720111111111", iccid="8910000000000000009")
|
||
|
|
assert imsi == "302720111111111"
|
||
|
|
assert iccid == "8910000000000000009"
|
||
|
|
|
||
|
|
@saip_required
|
||
|
|
def test_saip_and_summary(self):
|
||
|
|
_imsi, _iccid, _ki, _opc, data = mp.build_profile(CFG, "saip")
|
||
|
|
assert data[0] == 0xA0
|
||
|
|
summary = mp.summarize_saip(data)
|
||
|
|
assert "header" in summary and "usim" in summary
|
||
|
|
|
||
|
|
|
||
|
|
class TestMain:
|
||
|
|
def test_generate_v2_to_file(self, tmp_path, capsys):
|
||
|
|
out = tmp_path / "p.json"
|
||
|
|
rc = mp.main(["--config", _full_config(tmp_path), "--format", "v2",
|
||
|
|
"--out", str(out)])
|
||
|
|
assert rc == 0
|
||
|
|
obj = json.loads(out.read_bytes())
|
||
|
|
assert obj["type"] == "profile-package-v2"
|
||
|
|
|
||
|
|
@saip_required
|
||
|
|
def test_generate_and_inspect_saip(self, tmp_path, capsys):
|
||
|
|
out = tmp_path / "p.der"
|
||
|
|
mp.main(["--config", _full_config(tmp_path), "--format", "saip",
|
||
|
|
"--out", str(out)])
|
||
|
|
assert out.read_bytes()[0] == 0xA0
|
||
|
|
capsys.readouterr()
|
||
|
|
mp.main(["--inspect", str(out)])
|
||
|
|
printed = capsys.readouterr().out
|
||
|
|
assert "ProfileElement" in printed and "usim" in printed
|