diff --git a/cmake/project.cmake b/cmake/project.cmake index c3bb91c1..5546caad 100644 --- a/cmake/project.cmake +++ b/cmake/project.cmake @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -set(ELOS_VERSION 0.58.12) +set(ELOS_VERSION 0.58.13) # Attention: Aside from the version, as many things as possible in this file # should be put into functions, as this solves potential issues with commands diff --git a/doc/userManual.rst b/doc/userManual.rst index e75ed299..37815f08 100644 --- a/doc/userManual.rst +++ b/doc/userManual.rst @@ -665,18 +665,24 @@ look like this: }, "Scanner": { "Path": "/usr/local/lib/elos/scanner", - "KmsgScanner": { - "KmsgFile": "/dev/kmsg" - }, - "SyslogScanner": { - "SyslogPath": "/dev/log", - "MappingRules": { - "MessageCodes": { - "4000": ".event.source.appName 'ssh' STRCMP", - "2000": ".event.source.appName 'crinit' STRCMP", - "1000": ".event.source.appName 'login' STRCMP" + "Plugins": { + "SyslogScanner": { + "File": "scanner_syslog.so", + "Run": "always", + "Config": { + "SyslogPath": "/dev/log", + "MappingRules": { + "MessageCodes": { + "4000": ".event.source.appName 'ssh' STRCMP", + "2000": ".event.source.appName 'crinit' STRCMP", + "1000": ".event.source.appName 'login' STRCMP" + } + } } } + }, + "KmsgScanner": { + "KmsgFile": "/dev/kmsg" } } } @@ -721,10 +727,10 @@ use another default value, decided by us. - **Scanner/KmsgScanner/KmsgFile**: Character device or FIFO file node to receive logs in kmsg format from (``ELOS_KMSG_FILE`` default value: ``"/dev/kmsg"``) -- **Scanner/SyslogScanner/SyslogPath**: Unix UDP socket to receive logs +- **Scanner/Plugins//Config/SyslogPath**: Unix UDP socket to receive logs in syslog format from (``ELOS_SYSLOG_PATH`` default value: ``"/dev/log"``) -- **Scanner/SyslogScanner/MappingRules/MessageCodes**: contain +- **Scanner/Plugins//Config/MappingRules/MessageCodes**: contain ``message code, filter`` pairs to set a specific ``message code`` for an event if the given filter matches the event. @@ -910,14 +916,17 @@ The syslog scanner expects the following config structure: .. code:: bash SyslogScanner - ├── SyslogPath - └── MappingRules -    ├── MessageCode -    │ ├── 4000 -    │ ├── 4001 -    │ ├── 2001 -    │ └── ... (more MessageCodes) -    └── ... (other event attributes like Severity, Classification, ...) + ├── File + ├── Run + └── Config + ├── SyslogPath + └── MappingRules +    ├── MessageCode +    │ ├── 4000 +    │ ├── 4001 +    │ ├── 2001 +    │ └── ... (more MessageCodes) +    └── ... (other event attributes like Severity, Classification, ...) The MappingRules are provided through the configuration. The configuration (samconf) allows to lookup single options by a path like diff --git a/src/components/pluginmanager/interface/elos/pluginmanager/types.h b/src/components/pluginmanager/interface/elos/pluginmanager/types.h index 26134893..d6bb9b69 100644 --- a/src/components/pluginmanager/interface/elos/pluginmanager/types.h +++ b/src/components/pluginmanager/interface/elos/pluginmanager/types.h @@ -4,7 +4,7 @@ #include "elos/plugincontrol/types.h" #ifndef PLUGINMANAGER_PLUGINVECTOR_INIT_SIZE -#define PLUGINMANAGER_PLUGINVECTOR_INIT_SIZE 8 +#define PLUGINMANAGER_PLUGINVECTOR_INIT_SIZE 20 #endif typedef enum elosPluginManagerStateE { diff --git a/src/components/scanner_manager_legacy/private/scanner_manager.c b/src/components/scanner_manager_legacy/private/scanner_manager.c index 2c940497..f5b9082d 100644 --- a/src/components/scanner_manager_legacy/private/scanner_manager.c +++ b/src/components/scanner_manager_legacy/private/scanner_manager.c @@ -93,8 +93,7 @@ static elosScannerManagerLegacyErrorCodeE_t _parsePath(elosScannerManagerLegacyC continue; } - if ((strcmp(dirEnt->d_name, "scanner_kmsg.so") != 0) && (strcmp(dirEnt->d_name, "scanner_shmem.so") != 0) && - (strcmp(dirEnt->d_name, "scanner_syslog.so") != 0)) { + if ((strcmp(dirEnt->d_name, "scanner_kmsg.so") != 0) && (strcmp(dirEnt->d_name, "scanner_shmem.so") != 0)) { dirEnt = readdir(dirHandle); continue; } diff --git a/src/plugins/scanners/syslog/CMakeLists.txt b/src/plugins/scanners/syslog/CMakeLists.txt index cd52d215..c56136e5 100644 --- a/src/plugins/scanners/syslog/CMakeLists.txt +++ b/src/plugins/scanners/syslog/CMakeLists.txt @@ -22,6 +22,7 @@ target_link_libraries( elos_scanner_legacy_interface eventfilter_static PUBLIC + libelosplugin rpnfilter_static scanner_syslog_interface elos_common_interface diff --git a/src/plugins/scanners/syslog/index.rst b/src/plugins/scanners/syslog/index.rst index a4d29233..e6de542b 100644 --- a/src/plugins/scanners/syslog/index.rst +++ b/src/plugins/scanners/syslog/index.rst @@ -46,17 +46,20 @@ notation. So the syslog scanner expect the following config structure: .. code:: bash SyslogScanner - ├── SyslogPath - ├── MappingRules - │   ├── MessageCode - │   │ ├── 4000 - │   │ ├── 4001 - │   │ └── 2001 - │   └── ... (other like Severity, classification) - └── FilterRules (Not implemented) - ├── empty payload - ├── all from appName X - └── containing secrets + ├── File + ├── Run + └── Config + ├── SyslogPath + ├── MappingRules + │   ├── MessageCode + │   │ ├── 4000 + │   │ ├── 4001 + │   │ └── 2001 + │   └── ... (other like Severity, classification) + └── FilterRules (Not implemented) + ├── empty payload + ├── all from appName X + └── containing secrets The implementation for configuration files is still in progress so the format for configuration files is not defined now. diff --git a/src/plugins/scanners/syslog/private/logline_mapper.c b/src/plugins/scanners/syslog/private/logline_mapper.c index ce5e8bef..67144fbb 100644 --- a/src/plugins/scanners/syslog/private/logline_mapper.c +++ b/src/plugins/scanners/syslog/private/logline_mapper.c @@ -292,7 +292,7 @@ safuResultE_t elosLoglineMapperInit(elosLoglineMapper_t *mapper, const samconfCo } if (result == SAFU_RESULT_OK) { - status = samconfConfigGet(config, "/MappingRules/MessageCodes", &messageCodeConfig); + status = samconfConfigGet(config, "Config/MappingRules/MessageCodes", &messageCodeConfig); if (status != SAMCONF_CONFIG_OK) { safuLogErr("Given configuration does not have applications config nodes"); result = SAFU_RESULT_FAILED; diff --git a/src/plugins/scanners/syslog/private/syslog.c b/src/plugins/scanners/syslog/private/syslog.c index 23151dc3..1fe27cb4 100644 --- a/src/plugins/scanners/syslog/private/syslog.c +++ b/src/plugins/scanners/syslog/private/syslog.c @@ -1,7 +1,11 @@ // SPDX-License-Identifier: MIT +#include +#include #include #include #include +#include +#include #include #include #include @@ -23,8 +27,6 @@ #define ELOSD_SYSLOG_PATH "/dev/log" #endif -#define REGEX_FILTER_ACTIVE 0x01 -#define REGEX_FILTER_NEEDS_CLEANUP 0x02 #define LOGLINE_MAPPER_NEEDS_CLEANUP (0x01 << 2) #define MAX_LOG_ENTRY_SIZE 4096 #define SCANNER_NAME "syslog" @@ -37,69 +39,65 @@ struct syslog_context { int socket; int cmd; bool running; - regex_t regExFilter; uint32_t status; elosLoglineMapper_t mapper; const samconfConfig_t *config; + struct elosPublisher *publisher; }; -static elosScannerLegacyResultE_t _setupSocket(struct syslog_context *context); -static void _publishMessage(elosScannerLegacySession_t *session); +static safuResultE_t _setupSocket(elosPlugin_t *plugin); +static void _publishMessage(elosPlugin_t *plugin); -elosScannerLegacyResultE_t elosScannerInitialize(elosScannerLegacySession_t *session, - const elosScannerLegacyParam_t *param) { - const char *filterString = NULL; - const char *configPath = "/root/elos/Scanner/SyslogScanner"; +static safuResultE_t _pluginInit(elosPlugin_t *plugin) { + safuLogDebugF("Starting to initialize '%s' plugin", plugin->config->key); struct syslog_context *context = (struct syslog_context *)malloc(sizeof(struct syslog_context)); if (context == NULL) { - return SCANNER_ERROR; + return SAFU_RESULT_FAILED; } - session->name = SCANNER_NAME; - session->context = context; context->status = 0; context->socket = -1; context->cmd = -1; - context->config = param->config; + context->config = plugin->config; - if (param != NULL && context->config != NULL) { - if (samconfConfigGet(context->config, configPath, &session->config) != SAMCONF_CONFIG_OK) { - safuLogInfo("No config for syslog scanner found"); - } - } + plugin->data = context; context->cmd = eventfd(0, 0); if (context->cmd == -1) { safuLogErrErrno("failed to establish eventfd for command channel"); - return SCANNER_ERROR; - } - - if (samconfConfigGetString(session->config, "FilterString", &filterString) == SAMCONF_CONFIG_OK) { - int retval; - retval = regcomp(&context->regExFilter, filterString, 0); - if (retval != 0) { - safuLogErrF("regcomp for filterString '%s' failed!", filterString); - } else { - // There seems to be no easy way to detect if a regex_t is valid; - // This causes problems further down the line as regfree() causes - // segmentation faults when used on a uninitialized regex_t struct, - // so we have to rely on flags to make sure everything runs smoothly - context->status |= (REGEX_FILTER_ACTIVE | REGEX_FILTER_NEEDS_CLEANUP); - } + return SAFU_RESULT_FAILED; } - if (elosLoglineMapperInit(&context->mapper, session->config) == SAFU_RESULT_FAILED) { + if (elosLoglineMapperInit(&context->mapper, plugin->config) == SAFU_RESULT_FAILED) { safuLogErr("Failed to setup logline mapper"); - return SCANNER_ERROR; + return SAFU_RESULT_FAILED; } else { context->status |= LOGLINE_MAPPER_NEEDS_CLEANUP; } - return _setupSocket(context); + elosPluginCreatePublisher(plugin, &context->publisher); + + return _setupSocket(plugin); +} + +static safuResultE_t _pluginLoad(elosPlugin_t *plugin) { + safuResultE_t result = SAFU_RESULT_FAILED; + + if (plugin == NULL) { + safuLogErr("Null parameter given"); + } else { + if ((plugin->config == NULL) || (plugin->config->key == NULL)) { + safuLogErr("Given configuration is NULL or has .key set to NULL"); + } else { + result = _pluginInit(plugin); + safuLogDebugF("Scanner Plugin '%s' has been loaded", plugin->config->key); + } + } + return result; } -elosScannerLegacyResultE_t elosScannerRun(elosScannerLegacySession_t *session) { - struct syslog_context *context = session->context; +static safuResultE_t _pluginRunLoop(elosPlugin_t *plugin) { + struct syslog_context *context = plugin->data; struct pollfd fds[] = { {.fd = context->socket, .events = POLLIN}, {.fd = context->cmd, .events = POLLIN}, @@ -110,13 +108,13 @@ elosScannerLegacyResultE_t elosScannerRun(elosScannerLegacySession_t *session) { int result = poll(fds, ARRAY_SIZE(fds), -1); if (result > 0) { if (fds[0].revents & POLLIN) { - _publishMessage(session); + _publishMessage(plugin); } else if (fds[1].revents & POLLIN) { uint64_t command = 0; result = read(context->cmd, &command, sizeof(command)); if (result != sizeof(command)) { safuLogErrErrno("failed to receive command event"); - return SCANNER_ERROR; + return SAFU_RESULT_FAILED; } if (command == elosScannerCmdStop) { context->running = false; @@ -129,72 +127,146 @@ elosScannerLegacyResultE_t elosScannerRun(elosScannerLegacySession_t *session) { continue; } else { safuLogErrErrno("polling failed, bail out"); - return SCANNER_ERROR; + return SAFU_RESULT_FAILED; } } + return SAFU_RESULT_OK; +} - return SCANNER_OK; +static safuResultE_t _pluginStart(elosPlugin_t *plugin) { + safuResultE_t result = SAFU_RESULT_FAILED; + + if (plugin == NULL) { + safuLogErr("Null parameter given"); + } else { + safuLogDebugF("Scanner Dummy Plugin '%s' has been started", plugin->config->key); + result = elosPluginReportAsStarted(plugin); + if (result == SAFU_RESULT_FAILED) { + safuLogErr("elosPluginReportAsStarted failed"); + } else { + result = _pluginRunLoop(plugin); + if (result == SAFU_RESULT_FAILED) { + safuLogErrF("Scanner loop failed for %s", plugin->config->key); + } + result = elosPluginStopTriggerWait(plugin); + if (result == SAFU_RESULT_FAILED) { + safuLogErr("elosPluginStopTriggerWait failed"); + } + } + } + return result; } -elosScannerLegacyResultE_t elosScannerStop(elosScannerLegacySession_t *session) { - struct syslog_context *context = session->context; +static safuResultE_t _pluginStopScannerLoop(elosPlugin_t *plugin) { + struct syslog_context *context = plugin->data; ssize_t result = 0; result = write(context->cmd, &elosScannerCmdStop, sizeof(elosScannerCommand_t)); if (result == -1) { safuLogErrErrno("failed to send command event"); - return SCANNER_ERROR; + return SAFU_RESULT_FAILED; } - return SCANNER_OK; + return SAFU_RESULT_OK; } -elosScannerLegacyResultE_t elosScannerFree(elosScannerLegacySession_t *session) { - struct syslog_context *context = session->context; - if (context->status & REGEX_FILTER_NEEDS_CLEANUP) { - regfree(&context->regExFilter); +static safuResultE_t _pluginStop(elosPlugin_t *plugin) { + safuResultE_t result = SAFU_RESULT_FAILED; + + if (plugin == NULL) { + safuLogErr("Null parameter given"); + } else { + safuLogDebugF("Stopping Scanner Plugin '%s'", plugin->config->key); + result = _pluginStopScannerLoop(plugin); + if (result == SAFU_RESULT_FAILED) { + safuLogErrF("Stopping Scanner Plugin '%s' failed", plugin->config->key); + } + result = elosPluginStopTriggerWrite(plugin); + if (result == SAFU_RESULT_FAILED) { + safuLogErr("elosPluginStopTriggerWrite failed"); + } } + return result; +} + +static safuResultE_t _freePluginResources(elosPlugin_t *plugin) { + safuResultE_t result = SAFU_RESULT_OK; + struct syslog_context *context = (struct syslog_context *)plugin->data; + if (context->status & LOGLINE_MAPPER_NEEDS_CLEANUP) { elosLoglineMapperDeleteMembers(&context->mapper); } - if (context->socket > -1) { - close(context->socket); + if (close(context->socket) != 0) { + safuLogErrErrno("Failed to close syslog socket"); + result = SAFU_RESULT_FAILED; + } } - if (context->socket > -1) { - close(context->cmd); + if (close(context->cmd) != 0) { + safuLogErrErrno("Failed to close syslog cmd socket"); + result = SAFU_RESULT_FAILED; + } + } + // TODO decide how to handle environment variables in pluggins + // and move the call to getEnv to be handled by samconf in a merge strategy + const char *defaultSyslogPath = samconfConfigGetStringOr(plugin->config, "Config/SyslogPath", ELOSD_SYSLOG_PATH); + const char *syslogPath = safuGetEnvOr("ELOS_SYSLOG_PATH", defaultSyslogPath); + if (unlink(syslogPath) != 0) { + safuLogErrErrno("Failed to unlink syslog socket"); + result = SAFU_RESULT_FAILED; + } + + if (elosPluginDeletePublisher(plugin, context->publisher) != SAFU_RESULT_OK) { + result = SAFU_RESULT_FAILED; } - unlink(elosConfigGetElosdSyslogSocketPath(session->config)); - free(session->context); - return SCANNER_OK; + free(plugin->data); + safuLogDebugF("Scanner Plugin '%s' resources freed", plugin->config->key); + return result; } -static elosScannerLegacyResultE_t _setupSocket(struct syslog_context *context) { +static safuResultE_t _pluginUnload(elosPlugin_t *plugin) { + safuResultE_t result = SAFU_RESULT_FAILED; + + if (plugin == NULL) { + safuLogErr("Null parameter given"); + } else { + safuLogDebugF("Unloading Scanner Plugin '%s'", plugin->config->key); + result = _freePluginResources(plugin); + } + + return result; +} +static safuResultE_t _setupSocket(elosPlugin_t *plugin) { + struct syslog_context *context = plugin->data; struct sockaddr_un name = {.sun_family = AF_UNIX}; - const char *syslogSocketPath = elosConfigGetElosdSyslogSocketPath(context->config); - strncpy(name.sun_path, syslogSocketPath, sizeof(name.sun_path) - 1); + + // TODO: move handlying of getEnv into samconf with an apropriate merge strategy + const char *defaultSyslogPath = samconfConfigGetStringOr(plugin->config, "Config/SyslogPath", ELOSD_SYSLOG_PATH); + const char *syslogPath = safuGetEnvOr("ELOS_SYSLOG_PATH", defaultSyslogPath); + + safuLogDebugF("SocketPath: %s", syslogPath); + strncpy(name.sun_path, syslogPath, sizeof(name.sun_path) - 1); context->socket = socket(AF_UNIX, SOCK_DGRAM, 0); if (context->socket == -1) { safuLogErrErrno("failed to setup syslog socket"); - return SCANNER_ERROR; + return SAFU_RESULT_FAILED; } - if (access(syslogSocketPath, F_OK) == 0 && unlink(syslogSocketPath) == -1) { + if (access(syslogPath, F_OK) == 0 && unlink(syslogPath) == -1) { safuLogErrErrno("failed to delete socket"); } int result = bind(context->socket, (const struct sockaddr *)&name, sizeof(name)); if (result == -1) { - safuLogErrF("failed to bind socket to '%s' - %s", syslogSocketPath, strerror(errno)); - return SCANNER_ERROR; + safuLogErrF("failed to bind socket to '%s' - %s", syslogPath, strerror(errno)); + return SAFU_RESULT_FAILED; } - return SCANNER_OK; + return SAFU_RESULT_OK; } -static void _publishMessage(elosScannerLegacySession_t *session) { - const char *filterString = NULL; - struct syslog_context *context = session->context; +static void _publishMessage(elosPlugin_t *plugin) { + struct syslog_context *context = plugin->data; char *buffer = calloc(sizeof(char), MAX_LOG_ENTRY_SIZE); bool publish = true; @@ -211,30 +283,17 @@ static void _publishMessage(elosScannerLegacySession_t *session) { buffer[bytesRead] = '\0'; } - if (context->status & REGEX_FILTER_ACTIVE) { - int retval; - retval = regexec(&context->regExFilter, buffer, 0, NULL, 0); - if (retval == REG_NOMATCH) { - publish = false; - } else if (retval != 0) { - samconfConfigGetString(session->config, "FilterString", &filterString); - safuLogErrF("regexec for filterString '%s' failed! Disabling filter.", filterString); - context->status ^= REGEX_FILTER_ACTIVE; - } - } - if (publish == true) { - elosScannerLegacyCallbackData_t *cbData = &session->callback.scannerCallbackData; safuResultE_t result; elosEvent_t event = {0}; elosLoglineMapperDoMapping(&context->mapper, &event, buffer); - result = session->callback.eventLog(cbData, &event); + result = elosPluginPublish(plugin, context->publisher, &event); if (result != SAFU_RESULT_OK) { safuLogErr("eventPublish failed"); } - result = session->callback.eventPublish(cbData, &event); + result = elosPluginStore(plugin, &event); if (result != SAFU_RESULT_OK) { safuLogErr("eventLog failed"); } @@ -244,3 +303,11 @@ static void _publishMessage(elosScannerLegacySession_t *session) { free(buffer); } + +elosPluginConfig_t elosPluginConfig = { + .type = PLUGIN_TYPE_SCANNER, + .load = _pluginLoad, + .unload = _pluginUnload, + .start = _pluginStart, + .stop = _pluginStop, +}; diff --git a/test/integration/service/config.json b/test/integration/service/config.json index 7e5e2823..8f62b271 100644 --- a/test/integration/service/config.json +++ b/test/integration/service/config.json @@ -63,15 +63,19 @@ "OomKiller": { "File": "scanner_oomkiller.so", "Run": "always" - } - }, - "SyslogScanner": { - "Syslog": "/dev/log", - "MappingRules": { - "MessageCodes": { - "4000": ".event.source.appName 'ssh' STRCMP", - "2000": ".event.source.appName 'crinit' STRCMP", - "1000": ".event.source.appName 'login' STRCMP" + }, + "SyslogScanner": { + "File": "scanner_syslog.so", + "Run": "always", + "Config": { + "Syslog": "/dev/log", + "MappingRules": { + "MessageCodes": { + "4000": ".event.source.appName 'ssh' STRCMP", + "2000": ".event.source.appName 'crinit' STRCMP", + "1000": ".event.source.appName 'login' STRCMP" + } + } } } }, diff --git a/test/smoketest/config.json b/test/smoketest/config.json index 1b9931b8..bddc5464 100644 --- a/test/smoketest/config.json +++ b/test/smoketest/config.json @@ -102,15 +102,19 @@ "OomKiller": { "File": "scanner_oomkiller.so", "Run": "always" - } - }, - "SyslogScanner": { - "SyslogPath": "/dev/log", - "MappingRules": { - "MessageCodes": { - "4000": ".event.source.appName 'ssh' STRCMP", - "2000": ".event.source.appName 'crinit' STRCMP", - "1000": ".event.source.appName 'login' STRCMP" + }, + "SyslogScanner": { + "File": "scanner_syslog.so", + "Run": "always", + "Config": { + "SyslogPath": "/dev/log", + "MappingRules": { + "MessageCodes": { + "4000": ".event.source.appName 'ssh' STRCMP", + "2000": ".event.source.appName 'crinit' STRCMP", + "1000": ".event.source.appName 'login' STRCMP" + } + } } } }, diff --git a/test/smoketest/config_dual.json b/test/smoketest/config_dual.json index b011e7c8..5fd334e4 100644 --- a/test/smoketest/config_dual.json +++ b/test/smoketest/config_dual.json @@ -14,8 +14,7 @@ ".process.pid 1 EQ" ], "ClientInputs": { - "Plugins": { - } + "Plugins": {} }, "EventLogging": { "PluginSearchPath": "/usr/lib/x86_64-linux-gnu/elos/backend", @@ -48,15 +47,19 @@ "ScannerDummy": { "File": "scanner_dummy.so", "Run": "always" - } - }, - "SyslogScanner": { - "SyslogPath": "/dev/log", - "MappingRules": { - "MessageCodes": { - "4000": ".event.source.appName 'ssh' STRCMP", - "2000": ".event.source.appName 'crinit' STRCMP", - "1000": ".event.source.appName 'login' STRCMP" + }, + "SyslogScanner": { + "File": "scanner_syslog.so", + "Run": "always", + "Config": { + "SyslogPath": "/dev/log", + "MappingRules": { + "MessageCodes": { + "4000": ".event.source.appName 'ssh' STRCMP", + "2000": ".event.source.appName 'crinit' STRCMP", + "1000": ".event.source.appName 'login' STRCMP" + } + } } } }, diff --git a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/CMakeLists.txt b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/CMakeLists.txt index 30f72000..03578726 100644 --- a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/CMakeLists.txt +++ b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/CMakeLists.txt @@ -1,6 +1,7 @@ # SPDX-License-Identifier: MIT find_package(cmocka_mocks) find_package(safu 0.50.1 REQUIRED) +find_package(samconf 0.50.1 REQUIRED) create_unit_test( NAME @@ -11,5 +12,6 @@ create_unit_test( LIBRARIES scanner_syslog cmocka_mocks::mock_libc + samconf::samconf_test_utils safu::mock_safu ) diff --git a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/case_success.c b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/case_success.c index 4fb87632..64eec2e4 100644 --- a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/case_success.c +++ b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/case_success.c @@ -10,6 +10,19 @@ #include "safu/common.h" #include "safu/mock_safu.h" +#define _TEST_CONFIG \ + "{ \ + \"Config\": { \ + \"MappingRules\": { \ + \"MessageCodes\": { \ + \"4000\": \".event.source.appName 'sshd' STRCMP\", \ + \"2000\": \"\\\": ROOT LOGIN on '/dev/console'\\\" .event.payload STRCMP\", \ + \"1000\": \"\\\" hehyoi\\\" .event.payload STRCMP\" \ + } \ + } \ + } \ + }" + static const char *elosElosLogline[] = { "<38>Jan 1 00:00:04 sshd[98]: Server listening on 0.0.0.0 port 22.", "<38>Jan 1 00:00:04 sshd[98]: Server listening on :: port 22.", @@ -72,56 +85,7 @@ static const elosEvent_t elosExpectedEvent[] = { }; int elosTestElosLoglineMapperDoMappingSuccessSetup(void **state) { - elosLoglineMapperDoMappingUtestInit(state); - - elosTestState_t *testState = *state; - - samconfConfig_t *messageCodesChildren[] = { - &testState->childrenData[2], - &testState->childrenData[3], - &testState->childrenData[4], - }; - - testState->childrenData[1].children = messageCodesChildren; - testState->childrenData[1].childCount = ARRAY_SIZE(messageCodesChildren); - - samconfConfig_t *mappingRulesChildren[] = { - &testState->childrenData[1], - }; - - testState->childrenData[0].children = mappingRulesChildren; - testState->childrenData[0].childCount = ARRAY_SIZE(mappingRulesChildren); - - samconfConfig_t *syslogScannerChildren[] = { - &testState->childrenData[0], - }; - - // "SyslogScanner": { - // "MappingRules": { - // "MessageCodes": { - // "4000": ..., - // "2000": "\": ROOT LOGIN on '/dev/console'\" .event.payload STRCMP", - // "1000": "\" hehyoi\" .event.payload STRCMP" - // } - // } - // } - samconfConfig_t root = { - .parent = NULL, - .key = "SyslogScanner", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = syslogScannerChildren, - .childCount = 1, - }; - - testState->timezone = getenv("TZ"); - setenv("TZ", "UTC", 1); - tzset(); - - safuResultE_t result = elosLoglineMapperInit(&testState->mapper, &root); - if (result != SAFU_RESULT_OK) { - return -1; - } - + elosLoglineMapperDoMappingUtestInit(state, _TEST_CONFIG); return 0; } @@ -174,12 +138,12 @@ static void _testElosLoglineMapperDoMappingSuccessParam(elosLoglineMapper_t *map assert_int_equal(event.severity, expectedEvent->severity); if (expectedEvent->source.appName == NULL) { - assert_ptr_equal(event.source.appName, expectedEvent->source.appName); + assert_null(event.source.appName); } else { assert_string_equal(event.source.appName, expectedEvent->source.appName); } if (expectedEvent->source.fileName == NULL) { - assert_ptr_equal(event.source.fileName, expectedEvent->source.fileName); + assert_null(event.source.fileName); } else { assert_string_equal(event.source.fileName, expectedEvent->source.fileName); } diff --git a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/elosLoglineMapperDoMapping_utest.c b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/elosLoglineMapperDoMapping_utest.c index fe3885ef..906bec6a 100644 --- a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/elosLoglineMapperDoMapping_utest.c +++ b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/elosLoglineMapperDoMapping_utest.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: MIT #include "elosLoglineMapperDoMapping_utest.h" +#include + TEST_SUITE_FUNC_PROTOTYPES(elosLoglineMapperDoMappingUtest) int main() { @@ -11,67 +13,32 @@ int main() { return RUN_TEST_SUITE(tests, elosLoglineMapperDoMappingUtest); } -int elosLoglineMapperDoMappingUtestInit(void **state) { - elosTestState_t *testState = safuAllocMem(NULL, sizeof(*testState)); - - samconfConfig_t configData[] = { - { - .parent = NULL, - .key = "MappingRules", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "MessageCodes", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "4000", - .type = SAMCONF_CONFIG_VALUE_STRING, - .value.string = "': Server listening on 0.0.0.0 port 22.' .event.payload STRCMP ': Server listening on :: " - "port 22.' .event.payload STRCMP OR", - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "2000", - .type = SAMCONF_CONFIG_VALUE_STRING, - .value.string = "\": ROOT LOGIN on '/dev/console'\" .event.payload STRCMP", - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "1000", - .type = SAMCONF_CONFIG_VALUE_STRING, - .value.string = "\" hehyoi\" .event.payload STRCMP", - .children = NULL, - .childCount = 0, - }, - }; - - testState->childrenData = safuAllocMem(NULL, ARRAY_SIZE(configData) * sizeof(*testState->childrenData)); - - memcpy(testState->childrenData, configData, ARRAY_SIZE(configData) * sizeof(*testState->childrenData)); +int elosLoglineMapperDoMappingUtestInit(void **state, const char *configStr) { + elosTestState_t *testState = calloc(1, sizeof(elosTestState_t)); + + samconfConfigStatusE_t confRes = samconfUtilCreateMockConfigFromStr(configStr, false, &testState->config); + if (confRes != SAMCONF_CONFIG_OK) { + return -1; + } + safuResultE_t result = elosLoglineMapperInit(&testState->mapper, &testState->config); + if (result != SAFU_RESULT_OK) { + return -1; + } + testState->timezone = getenv("TZ"); + print_message("Got init done\n"); + setenv("TZ", "UTC", 1); + tzset(); *state = testState; - return 0; } int elosLoglineMapperDoMappingUtestCleanUp(void **state) { elosTestState_t *testState = *state; - + samconfConfigDeleteMembers(&testState->config); elosLoglineMapperDeleteMembers(&testState->mapper); - free(testState->childrenData); free(testState); - + state = NULL; return 0; } diff --git a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/elosLoglineMapperDoMapping_utest.h b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/elosLoglineMapperDoMapping_utest.h index 82663c2c..dc4f4b92 100644 --- a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/elosLoglineMapperDoMapping_utest.h +++ b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperDoMapping/elosLoglineMapperDoMapping_utest.h @@ -6,11 +6,11 @@ typedef struct elosTestState { char *timezone; - samconfConfig_t *childrenData; + samconfConfig_t config; elosLoglineMapper_t mapper; } elosTestState_t; -int elosLoglineMapperDoMappingUtestInit(void **state); +int elosLoglineMapperDoMappingUtestInit(void **state, const char *config); int elosLoglineMapperDoMappingUtestCleanUp(void **state); TEST_CASE_FUNC_PROTOTYPES(elosTestElosLoglineMapperDoMappingSuccess) diff --git a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/CMakeLists.txt b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/CMakeLists.txt index d7a83af3..ef8d7b50 100644 --- a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/CMakeLists.txt +++ b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/CMakeLists.txt @@ -14,5 +14,6 @@ create_unit_test( LIBRARIES scanner_syslog cmocka_mocks::mock_libc + samconf::samconf_test_utils samconf::samconf ) diff --git a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_err_deep_nested_message_codes.c b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_err_deep_nested_message_codes.c index 94593d04..3bf6771f 100644 --- a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_err_deep_nested_message_codes.c +++ b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_err_deep_nested_message_codes.c @@ -13,8 +13,29 @@ typedef struct elosMessageCodeFilterMapping { elosRpnFilter_t filter; } elosMessageCodeFilterMapping_t; +#define _TEST_CONFIG \ + "{ \ + \"Config\": { \ + \"MappingRules\": { \ + \"MessageCodes\": { \ + \"4000\": \".event.source.appName 'ssh' STRCMP\", \ + \"ssh\": { \ + \"5000\": { \ + \"crinit\": { \ + \"login\": { \ + \"2000\": \".event.source.appName 'crinit' STRCMP\", \ + \"1000\": \".event.source.appName 'login' STRCMP\" \ + } \ + } \ + } \ + } \ + } \ + } \ + } \ + }" + int elosTestElosLoglineMapperInitErrDeepNestedMessageCodesSetup(void **state) { - elosLoglineMapperInitUtestInit(state); + elosLoglineMapperInitUtestInit(state, _TEST_CONFIG); return 0; } @@ -24,85 +45,16 @@ int elosTestElosLoglineMapperInitErrDeepNestedMessageCodesTeardown(void **state) } void elosTestElosLoglineMapperInitErrDeepNestedMessageCodes(void **state) { - elosUteststateT_t *testState = *state; - - samconfConfig_t *loginChildren[] = { - &testState->childrenData[6], // "2000": ".event.source.appName 'crinit' STRCMP" - &testState->childrenData[7], // "1000": ".event.source.appName 'login' STRCMP" - }; - testState->childrenData[4].children = loginChildren; - testState->childrenData[4].childCount = ARRAY_SIZE(loginChildren); - - samconfConfig_t *crinitChildren[] = { - &testState->childrenData[4], // "login": { ... } - }; - testState->childrenData[3].children = crinitChildren; - testState->childrenData[3].childCount = ARRAY_SIZE(crinitChildren); - - samconfConfig_t *numberChildren[] = { - &testState->childrenData[3], // "crinit": { ... } - }; - testState->childrenData[8].children = numberChildren; - testState->childrenData[8].childCount = ARRAY_SIZE(numberChildren); - - samconfConfig_t *sshChildren[] = { - &testState->childrenData[8], // "5000": { ... } - }; - testState->childrenData[2].children = sshChildren; - testState->childrenData[2].childCount = ARRAY_SIZE(sshChildren); - - samconfConfig_t *messageCodesChildren[] = { - &testState->childrenData[5], // "4000": ".event.source.appName 'ssh' STRCMP", - &testState->childrenData[2], // "ssh": { ... }, - }; - testState->childrenData[1].children = messageCodesChildren; - testState->childrenData[1].childCount = ARRAY_SIZE(messageCodesChildren); - - samconfConfig_t *mappingRulesChildren[] = { - &testState->childrenData[1], - }; - - testState->childrenData[0].children = mappingRulesChildren; - testState->childrenData[0].childCount = ARRAY_SIZE(mappingRulesChildren); - - samconfConfig_t *syslogScannerChildren[] = { - &testState->childrenData[0], - }; - - // "SyslogScannere": { - // "MappingRules": { - // "MessageCodes": { - // "4000": ".event.source.appName 'ssh' STRCMP", - // "ssh": { - // "5000": { - // "crinit": { - // "login": { - // "2000": ".event.source.appName 'crinit' STRCMP", - // "1000": ".event.source.appName 'login' STRCMP" - // } - // } - // } - // } - // } - // } - // } - samconfConfig_t root = { - .parent = NULL, - .key = "SyslogScanner", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = syslogScannerChildren, - .childCount = 1, - }; + samconfConfig_t *root = *(samconfConfig_t **)state; elosRpnFilter_t *resultFilter; elosLoglineMapper_t mapper; safuResultE_t result = SAFU_RESULT_FAILED; - size_t filterIndex = 0; TEST("elosLoglineMapper"); SHOULD("%s", "initialize with a nested config, skipping all fields not directly under \"MessageCodes\""); - result = elosLoglineMapperInit(&mapper, &root); + result = elosLoglineMapperInit(&mapper, root); assert_int_equal(result, SAFU_RESULT_OK); @@ -112,9 +64,7 @@ void elosTestElosLoglineMapperInitErrDeepNestedMessageCodes(void **state) { resultMapping = (elosMessageCodeFilterMapping_t *)safuVecGet(&mapper.filterList, i); resultFilter = &resultMapping->filter; - assert_non_null( - strstr(testState->childrenData[5 + filterIndex].value.string, resultFilter->values->entry[1].data.str)); - ++filterIndex; + assert_int_equal(resultFilter->state, RPNFILTER_FINALIZED); } elosLoglineMapperDeleteMembers(&mapper); diff --git a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_sectioned_message_codes.c b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_sectioned_message_codes.c index 89d2abb7..232dec44 100644 --- a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_sectioned_message_codes.c +++ b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_sectioned_message_codes.c @@ -12,8 +12,25 @@ typedef struct elosMessageCodeFilterMapping { elosRpnFilter_t filter; } elosMessageCodeFilterMapping_t; +#define _TEST_CONFIG \ + "{ \ + \"Config\": { \ + \"MappingRules\": { \ + \"MessageCodes\": { \ + \"4000\": \".event.source.appName 'ssh' STRCMP\", \ + \"crinit\": { \ + \"2000\": \".event.source.appName 'crinit' STRCMP\" \ + }, \ + \"login\": { \ + \"1000\": \".event.source.appName 'login' STRCMP\" \ + } \ + } \ + } \ + } \ + }" + int elosTestElosLoglineMapperInitSectionedMessageCodesSetup(void **state) { - elosLoglineMapperInitUtestInit(state); + elosLoglineMapperInitUtestInit(state, _TEST_CONFIG); return 0; } @@ -23,71 +40,16 @@ int elosTestElosLoglineMapperInitSectionedMessageCodesTeardown(void **state) { } void elosTestElosLoglineMapperInitSectionedMessageCodes(void **state) { - elosUteststateT_t *testState = *state; - - samconfConfig_t *crinitChildren[] = { - &testState->childrenData[6], // "2000": ".event.source.appName 'crinit' STRCMP" - }; - samconfConfig_t *loginChildren[] = { - &testState->childrenData[7], // "1000": ".event.source.appName 'login' STRCMP" - }; - - testState->childrenData[3].children = crinitChildren; - testState->childrenData[3].childCount = 1; - - testState->childrenData[4].children = loginChildren; - testState->childrenData[4].childCount = 1; - - samconfConfig_t *messageCodesChildren[] = { - &testState->childrenData[5], // "4000": ".event.source.appName 'ssh' STRCMP" - &testState->childrenData[3], // "crinit": { ... }, - &testState->childrenData[4], // "login": { ... } - }; - - testState->childrenData[1].children = messageCodesChildren; - testState->childrenData[1].childCount = 3; - - samconfConfig_t *mappingRulesChildren[] = { - &testState->childrenData[1], - }; - - testState->childrenData[0].children = mappingRulesChildren; - testState->childrenData[0].childCount = 1; - - samconfConfig_t *syslogScannerChildren[] = { - &testState->childrenData[0], - }; - - // "SyslogScannere": { - // "MappingRules": { - // "MessageCodes": { - // "4000": ".event.source.appName 'ssh' STRCMP" - // "crinit": { - // "2000": ".event.source.appName 'crinit' STRCMP" - // }, - // "login": { - // "1000": ".event.source.appName 'login' STRCMP" - // } - // } - // } - // } - samconfConfig_t root = { - .parent = NULL, - .key = "SyslogScanner", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = syslogScannerChildren, - .childCount = 1, - }; + samconfConfig_t *root = *(samconfConfig_t **)state; elosRpnFilter_t *resultFilter; elosLoglineMapper_t mapper; safuResultE_t result = SAFU_RESULT_FAILED; - size_t filterIndex = 0; TEST("elosLoglineMapper"); SHOULD("%s", "skip initializing all the fields in the old config format"); - result = elosLoglineMapperInit(&mapper, &root); + result = elosLoglineMapperInit(&mapper, root); assert_int_equal(result, SAFU_RESULT_OK); @@ -97,9 +59,7 @@ void elosTestElosLoglineMapperInitSectionedMessageCodes(void **state) { resultMapping = (elosMessageCodeFilterMapping_t *)safuVecGet(&mapper.filterList, i); resultFilter = &resultMapping->filter; - assert_non_null( - strstr(testState->childrenData[5 + filterIndex].value.string, resultFilter->values->entry[1].data.str)); - ++filterIndex; + assert_int_equal(resultFilter->state, RPNFILTER_FINALIZED); } elosLoglineMapperDeleteMembers(&mapper); diff --git a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_success.c b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_success.c index 24e2a2b5..e1e8f507 100644 --- a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_success.c +++ b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/case_success.c @@ -1,20 +1,34 @@ // SPDX-License-Identifier: MIT +#include #include #include #include +#include "elos/eventfilter/eventfilter.h" #include "elos/rpnfilter/rpnfilter.h" #include "elosLoglineMapperInit_utest.h" #include "logline_mapper.h" -#include "safu/common.h" typedef struct elosMessageCodeFilterMapping { elosEventMessageCodeE_t messageCode; elosRpnFilter_t filter; } elosMessageCodeFilterMapping_t; +#define _TEST_CONFIG \ + "{ \ + \"Config\": { \ + \"MappingRules\": { \ + \"MessageCodes\": { \ + \"4000\": \".event.source.appName 'ssh' STRCMP\", \ + \"2000\": \".event.source.appName 'crinit' STRCMP\", \ + \"1000\": \".event.source.appName 'login' STRCMP\" \ + } \ + } \ + } \ + }" + int elosTestElosLoglineMapperInitSuccessSetup(void **state) { - elosLoglineMapperInitUtestInit(state); + elosLoglineMapperInitUtestInit(state, _TEST_CONFIG); return 0; } @@ -24,54 +38,16 @@ int elosTestElosLoglineMapperInitSuccessTeardown(void **state) { } void elosTestElosLoglineMapperInitSuccess(void **state) { - elosUteststateT_t *testState = *state; - - samconfConfig_t *messageCodesChildren[] = { - &testState->childrenData[5], // "4000": ".event.source.appName 'ssh' STRCMP", - &testState->childrenData[6], // "2000": ".event.source.appName 'crinit' STRCMP", - &testState->childrenData[7], // "1000": ".event.source.appName 'login' STRCMP" - }; - - testState->childrenData[1].children = messageCodesChildren; - testState->childrenData[1].childCount = 3; - - samconfConfig_t *mappingRulesChildren[] = { - &testState->childrenData[1], - }; - - testState->childrenData[0].children = mappingRulesChildren; - testState->childrenData[0].childCount = 1; - - samconfConfig_t *syslogScannerChildren[] = { - &testState->childrenData[0], - }; - - // "SyslogScannere": { - // "MappingRules": { - // "MessageCodes": { - // "4000": ".event.source.appName 'ssh' STRCMP", - // "2000": ".event.source.appName 'crinit' STRCMP", - // "1000": ".event.source.appName 'login' STRCMP" - // } - // } - // } - samconfConfig_t root = { - .parent = NULL, - .key = "SyslogScanner", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = syslogScannerChildren, - .childCount = 1, - }; + samconfConfig_t *root = *(samconfConfig_t **)state; elosRpnFilter_t *resultFilter; elosLoglineMapper_t mapper; safuResultE_t result = SAFU_RESULT_FAILED; - size_t filterIndex = 0; TEST("ElosLoglineMapper"); SHOULD("%s", "initialize a mapper with given, correctly formated, config"); - result = elosLoglineMapperInit(&mapper, &root); + result = elosLoglineMapperInit(&mapper, root); assert_int_equal(result, SAFU_RESULT_OK); @@ -81,10 +57,7 @@ void elosTestElosLoglineMapperInitSuccess(void **state) { resultMapping = (elosMessageCodeFilterMapping_t *)safuVecGet(&mapper.filterList, i); resultFilter = &resultMapping->filter; - assert_non_null( - strstr(testState->childrenData[5 + filterIndex].value.string, resultFilter->values->entry[1].data.str)); - ++filterIndex; + assert_int_equal(resultFilter->state, RPNFILTER_FINALIZED); } - elosLoglineMapperDeleteMembers(&mapper); } diff --git a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/elosLoglineMapperInit_utest.c b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/elosLoglineMapperInit_utest.c index a7432b5f..5eca99d7 100644 --- a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/elosLoglineMapperInit_utest.c +++ b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/elosLoglineMapperInit_utest.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: MIT #include "elosLoglineMapperInit_utest.h" +#include + TEST_SUITE_FUNC_PROTOTYPES(elosLoglineMapperInitUtest) int main() { @@ -14,93 +16,17 @@ int main() { return RUN_TEST_SUITE(tests, elosLoglineMapperInitUtest); } -int elosLoglineMapperInitUtestInit(void **state) { - elosUteststateT_t *testState = safuAllocMem(NULL, sizeof(*testState)); - - samconfConfig_t configData[] = { - { - .parent = NULL, - .key = "MappingRules", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "MessageCodes", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "sshd", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "crinit", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "login", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "4000", - .type = SAMCONF_CONFIG_VALUE_STRING, - .value.string = ".event.source.appName 'ssh' STRCMP", - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "2000", - .type = SAMCONF_CONFIG_VALUE_STRING, - .value.string = ".event.source.appName 'crinit' STRCMP", - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "1000", - .type = SAMCONF_CONFIG_VALUE_STRING, - .value.string = ".event.source.appName 'login' STRCMP", - .children = NULL, - .childCount = 0, - }, - { - .parent = NULL, - .key = "5000", - .type = SAMCONF_CONFIG_VALUE_OBJECT, - .children = NULL, - .childCount = 0, - }, - }; - - testState->childrenData = safuAllocMem(NULL, ARRAY_SIZE(configData) * sizeof(*testState->childrenData)); - - memcpy(testState->childrenData, configData, ARRAY_SIZE(configData) * sizeof(*testState->childrenData)); - - *state = testState; - +int elosLoglineMapperInitUtestInit(void **state, const char *config) { + samconfConfig_t *testConfig = calloc(1, sizeof(samconfConfig_t)); + samconfUtilCreateMockConfigFromStr(config, false, testConfig); + *state = testConfig; return 0; } int elosLoglineMapperInitUtestCleanUp(void **state) { - elosUteststateT_t *testState = *state; - - free(testState->childrenData); - free(testState); - + samconfConfig_t *testConfig = *(samconfConfig_t **)state; + samconfConfigDelete(testConfig); + state = NULL; return 0; } diff --git a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/elosLoglineMapperInit_utest.h b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/elosLoglineMapperInit_utest.h index 8bafd268..b0895616 100644 --- a/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/elosLoglineMapperInit_utest.h +++ b/test/utest/plugins/scanner/syslog/logline_mapper/elosLoglineMapperInit/elosLoglineMapperInit_utest.h @@ -6,12 +6,8 @@ #include "logline_mapper_utest.h" -typedef struct { - samconfConfig_t *childrenData; -} elosUteststateT_t; - int elosLoglineMapperInitUtestCleanUp(void **state); -int elosLoglineMapperInitUtestInit(void **state); +int elosLoglineMapperInitUtestInit(void **state, const char *config); TEST_CASE_FUNC_PROTOTYPES(elosTestElosLoglineMapperInitSuccess) TEST_CASE_FUNC_PROTOTYPES(elosTestElosLoglineMapperInitSectionedMessageCodes)