Skip to content

[TECH DEBT] Test Coverage #30

@humanauction

Description

@humanauction

Test Coverage Improvements

Summary

Current test coverage is 16% with several critical production files having 0% coverage. This document tracks necessary test additions and fixes to reach target coverage of 60-70%.


Critical Issues (0% Coverage)

1. NetMonDaemon.cpp (358 lines, 0% covered) 🔴 HIGH PRIORITY

Problem: No integration tests exist for the HTTP daemon that serves metrics endpoints.

Required Tests:

  • Test daemon starts successfully on specified port
  • Test /api/metrics endpoint returns valid JSON
  • Test /api/flows endpoint returns flow data
  • Test /api/sessions endpoint with authentication
  • Test graceful shutdown on SIGTERM/SIGINT
  • Test concurrent request handling
  • Test error responses (404, 500, etc.)
  • Test CORS headers if applicable

Implementation:

// filepath: tests/integration/test_netmon_daemon.cpp
- Start daemon in background thread
- Make HTTP requests using httplib
- Verify responses and status codes
- Clean shutdown after tests

Estimated Coverage Gain: +215 lines (~60% of file)


2. PcapAdapter.cpp (86 lines, 0% covered) 🔴 HIGH PRIORITY

Problem: test_pcap_adapter.cpp exists (47 lines) but produces no coverage data. Test is likely failing silently or not running.

Investigation Needed:

  • Check if test segfaults when run directly: ./build/test_runner
  • Verify test is built and linked in CMakeLists.txt
  • Check if test requires root/sudo (pcap needs privileges)
  • Review test expectations vs actual PcapAdapter behavior

Required Tests:

  • Test opening pcap file (offline mode)
  • Test opening network interface with proper error handling
  • Test setting pcap filter expressions
  • Test packet callback mechanism
  • Test statistics retrieval (packets captured/dropped)
  • Mock pcap calls for CI environment (no sudo)
  • Test graceful handling of missing interfaces

Implementation:

// filepath: tests/unit/test_pcap_adapter.cpp
- Use GTEST_SKIP() if not running as root
- Test with sample.pcap file instead of live interface
- Mock pcap_* functions for unit testing
- Add privilege check before interface tests

Estimated Coverage Gain: +60 lines (~70% of file)


3. StatsPersistence.cpp (78 lines, 0% covered) 🟡 MEDIUM PRIORITY

Problem: No tests for SQLite database operations (save/load stats).

Required Tests:

  • Test saveStats() writes to database successfully
  • Test loadStats() retrieves saved stats correctly
  • Test database schema creation
  • Test handling of corrupt database files
  • Test concurrent access (if applicable)
  • Test in-memory database for fast testing
  • Test migration between schema versions
  • Test cleanup of old data

Implementation:

// filepath: tests/unit/test_stats_persistence.cpp
- Use `:memory:` SQLite database for tests
- Create sample AggregatedStats with known values
- Verify round-trip save/load preserves data
- Test error conditions (disk full, permissions, etc.)

Estimated Coverage Gain: +62 lines (~80% of file)


4. main.cpp (58 lines, 0% covered) 🟢 LOW PRIORITY

Problem: CLI entry point has no functional tests.

Required Tests:

  • Test --help flag displays usage
  • Test --version flag displays version
  • Test invalid argument handling
  • Test signal handling (SIGINT, SIGTERM)
  • Test daemon mode vs foreground mode
  • Test configuration file loading
  • Test interface selection (-i eth0)
  • Test filter expression parsing

Implementation:

// filepath: tests/integration/test_main.cpp
- Use system() to invoke ./netnet binary
- Capture stdout/stderr
- Verify exit codes
- Test argument combinations

Estimated Coverage Gain: +17 lines (~30% of file)


Medium Priority Issues

5. test_pcap_adapter.cpp (47 lines, 0% covered)

Problem: Test file itself has no coverage, indicating it's not running.

Action Items:

  • Verify test is registered with gtest
  • Check CMakeLists.txt for proper test executable definition
  • Run test manually and capture output/errors
  • Add to CI test runner if missing
  • Fix any segfaults or assertion failures

6. Parser.cpp (49 lines, 59% covered)

Problem: Missing edge case coverage.

Additional Tests Needed:

  • Test IPv6 packet parsing
  • Test fragmented packets
  • Test VLAN-tagged packets
  • Test malformed headers (fuzz testing)
  • Test non-TCP/UDP protocols (ICMP, etc.)
  • Test jumbo frames
  • Test zero-length payloads

Estimated Coverage Gain: +20 lines (→ 100%)


7. ConnectionTracker.cpp (59 lines, 79% covered)

Problem: Missing coverage for edge cases.

Additional Tests Needed:

  • Test flow expiration/cleanup
  • Test bidirectional flow tracking
  • Test flow state transitions (SYN, FIN, RST)
  • Test maximum flow table size limits
  • Test flow serialization/deserialization

Estimated Coverage Gain: +12 lines (→ 100%)


8. StatsAggregator.cpp (56 lines, 92% covered)

Problem: Near-complete but missing edge cases.

Additional Tests Needed:

  • Test window advancement with no packets
  • Test history overflow (circular buffer wraparound)
  • Test concurrent access (if multithreaded)
  • Test stats reset/clear

Estimated Coverage Gain: +4 lines (→ 100%)


Testing Infrastructure Improvements

CI/CD Pipeline

  • Add coverage threshold check (fail if below 60%)
  • Generate HTML coverage reports as artifacts
  • Add branch coverage metrics (not just line coverage)
  • Set up nightly full test runs with valgrind
  • Add performance regression tests

Test Fixtures

  • Create diverse sample.pcap files for testing
    • Small packets (< 64 bytes)
    • Large packets (jumbo frames)
    • IPv4 and IPv6 traffic
    • TCP handshakes, UDP streams, ICMP
    • Malformed/corrupted packets
  • Add test database fixtures
  • Create mock HTTP responses for daemon tests

Test Organization

  • Split unit tests from integration tests
  • Add smoke tests for quick validation
  • Add end-to-end tests (full pipeline)
  • Document test naming conventions
  • Add test data generators for large-scale tests

Excluded from Coverage (Vendor Code)

The following files are intentionally excluded from coverage metrics:

  • include/net-net/vendor/httplib.h (2175 lines) - Third-party HTTP library
  • include/net-net/vendor/bcrypt.cpp (45 lines) - Third-party crypto library
  • include/net-net/vendor/uuid_gen.cpp - UUID generation utility

Total vendor lines: ~2220 (should not count toward coverage goals)


Coverage Goals

Metric Current Target Stretch Goal
Overall Coverage 16% 60% 70%
src/ Coverage 25% 70% 85%
Critical Files (NetMonDaemon, PcapAdapter) 0% 60% 80%
Unit Test Coverage ~86% 90% 95%

Estimated Timeline

Phase 1: Critical Fixes (Week 1)

  • Fix test_pcap_adapter (not running)
  • Add basic test_netmon_daemon (HTTP endpoint smoke tests)
  • Expected gain: 16% → 25%

Phase 2: Core Coverage (Week 2)

  • Complete NetMonDaemon integration tests
  • Add StatsPersistence unit tests
  • Expand Parser edge case tests
  • Expected gain: 25% → 45%

Phase 3: Comprehensive Testing (Week 3)

  • Add main.cpp functional tests
  • Add end-to-end integration tests
  • Performance and stress tests
  • Expected gain: 45% → 60%

Phase 4: Polish (Week 4)

  • Reach for 70% coverage
  • Add fuzz testing
  • Valgrind clean runs
  • Documentation updates
  • Expected gain: 60% → 70%

Related Issues

  • #TODO: Document test execution requirements (sudo for pcap)
  • #TODO: Set up code coverage dashboard (Codecov integration)
  • #TODO: Add pre-commit hook for running tests
  • #TODO: Create developer guide for writing tests

Notes

  • Current test suite takes ~6 seconds to run (mostly SessionManager sleep tests)
  • Consider mocking time in SessionManager tests to speed up CI
  • PcapAdapter tests may need elevated privileges in CI
  • NetMonDaemon tests need to bind to ephemeral ports to avoid conflicts

Last Updated: December 8, 2025
Owner: @guns4kids
Priority: High - Blocking production readiness

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions