source: mainline/uspace/lib/pcut/src/os/helenos.c@ 9b20126

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9b20126 was 9b20126, checked in by Vojtech Horky <vojtechhorky@…>, 11 years ago

Update PCUT to newest version

  • Property mode set to 100644
File size: 6.5 KB
Line 
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>
36#include <str_error.h>
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>
44#include <fibril_synch.h>
45#include "../internal.h"
46
47
48/* String functions. */
49
50int pcut_str_equals(const char *a, const char *b) {
51 return str_cmp(a, b) == 0;
52}
53
54
55int pcut_str_start_equals(const char *a, const char *b, int len) {
56 return str_lcmp(a, b, len) == 0;
57}
58
59int pcut_str_size(const char *s) {
60 return str_size(s);
61}
62
63int pcut_str_to_int(const char *s) {
64 int result = strtol(s, NULL, 10);
65 return result;
66}
67
68char *pcut_str_find_char(const char *haystack, const char needle) {
69 return str_chr(haystack, needle);
70}
71
72void 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
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. */
96static char error_message_buffer[OUTPUT_BUFFER_SIZE];
97
98/** Buffer for stdout from the test. */
99static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
100
101/** Prepare for a new test.
102 *
103 * @param test Test that is about to be run.
104 */
105static 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
112/** Mutex guard for forced_termination_cv. */
113static fibril_mutex_t forced_termination_mutex
114 = FIBRIL_MUTEX_INITIALIZER(forced_termination_mutex);
115
116/** Condition-variable for checking whether test timed-out. */
117static fibril_condvar_t forced_termination_cv
118 = FIBRIL_CONDVAR_INITIALIZER(forced_termination_cv);
119
120/** Spawned task id. */
121static 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 */
127static 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 */
134static 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 }
148leave_no_kill:
149 fibril_mutex_unlock(&forced_termination_mutex);
150 return EOK;
151}
152
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 */
158void 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 int *files[4];
173 int fd_stdin = fileno(stdin);
174 files[0] = &fd_stdin;
175 files[1] = &tempfile;
176 files[2] = &tempfile;
177 files[3] = NULL;
178
179 const char *const arguments[3] = {
180 self_path,
181 test_number_argument,
182 NULL
183 };
184
185 int status = TEST_OUTCOME_PASS;
186
187 task_wait_t test_task_wait;
188 int rc = task_spawnvf(&test_task_id, &test_task_wait, self_path, arguments, files);
189 if (rc != EOK) {
190 status = TEST_OUTCOME_ERROR;
191 goto leave_close_tempfile;
192 }
193
194 test_running = 1;
195
196 fid_t killer_fibril = fibril_create(test_timeout_handler_fibril, test);
197 if (killer_fibril == 0) {
198 /* FIXME: somehow announce this problem. */
199 task_kill(test_task_id);
200 } else {
201 fibril_add_ready(killer_fibril);
202 }
203
204 task_exit_t task_exit;
205 int task_retval;
206 rc = task_wait(&test_task_wait, &task_exit, &task_retval);
207 if (rc != EOK) {
208 status = TEST_OUTCOME_ERROR;
209 goto leave_close_tempfile;
210 }
211 if (task_exit == TASK_EXIT_UNEXPECTED) {
212 status = TEST_OUTCOME_ERROR;
213 } else {
214 status = task_retval == 0 ? TEST_OUTCOME_PASS : TEST_OUTCOME_FAIL;
215 }
216
217 fibril_mutex_lock(&forced_termination_mutex);
218 test_running = 0;
219 fibril_condvar_signal(&forced_termination_cv);
220 fibril_mutex_unlock(&forced_termination_mutex);
221
222 read_all(tempfile, extra_output_buffer, OUTPUT_BUFFER_SIZE);
223
224leave_close_tempfile:
225 close(tempfile);
226 unlink(tempfile_name);
227
228 pcut_report_test_done_unparsed(test, status, extra_output_buffer, OUTPUT_BUFFER_SIZE);
229}
230
231void pcut_hook_before_test(pcut_item_t *test) {
232 PCUT_UNUSED(test);
233
234 /* Do nothing. */
235}
Note: See TracBrowser for help on using the repository browser.