HelenOS sources
This source file includes following definitions.
- pcut_str_equals
- pcut_str_start_equals
- pcut_str_size
- pcut_str_to_int
- pcut_str_find_char
- pcut_str_error
- before_test_start
- test_timeout_handler_fibril
- pcut_run_test_forking
- pcut_hook_before_test
#include <stdlib.h>
#include <str.h>
#include <str_error.h>
#include <errno.h>
#include <assert.h>
#include <stdio.h>
#include <task.h>
#include <fibril_synch.h>
#include <vfs/vfs.h>
#include "../internal.h"
int pcut_str_equals(const char *a, const char *b) {
return str_cmp(a, b) == 0;
}
int pcut_str_start_equals(const char *a, const char *b, int len) {
return str_lcmp(a, b, len) == 0;
}
int pcut_str_size(const char *s) {
return str_size(s);
}
int pcut_str_to_int(const char *s) {
int result = strtol(s, NULL, 10);
return result;
}
char *pcut_str_find_char(const char *haystack, const char needle) {
return str_chr(haystack, needle);
}
void pcut_str_error(int error, char *buffer, int size) {
const char *str = str_error(error);
if (str == NULL) {
str = "(strerror failure)";
}
str_cpy(buffer, size, str);
}
#define MAX_TEST_NUMBER_WIDTH 24
#define PCUT_TEMP_FILENAME_BUFFER_SIZE 128
#define MAX_COMMAND_LINE_LENGTH 1024
#define OUTPUT_BUFFER_SIZE 8192
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 FIBRIL_MUTEX_INITIALIZE(forced_termination_mutex);
static FIBRIL_CONDVAR_INITIALIZE(forced_termination_cv);
static task_id_t test_task_id;
static int test_running;
static int test_timeout_handler_fibril(void *arg) {
pcut_item_t *test = arg;
int timeout_sec = pcut_get_test_timeout(test);
usec_t timeout_us = SEC2USEC(timeout_sec);
fibril_mutex_lock(&forced_termination_mutex);
if (!test_running) {
goto leave_no_kill;
}
errno_t rc = fibril_condvar_wait_timeout(&forced_termination_cv,
&forced_termination_mutex, timeout_us);
if (rc == ETIMEOUT) {
task_kill(test_task_id);
}
leave_no_kill:
fibril_mutex_unlock(&forced_termination_mutex);
return EOK;
}
int pcut_run_test_forking(const char *self_path, pcut_item_t *test) {
before_test_start(test);
char tempfile_name[PCUT_TEMP_FILENAME_BUFFER_SIZE];
snprintf(tempfile_name, PCUT_TEMP_FILENAME_BUFFER_SIZE - 1, "pcut_%lld.tmp", (unsigned long long) task_get_id());
int tempfile;
errno_t rc = vfs_lookup_open(tempfile_name, WALK_REGULAR | WALK_MAY_CREATE, MODE_READ | MODE_WRITE, &tempfile);
if (rc != EOK) {
pcut_report_test_done(test, PCUT_OUTCOME_INTERNAL_ERROR, "Failed to create temporary file.", NULL, NULL);
return PCUT_OUTCOME_INTERNAL_ERROR;
}
char test_number_argument[MAX_TEST_NUMBER_WIDTH];
snprintf(test_number_argument, MAX_TEST_NUMBER_WIDTH, "-t%d", test->id);
const char *const arguments[3] = {
self_path,
test_number_argument,
NULL
};
int status = PCUT_OUTCOME_PASS;
task_wait_t test_task_wait;
rc = task_spawnvf(&test_task_id, &test_task_wait, self_path, arguments,
fileno(stdin), tempfile, tempfile);
if (rc != EOK) {
status = PCUT_OUTCOME_INTERNAL_ERROR;
goto leave_close_tempfile;
}
test_running = 1;
fid_t killer_fibril = fibril_create(test_timeout_handler_fibril, test);
if (killer_fibril == 0) {
task_kill(test_task_id);
} else {
fibril_add_ready(killer_fibril);
}
task_exit_t task_exit;
int task_retval;
rc = task_wait(&test_task_wait, &task_exit, &task_retval);
if (rc != EOK) {
status = PCUT_OUTCOME_INTERNAL_ERROR;
goto leave_close_tempfile;
}
if (task_exit == TASK_EXIT_UNEXPECTED) {
status = PCUT_OUTCOME_INTERNAL_ERROR;
} else {
status = task_retval == 0 ? PCUT_OUTCOME_PASS : PCUT_OUTCOME_FAIL;
}
fibril_mutex_lock(&forced_termination_mutex);
test_running = 0;
fibril_condvar_signal(&forced_termination_cv);
fibril_mutex_unlock(&forced_termination_mutex);
aoff64_t pos = 0;
size_t nread;
vfs_read(tempfile, &pos, extra_output_buffer, OUTPUT_BUFFER_SIZE, &nread);
leave_close_tempfile:
vfs_put(tempfile);
vfs_unlink_path(tempfile_name);
pcut_report_test_done_unparsed(test, status, extra_output_buffer, OUTPUT_BUFFER_SIZE);
return status;
}
void pcut_hook_before_test(pcut_item_t *test) {
PCUT_UNUSED(test);
}
HelenOS homepage, sources at GitHub