|
| 1 | +"""The pypcap engine falls back when the ``pcap`` module cannot load (#2132). |
| 2 | +
|
| 3 | +A ``pcap`` module can be installed and still fail to import with something other |
| 4 | +than :exc:`ImportError` -- ``OSError: Cannot find libpcap.so library`` with no |
| 5 | +system libpcap, for one. :meth:`Extractor.import_test |
| 6 | +<pcapkit.foundation.extraction.Extractor.import_test>` lets that through, so the |
| 7 | +engine's ``unsupported_reason()`` has to report it. ``PCAP_CT`` did and ``PyPCAP`` |
| 8 | +did not, so ``extract(engine='pypcap')`` raised the loader's error where |
| 9 | +``engine='pcap_ct'`` warned and fell back. Both now ask |
| 10 | +:func:`pcapkit.foundation.engines._pcap_backend.engine_reason`. |
| 11 | +
|
| 12 | +""" |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import importlib.util |
| 16 | +import json |
| 17 | +import os |
| 18 | +import pathlib |
| 19 | +import subprocess # nosec: B404 |
| 20 | +import sys |
| 21 | +import tempfile |
| 22 | +import types |
| 23 | +import unittest |
| 24 | +from unittest import mock |
| 25 | + |
| 26 | +from tests._support import scale_timeout |
| 27 | +from tests._tiers import ROOT |
| 28 | + |
| 29 | +RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper') |
| 30 | +HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS) |
| 31 | + |
| 32 | +#: What the fake ``pcap`` package raises on import: the libpcap loader's message. |
| 33 | +LOADER_ERROR = 'Cannot find libpcap.so library' |
| 34 | +#: The verdict ``PCAP_CT`` already gave on that failure, which ``PyPCAP`` now shares. |
| 35 | +REASON = f'the `pcap` module is installed but unusable -- OSError: {LOADER_ERROR}' |
| 36 | +#: Hang guard for the child interpreter, in seconds. |
| 37 | +TIMEOUT = 120 |
| 38 | + |
| 39 | +#: Child script: run one extraction with the fake ``pcap`` first on the path and |
| 40 | +#: report, as JSON, which ``pcapkit`` ran, what was warned and what happened. |
| 41 | +CHILD = ''' |
| 42 | +import json, sys, warnings |
| 43 | +engine, capture = sys.argv[1], sys.argv[2] |
| 44 | +import pcapkit |
| 45 | +from pcapkit.foundation.engines.pcap_ct import PCAP_CT |
| 46 | +from pcapkit.foundation.engines.pypcap import PyPCAP |
| 47 | +result = { |
| 48 | + 'pcapkit': pcapkit.__file__, |
| 49 | + 'pypcap_reason': PyPCAP.unsupported_reason(), |
| 50 | + 'pcap_ct_reason': PCAP_CT.unsupported_reason(), |
| 51 | +} |
| 52 | +with warnings.catch_warnings(record=True) as caught: |
| 53 | + warnings.simplefilter('always') |
| 54 | + try: |
| 55 | + extractor = pcapkit.extract(fin=capture, engine=engine, nofile=True) |
| 56 | + except BaseException as exc: |
| 57 | + result['error'] = f'{type(exc).__name__}: {exc}' |
| 58 | + else: |
| 59 | + result['engine'] = extractor._exnam |
| 60 | + result['frames'] = len(extractor.frame) |
| 61 | +result['warnings'] = [f'{w.category.__name__}: {w.message}' for w in caught] |
| 62 | +print(json.dumps(result)) |
| 63 | +''' |
| 64 | + |
| 65 | + |
| 66 | +@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed') |
| 67 | +class EngineReasonTests(unittest.TestCase): |
| 68 | + """The shared verdict, and both engines giving it.""" |
| 69 | + |
| 70 | + def probe(self, **overrides): |
| 71 | + from pcapkit.foundation.engines import _pcap_backend |
| 72 | + |
| 73 | + values = dict(name=None, version=None, origin=None, |
| 74 | + failure=f'OSError: {LOADER_ERROR}', missing=False, installed=()) |
| 75 | + values.update(overrides) |
| 76 | + return _pcap_backend.Probe(**values) |
| 77 | + |
| 78 | + def test_an_installed_but_unusable_module_is_a_reason_for_either_engine(self) -> None: |
| 79 | + from pcapkit.foundation.engines import _pcap_backend |
| 80 | + |
| 81 | + for wanted in _pcap_backend.DISTRIBUTIONS: |
| 82 | + with self.subTest(wanted=wanted): |
| 83 | + self.assertEqual(_pcap_backend.engine_reason(wanted, self.probe()), REASON) |
| 84 | + |
| 85 | + def test_an_absent_module_is_left_to_the_import_test(self) -> None: |
| 86 | + from pcapkit.foundation.engines import _pcap_backend |
| 87 | + |
| 88 | + found = self.probe(failure='no module named `pcap`', missing=True) |
| 89 | + self.assertIsNone(_pcap_backend.engine_reason(_pcap_backend.PYPCAP, found)) |
| 90 | + |
| 91 | + def test_the_wrong_backend_keeps_its_own_reason(self) -> None: |
| 92 | + from pcapkit.foundation.engines import _pcap_backend |
| 93 | + |
| 94 | + found = self.probe(name=_pcap_backend.PCAP_CT, version='1.3.0b3', |
| 95 | + origin='/stub/pcap/__init__.py', failure=None) |
| 96 | + self.assertEqual(_pcap_backend.engine_reason(_pcap_backend.PYPCAP, found), |
| 97 | + _pcap_backend.wrong_backend_reason(_pcap_backend.PYPCAP, found)) |
| 98 | + |
| 99 | + def broken_import(self): |
| 100 | + from pcapkit.foundation.engines import _pcap_backend |
| 101 | + |
| 102 | + return (mock.patch('importlib.import_module', side_effect=OSError(LOADER_ERROR)), |
| 103 | + mock.patch.object(_pcap_backend, 'installed_distributions', |
| 104 | + return_value=(_pcap_backend.PYPCAP,))) |
| 105 | + |
| 106 | + def test_pypcap_reports_the_module_that_cannot_load(self) -> None: |
| 107 | + from pcapkit.foundation.engines.pcap_ct import PCAP_CT |
| 108 | + from pcapkit.foundation.engines.pypcap import PyPCAP |
| 109 | + |
| 110 | + importer, installed = self.broken_import() |
| 111 | + with importer, installed: |
| 112 | + reason = PyPCAP.unsupported_reason() |
| 113 | + peer = PCAP_CT.unsupported_reason() |
| 114 | + |
| 115 | + self.assertEqual(reason, REASON) |
| 116 | + self.assertEqual(reason, peer) |
| 117 | + |
| 118 | + def test_pypcap_constructor_refuses_instead_of_raising_the_oserror(self) -> None: |
| 119 | + from pcapkit.foundation.engines.pypcap import PyPCAP |
| 120 | + from pcapkit.utilities.exceptions import UnsupportedCall |
| 121 | + |
| 122 | + importer, installed = self.broken_import() |
| 123 | + with importer, installed: |
| 124 | + with self.assertRaises(UnsupportedCall) as caught: |
| 125 | + PyPCAP(types.SimpleNamespace()) # type: ignore[arg-type] |
| 126 | + |
| 127 | + self.assertIn(REASON, str(caught.exception)) |
| 128 | + |
| 129 | + |
| 130 | +@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed') |
| 131 | +class FakePcapPackageTests(unittest.TestCase): |
| 132 | + """End to end, with a real ``pcap`` package on :data:`sys.path` that cannot load. |
| 133 | +
|
| 134 | + Run in a child interpreter so that the fake package never reaches this |
| 135 | + process's :data:`sys.modules` or import caches. |
| 136 | +
|
| 137 | + """ |
| 138 | + |
| 139 | + @classmethod |
| 140 | + def setUpClass(cls) -> None: |
| 141 | + tmp = tempfile.TemporaryDirectory() |
| 142 | + cls.addClassCleanup(tmp.cleanup) |
| 143 | + package = pathlib.Path(tmp.name) / 'pcap' |
| 144 | + package.mkdir() |
| 145 | + (package / '__init__.py').write_text(f'raise OSError({LOADER_ERROR!r})\n') |
| 146 | + cls.site = tmp.name |
| 147 | + |
| 148 | + def extract(self, engine: str) -> dict: |
| 149 | + env = dict(os.environ) |
| 150 | + env.pop('PYTHONSAFEPATH', None) # cwd must win, so the tree under test runs |
| 151 | + env['PYTHONPATH'] = os.pathsep.join( |
| 152 | + filter(None, (self.site, env.get('PYTHONPATH')))) |
| 153 | + capture = str(ROOT / 'examples' / 'captures' / 'in.pcap') |
| 154 | + completed = subprocess.run( # nosec: B603 |
| 155 | + [sys.executable, '-c', CHILD, engine, capture], |
| 156 | + cwd=str(ROOT), env=env, capture_output=True, text=True, |
| 157 | + timeout=scale_timeout(TIMEOUT), check=False, |
| 158 | + ) |
| 159 | + self.assertEqual(completed.returncode, 0, completed.stderr) |
| 160 | + result = json.loads(completed.stdout.strip().splitlines()[-1]) |
| 161 | + loaded = os.path.realpath(result['pcapkit']) |
| 162 | + self.assertEqual(os.path.commonpath([loaded, str(ROOT)]), str(ROOT), loaded) |
| 163 | + return result |
| 164 | + |
| 165 | + def assert_falls_back(self, engine: str, name: str) -> None: |
| 166 | + result = self.extract(engine) |
| 167 | + |
| 168 | + self.assertNotIn('error', result) |
| 169 | + self.assertEqual(result['engine'], 'default') |
| 170 | + self.assertGreater(result['frames'], 0) |
| 171 | + self.assertIn(f'EngineWarning: engine {name} is not supported on this ' |
| 172 | + f'interpreter ({REASON}); using default engine instead', |
| 173 | + result['warnings']) |
| 174 | + |
| 175 | + def test_pcap_ct_falls_back(self) -> None: |
| 176 | + self.assert_falls_back('pcap_ct', 'PCAP_CT') |
| 177 | + |
| 178 | + def test_pypcap_falls_back_like_pcap_ct(self) -> None: |
| 179 | + self.assert_falls_back('pypcap', 'PyPCAP') |
| 180 | + |
| 181 | + def test_pypcap_unsupported_reason_names_the_loader_error(self) -> None: |
| 182 | + result = self.extract('pypcap') |
| 183 | + |
| 184 | + self.assertEqual(result['pypcap_reason'], REASON) |
| 185 | + self.assertEqual(result['pypcap_reason'], result['pcap_ct_reason']) |
| 186 | + |
| 187 | + |
| 188 | +if __name__ == '__main__': |
| 189 | + unittest.main() |
0 commit comments