#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Self-test the static checker against a deliberately BAD source copy. Generates a copy of the real backend with `net_close_sock` injected into the IEC104_Net_Close CLIENT branch, runs check_static.py.main() on the copy, and asserts S1 fails there while the real source still passes. The product source is never edited; the mutant stays under results/static_mutant/. """ import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import check_static # noqa: E402 ROOT = os.path.dirname(os.path.dirname(os.path.dirname(HERE))) SRC = check_static.SRC OUT = os.path.join(HERE, "results", "static_mutant") MUTANT = os.path.join(OUT, "iec104_net_mcu_BAD.c") NEEDLE = " client_slot_drop(slot);\n return result(IEC104_NET_OK, 0, native_error);\n }\n /* LISTEN 分支" def main(): os.makedirs(OUT, exist_ok=True) with open(SRC, "r", encoding="utf-8") as fh: text = fh.read() assert text.count(NEEDLE) == 1, "Close CLIENT branch marker not unique" bad = text.replace( NEEDLE, " (void)net_close_sock(net_get_sock(slot->fd)); /* BAD mutant */\n" + NEEDLE, 1, ) assert bad != text, "mutant injection did not change the copy" with open(MUTANT, "w", encoding="utf-8", newline="\n") as fh: fh.write(bad) real_rc = check_static.main() mutant_rc = check_static.main(MUTANT, OUT) with open(os.path.join(OUT, "selftest.txt"), "w", encoding="utf-8") as fh: fh.write("real_rc={}\nmutant_rc={}\n".format(real_rc, mutant_rc)) fh.write("real source: {}\nmutant: {}\n".format(SRC, MUTANT)) print("static-checker selftest: real_rc={} mutant_rc={}".format(real_rc, mutant_rc)) return 0 if real_rc == 0 and mutant_rc != 0 else 1 if __name__ == "__main__": raise SystemExit(main())