[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 <errno.h>
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <task.h>
|
---|
[134ac5d] | 41 | #include <fibril_synch.h>
|
---|
[79ea5af] | 42 | #include <vfs/vfs.h>
|
---|
[01579ad] | 43 | #include "../internal.h"
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | /* String functions. */
|
---|
| 47 |
|
---|
[3bacee1] | 48 | int pcut_str_equals(const char *a, const char *b)
|
---|
| 49 | {
|
---|
[01579ad] | 50 | return str_cmp(a, b) == 0;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 |
|
---|
[3bacee1] | 54 | int pcut_str_start_equals(const char *a, const char *b, int len)
|
---|
| 55 | {
|
---|
[01579ad] | 56 | return str_lcmp(a, b, len) == 0;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[3bacee1] | 59 | int pcut_str_size(const char *s)
|
---|
| 60 | {
|
---|
[01579ad] | 61 | return str_size(s);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[3bacee1] | 64 | int pcut_str_to_int(const char *s)
|
---|
| 65 | {
|
---|
[01579ad] | 66 | int result = strtol(s, NULL, 10);
|
---|
| 67 | return result;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[3bacee1] | 70 | char *pcut_str_find_char(const char *haystack, const char needle)
|
---|
| 71 | {
|
---|
[01579ad] | 72 | return str_chr(haystack, needle);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[3bacee1] | 75 | void pcut_str_error(errno_t error, char *buffer, int size)
|
---|
| 76 | {
|
---|
[134ac5d] | 77 | const char *str = str_error(error);
|
---|
| 78 | if (str == NULL) {
|
---|
| 79 | str = "(strerror failure)";
|
---|
| 80 | }
|
---|
| 81 | str_cpy(buffer, size, str);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[01579ad] | 84 |
|
---|
| 85 | /* Forking-mode related functions. */
|
---|
| 86 |
|
---|
| 87 | /** Maximum width of a test number. */
|
---|
| 88 | #define MAX_TEST_NUMBER_WIDTH 24
|
---|
| 89 |
|
---|
| 90 | /** Maximum length of a temporary file name. */
|
---|
| 91 | #define PCUT_TEMP_FILENAME_BUFFER_SIZE 128
|
---|
| 92 |
|
---|
| 93 | /** Maximum command-line length. */
|
---|
| 94 | #define MAX_COMMAND_LINE_LENGTH 1024
|
---|
| 95 |
|
---|
| 96 | /** Maximum size of stdout we are able to capture. */
|
---|
| 97 | #define OUTPUT_BUFFER_SIZE 8192
|
---|
| 98 |
|
---|
| 99 | /** Buffer for assertion and other error messages. */
|
---|
| 100 | static char error_message_buffer[OUTPUT_BUFFER_SIZE];
|
---|
| 101 |
|
---|
| 102 | /** Buffer for stdout from the test. */
|
---|
| 103 | static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
|
---|
| 104 |
|
---|
[134ac5d] | 105 | /** Prepare for a new test.
|
---|
| 106 | *
|
---|
| 107 | * @param test Test that is about to be run.
|
---|
| 108 | */
|
---|
[3bacee1] | 109 | static void before_test_start(pcut_item_t *test)
|
---|
| 110 | {
|
---|
[01579ad] | 111 | pcut_report_test_start(test);
|
---|
| 112 |
|
---|
| 113 | memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
| 114 | memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[134ac5d] | 117 | /** Mutex guard for forced_termination_cv. */
|
---|
[3bacee1] | 118 | static fibril_mutex_t forced_termination_mutex =
|
---|
| 119 | FIBRIL_MUTEX_INITIALIZER(forced_termination_mutex);
|
---|
[134ac5d] | 120 |
|
---|
| 121 | /** Condition-variable for checking whether test timed-out. */
|
---|
[3bacee1] | 122 | static fibril_condvar_t forced_termination_cv =
|
---|
| 123 | FIBRIL_CONDVAR_INITIALIZER(forced_termination_cv);
|
---|
[134ac5d] | 124 |
|
---|
| 125 | /** Spawned task id. */
|
---|
| 126 | static task_id_t test_task_id;
|
---|
| 127 |
|
---|
| 128 | /** Flag whether test is still running.
|
---|
| 129 | *
|
---|
| 130 | * This flag is used when checking whether test timed-out.
|
---|
| 131 | */
|
---|
| 132 | static int test_running;
|
---|
| 133 |
|
---|
| 134 | /** Main fibril for checking whether test timed-out.
|
---|
| 135 | *
|
---|
| 136 | * @param arg Test that is currently running (pcut_item_t *).
|
---|
| 137 | * @return EOK Always.
|
---|
| 138 | */
|
---|
[3bacee1] | 139 | static errno_t test_timeout_handler_fibril(void *arg)
|
---|
| 140 | {
|
---|
[134ac5d] | 141 | pcut_item_t *test = arg;
|
---|
| 142 | int timeout_sec = pcut_get_test_timeout(test);
|
---|
| 143 | suseconds_t timeout_us = (suseconds_t) timeout_sec * 1000 * 1000;
|
---|
| 144 |
|
---|
| 145 | fibril_mutex_lock(&forced_termination_mutex);
|
---|
| 146 | if (!test_running) {
|
---|
| 147 | goto leave_no_kill;
|
---|
| 148 | }
|
---|
[b7fd2a0] | 149 | errno_t rc = fibril_condvar_wait_timeout(&forced_termination_cv,
|
---|
[3bacee1] | 150 | &forced_termination_mutex, timeout_us);
|
---|
[134ac5d] | 151 | if (rc == ETIMEOUT) {
|
---|
| 152 | task_kill(test_task_id);
|
---|
| 153 | }
|
---|
| 154 | leave_no_kill:
|
---|
| 155 | fibril_mutex_unlock(&forced_termination_mutex);
|
---|
| 156 | return EOK;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[01579ad] | 159 | /** Run the test as a new task and report the result.
|
---|
| 160 | *
|
---|
| 161 | * @param self_path Path to itself, that is to current binary.
|
---|
| 162 | * @param test Test to be run.
|
---|
| 163 | */
|
---|
[3bacee1] | 164 | int pcut_run_test_forking(const char *self_path, pcut_item_t *test)
|
---|
| 165 | {
|
---|
[01579ad] | 166 | before_test_start(test);
|
---|
| 167 |
|
---|
| 168 | char tempfile_name[PCUT_TEMP_FILENAME_BUFFER_SIZE];
|
---|
| 169 | snprintf(tempfile_name, PCUT_TEMP_FILENAME_BUFFER_SIZE - 1, "pcut_%lld.tmp", (unsigned long long) task_get_id());
|
---|
[f77c1c9] | 170 | int tempfile;
|
---|
[b7fd2a0] | 171 | errno_t rc = vfs_lookup_open(tempfile_name, WALK_REGULAR | WALK_MAY_CREATE, MODE_READ | MODE_WRITE, &tempfile);
|
---|
[f77c1c9] | 172 | if (rc != EOK) {
|
---|
[9eb1ff5] | 173 | pcut_report_test_done(test, PCUT_OUTCOME_INTERNAL_ERROR, "Failed to create temporary file.", NULL, NULL);
|
---|
| 174 | return PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
[01579ad] | 175 | }
|
---|
| 176 |
|
---|
| 177 | char test_number_argument[MAX_TEST_NUMBER_WIDTH];
|
---|
| 178 | snprintf(test_number_argument, MAX_TEST_NUMBER_WIDTH, "-t%d", test->id);
|
---|
| 179 |
|
---|
| 180 | const char *const arguments[3] = {
|
---|
| 181 | self_path,
|
---|
| 182 | test_number_argument,
|
---|
| 183 | NULL
|
---|
| 184 | };
|
---|
| 185 |
|
---|
[9eb1ff5] | 186 | int status = PCUT_OUTCOME_PASS;
|
---|
[01579ad] | 187 |
|
---|
[9b20126] | 188 | task_wait_t test_task_wait;
|
---|
[f77c1c9] | 189 | rc = task_spawnvf(&test_task_id, &test_task_wait, self_path, arguments,
|
---|
[bb9ec2d] | 190 | fileno(stdin), tempfile, tempfile);
|
---|
[01579ad] | 191 | if (rc != EOK) {
|
---|
[9eb1ff5] | 192 | status = PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
[01579ad] | 193 | goto leave_close_tempfile;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[134ac5d] | 196 | test_running = 1;
|
---|
| 197 |
|
---|
| 198 | fid_t killer_fibril = fibril_create(test_timeout_handler_fibril, test);
|
---|
| 199 | if (killer_fibril == 0) {
|
---|
| 200 | /* FIXME: somehow announce this problem. */
|
---|
| 201 | task_kill(test_task_id);
|
---|
| 202 | } else {
|
---|
| 203 | fibril_add_ready(killer_fibril);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[01579ad] | 206 | task_exit_t task_exit;
|
---|
| 207 | int task_retval;
|
---|
[9b20126] | 208 | rc = task_wait(&test_task_wait, &task_exit, &task_retval);
|
---|
[01579ad] | 209 | if (rc != EOK) {
|
---|
[9eb1ff5] | 210 | status = PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
[01579ad] | 211 | goto leave_close_tempfile;
|
---|
| 212 | }
|
---|
| 213 | if (task_exit == TASK_EXIT_UNEXPECTED) {
|
---|
[9eb1ff5] | 214 | status = PCUT_OUTCOME_INTERNAL_ERROR;
|
---|
[01579ad] | 215 | } else {
|
---|
[9eb1ff5] | 216 | status = task_retval == 0 ? PCUT_OUTCOME_PASS : PCUT_OUTCOME_FAIL;
|
---|
[01579ad] | 217 | }
|
---|
| 218 |
|
---|
[134ac5d] | 219 | fibril_mutex_lock(&forced_termination_mutex);
|
---|
| 220 | test_running = 0;
|
---|
| 221 | fibril_condvar_signal(&forced_termination_cv);
|
---|
| 222 | fibril_mutex_unlock(&forced_termination_mutex);
|
---|
| 223 |
|
---|
[58898d1d] | 224 | aoff64_t pos = 0;
|
---|
[8e3498b] | 225 | size_t nread;
|
---|
| 226 | vfs_read(tempfile, &pos, extra_output_buffer, OUTPUT_BUFFER_SIZE, &nread);
|
---|
[01579ad] | 227 |
|
---|
| 228 | leave_close_tempfile:
|
---|
[9c4cf0d] | 229 | vfs_put(tempfile);
|
---|
[79ea5af] | 230 | vfs_unlink_path(tempfile_name);
|
---|
[01579ad] | 231 |
|
---|
| 232 | pcut_report_test_done_unparsed(test, status, extra_output_buffer, OUTPUT_BUFFER_SIZE);
|
---|
[9eb1ff5] | 233 |
|
---|
| 234 | return status;
|
---|
[01579ad] | 235 | }
|
---|
[9b20126] | 236 |
|
---|
[3bacee1] | 237 | void pcut_hook_before_test(pcut_item_t *test)
|
---|
| 238 | {
|
---|
[9b20126] | 239 | PCUT_UNUSED(test);
|
---|
| 240 |
|
---|
| 241 | /* Do nothing. */
|
---|
| 242 | }
|
---|