system/nxinit: add cmocka unit tests for parser/action/service - #3755
Draft
JianyuWang0623 wants to merge 2 commits into
Draft
system/nxinit: add cmocka unit tests for parser/action/service#3755JianyuWang0623 wants to merge 2 commits into
JianyuWang0623 wants to merge 2 commits into
Conversation
init_parse_config_lines() had a dead early "continue" for a truly empty line (buf == "\0") that skipped the memmove() bookkeeping its sibling whitespace-only-line branch performs. When a real empty line appeared mid-buffer, subsequent bytes were never shifted to the front of the working buffer, corrupting the remaining-length tracking and silently dropping every line after it for that refill chunk. The whitespace-skip loop right below already handles the empty-string case correctly (the loop body never executes, so it falls straight into the "only whitespace" -> memmove -> continue path), so the buggy early exit is simply redundant and removed. Assisted-by: GitHubCopilot:claude-sonnet-5 Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
Add a test/ subdirectory (mirroring the apps/system/uorb/test/
layout) with cmocka-based unit tests covering the logic most prone
to regression in NxInit:
- init_parse_arguments(): plain/quoted arguments, the "--" separator
vs. "--option" long options (regression coverage for a previously
fixed bug), and argv-capacity truncation.
- init_parse_config_file()/init_parse_config_lines(): section
routing, blank/whitespace-only line skipping, unknown-section
rejection, over-length line rejection, and a line that straddles
two read-buffer refills.
- Action event matching (init_action_parse()/
init_action_trigger_event()): exact match, invert ("!="), fnmatch
wildcards, and AND semantics across multiple events per action
(the latter needs CONFIG_SYSTEM_NXINIT_ACTION_EVENTS_MAX > 1 and
is skipped, not failed, at the default value of 1).
- Service conflict detection (init_service_parse()/
init_service_check()): duplicate service name rejection, the
"override" option replacing an earlier duplicate instead of
failing, and the SERVICE_ARGS_MAX boundary.
Tests exercise only the existing public parser.h/action.h/service.h
API; no production symbols were made non-static. The test sources
compile action.c/parser.c/service.c a second time into a separate
"nxinit_unit_test" program, gated behind the new
CONFIG_SYSTEM_NXINIT_TEST (depends on TESTING_CMOCKA); the default
"init" program is unaffected.
While building this suite, a real bug was found in
init_parse_config_lines() and fixed in a separate commit.
Assisted-by: GitHubCopilot:claude-sonnet-5
Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
JianyuWang0623
force-pushed
the
nxinit-unit-tests-upstream
branch
2 times, most recently
from
August 25, 2026 14:40
3352764 to
1aacc30
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix a real bug in
init_parse_config_lines(): a dead earlycontinuefor a truly-empty line (buf == "\0") skipped thememmove()bookkeeping its sibling whitespace-only-line branch performs, corrupting the remaining-length tracking and silently dropping every subsequent line in that refill chunk. The whitespace-skip loop right below already handles the empty-string case correctly, so the buggy early exit is simply redundant and removed.Add a
test/subdirectory (mirroringapps/system/uorb/test/) with cmocka-based unit tests covering the logic most prone to regression in NxInit:init_parse_arguments(): plain/quoted arguments,--separator vs.--optionlong options (regression coverage for a previously fixed bug), argv-capacity truncation.init_parse_config_file()/init_parse_config_lines(): section routing, blank/whitespace-only line skipping, unknown-section rejection, over-length line rejection, and a line straddling two read-buffer refills.init_action_parse()/init_action_trigger_event()): exact match, invert (!=), fnmatch wildcards, AND semantics across multiple events per action (needsCONFIG_SYSTEM_NXINIT_ACTION_EVENTS_MAX > 1, skipped rather than failed at the default value of 1).init_service_parse()/init_service_check()): duplicate service name rejection, override replacing an earlier duplicate,SERVICE_ARGS_MAXboundary.Tests exercise only the existing public
parser.h/action.h/service.hAPI; no production symbols were made non-static. Test sources compileaction.c/parser.c/service.ca second time into a separatenxinit_unit_testprogram, gated behind newCONFIG_SYSTEM_NXINIT_TEST(depends onTESTING_CMOCKA); the defaultinitprogram is unaffected.Opened as draft for early review/CI feedback.
Impact
Testing
Build Host: Ubuntu 22.04 LTS x86_64, gcc 13.4.0 (Ubuntu 13.4.0-6ubuntu1
22ppa2)Target: sim:citest (nuttx
simconfig, no cross toolchain needed; NxInit is pure apps-layer logic)nxstyle/checkpatch.shpass on all changed files.Actual
nxinit_unit_testruntime output onsim(default Kconfig,ACTION_EVENTS_MAX=1):With
CONFIG_SYSTEM_NXINIT_ACTION_EVENTS_MAX=4,test_nxinit_action_event_and_semanticsruns and passes (17/17 PASSED), confirming the AND-semantics test is not dead code.Both commits were re-verified by
git cherry-pick-ing directly onto currentapache/master(zero conflicts) before opening this PR, since they were originally authored on a branch that has since been squash-merged upstream via #3750.