|
| 1 | +from pathlib import Path |
| 2 | +from types import SimpleNamespace |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from cycode.cli.utils import host_info |
| 8 | + |
| 9 | +_SERIAL = 'C02XY1234567' |
| 10 | +_IOREG_OUTPUT = """ |
| 11 | + +-o Root <class IORegistryEntry, id 1, retain 42> |
| 12 | + "IOPlatformSerialNumber" = "C02XY1234567" |
| 13 | +""" |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(autouse=True) |
| 17 | +def _home_in_tmp(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: |
| 18 | + monkeypatch.setattr(Path, 'home', classmethod(lambda _cls: tmp_path)) |
| 19 | + return tmp_path |
| 20 | + |
| 21 | + |
| 22 | +def _cache_path(tmp_path: Path) -> Path: |
| 23 | + return tmp_path / '.cycode' / 'device-id' |
| 24 | + |
| 25 | + |
| 26 | +class _ComCalls: |
| 27 | + def __init__(self) -> None: |
| 28 | + self.initialized = 0 |
| 29 | + self.uninitialized = 0 |
| 30 | + |
| 31 | + |
| 32 | +def _install_fake_pywin32( |
| 33 | + monkeypatch: pytest.MonkeyPatch, |
| 34 | + serial: Optional[str] = _SERIAL, |
| 35 | + get_object_error: Optional[Exception] = None, |
| 36 | +) -> _ComCalls: |
| 37 | + calls = _ComCalls() |
| 38 | + |
| 39 | + pythoncom = SimpleNamespace( |
| 40 | + CoInitialize=lambda: setattr(calls, 'initialized', calls.initialized + 1), |
| 41 | + CoUninitialize=lambda: setattr(calls, 'uninitialized', calls.uninitialized + 1), |
| 42 | + ) |
| 43 | + |
| 44 | + class _Bios: |
| 45 | + SerialNumber = serial |
| 46 | + |
| 47 | + class _WmiService: |
| 48 | + def InstancesOf(self, class_name: str) -> list: # noqa: N802 - mirrors the COM API |
| 49 | + assert class_name == 'Win32_BIOS' |
| 50 | + return [_Bios()] |
| 51 | + |
| 52 | + def get_object(moniker: str) -> _WmiService: |
| 53 | + assert moniker == 'winmgmts:' |
| 54 | + if get_object_error is not None: |
| 55 | + raise get_object_error |
| 56 | + return _WmiService() |
| 57 | + |
| 58 | + win32com_client = SimpleNamespace(GetObject=get_object) |
| 59 | + |
| 60 | + # host_info imports pywin32 at module level (guarded by sys.platform), so patch the bound names |
| 61 | + monkeypatch.setattr(host_info, 'pythoncom', pythoncom, raising=False) |
| 62 | + monkeypatch.setattr(host_info, 'win32com_client', win32com_client, raising=False) |
| 63 | + return calls |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture |
| 67 | +def _windows(monkeypatch: pytest.MonkeyPatch) -> None: |
| 68 | + monkeypatch.setattr(host_info.platform, 'system', lambda: 'Windows') |
| 69 | + |
| 70 | + |
| 71 | +def test_cache_path_is_under_the_cycode_home_dir(tmp_path: Path) -> None: |
| 72 | + assert host_info._serial_number_cache_path() == _cache_path(tmp_path) |
| 73 | + |
| 74 | + |
| 75 | +def test_cached_value_short_circuits_resolution(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: |
| 76 | + _cache_path(tmp_path).parent.mkdir(parents=True) |
| 77 | + _cache_path(tmp_path).write_text('CACHED-ID', encoding='utf-8') |
| 78 | + |
| 79 | + def _fail() -> str: |
| 80 | + raise AssertionError('must not resolve when the cache is warm') |
| 81 | + |
| 82 | + monkeypatch.setattr(host_info, '_resolve_serial_number', _fail) |
| 83 | + |
| 84 | + assert host_info.get_serial_number() == 'CACHED-ID' |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.usefixtures('_windows') |
| 88 | +def test_windows_reads_bios_serial_over_wmi_and_caches_it(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: |
| 89 | + calls = _install_fake_pywin32(monkeypatch) |
| 90 | + |
| 91 | + assert host_info.get_serial_number() == _SERIAL |
| 92 | + assert _cache_path(tmp_path).read_text(encoding='utf-8') == _SERIAL |
| 93 | + assert (calls.initialized, calls.uninitialized) == (1, 1) |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.usefixtures('_windows') |
| 97 | +def test_windows_uninitializes_com_when_wmi_fails(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: |
| 98 | + calls = _install_fake_pywin32(monkeypatch, get_object_error=OSError('WMI is unavailable')) |
| 99 | + |
| 100 | + assert host_info.get_serial_number() is None |
| 101 | + assert (calls.initialized, calls.uninitialized) == (1, 1) |
| 102 | + assert not _cache_path(tmp_path).exists() |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.usefixtures('_windows') |
| 106 | +def test_windows_blank_serial_is_none_and_not_cached(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: |
| 107 | + _install_fake_pywin32(monkeypatch, serial=' ') |
| 108 | + |
| 109 | + assert host_info.get_serial_number() is None |
| 110 | + assert not _cache_path(tmp_path).exists() |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.usefixtures('_windows') |
| 114 | +def test_windows_without_pywin32_returns_none(monkeypatch: pytest.MonkeyPatch) -> None: |
| 115 | + monkeypatch.setattr(host_info, 'pythoncom', None, raising=False) |
| 116 | + monkeypatch.setattr(host_info, 'win32com_client', None, raising=False) |
| 117 | + |
| 118 | + assert host_info.get_serial_number() is None |
| 119 | + |
| 120 | + |
| 121 | +def test_macos_serial_number_is_parsed_from_ioreg(monkeypatch: pytest.MonkeyPatch) -> None: |
| 122 | + monkeypatch.setattr(host_info.platform, 'system', lambda: 'Darwin') |
| 123 | + monkeypatch.setattr(host_info, '_run', lambda *_args, **_kwargs: _IOREG_OUTPUT) |
| 124 | + |
| 125 | + assert host_info.get_serial_number() == _SERIAL |
| 126 | + |
| 127 | + |
| 128 | +def test_linux_returns_none(monkeypatch: pytest.MonkeyPatch) -> None: |
| 129 | + monkeypatch.setattr(host_info.platform, 'system', lambda: 'Linux') |
| 130 | + |
| 131 | + assert host_info.get_serial_number() is None |
0 commit comments