/** * Write a formatted string to the console, lookup the last port error message and port error number, then store it. * * Update the number of failed tests * * * @param[in] portLibrary The port library * @param[in] fileName File requesting message output * @param[in] lineNumber Line number in the file of request * @param[in] testName Name of the test requesting output * @param[in] foramt Format of string to be output * @param[in] ... argument list for format string */ void outputErrorMessage(struct OMRPortLibrary *portLibrary, const char *fileName, int32_t lineNumber, const char *testName, const char *format, ...) { char *buf, *portErrorBuf = NULL; uintptr_t sizeBuf; size_t sizePortErrorBuf; va_list args; char *lastErrorMessage = NULL; int32_t lastErrorNumber = 0; OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); lastErrorMessage = (char *)omrerror_last_error_message(); lastErrorNumber = omrerror_last_error_number(); /* Capture the error message now * Get the size needed to hold the last error message (don't use str_printf to avoid polluting error message */ sizePortErrorBuf = strlen(lastErrorMessage) + 1 /* for the null terminator */; portErrorBuf = (char *)omrmem_allocate_memory(sizePortErrorBuf, OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL != portErrorBuf) { strncpy(portErrorBuf, lastErrorMessage, sizePortErrorBuf); } else { portTestEnv->log(LEVEL_ERROR, "\n\n******* omrmem_allocate_memory failed to allocate %i bytes, exiting.\n\n", sizePortErrorBuf); exit(EXIT_OUT_OF_MEMORY); } va_start(args, format); /* get the size needed to hold the error message that was passed in */ sizeBuf = omrstr_vprintf(NULL, 0, format, args); buf = (char *)omrmem_allocate_memory(sizeBuf, OMRMEM_CATEGORY_PORT_LIBRARY); if (NULL != buf) { omrstr_vprintf(buf, sizeBuf, format, args); } else { portTestEnv->log(LEVEL_ERROR, "\n\n******* omrmem_allocate_memory failed to allocate %i bytes, exiting.\n\n", sizeBuf); exit(EXIT_OUT_OF_MEMORY); } va_end(args); portTestEnv->log(LEVEL_ERROR, "%s line %4zi: %s ", fileName, lineNumber, testName); portTestEnv->log(LEVEL_ERROR, "%s\n", buf); portTestEnv->log(LEVEL_ERROR, "\t\tLastErrorNumber: %i\n", lastErrorNumber); portTestEnv->log(LEVEL_ERROR, "\t\tLastErrorMessage: %s\n\n", portErrorBuf); logTestFailure(OMRPORTLIB, fileName, lineNumber, testName, lastErrorNumber, portErrorBuf, buf); omrmem_free_memory(portErrorBuf); omrmem_free_memory(buf); numberFailedTestsInComponent++; }
intptr_t waitForTestProcess(OMRPortLibrary *portLibrary, OMRProcessHandle processHandle) { intptr_t retval = -1; OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); if (NULL == processHandle) { portTestEnv->log(LEVEL_ERROR, "waitForTestProcess: processHandle == NULL\n"); goto done; } if (0 != j9process_waitfor(OMRPORTLIB, processHandle)) { int32_t portableErrno = omrerror_last_error_number(); const char *errMsg = omrerror_last_error_message(); if (NULL == errMsg) { portTestEnv->log(LEVEL_ERROR, "waitForTestProcess: j9process_waitfor() failed\n\tportableErrno = %d\n" , portableErrno); } else { portTestEnv->log(LEVEL_ERROR, "waitForTestProcess: j9process_waitfor() failed\n\tportableErrno = %d portableErrMsg = %s\n" , portableErrno, errMsg); } goto done; } retval = j9process_get_exitCode(OMRPORTLIB, processHandle); if (0 != j9process_close(OMRPORTLIB, &processHandle, 0)) { int32_t portableErrno = omrerror_last_error_number(); const char *errMsg = omrerror_last_error_message(); if (NULL == errMsg) { portTestEnv->log(LEVEL_ERROR, "waitForTestProcess: j9process_close() failed\n\tportableErrno = %d\n" , portableErrno); } else { portTestEnv->log(LEVEL_ERROR, "waitForTestProcess: j9process_close() failed\n\tportableErrno = %d portableErrMsg = %s\n" , portableErrno, errMsg); } goto done; } done: return retval; }
/** * Check that the file exists on the file system. * * This takes the porttest filename and line number information so that the caller is properly identified as failing. * * @param[in] portLibrary The port library * @param[in] fileName File requesting file check * @param[in] lineNumber Line number in the file of request * @param[in] testName Name of the test requesting file check * @param[in] fileName file who's existence we need to verify * @param[in] lineNumber Line number in the file of request * * @return 0 if the file exists, non-zero otherwise. */ uintptr_t verifyFileExists(struct OMRPortLibrary *portLibrary, const char *pltestFileName, int32_t lineNumber, const char *testName, const char *fileName) { uintptr_t rc = 1; OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); portTestEnv->changeIndent(1); #if defined(J9ZOS390) char dumpName[EsMaxPath] = {0}; /* Replace trailing ".X&DS" on 64bit and add required "//" prefix */ strncpy(dumpName, "//", 2); strncat(dumpName, (strstr(fileName, ".") + 1), strlen(fileName)); char *ending = strstr(dumpName, ".X&DS"); if (NULL != ending) { strncpy(ending, ".X001", 5); } portTestEnv->log("checking for data set: %s\n", dumpName); FILE *file = fopen(dumpName, "r"); if (NULL == file) { outputErrorMessage(OMRPORTLIB, pltestFileName, lineNumber, testName, "\tdata set: %s does not exist!\n", -1, fileName); } else { portTestEnv->log("data set: %s exists\n", dumpName); fclose(file); rc = 0; } #else /* defined(J9ZOS390) */ J9FileStat fileStat; int32_t fileStatRC = 99; /* stat the fileName */ fileStatRC = omrfile_stat(fileName, 0, &fileStat); if (0 == fileStatRC) { if (fileStat.isFile) { portTestEnv->log("file: %s exists\n", fileName); rc = 0; } else { outputErrorMessage(OMRPORTLIB, pltestFileName, lineNumber, testName, "\tfile: %s does not exist!\n", -1, fileName); } } else { /* error in file_stat */ outputErrorMessage(OMRPORTLIB, pltestFileName, lineNumber, testName, "\nomrfile_stat call in verifyFileExists() returned %i: %s\n", fileStatRC, omrerror_last_error_message()); } #endif /* defined(J9ZOS390) */ portTestEnv->changeIndent(-1); return rc; }
OMRProcessHandle launchChildProcess(OMRPortLibrary *portLibrary, const char *testname, const char *argv0, const char *options) { OMRProcessHandle processHandle = NULL; char *command[2]; uintptr_t commandLength = 2; uint32_t exeoptions = 0; intptr_t retVal; OMRPORT_ACCESS_FROM_OMRPORT(portLibrary); retVal = omrsysinfo_get_executable_name((char *)argv0, &(command[0])); if (retVal != 0) { int32_t portableErrno = omrerror_last_error_number(); const char *errMsg = omrerror_last_error_message(); if (NULL == errMsg) { portTestEnv->log(LEVEL_ERROR, "%s: launchChildProcess: omrsysinfo_get_executable_name failed!\n\tportableErrno = %d\n", testname, portableErrno); } else { portTestEnv->log(LEVEL_ERROR, "%s: launchChildProcess: omrsysinfo_get_executable_name failed!\n\tportableErrno = %d portableErrMsg = %s\n", testname, portableErrno, errMsg); } goto done; } command[1] = (char *)options; #if defined(PORTTEST_PROCESS_HELPERS_DEBUG) /* print our the commandline options for the process being launched * and have the child write its to the console by inheriting stdout and stderr */ { int i; portTestEnv->log("\n\n"); for (i = 0; i < commandLength; i++) { portTestEnv->log("\t\tcommand[%i]: %s\n", i, command[i]); } portTestEnv->log("\n\n"); exeoptions = OMRPROCESS_DEBUG; } #endif retVal = j9process_create(OMRPORTLIB, (const char **)command, commandLength, ".", exeoptions, &processHandle); if (0 != retVal) { int32_t portableErrno = omrerror_last_error_number(); const char *errMsg = omrerror_last_error_message(); if (NULL == errMsg) { portTestEnv->log(LEVEL_ERROR, "%s: launchChildProcess: Failed to start process '%s %s'\n\tportableErrno = %d\n", testname, command[0], command[1], portableErrno); } else { portTestEnv->log(LEVEL_ERROR, "%s: launchChildProcess: Failed to start process '%s %s'\n\tportableErrno = %d portableErrMsg = %s\n", testname, command[0], command[1], portableErrno, errMsg); } processHandle = NULL; goto done; } done: return processHandle; }
/** * Verify port library error handling operations. * * Error codes are stored in per thread buffers so errors reported by one thread * do not effect those reported by a second. Errors stored via the helper * function @ref omrerror.c::omrerror_set_last_error_with_message_format "omrerror_set_last_error_with_message_format()" * are recorded in the per thread buffers without an error message. * Verify the @ref omrerror_last_error_message "omrerror_last_error_message()" returns * the correct message. */ TEST(PortErrorTest, error_test3) { OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary()); const char *testName = "omrerror_test3"; const char *message; const char *formatMessage1 = "This is a test message with format specifiers %s %d"; const char *formatMessage2 = "This is also a test message with format specifiers %s %d"; const char *arg1 = "arg1"; int32_t arg2 = 1; char knownMessage[1024]; /* 1024 is large enough to hold format message */ int32_t errorCode; reportTestEntry(OMRPORTLIB, testName); /* Delete the ptBuffers */ omrport_tls_free(); /* In theory there is now nothing stored, if we really did free the buffers. * Guess it is time to find out */ message = omrerror_last_error_message(); if (0 != strlen(message)) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned message of length %d expected %d\n", strlen(message), 0); } /* Set an error message, verify it is what we get back */ omrstr_printf(knownMessage, 1024, formatMessage1, arg1, arg2); errorCode = omrerror_set_last_error_with_message_format(200, formatMessage1, arg1, arg2); message = omrerror_last_error_message(); if (200 != errorCode) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_set_last_error_with_message() returned %d expected %d\n", errorCode, 200); } if (strlen(message) != strlen(knownMessage)) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned length %d expected %d\n", strlen(message), strlen(knownMessage)); } if (0 != memcmp(message, knownMessage, strlen(knownMessage))) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned \"%s\" expected \"%s\"\n", message, knownMessage); } /* Again, different message */ omrstr_printf(knownMessage, 1024, formatMessage2, arg1, arg2); errorCode = omrerror_set_last_error_with_message_format(100, formatMessage2, arg1, arg2); message = omrerror_last_error_message(); if (100 != errorCode) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_set_last_error_with_message() returned %d expected %d\n", errorCode, 100); } if (strlen(message) != strlen(knownMessage)) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned length %d expected %d\n", strlen(message), strlen(knownMessage)); } if (0 != memcmp(message, knownMessage, strlen(knownMessage))) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned \"%s\" expected \"%s\"\n", message, knownMessage); } /* Delete the ptBuffers, verify no error is stored */ omrport_tls_free(); errorCode = omrerror_last_error_number(); if (0 != errorCode) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_number() returned %d expected %d\n", errorCode, 0); } reportTestExit(OMRPORTLIB, testName); }
/** * Verify port library error handling operations. * * Error codes are stored in per thread buffers so errors reported by one thread * do not effect those reported by a second. Errors stored via the helper * function @ref omrerror.c::omrerror_set_last_error_with_message "omrerror_set_last_error_with_message()" * are recorded in the per thread buffers without an error message. * Verify the @ref omrerror_last_error_message "omrerror_last_error_message()" returns * the correct message. */ TEST(PortErrorTest, error_test2) { OMRPORT_ACCESS_FROM_OMRPORT(portTestEnv->getPortLibrary()); const char *testName = "omrerror_test2"; const char *message; const char *knownMessage = "This is a test"; const char *knownMessage2 = "This is also a test"; int32_t errorCode; reportTestEntry(OMRPORTLIB, testName); /* Delete the ptBuffers */ omrport_tls_free(); /* In theory there is now nothing stored, if we really did free the buffers. * Guess it is time to find out */ message = omrerror_last_error_message(); if (0 != strlen(message)) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned message of length %d expected %d\n", strlen(message), 0); } /* Set an error message, verify it is what we get back */ errorCode = omrerror_set_last_error_with_message(200, knownMessage); message = omrerror_last_error_message(); if (200 != errorCode) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_set_last_error_with_message() returned %d expected %d\n", errorCode, 200); } if (strlen(message) != strlen(knownMessage)) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned length %d expected %d\n", strlen(message), strlen(knownMessage)); } if (0 != memcmp(message, knownMessage, strlen(knownMessage))) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned \"%s\" expected \"%s\"\n", message, knownMessage); } /* Again, different message */ errorCode = omrerror_set_last_error_with_message(100, knownMessage2); message = omrerror_last_error_message(); if (100 != errorCode) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_set_last_error_with_message() returned %d expected %d\n", errorCode, 100); } if (strlen(message) != strlen(knownMessage2)) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned length %d expected %d\n", strlen(message), strlen(knownMessage2)); } if (0 != memcmp(message, knownMessage2, strlen(knownMessage2))) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned \"%s\" expected \"%s\"\n", message, knownMessage2); } /* A null message, valid test?*/ #if 0 errorCode = omrerror_set_last_error_with_message(-100, NULL); message = omrerror_last_error_message(); if (-100 != errorCode) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned %d expected %d\n", errorCode, -100); } if (NULL != message) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned \"%s\" expected NULL\n", message); } #endif omrport_tls_free(); errorCode = omrerror_set_last_error_with_message(-300, knownMessage); message = omrerror_last_error_message(); if (-300 != errorCode) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_number() returned %d expected %d\n", errorCode, -300); } if (strlen(message) != strlen(knownMessage)) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned length %d expected %d\n", strlen(message), strlen(knownMessage)); } if (0 != memcmp(message, knownMessage, strlen(knownMessage))) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_message() returned \"%s\" expected \"%s\"\n", message, knownMessage); } /* Delete the ptBuffers, verify no error is stored */ omrport_tls_free(); errorCode = omrerror_last_error_number(); if (0 != errorCode) { outputErrorMessage(PORTTEST_ERROR_ARGS, "omrerror_last_error_number() returned %d expected %d\n", errorCode, 0); } reportTestExit(OMRPORTLIB, testName); }