Files
spine/tests/unit/test_util_strings.c
Thomas Vincent 898d8c692c fix(privileges): drop root before input and retain owned ICMP sockets (#639)
Open and own the required ICMP sockets before permanently dropping
setuid root, then parse input without elevated identity. Close-on-exec
socket ownership, shared IPv4/IPv6 dispatch and fault-injected
identity/capability calls are covered. The owned fixture tests check
deny/allow behavior without contacting external devices.

Step 2/14 of the complete C17/native-correctness series. Base: the
preceding series branch; merge/review in order so this PR shows only its
own step.

Earlier validation, before this review follow-up, in isolated copied
snapshots on Linux arm64:

- Clean GCC build and make check: 13 passed, 1 existing prerequisite
skips, 0 failures.
- ASan/UBSan: 12 passed, 2 existing privilege/allocator-interposition
prerequisite skips, 0 failures; no sanitizer findings.
- Production source/profile integrity verification against disposable
MariaDB and loopback SNMP fixtures passed.
- Release archive extracted into a separate source tree and built/tested
out of tree; fuzz checks passed.

Earlier local profiles retain source hashes for the copied validation
tree; GitHub checks validate the published commit. No production
database/device, host installation or live privilege change is part of
validation. Existing prerequisite skips were retained; no test or
finding was suppressed to obtain a pass.


Series navigation: [Series
index](https://github.com/Cacti/spine/pull/638); [previous
step](https://github.com/Cacti/spine/pull/638). [Next
step](https://github.com/Cacti/spine/pull/640).

Review follow-up (2026-10-09): Restacked on the corrected preceding
branches. The SNMP status fix is introduced in #638, the
reindex/completion fixes in #640, and focused regression coverage in
#650.

Restacked tip validation: isolated Linux arm64 build and make check
passed (18 passed, 1 setuid prerequisite skip, no failures).
GitHub checks validate the updated published commits; earlier
profile/archive/sanitizer evidence above belongs to the previous
revisions.

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
2026-10-10 21:08:52 -04:00

63 lines
2.0 KiB
C

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define UNIT_TESTING
#include "../../common.h"
#include "../../spine.h"
/* Define globals that Spine expects */
config_t set;
double start_time;
char config_paths[CONFIG_PATHS][BUFSIZE];
int *debug_devices = NULL;
/* Mock functions needed by util.c */
int db_connect(int type, MYSQL *mysql) { return TRUE; }
void db_disconnect(MYSQL *mysql) {}
MYSQL_RES *db_query(MYSQL *mysql, int type, const char *query) { return NULL; }
void db_free_result(MYSQL_RES *result) {}
int db_insert(MYSQL *mysql, int type, const char *query) { return 0; }
void db_escape(MYSQL *mysql, char *output, int max_size, const char *input) {}
int append_hostrange(char *obuf, size_t capacity, const char *colname) { return 0; }
int parse_logdest(const char *res, int default_dest) { return 0; }
const char *printable_logdest(int dest) { return ""; }
void php_close(int php_process) {}
int ping_icmp_open_shared(void) { return 0; }
int ping_icmp_shared_available(void) { return 0; }
/* Include the actual source file */
#include "../../util.c"
static void test_strpos_found(void **state) {
const char *haystack = "The quick brown fox";
assert_int_equal(strpos(haystack, "quick"), 4);
assert_int_equal(strpos(haystack, "The"), 0);
assert_int_equal(strpos(haystack, "fox"), 16);
}
static void test_strpos_not_found(void **state) {
const char *haystack = "The quick brown fox";
assert_int_equal(strpos(haystack, "lazy"), -1);
assert_int_equal(strpos(haystack, "THE"), -1); /* Case sensitive */
}
static void test_strpos_empty(void **state) {
assert_int_equal(strpos("", "something"), -1);
assert_int_equal(strpos("something", ""), 0);
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_strpos_found),
cmocka_unit_test(test_strpos_not_found),
cmocka_unit_test(test_strpos_empty),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}