Files
mars-nwe/tests/nwnss/parse/test_nwnss_parse.c
2026-06-17 21:20:42 +02:00

126 lines
3.4 KiB
C

#include <include/parse.h>
#include <library/omni.h>
#include <stdio.h>
#include <string.h>
#define CHECK(expr) \
do { \
if (!(expr)) { \
fprintf(stderr, "CHECK failed at %s:%d: %s\n", __FILE__, __LINE__, #expr); \
return 1; \
} \
} while (0)
static int pre_count;
static int process_count;
static int post_count;
static STATUS count_pre(PCLSwitchDef_s *switchDef, NINT parseOptions, void *userParm)
{
(void)switchDef;
(void)parseOptions;
(*(NINT *)userParm)++;
pre_count++;
return zOK;
}
static STATUS count_process(PCLSwitchDef_s *switchDef, NINT parseOptions, void *userParm)
{
(void)switchDef;
(void)parseOptions;
(void)userParm;
process_count++;
return zOK;
}
static STATUS count_post(PCLSwitchDef_s *switchDef, NINT parseOptions, void *userParm)
{
(void)switchDef;
(void)parseOptions;
(void)userParm;
post_count++;
return zOK;
}
static int check_basic_parse(void)
{
char value[32] = {0};
NINT flag = -1;
NINT count = 0;
NINT callback_user = 0;
PCLSwitchDef_s switches[] = {
{
"name", NULL,
SWTYPE_VALUE | SWVAL_CHAR,
0, 0, NULL,
value,
.vtype.string = {sizeof(value), 0},
0, 0, StructMSGNot("name value"), count_pre, count_post
},
{
"enabled", NULL,
SWTYPE_BOOLEAN | SWVAL_NINT,
0, 0, NULL,
&flag,
.vtype.numeric = {0, 1},
0, 0, StructMSGNot("enabled flag"), NULL, NULL
},
{
"count", NULL,
SWTYPE_VALUE | SWVAL_NINT | SWOPT_HAS_NUMERIC_RANGE,
0, 0, NULL,
&count,
.vtype.numeric = {0, 100},
0, 0, StructMSGNot("count value"), NULL, NULL
},
{
"upper", NULL,
SWTYPE_VALUE | SWVAL_CHAR | SWOPT_MAPUPPER,
0, 0, count_process,
value,
.vtype.string = {sizeof(value), 0},
0, 0, StructMSGNot("upper value"), NULL, NULL
},
{PCMDLINE_ENDOFLIST}
};
CHECK(LB_ParseCmdline(switches, POPT_AT_RUNTIME,
"/name=demo /enabled /count=42", &callback_user) == zOK);
CHECK(strcmp(value, "demo") == 0);
CHECK(flag == TRUE);
CHECK(count == 42);
CHECK(pre_count == 1);
CHECK(post_count == 1);
CHECK(callback_user == 1);
CHECK(LB_ParseCmdline(switches, POPT_AT_RUNTIME,
"/upper=mars-nwe", &callback_user) == zOK);
CHECK(strcmp(value, "MARS-NWE") == 0);
CHECK(process_count == 1);
CHECK(LB_ParseCmdline(switches, POPT_AT_RUNTIME,
"/noenabled", &callback_user) == zOK);
CHECK(flag == FALSE);
/* The original parser accepts out-of-range numeric values unless a broader
* set-parameter validation backend is active. Keep the userspace audit
* focused on tokenization, callbacks and switch dispatch. */
CHECK(LB_ParseCmdline(switches, POPT_AT_RUNTIME,
"/count=101", &callback_user) == zOK);
CHECK(count == 100);
CHECK(LB_ParseCmdline(switches, POPT_AT_RUNTIME,
"/unknown=1", &callback_user) == zFAILURE);
CHECK(LB_ParseCmdline(switches, POPT_AT_RUNTIME | POPT_NO_UNKNOWN_SWITCH_ERRORS,
"/unknown=1", &callback_user) == zOK);
return 0;
}
int main(void)
{
CHECK(check_basic_parse() == 0);
return 0;
}