Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions pcapkit/protocols/application/ospf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@

"""
import ipaddress
import re
from typing import TYPE_CHECKING, cast

from pcapkit.const.ospf.authentication import Authentication as Enum_Authentication
Expand Down Expand Up @@ -89,9 +88,6 @@

__all__ = ['OSPF']

# Ethernet address pattern
PAT_MAC_ADDR = re.compile(rb'(?i)(?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2}')


class OSPF(Application[Data_OSPF, Schema_OSPF],
schema=Schema_OSPF, data=Data_OSPF):
Expand Down
58 changes: 58 additions & 0 deletions tests/protocols/application/test_ospf_dead_pattern_1844_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-
"""The unused Ethernet-address pattern in the OSPF module.

GitHub issue #1844. :mod:`pcapkit.protocols.application.ospf` defined a
``PAT_MAC_ADDR`` that nothing read, a leftover that came along when OSPF moved
to the application layer in #1031, and it imported :mod:`re` only to compile
it. The constant and the import are gone. This pins that the constant stays
gone and that the module does not import :mod:`re` without using it. The
copies of the pattern in the Ethernet, ARP and PCAP-NG modules have readers
and are untouched.

"""
from __future__ import annotations

import ast
import importlib.util
import unittest

from tests._support import reimport_once_per_class

RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper')
HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS)


@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed')
class OSPFDeadPatternUnitTests(unittest.TestCase):
def setUp(self) -> None:
reimport_once_per_class(self)

def test_pat_mac_addr_is_gone(self) -> None:
from pcapkit.protocols.application import ospf

self.assertFalse(hasattr(ospf, 'PAT_MAC_ADDR'))

def test_re_is_not_imported_unused(self) -> None:
# Read the source of the module actually imported, so the check runs
# against the tree under test. ``re`` may come back if something uses
# it; what must not return is the import with no reader.
from pcapkit.protocols.application import ospf

with open(ospf.__file__, encoding='utf-8') as file:
tree = ast.parse(file.read())

imports_re = any(
alias.name == 're' and alias.asname is None
for node in tree.body if isinstance(node, ast.Import)
for alias in node.names
)
reads_re = any(
isinstance(node, ast.Name) and node.id == 're'
for node in ast.walk(tree)
)
self.assertFalse(imports_re and not reads_re,
f'{ospf.__file__} imports re but never uses it')


if __name__ == '__main__':
unittest.main()
Loading