Skip to content

[Tech-debt] Tests for enable_dns_mode script don't exercise production code (#156 F1) #158

Description

@flyhigher139

Context

PR #156 added 3 tests to defend against #155's silent failure (proxy binary missing → DNS mode looks enabled but port 53 has no listener):

  • test_pid_file_content_format — asserts the script contains expected echo "$PROXY_PID ..." line
  • test_enable_script_contains_safety_layers — asserts the script has [ ! -x ] / kill -0 / log dump / PROXY_PID / networksetup ordering
  • test_enable_script_loudly_fails_when_proxy_missing — runs a 6-line [ ! -x ] snippet through /bin/sh

Problem: the first two rebuild the script with their own format! macro call instead of invoking the production enable_dns_mode script generator. The shell test executes only a synthetic snippet, not the real PROXY_PID / kill -0 / log-dump path.

Net effect: reverting enable_dns_mode to the old set -e + cmd & echo "\$! ..." buggy form would keep all 3 tests passing. The tests guard against script-shape regression, not against silent failure on production paths.

Repro

git checkout fix/dns-mode-silent-failure-155 -- src-tauri/crates/mhost-dns/src/platform.rs
# revert just the script body inside enable_dns_mode to the old `echo "$! {proxy}"` form
cargo test --package mhost-dns --lib test_enable_script_contains_safety_layers test_pid_file_content_format test_enable_script_loudly_fails_when_proxy_missing
# All 3 pass against the buggy version

Fix

Extract production builders and have all tests consume them:

// in platform.rs
pub(crate) fn build_enable_script(
    proxy_path: &Path, dns_port: u16, pid_file: &Path, log_path: &Path, interface: &str,
) -> String { /* the actual script body */ }

pub(crate) fn validate_proxy_binary(path: &Path) -> Result<(), PlatformError> { /* pre-check */ }

Test refactor:

  1. Structural tests consume build_enable_script(...) directly — assert against the production string.
  2. Execution test writes the production script to a temp file, swaps in fake mhost-dns-proxy (long-running script that just sleeps) and fake networksetup (writes args to a log file, exits 0 or non-zero per flag), runs /bin/sh on it with MHOST_RUNTIME_DIR=/tmp/path with space and interface="Thunderbolt Ethernet", asserts:
  3. Cover:
    • Path containing spaces (PID file / log / runtime_dir)
    • Interface containing spaces
    • Delayed proxy startup (kill -0 retry loop with timeout)
    • Proxy bind failure (early exit before bind)
    • networksetup failure post-proxy-startup (transactional cleanup)

Linked

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdns-modeDNS mode (本地 DNS server) 相关问题

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions