Skip to content

Conversation

@tevio
Copy link

@tevio tevio commented Nov 5, 2025

Improved Mapport Coverage

mapport: add hook-based seam for PCP/UPnP, expand unit tests, improve coverage; exclude seam header from coverage

Motivation / Context

  • The mapport thread manages NAT traversal (PCP/NAT-PMP/UPnP) and historically had limited, flaky coverage because tests could trigger real network I/O and OS firewalls.
  • We want deterministic tests that exercise mapport lifecycle and PCP control-flow without opening sockets, and a clean coverage report without noise from glue code.

What’s changed

  • Introduced a small, production-compiled hook seam to make network-dependent calls substitutable in tests:
    • New neutral hooks namespace: mapport_hooks with function-pointer variables that default to real implementations.
      • mapport_hooks::QueryDefaultGatewayFn
      • mapport_hooks::PCPRequestPortMapFn
      • mapport_hooks::NATPMPRequestPortMapFn
      • mapport_hooks::GetLocalAddressesFn
    • src/mapport.cpp now invokes these hooks instead of calling the functions directly inside ProcessPCP() and related logic. No behavior change in production: the hooks point to the real functions by default.
  • Expanded and hardened unit tests for mapport:
    • Tests exercise thread lifecycle (StartMapPort, InterruptMapPort, StopMapPort), dispatch toggling, idempotent starts, and key branches in ProcessPCP().
    • Tests override the hooks at runtime with stubs (via a small RAII guard) to drive PCP/NAT-PMP paths deterministically without any network traffic or firewall prompts.
    • Tests use the standard BasicTestingSetup fixture for required global initialization.
  • Coverage hygiene:
    • Excluded the trivial seam header from LCOV to avoid a spurious 0% row: src/mapport_hooks.h filtered in cmake/script/CoverageInclude.cmake.in.
  • CTest/coverage ergonomics (if not already present in your tree):
    • CTest runs with --output-on-failure and explicitly ensures mapport_tests execute during coverage, so unit-test hits always contribute to the HTML reports.

Before

Screenshot 2025-11-03 at 18 29 17 (1)

After

Screenshot 2025-11-05 at 12 40 19

@tevio tevio force-pushed the update-coverage-include-mapport-test branch from 1dd7ebd to 0ebd401 Compare November 5, 2025 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant