/*********************************************************************************************************************************** Dynamic module loader ***********************************************************************************************************************************/ static const char * embeddedModuleGetInternal(const char *moduleName) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM(STRINGZ, moduleName); FUNCTION_TEST_END(); // Find module const char *result = NULL; for (unsigned int moduleIdx = 0; moduleIdx < sizeof(embeddedModule) / sizeof(EmbeddedModule); moduleIdx++) { if (strcmp(embeddedModule[moduleIdx].name, moduleName) == 0) { result = embeddedModule[moduleIdx].data; break; } } // Error if the module was not found if (result == NULL) THROW_FMT(AssertError, "unable to load embedded module '%s'", moduleName); FUNCTION_TEST_RETURN(result); }
/*********************************************************************************************************************************** Make sure nothing is left in the log after all tests have completed ***********************************************************************************************************************************/ void harnessLogFinal(void) { FUNCTION_HARNESS_VOID(); harnessLogLoad(logFile); if (strcmp(harnessLogBuffer, "") != 0) THROW_FMT(AssertError, "\n\nexpected log to be empty but actual log was:\n\n%s\n\n", harnessLogBuffer); FUNCTION_HARNESS_RESULT_VOID(); }
/*********************************************************************************************************************************** Compare log to a static string After the comparison the log is cleared so the next result can be compared. ***********************************************************************************************************************************/ void harnessLogResult(const char *expected) { FUNCTION_HARNESS_BEGIN(); FUNCTION_HARNESS_PARAM(STRINGZ, expected); FUNCTION_HARNESS_ASSERT(expected != NULL); FUNCTION_HARNESS_END(); harnessLogLoad(logFile); if (strcmp(harnessLogBuffer, expected) != 0) THROW_FMT(AssertError, "\n\nexpected log:\n\n%s\n\nbut actual log was:\n\n%s\n\n", expected, harnessLogBuffer); close(logHandleFile); logHandleFile = harnessLogOpen(logFile, O_WRONLY | O_CREAT | O_TRUNC, 0640); FUNCTION_HARNESS_RESULT_VOID(); }
/*********************************************************************************************************************************** Compare log to a regexp After the comparison the log is cleared so the next result can be compared. ***********************************************************************************************************************************/ void harnessLogResultRegExp(const char *expression) { FUNCTION_HARNESS_BEGIN(); FUNCTION_HARNESS_PARAM(STRINGZ, expression); FUNCTION_HARNESS_ASSERT(expression != NULL); FUNCTION_HARNESS_END(); regex_t regExp; TRY_BEGIN() { harnessLogLoad(logFile); // Compile the regexp and process errors int result = 0; if ((result = regcomp(®Exp, expression, REG_NOSUB | REG_EXTENDED)) != 0) { char buffer[4096]; regerror(result, NULL, buffer, sizeof(buffer)); THROW(FormatError, buffer); } // Do the match if (regexec(®Exp, harnessLogBuffer, 0, NULL, 0) != 0) THROW_FMT(AssertError, "\n\nexpected log regexp:\n\n%s\n\nbut actual log was:\n\n%s\n\n", expression, harnessLogBuffer); close(logHandleFile); logHandleFile = harnessLogOpen(logFile, O_WRONLY | O_CREAT | O_TRUNC, 0640); } FINALLY() { regfree(®Exp); } TRY_END(); FUNCTION_HARNESS_RESULT_VOID(); }
/*********************************************************************************************************************************** Test for the existence of a stop file ***********************************************************************************************************************************/ void lockStopTest(void) { FUNCTION_LOG_VOID(logLevelDebug); MEM_CONTEXT_TEMP_BEGIN() { // Check the current stanza (if any) if (cfgOptionTest(cfgOptStanza)) { if (storageExistsNP(storageLocal(), lockStopFileName(cfgOptionStr(cfgOptStanza)))) THROW_FMT(StopError, "stop file exists for stanza %s", strPtr(cfgOptionStr(cfgOptStanza))); } // Check all stanzas if (storageExistsNP(storageLocal(), lockStopFileName(NULL))) THROW(StopError, "stop file exists for all stanzas"); } MEM_CONTEXT_TEMP_END(); FUNCTION_LOG_RETURN_VOID(); }
/*********************************************************************************************************************************** testBegin - should this test run? ***********************************************************************************************************************************/ bool testBegin(const char *name) { FUNCTION_HARNESS_BEGIN(); FUNCTION_HARNESS_PARAM(STRINGZ, name); FUNCTION_HARNESS_ASSERT(name != NULL); FUNCTION_HARNESS_END(); bool result = false; testRun++; if (testList[testRun - 1].selected) { if (testFirst) { // Set test user const char *testUserTemp = getpwuid(getuid())->pw_name; if (strlen(testUserTemp) > sizeof(testUserData) - 1) THROW_FMT(AssertError, "test user name must be less than %zu characters", sizeof(testUserData) - 1); strcpy(testUserData, testUserTemp); // Set test group const char *testGroupTemp = getgrgid(getgid())->gr_name; if (strlen(testGroupTemp) > sizeof(testGroupData) - 1) THROW_FMT(AssertError, "test group name must be less than %zu characters", sizeof(testGroupData) - 1); strcpy(testGroupData, testGroupTemp); } #ifndef NO_LOG if (!testFirst) { // Make sure there is nothing untested left in the log harnessLogFinal(); // Clear out the test directory so the next test starts clean char buffer[2048]; snprintf(buffer, sizeof(buffer), "sudo rm -rf %s/" "*", testPath()); if (system(buffer) != 0) { fprintf(stderr, "ERROR: unable to clear test path '%s'\n", testPath()); fflush(stderr); exit(255); } } #endif // No longer the first test testFirst = false; if (testRun != 1) printf("\n"); printf("run %03d - %s\n", testRun, name); fflush(stdout); timeMSecBegin = testTimeMSec(); #ifndef NO_LOG // Initialize logging harnessLogInit(); #endif result = true; } FUNCTION_HARNESS_RESULT(BOOL, result); }