Files
simple/tests/test_mcc_mnc.py
T
catarrh bed66c8ff8 fix: robust equip-state detection and review follow-ups (v3.5.4)
Code-review follow-ups for v3.5.2/v3.5.1, plus a test flake found while
re-running the suites:

- F1: a half-initialized equip is no longer reported as success.  cmd2
  swallows exceptions raised inside the equip command (and prints no
  traceback by default), while PysimApp.equip() assigns card/rs before it
  registers the command sets - so app.card alone once let the "CommandSet ...
  is already installed" abort pass as done while /api/tree stayed broken.
  The auto-equip attempt now captures the output with cmd2 debug on (a
  swallowed error prints a traceback), requires the new profile's command-set
  instances to be installed (_app_equip_complete), and the manual
  /api/command equip branch applies the post-equip refresh only when that
  check passes (and reports it in the output when it does not).
- F3: SCARD_E_SHARING_VIOLATION is recoverable (a rebuild cannot free another
  process's claim) instead of transport-fatal.
- F4: SPI1 b2b1 = 11 (Digital Signature) is refused instead of building an
  unsigned packet; help notes RC is CRC-32 only (KID CRC-16 not offered).
- F5: _clear_app_card_state removes the muted stdout again when the app object
  had no stdout attribute.
- F7: the watchdog re-arm keeps its rate-limit window when the trigger is
  busy/disabled instead of consuming it.
- F2: stale docstring in _auto_equip_attempt.
- tests: equip-state units, half-equip and captured-traceback failures,
  unequip-on-failure, busy trigger, sharing violation, DS refusal.
- bonus: the MCC/MNC random-pick test could fail because a dict keyed by
  (mcc, mnc) keeps one of two entries (the bundled list carries both a real
  and an MVNO entry for 234/18 and 234/28); the picker was correct - the
  assertion now checks the pair against the non-MVNO pairs.

549 frontend / 421 Python green; version 3.5.4; sw cache simple-v256.
2026-09-24 20:52:49 +03:00

105 lines
4.6 KiB
Python

# coding=utf-8
"""Tests for the bundled MCC/MNC operator list and the simulator picker.
The list ships inside the package (``data/mcc-mnc-list.json``, MIT, from
pbakondy/mcc-mnc-list). The Network simulation picker hides MVNO entries
(``bands`` marks them) because they do not operate their own network; the
Network state panel still resolves operator names from the full list.
"""
import os
import unittest
from pysim_simple_server import __main__, server
def _entry(mcc, mnc, brand, bands, **kw):
e = {'mcc': mcc, 'mnc': mnc, 'brand': brand, 'operator': brand + ' op',
'countryName': 'Testland', 'countryCode': 'TL', 'status': 'Operational',
'bands': bands}
e.update(kw)
return e
class BundledMccMncListTests(unittest.TestCase):
def test_default_path_is_bundled_and_parses(self):
path = __main__._default_mcc_mnc_list()
pkg_dir = os.path.dirname(os.path.abspath(server.__file__))
self.assertTrue(os.path.abspath(path).startswith(pkg_dir + os.sep), path)
self.assertTrue(os.path.isfile(path), path)
data = server._mcc_mnc_load(path)
self.assertIsInstance(data, list)
self.assertGreater(len(data), 3000)
for key in ('mcc', 'mnc', 'countryName', 'countryCode', 'brand',
'operator', 'status', 'bands'):
self.assertIn(key, data[0])
def test_bundled_list_has_mvno_entries_to_filter(self):
data = server._mcc_mnc_load(__main__._default_mcc_mnc_list())
mvno = [e for e in data if server._mcc_mnc_is_mvno(e)]
self.assertGreater(len(mvno), 300)
def test_mvno_marker_variants(self):
for bands in ('MVNO', 'Satellite MVNO', 'FULL MVNO', 'MVNO / LTE 800',
'5G 3500 / MVNO', 'mvno'):
self.assertTrue(server._mcc_mnc_is_mvno(_entry('262', '01', 'X', bands)),
bands)
for bands in ('LTE 1800', 'GSM 900 / LTE 800', '', None):
self.assertFalse(server._mcc_mnc_is_mvno(_entry('262', '01', 'X', bands)),
repr(bands))
class MccMncFilterTests(unittest.TestCase):
DATA = [
_entry('262', '01', 'Telekom', 'GSM 900 / LTE 800'),
_entry('262', '77', 'MVNO brand', 'MVNO'),
_entry('262', '78', 'Sat MVNO', 'Satellite MVNO'),
_entry('262', '79', 'Mentions MVNO', 'LTE 1800',
notes='Used by MVNO Kartu As'),
]
def test_search_skips_mvno_entries(self):
res = server._mcc_mnc_search(self.DATA, '262')
self.assertEqual([(r['mcc'], r['mnc']) for r in res],
[('262', '01'), ('262', '79')])
# the MVNO brands would match these queries without the filter, but the
# notes-only MVNO mention (a real network) still matches 'mvno'
self.assertEqual([(r['mcc'], r['mnc']) for r in server._mcc_mnc_search(self.DATA, 'mvno')],
[('262', '79')])
self.assertEqual(server._mcc_mnc_search(self.DATA, 'sat mvno'), [])
def test_search_real_list_hides_mvno_entry(self):
data = server._mcc_mnc_load(__main__._default_mcc_mnc_list())
mvno = next(e for e in data if server._mcc_mnc_is_mvno(e) and e.get('brand'))
res = server._mcc_mnc_search(data, mvno['brand'], limit=10000)
self.assertNotIn((mvno['mcc'], mvno['mnc']),
[(r['mcc'], r['mnc']) for r in res])
def test_random_skips_mvno_entries(self):
mixed = [_entry('262', '77', 'MVNO brand', 'MVNO'),
_entry('262', '78', 'Sat MVNO', 'Satellite MVNO')]
self.assertIsNone(server._mcc_mnc_random(mixed))
data = self.DATA
for _ in range(100):
r = server._mcc_mnc_random(data)
self.assertIn((r['mcc'], r['mnc']), [('262', '01'), ('262', '79')])
# exclusion still applies within the non-MVNO pool
for _ in range(50):
self.assertEqual(server._mcc_mnc_random(data, exclude='26201')['mnc'],
'79')
def test_random_real_list_never_picks_mvno(self):
data = server._mcc_mnc_load(__main__._default_mcc_mnc_list())
# A few (mcc, mnc) pairs exist both as a real network and as an MVNO
# entry (e.g. 234/18, 234/28), so a dict keyed by the pair can keep the
# wrong entry: assert the picked pair has a non-MVNO entry behind it.
real_pairs = {(e['mcc'], e['mnc']) for e in data
if not server._mcc_mnc_is_mvno(e)}
for _ in range(100):
r = server._mcc_mnc_random(data)
self.assertIn((r['mcc'], r['mnc']), real_pairs)
if __name__ == '__main__':
unittest.main()