-
Notifications
You must be signed in to change notification settings - Fork 1.6k
unittests: convert detect-base64-data.c to FAIL/PASS API #14058
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
NOTE: This PR may contain new authors. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #14058 +/- ##
=======================================
Coverage 84.43% 84.44%
=======================================
Files 1011 1011
Lines 272251 272254 +3
=======================================
+ Hits 229876 229904 +28
+ Misses 42375 42350 -25
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
|
Memory leaks in the failure path are acceptable, just not in the pass-path. |
src/detect-base64-data.c
Outdated
FAIL_IF_NOT(sm->type == DETECT_BASE64_DECODE); | ||
FAIL_IF_NULL(de_ctx->sig_list->init_data->smlists[DETECT_SM_LIST_BASE64_DATA]); | ||
|
||
SigGroupCleanup(de_ctx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while doing these conversions a few other cleanups can be done:
- declare variables on first use
- remove calls to
SigGroupCleanup
andSigCleanSignatures
(redundant asDetectEngineCtxFree
handles this) - replace
SigInit
withDetectEngineAppendSig
Could you update these tests to incorporate this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I’ll get to it immediately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution! The changes are looking OK but we need to work on the commit message. Please make sure to read the contribution guidelines https://docs.suricata.io/en/suricata-8.0.1/devguide/contributing/code-submission-process.html#commits
You could also check out previous contributions around the same issue: 4a98c4bde for examples. 😉
oops. misread. Sorry. |
The unit tests for the 'detect-base64-data.c' keyword were refactored to align with coding style guidelines. Changes include: - replaced SigInit with DetectEngineAppendSig in unit tests. - variables are now declared at the point of their first use. - remove calls to SigGroupCleanup and SigCleanSignatures. Ticket: OISF#6320
src/detect-base64-data.c
Outdated
|
||
de_ctx->flags |= DE_QUIET; | ||
de_ctx->sig_list = SigInit(de_ctx, | ||
de_ctx->sig_list = DetectEngineAppendSig(de_ctx, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DetectEngineAppendSig
does the list append itself, so you'd use Signature *s = DetectEngineAppendSig...
here, followed by FAIL_IF_NULL(s);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As per the other reviewers' comments.
The function DetectEngineAppendSig() returns a pointer to the newly created Signature object. The unit tests DetectBase64DataSetupTest01 and DetectBase64DataSetupTest04 were incorrectly relying on de_ctx->sig_list to reference the parsed signature. This commit updates both tests to explicitly capture the Signature *s returned by DetectEngineAppendSig and use this specific pointer for all validation checks, ensuring test stability and correct API usage. Ticket: OISF#6320
src/detect-base64-data.c
Outdated
printf("sm->type should be DETECT_BASE64_DECODE: "); | ||
goto end; | ||
} | ||
SigMatch *sm = de_ctx->sig_list->init_data->smlists[DETECT_SM_LIST_PMATCH]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now that we have the s
pointer, it's cleaner and clearer to use this here SigMatch *sm = s->init_data->smlists[DETECT_SM_LIST_PMATCH];
NOTE: This PR may contain new authors. |
…) for accessing the rule's initial data (`s->init_data`). Ticket: OISF#6320
Title: Convert
base64_data
unit tests to FAIL/PASS APIDescription
This PR refactors the unit tests in
detect-base64-data.c
to strictly adhere to the Suricata unit testing API (util-unittest.h
).The functions
DetectBase64DataSetupTest01
andDetectBase64DataSetupTest04
were updated to replace internal C failure logic (if (cond) { printf(...); goto end; }
) with immediate assertions using theFAIL/PASS
macros.Issue informartion
Issue 6320: https://redmine.openinfosecfoundation.org/issues/6320
Specific Changes:
FAIL_IF_NULL()
orFAIL_IF_NOT()
.PASS
).Note on Memory Safety Policy:
Following the project's guidance that memory leaks are acceptable on unit test failure paths (as the process terminates immediately), I removed the resource-safe
goto end
pattern. This ensures that the code fully uses the immediate exit behavior of theFAIL/PASS
API.Testing
Verified by running the filtered unit tests: