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 <unistd.h>
|
---|
37 | #include <sys/types.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <assert.h>
|
---|
40 | #include <stdio.h>
|
---|
41 | #include <task.h>
|
---|
42 | #include <fcntl.h>
|
---|
43 | #include "../internal.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | /* String functions. */
|
---|
47 |
|
---|
48 | int pcut_str_equals(const char *a, const char *b) {
|
---|
49 | return str_cmp(a, b) == 0;
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | int pcut_str_start_equals(const char *a, const char *b, int len) {
|
---|
54 | return str_lcmp(a, b, len) == 0;
|
---|
55 | }
|
---|
56 |
|
---|
57 | int pcut_str_size(const char *s) {
|
---|
58 | return str_size(s);
|
---|
59 | }
|
---|
60 |
|
---|
61 | int pcut_str_to_int(const char *s) {
|
---|
62 | int result = strtol(s, NULL, 10);
|
---|
63 | return result;
|
---|
64 | }
|
---|
65 |
|
---|
66 | char *pcut_str_find_char(const char *haystack, const char needle) {
|
---|
67 | return str_chr(haystack, needle);
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | /* Forking-mode related functions. */
|
---|
72 |
|
---|
73 | /** Maximum width of a test number. */
|
---|
74 | #define MAX_TEST_NUMBER_WIDTH 24
|
---|
75 |
|
---|
76 | /** Maximum length of a temporary file name. */
|
---|
77 | #define PCUT_TEMP_FILENAME_BUFFER_SIZE 128
|
---|
78 |
|
---|
79 | /** Maximum command-line length. */
|
---|
80 | #define MAX_COMMAND_LINE_LENGTH 1024
|
---|
81 |
|
---|
82 | /** Maximum size of stdout we are able to capture. */
|
---|
83 | #define OUTPUT_BUFFER_SIZE 8192
|
---|
84 |
|
---|
85 | /** Buffer for assertion and other error messages. */
|
---|
86 | static char error_message_buffer[OUTPUT_BUFFER_SIZE];
|
---|
87 |
|
---|
88 | /** Buffer for stdout from the test. */
|
---|
89 | static char extra_output_buffer[OUTPUT_BUFFER_SIZE];
|
---|
90 |
|
---|
91 | /** Prepare for a new test. */
|
---|
92 | static void before_test_start(pcut_item_t *test) {
|
---|
93 | pcut_report_test_start(test);
|
---|
94 |
|
---|
95 | memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
96 | memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE);
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** Run the test as a new task and report the result.
|
---|
100 | *
|
---|
101 | * @param self_path Path to itself, that is to current binary.
|
---|
102 | * @param test Test to be run.
|
---|
103 | */
|
---|
104 | void pcut_run_test_forking(const char *self_path, pcut_item_t *test) {
|
---|
105 | before_test_start(test);
|
---|
106 |
|
---|
107 | char tempfile_name[PCUT_TEMP_FILENAME_BUFFER_SIZE];
|
---|
108 | snprintf(tempfile_name, PCUT_TEMP_FILENAME_BUFFER_SIZE - 1, "pcut_%lld.tmp", (unsigned long long) task_get_id());
|
---|
109 | int tempfile = open(tempfile_name, O_CREAT | O_RDWR);
|
---|
110 | if (tempfile < 0) {
|
---|
111 | pcut_report_test_done(test, TEST_OUTCOME_ERROR, "Failed to create temporary file.", NULL, NULL);
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | char test_number_argument[MAX_TEST_NUMBER_WIDTH];
|
---|
116 | snprintf(test_number_argument, MAX_TEST_NUMBER_WIDTH, "-t%d", test->id);
|
---|
117 |
|
---|
118 | int *files[4];
|
---|
119 | int fd_stdin = fileno(stdin);
|
---|
120 | files[0] = &fd_stdin;
|
---|
121 | files[1] = &tempfile;
|
---|
122 | files[2] = &tempfile;
|
---|
123 | files[3] = NULL;
|
---|
124 |
|
---|
125 | const char *const arguments[3] = {
|
---|
126 | self_path,
|
---|
127 | test_number_argument,
|
---|
128 | NULL
|
---|
129 | };
|
---|
130 |
|
---|
131 | int status = TEST_OUTCOME_PASS;
|
---|
132 |
|
---|
133 | task_id_t task_id;
|
---|
134 | int rc = task_spawnvf(&task_id, self_path, arguments, files);
|
---|
135 | if (rc != EOK) {
|
---|
136 | status = TEST_OUTCOME_ERROR;
|
---|
137 | goto leave_close_tempfile;
|
---|
138 | }
|
---|
139 |
|
---|
140 | task_exit_t task_exit;
|
---|
141 | int task_retval;
|
---|
142 | rc = task_wait(task_id, &task_exit, &task_retval);
|
---|
143 | if (rc != EOK) {
|
---|
144 | status = TEST_OUTCOME_ERROR;
|
---|
145 | goto leave_close_tempfile;
|
---|
146 | }
|
---|
147 | if (task_exit == TASK_EXIT_UNEXPECTED) {
|
---|
148 | status = TEST_OUTCOME_ERROR;
|
---|
149 | } else {
|
---|
150 | status = task_retval == 0 ? TEST_OUTCOME_PASS : TEST_OUTCOME_FAIL;
|
---|
151 | }
|
---|
152 |
|
---|
153 | read_all(tempfile, extra_output_buffer, OUTPUT_BUFFER_SIZE);
|
---|
154 |
|
---|
155 | leave_close_tempfile:
|
---|
156 | close(tempfile);
|
---|
157 | unlink(tempfile_name);
|
---|
158 |
|
---|
159 | pcut_report_test_done_unparsed(test, status, extra_output_buffer, OUTPUT_BUFFER_SIZE);
|
---|
160 | }
|
---|