HelenOS sources
This source file includes following definitions.
- before_test_start
- convert_wait_status_to_outcome
- pcut_run_test_forking
- pcut_hook_before_test
#pragma warning(push, 0)
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#pragma warning(pop)
#include "../internal.h"
#define PCUT_COMMAND_LINE_BUFFER_SIZE 256
#define PCUT_TEMP_FILENAME_BUFFER_SIZE 128
#define OUTPUT_BUFFER_SIZE 8192
#if defined(__WIN64) || defined(__WIN32) || defined(_WIN32)
#include <process.h>
#define FORMAT_COMMAND(buffer, buffer_size, self_path, test_id, temp_file) \
pcut_snprintf(buffer, buffer_size, "\"\"%s\" -t%d >%s\"", self_path, test_id, temp_file)
#define FORMAT_TEMP_FILENAME(buffer, buffer_size) \
pcut_snprintf(buffer, buffer_size, "pcut_%d.tmp", _getpid())
#elif defined(__unix)
#include <unistd.h>
#define FORMAT_COMMAND(buffer, buffer_size, self_path, test_id, temp_file) \
pcut_snprintf(buffer, buffer_size, "%s -t%d &>%s", self_path, test_id, temp_file)
#define FORMAT_TEMP_FILENAME(buffer, buffer_size) \
pcut_snprintf(buffer, buffer_size, "pcut_%d.tmp", getpid())
#else
#error "Unknown operating system."
#endif
static char error_message_buffer[OUTPUT_BUFFER_SIZE];
static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
static void before_test_start(pcut_item_t *test) {
pcut_report_test_start(test);
memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
}
static int convert_wait_status_to_outcome(int status) {
if (status < 0) {
return PCUT_OUTCOME_INTERNAL_ERROR;
} else if (status == 0) {
return PCUT_OUTCOME_PASS;
} else {
return PCUT_OUTCOME_FAIL;
}
}
int pcut_run_test_forking(const char *self_path, pcut_item_t *test) {
int rc, outcome;
FILE *tempfile;
char tempfile_name[PCUT_TEMP_FILENAME_BUFFER_SIZE];
char command[PCUT_COMMAND_LINE_BUFFER_SIZE];
before_test_start(test);
FORMAT_TEMP_FILENAME(tempfile_name, PCUT_TEMP_FILENAME_BUFFER_SIZE - 1);
FORMAT_COMMAND(command, PCUT_COMMAND_LINE_BUFFER_SIZE - 1,
self_path, (test)->id, tempfile_name);
PCUT_DEBUG("Will execute <%s> (temp file <%s>) with system().",
command, tempfile_name);
rc = system(command);
PCUT_DEBUG("system() returned 0x%04X", rc);
outcome = convert_wait_status_to_outcome(rc);
tempfile = fopen(tempfile_name, "rb");
if (tempfile == NULL) {
pcut_report_test_done(test, TEST_OUTCOME_ERROR, "Failed to open temporary file.", NULL, NULL);
return PCUT_OUTCOME_INTERNAL_ERROR;
}
fread(extra_output_buffer, 1, OUTPUT_BUFFER_SIZE, tempfile);
fclose(tempfile);
remove(tempfile_name);
pcut_report_test_done_unparsed(test, outcome, extra_output_buffer, OUTPUT_BUFFER_SIZE);
return outcome;
}
void pcut_hook_before_test(pcut_item_t *test) {
PCUT_UNUSED(test);
}
HelenOS homepage, sources at GitHub