From a54421dd976fd8081e96c11c2621076876c9986b Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Tue, 17 Feb 2026 11:07:37 +0100 Subject: Replace CLI arguments with config-file parser and add integration tests Introduce a reusable key=value config parser (config.h/c) and convert all three programs to read their settings from config files instead of positional command-line arguments. Add example config files in config/ and 6 CTest integration tests covering None/Basic256Sha256/Aes128 with anonymous and user authentication. Remove the now-obsolete launch.sh. --- src/client_find_servers.c | 80 ++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 32 deletions(-) (limited to 'src/client_find_servers.c') diff --git a/src/client_find_servers.c b/src/client_find_servers.c index 21d48ca..e50623f 100644 --- a/src/client_find_servers.c +++ b/src/client_find_servers.c @@ -10,6 +10,7 @@ */ #include "common.h" +#include "config.h" #include #include @@ -210,64 +211,71 @@ readServerTime (UA_Client *client, int main (int argc, char **argv) { - if (argc < 8) + if (argc != 2) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "Usage: %s \n" - " \n" - " \n" - " \n" - " [ ]\n" - " [, ...]\n" - "\n" - "Security modes : None, Sign, SignAndEncrypt\n" - "Security policies: None, Basic256Sha256, " - "Aes256_Sha256_RsaPss,\n" - " Aes128_Sha256_RsaOaep, ECC_nistP256\n" - "Auth modes : anonymous, user", - argv[0]); + "Usage: %s ", argv[0]); return EXIT_FAILURE; } - const char *discoveryServerEndpoint = argv[1]; - const char *applicationUri = argv[2]; - const char *certPath = argv[3]; - const char *keyPath = argv[4]; + Config cfg; + if (configLoad (argv[1], &cfg) != 0) + return EXIT_FAILURE; + + const char *discoveryServerEndpoint + = configRequire (&cfg, "discoveryEndpoint", "ClientFindServers"); + const char *applicationUri + = configRequire (&cfg, "applicationUri", "ClientFindServers"); + const char *certPath + = configRequire (&cfg, "certificate", "ClientFindServers"); + const char *keyPath + = configRequire (&cfg, "privateKey", "ClientFindServers"); + const char *securityModeStr + = configRequire (&cfg, "securityMode", "ClientFindServers"); + const char *securityPolicyStr + = configRequire (&cfg, "securityPolicy", "ClientFindServers"); + const char *authMode = configRequire (&cfg, "authMode", "ClientFindServers"); + + if (!discoveryServerEndpoint || !applicationUri || !certPath || !keyPath + || !securityModeStr || !securityPolicyStr || !authMode) + { + configFree (&cfg); + return EXIT_FAILURE; + } - UA_MessageSecurityMode securityMode = parseSecurityMode (argv[5]); + UA_MessageSecurityMode securityMode = parseSecurityMode (securityModeStr); if (securityMode == UA_MESSAGESECURITYMODE_INVALID) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "Unknown security mode: %s", argv[5]); + "Unknown security mode: %s", securityModeStr); + configFree (&cfg); return EXIT_FAILURE; } - const char *securityPolicyUri = resolveSecurityPolicyUri (argv[6]); + const char *securityPolicyUri = resolveSecurityPolicyUri (securityPolicyStr); if (!securityPolicyUri) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "Unknown security policy: %s", argv[6]); + "Unknown security policy: %s", securityPolicyStr); + configFree (&cfg); return EXIT_FAILURE; } - int idx = 7; - const char *authMode = argv[idx++]; const char *username = NULL, *password = NULL; if (strcmp (authMode, "anonymous") == 0) { - /* No extra args needed */ + /* No credentials needed */ } else if (strcmp (authMode, "user") == 0) { - if (idx + 2 > argc) + username = configRequire (&cfg, "username", "ClientFindServers"); + password = configRequire (&cfg, "password", "ClientFindServers"); + if (!username || !password) { - UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "Auth mode 'user' requires "); + configFree (&cfg); return EXIT_FAILURE; } - username = argv[idx++]; - password = argv[idx++]; } else { @@ -275,11 +283,13 @@ main (int argc, char **argv) "Unknown auth mode: %s " "(expected 'anonymous' or 'user')", authMode); + configFree (&cfg); return EXIT_FAILURE; } - char **trustPaths = argv + idx; - size_t trustSize = (idx < argc) ? (size_t)(argc - idx) : 0; + char **trustPaths = NULL; + size_t trustSize = 0; + configGetAll (&cfg, "trustList", &trustPaths, &trustSize); UA_Client *client = UA_Client_new (); UA_StatusCode retval = createSecureClientConfig ( @@ -288,6 +298,8 @@ main (int argc, char **argv) if (retval != UA_STATUSCODE_GOOD) { UA_Client_delete (client); + free (trustPaths); + configFree (&cfg); return EXIT_FAILURE; } @@ -300,6 +312,8 @@ main (int argc, char **argv) if (retval != UA_STATUSCODE_GOOD) { UA_Client_delete (client); + free (trustPaths); + configFree (&cfg); return EXIT_FAILURE; } @@ -313,6 +327,8 @@ main (int argc, char **argv) UA_Array_delete (applicationDescriptionArray, applicationDescriptionArraySize, &UA_TYPES[UA_TYPES_APPLICATIONDESCRIPTION]); + free (trustPaths); + configFree (&cfg); return EXIT_SUCCESS; } -- cgit v1.2.3