Skip to content

test: kill the surviving mutants in the Livestatus backend, commands and host import - #83

Merged
somethingwithproof merged 10 commits into
developfrom
test/npc-mutation-coverage
Dec 4, 2026
Merged

somethingwithproof merged 10 commits into
developfrom
test/npc-mutation-coverage

Conversation

@somethingwithproof

Copy link
Copy Markdown
Member

Adds unit tests for code merged in #75. Tests only: no production file is
changed.

Fixes #82

What the tests pin

  • NpcLql value conversion and the 4096 byte bound, the Limit: line, and the
    Livestatus client's header strictness, size and time clamps, multi-chunk
    reads, error message trimming, connection failure handling and the
    command wire line (a raw mode was added to the fake server for this)
  • NagiosCmd integer normalisation and its 2147483647 bound, the 255 and 1024
    character limits, and the downtime time and duration checks
  • every field of every row returned by NpcLivestatusBackend, the object id
    lookups (type, chunking at 500, 128 character names), sorting, paging, the
    state and search filters, and the group listings
  • the exact HTML of the import list, confirmation and denial, the request id
    and template limits, the import key handling, and the row shading
  • the layout frame text and encoding, the sync log lines and device arguments,
    and the JSON answers of the small controllers

Results

Infection 0.35.15, changed lines against the pre-merge base, 1743 mutants in
the production files plus 7 in controllers/servicegroups.php.

Unit suite alone. These figures were measured before the final two commits,
which only add tests, so they can only rise; the mutants those commits target
were re-run one by one and are killed (see below):

before after
mutants 1743 1743
killed (incl. 4 timeouts) 1177 1608
escaped 492 61
not covered by a unit test 74 74
MSI 67.5 % 92.3 %
covered-code MSI 70.5 % 96.3 %

The 74 uncovered mutants are mostly method visibility changes on declaration
lines. Every escaped and uncovered mutant was then run against the whole suite
(Security, Unit, Integration and E2E, with MariaDB and Naemon 1.6.0; a control
run of the unmutated tree after every 25 mutants stayed green). That killed
the 74 uncovered mutants and some of the escapes, leaving 72 escaped.
The last commit kills 6 more (verified one by one), so 66 remain:

after, full suite
mutants 1750 (with the 7 in servicegroups.php)
escaped 66
MSI and covered-code MSI 96.2 %

Remaining 66 survivors

None can be killed by a test that is deterministic and does not change
production code.

Equivalent (the output cannot differ):

  • casts on values that are only concatenated or compared numerically
    (backend 345, 453, 1006, 1012, 1073, 1105, 1109, 1116; import 82, 216, 218,
    259, 279, 323; nagioscmd 1783, 1788, 1813, 1818; livestatus 295, 419)
  • sort direction sign or magnitude (backend 506, 515, 1085, 1092) and the
    array_fill start index (852, 1067)
  • array_map('intval') and array_values removal where array keys convert
    anyway (backend 528, 969; import 244)
  • a null return that equals 0 (backend 808, 1096) and datetime(-1) (646)
  • the default sort is already entry_time (backend 504); numeric-string
    comparison (1089)
  • the boolean case falls through to the integer case (nagioscmd 1781); $i--
    alternates row shading the same way (import 83, 158)
  • limit default 0 or -1 both omit the Limit: line (livestatus 210, 326)
  • the tcp:// prefix, because stream_socket_client() defaults to TCP (377);
    fread() chunk sizes (434), which the reassembled answer hides
  • backend 1096 (a tie returning -1): not observed to change the order on
    PHP 8's stable sort, and no proof of equivalence was made.
  • finally unwrapping that only closes a handle (livestatus 286, 356, 385,
    409), whose effect is a leaked descriptor a test cannot see

Socket timing (depends on peer reset and buffer timing, so any assertion would
be flaky): livestatus 353, 382, 403, 405, 406 (two), 412, 415, 416,
441, 442 and the stream timeout arithmetic at 458. Targeted timing tests added
here (delayed answer within the limit, silent peer giving up after about one
second) kill the deadline mutants at 283.

Not observable from tests: import 363, the removal of Cacti's
general_header() call, which the tests replace with a stub.

The lists are by source line; several mutants can share a line.

Files that produced no mutants

  • npc.php is top-level front-controller code; Infection only mutates code
    inside functions and methods, so it yields none in any configuration. The
    RealServerTest E2E suite exercises it.
  • controllers/servicegroups.php is not loaded by any unit test, so it has no
    unit coverage entry and is skipped unless --with-uncovered is used. With
    that flag it yields 7 mutants; the full suite kills all 7.

Suite

Full suite on this branch with the merged code (Security, Unit, Integration and
E2E; MariaDB 11.8 and Naemon 1.6.0 in Docker): 1206 passed and 1 skipped on PHP
8.2 and on PHP 8.3, 0 failed. The skip is the js/npc.js test, which needs
node; the test image has none. With node installed it runs, giving 1207
passed and 0 skipped. Unit and Security alone pass on PHP 8.2, 8.3 and 8.5
(1018 passed, 1 skipped for the same reason), also in random order with 20
seeds on 8.3 and one seed on 8.2 and 8.5.

Notes

  • Infection's own test runner cannot be used as is: the Pest suite is not
    recognised by its PHPUnit output parser and its include interceptor breaks
    the file and socket tests. Infection generated the mutants and a separate
    runner ran them against real files with the covering tests.
  • The tests share one stand-in for api_device_save() (tests/Unit/support/cacti-stubs.php), with the
    parameter names, order and defaults of Cacti's function, together with the
    page frame and CSRF stubs. This also removes an order dependence between
    ImportControllerTest and the layout tests that made the former fail under
    random ordering (--order-by=random, for example seeds 0, 5 and 6).

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
…ection

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Replace the per-file api_device_save() stand-ins by one stub with the
signature of lib/api_device.php, recorded by parameter name, and move the
page frame and CSRF stubs into the same file so the result no longer depends
on which test file runs first. Reset the denied realms before each layout
test. Widen the silent-server bound, take the process status time from a
range captured around the call, and require the delayed server to delay.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
SonarCloud reports 8.4% duplicated lines on new code. This removes the
repetition it found without changing what any test asserts.

BackendMappingTest: the host, service and servicegroup row expectations
listed the same run of status keys three times. The keys now live in one
ordered list (npcm_status_keys) and npcm_status_row() pairs them with the
values, so key order is unchanged and a missing or extra value is an
error. The service and servicegroup tests share one expected set of
service values. The two tests that built the same two-group backend now
call npcm_two_group_backend(). All rows are still compared whole with
toBe().

ImportControllerTest and ImportMutationTest: the identical Cacti stub
setup, per-test reset and host fixture move to
tests/Unit/support/import-controller.php, loaded by both files.

The Unit suite runs the same 958 tests and 5649 assertions before and
after.

Refs #83

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

@somethingwithproof
somethingwithproof merged commit 8eba1a9 into develop Dec 4, 2026
9 checks passed
@somethingwithproof
somethingwithproof deleted the test/npc-mutation-coverage branch December 4, 2026 09:45
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.

Tests do not pin the exact output of the Livestatus backend, command validation or host import

2 participants