[01579ad] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @file
|
---|
| 30 | *
|
---|
| 31 | * Implementation of platform-dependent functions for HelenOS.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | #include <stdlib.h>
|
---|
| 35 | #include <str.h>
|
---|
[134ac5d] | 36 | #include <str_error.h>
|
---|
[01579ad] | 37 | #include <unistd.h>
|
---|
| 38 | #include <sys/types.h>
|
---|
| 39 | #include <errno.h>
|
---|
| 40 | #include <assert.h>
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 | #include <task.h>
|
---|
| 43 | #include <fcntl.h>
|
---|
[134ac5d] | 44 | #include <fibril_synch.h>
|
---|
[01579ad] | 45 | #include "../internal.h"
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | /* String functions. */
|
---|
| 49 |
|
---|
| 50 | int pcut_str_equals(const char *a, const char *b) {
|
---|
| 51 | return str_cmp(a, b) == 0;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | int pcut_str_start_equals(const char *a, const char *b, int len) {
|
---|
| 56 | return str_lcmp(a, b, len) == 0;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | int pcut_str_size(const char *s) {
|
---|
| 60 | return str_size(s);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | int pcut_str_to_int(const char *s) {
|
---|
| 64 | int result = strtol(s, NULL, 10);
|
---|
| 65 | return result;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | char *pcut_str_find_char(const char *haystack, const char needle) {
|
---|
| 69 | return str_chr(haystack, needle);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[134ac5d] | 72 | void pcut_str_error(int error, char *buffer, int size) {
|
---|
| 73 | const char *str = str_error(error);
|
---|
| 74 | if (str == NULL) {
|
---|
| 75 | str = "(strerror failure)";
|
---|
| 76 | }
|
---|
| 77 | str_cpy(buffer, size, str);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[01579ad] | 80 |
|
---|
| 81 | /* Forking-mode related functions. */
|
---|
| 82 |
|
---|
| 83 | /** Maximum width of a test number. */
|
---|
| 84 | #define MAX_TEST_NUMBER_WIDTH 24
|
---|
| 85 |
|
---|
| 86 | /** Maximum length of a temporary file name. */
|
---|
| 87 | #define PCUT_TEMP_FILENAME_BUFFER_SIZE 128
|
---|
| 88 |
|
---|
| 89 | /** Maximum command-line length. */
|
---|
| 90 | #define MAX_COMMAND_LINE_LENGTH 1024
|
---|
| 91 |
|
---|
| 92 | /** Maximum size of stdout we are able to capture. */
|
---|
| 93 | #define OUTPUT_BUFFER_SIZE 8192
|
---|
| 94 |
|
---|
| 95 | /** Buffer for assertion and other error messages. */
|
---|
| 96 | static char error_message_buffer[OUTPUT_BUFFER_SIZE];
|
---|
| 97 |
|
---|
| 98 | /** Buffer for stdout from the test. */
|
---|
| 99 | static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
|
---|
| 100 |
|
---|
[134ac5d] | 101 | /** Prepare for a new test.
|
---|
| 102 | *
|
---|
| 103 | * @param test Test that is about to be run.
|
---|
| 104 | */
|
---|
[01579ad] | 105 | static void before_test_start(pcut_item_t *test) {
|
---|
| 106 | pcut_report_test_start(test);
|
---|
| 107 |
|
---|
| 108 | memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
| 109 | memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[134ac5d] | 112 | /** Mutex guard for forced_termination_cv. */
|
---|
| 113 | static fibril_mutex_t forced_termination_mutex
|
---|
| 114 | = FIBRIL_MUTEX_INITIALIZER(forced_termination_mutex);
|
---|
| 115 |
|
---|
| 116 | /** Condition-variable for checking whether test timed-out. */
|
---|
| 117 | static fibril_condvar_t forced_termination_cv
|
---|
| 118 | = FIBRIL_CONDVAR_INITIALIZER(forced_termination_cv);
|
---|
| 119 |
|
---|
| 120 | /** Spawned task id. */
|
---|
| 121 | static task_id_t test_task_id;
|
---|
| 122 |
|
---|
| 123 | /** Flag whether test is still running.
|
---|
| 124 | *
|
---|
| 125 | * This flag is used when checking whether test timed-out.
|
---|
| 126 | */
|
---|
| 127 | static int test_running;
|
---|
| 128 |
|
---|
| 129 | /** Main fibril for checking whether test timed-out.
|
---|
| 130 | *
|
---|
| 131 | * @param arg Test that is currently running (pcut_item_t *).
|
---|
| 132 | * @return EOK Always.
|
---|
| 133 | */
|
---|
| 134 | static int test_timeout_handler_fibril(void *arg) {
|
---|
| 135 | pcut_item_t *test = arg;
|
---|
| 136 | int timeout_sec = pcut_get_test_timeout(test);
|
---|
| 137 | suseconds_t timeout_us = (suseconds_t) timeout_sec * 1000 * 1000;
|
---|
| 138 |
|
---|
| 139 | fibril_mutex_lock(&forced_termination_mutex);
|
---|
| 140 | if (!test_running) {
|
---|
| 141 | goto leave_no_kill;
|
---|
| 142 | }
|
---|
| 143 | int rc = fibril_condvar_wait_timeout(&forced_termination_cv,
|
---|
| 144 | &forced_termination_mutex, timeout_us);
|
---|
| 145 | if (rc == ETIMEOUT) {
|
---|
| 146 | task_kill(test_task_id);
|
---|
| 147 | }
|
---|
| 148 | leave_no_kill:
|
---|
| 149 | fibril_mutex_unlock(&forced_termination_mutex);
|
---|
| 150 | return EOK;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[01579ad] | 153 | /** Run the test as a new task and report the result.
|
---|
| 154 | *
|
---|
| 155 | * @param self_path Path to itself, that is to current binary.
|
---|
| 156 | * @param test Test to be run.
|
---|
| 157 | */
|
---|
| 158 | void pcut_run_test_forking(const char *self_path, pcut_item_t *test) {
|
---|
| 159 | before_test_start(test);
|
---|
| 160 |
|
---|
| 161 | char tempfile_name[PCUT_TEMP_FILENAME_BUFFER_SIZE];
|
---|
| 162 | snprintf(tempfile_name, PCUT_TEMP_FILENAME_BUFFER_SIZE - 1, "pcut_%lld.tmp", (unsigned long long) task_get_id());
|
---|
| 163 | int tempfile = open(tempfile_name, O_CREAT | O_RDWR);
|
---|
| 164 | if (tempfile < 0) {
|
---|
| 165 | pcut_report_test_done(test, TEST_OUTCOME_ERROR, "Failed to create temporary file.", NULL, NULL);
|
---|
| 166 | return;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | char test_number_argument[MAX_TEST_NUMBER_WIDTH];
|
---|
| 170 | snprintf(test_number_argument, MAX_TEST_NUMBER_WIDTH, "-t%d", test->id);
|
---|
| 171 |
|
---|
| 172 | const char *const arguments[3] = {
|
---|
| 173 | self_path,
|
---|
| 174 | test_number_argument,
|
---|
| 175 | NULL
|
---|
| 176 | };
|
---|
| 177 |
|
---|
| 178 | int status = TEST_OUTCOME_PASS;
|
---|
| 179 |
|
---|
[9b20126] | 180 | task_wait_t test_task_wait;
|
---|
[bb9ec2d] | 181 | int rc = task_spawnvf(&test_task_id, &test_task_wait, self_path, arguments,
|
---|
| 182 | fileno(stdin), tempfile, tempfile);
|
---|
[01579ad] | 183 | if (rc != EOK) {
|
---|
| 184 | status = TEST_OUTCOME_ERROR;
|
---|
| 185 | goto leave_close_tempfile;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[134ac5d] | 188 | test_running = 1;
|
---|
| 189 |
|
---|
| 190 | fid_t killer_fibril = fibril_create(test_timeout_handler_fibril, test);
|
---|
| 191 | if (killer_fibril == 0) {
|
---|
| 192 | /* FIXME: somehow announce this problem. */
|
---|
| 193 | task_kill(test_task_id);
|
---|
| 194 | } else {
|
---|
| 195 | fibril_add_ready(killer_fibril);
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[01579ad] | 198 | task_exit_t task_exit;
|
---|
| 199 | int task_retval;
|
---|
[9b20126] | 200 | rc = task_wait(&test_task_wait, &task_exit, &task_retval);
|
---|
[01579ad] | 201 | if (rc != EOK) {
|
---|
| 202 | status = TEST_OUTCOME_ERROR;
|
---|
| 203 | goto leave_close_tempfile;
|
---|
| 204 | }
|
---|
| 205 | if (task_exit == TASK_EXIT_UNEXPECTED) {
|
---|
| 206 | status = TEST_OUTCOME_ERROR;
|
---|
| 207 | } else {
|
---|
| 208 | status = task_retval == 0 ? TEST_OUTCOME_PASS : TEST_OUTCOME_FAIL;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[134ac5d] | 211 | fibril_mutex_lock(&forced_termination_mutex);
|
---|
| 212 | test_running = 0;
|
---|
| 213 | fibril_condvar_signal(&forced_termination_cv);
|
---|
| 214 | fibril_mutex_unlock(&forced_termination_mutex);
|
---|
| 215 |
|
---|
[6afc9d7] | 216 | read(tempfile, extra_output_buffer, OUTPUT_BUFFER_SIZE);
|
---|
[01579ad] | 217 |
|
---|
| 218 | leave_close_tempfile:
|
---|
| 219 | close(tempfile);
|
---|
| 220 | unlink(tempfile_name);
|
---|
| 221 |
|
---|
| 222 | pcut_report_test_done_unparsed(test, status, extra_output_buffer, OUTPUT_BUFFER_SIZE);
|
---|
| 223 | }
|
---|
[9b20126] | 224 |
|
---|
| 225 | void pcut_hook_before_test(pcut_item_t *test) {
|
---|
| 226 | PCUT_UNUSED(test);
|
---|
| 227 |
|
---|
| 228 | /* Do nothing. */
|
---|
| 229 | }
|
---|