diff --git a/modules/libcom/test/Makefile b/modules/libcom/test/Makefile index 7d86e5a4a..ffee73fe4 100644 --- a/modules/libcom/test/Makefile +++ b/modules/libcom/test/Makefile @@ -256,6 +256,11 @@ osiSockTest_SRCS += osiSockTest.c testHarness_SRCS += osiSockTest.c TESTS += osiSockTest +TESTPROD_HOST += atInitTest +atInitTest_SRCS += atInitTest.cpp +TESTS += atInitTest +testHarness_SRCS += atInitTest.cpp + TESTPROD_HOST += testexecname testexecname_SRCS += testexecname.c # no point in including in testHarness. Not implemented for RTEMS/vxWorks. diff --git a/modules/libcom/test/atInitTest.cpp b/modules/libcom/test/atInitTest.cpp new file mode 100644 index 000000000..126220b45 --- /dev/null +++ b/modules/libcom/test/atInitTest.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +void atInitTestEnv(const char *varName, const char *varValue) +{ + const char *val = getenv(varName); + testOk(val && strcmp(val, varValue) == 0, + "%s=%s", varName, val ? val : "(null)"); +} + +MAIN(atInitTest) +{ + testPlan(12); + + libComRegister(); + + // Reset environment variables + iocshCmd("epicsEnvSet \"ATINIT_TEST_VAR\" \"BeforeIocInit\""); + iocshCmd("epicsEnvSet \"ATINIT_TEST_VAR_ONE\" \"BeforeIocInit\""); + iocshCmd("epicsEnvSet \"ATINIT_TEST_VAR_TWO\" \"BeforeIocInit\""); + + printf("Test if the variables are 'BeforeIocInit'.\n"); + atInitTestEnv("ATINIT_TEST_VAR", "BeforeIocInit"); + atInitTestEnv("ATINIT_TEST_VAR_ONE", "BeforeIocInit"); + atInitTestEnv("ATINIT_TEST_VAR_TWO", "BeforeIocInit"); + + // Basic test + iocshCmd("atInit \"epicsEnvSet ATINIT_TEST_VAR AfterIocInit\""); + iocshCmd("atInit \"epicsEnvSet ATINIT_TEST_VAR_ONE AfterIocInit\""); + iocshCmd("atInit \"epicsEnvSet ATINIT_TEST_VAR_TWO AfterIocInit\""); + iocshCmd("atInit \"date\""); + + epicsThreadSleep(1.0); + // Verify error handling and robustness + iocshCmd("atInit \"nonexistentCommand arg1 arg2\""); + iocshCmd("atInit \"\""); // empty string + iocshCmd("atInit \" \""); // only spaces + + printf("Test if the variables are 'BeforeIocInit' after execution 'atInit'.\n"); + atInitTestEnv("ATINIT_TEST_VAR", "BeforeIocInit"); + atInitTestEnv("ATINIT_TEST_VAR_ONE", "BeforeIocInit"); + atInitTestEnv("ATINIT_TEST_VAR_TWO", "BeforeIocInit"); + + // Simulate iocInit + initHookAnnounce(initHookAfterIocRunning); + printf("=== iocInit Simulation ===\n"); + epicsThreadSleep(1.0); + + // Verify the results + printf("Test if the variables are 'AfterIocInit' after 'iocInit'.\n"); + atInitTestEnv("ATINIT_TEST_VAR", "AfterIocInit"); + atInitTestEnv("ATINIT_TEST_VAR_ONE", "AfterIocInit"); + atInitTestEnv("ATINIT_TEST_VAR_TWO", "AfterIocInit"); + testPass("Command 'date' executed"); + + testPass("Invalid command did not crash IOC"); + testPass("Empty atInit commands do not cause failure"); + + return testDone(); +}