source: mainline/uspace/lib/pcut/src/report/report.c@ a05ec66

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a05ec66 was 76d0981d, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Use proper boolean constant in while loops.

  • Property mode set to 100644
File size: 6.9 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 * Common functions for test results reporting.
32 */
33
34#include "../internal.h"
35#ifdef __helenos__
36#include <str.h>
37#else
38#include <string.h>
39#endif
40#include <stdbool.h>
41#include <stdio.h>
42
43/** Currently used report ops. */
44static pcut_report_ops_t *report_ops = NULL;
45
46/** Call a report function if it is available.
47 *
48 * @param op Operation to be called on the pcut_report_ops_t.
49 * @param ... Arguments to the operation.
50 */
51#define REPORT_CALL(op, ...) \
52 if ((report_ops != NULL) && (report_ops->op != NULL)) report_ops->op(__VA_ARGS__)
53
54/** Call a report function if it is available.
55 *
56 * @param op Operation to be called on the pcut_report_ops_t.
57 */
58#define REPORT_CALL_NO_ARGS(op) \
59 if ((report_ops != NULL) && (report_ops->op != NULL)) report_ops->op()
60
61/** Print error message.
62 *
63 * NULL or empty message is silently ignored.
64 *
65 * The message is printed with a special 3-zero-byte prefix to be later
66 * parsed when reporting the results from a different process.
67 *
68 * @param msg The message to be printed.
69 */
70void pcut_print_fail_message(const char *msg)
71{
72 if (msg == NULL) {
73 return;
74 }
75 if (pcut_str_size(msg) == 0) {
76 return;
77 }
78
79 printf("%c%c%c%s\n%c", 0, 0, 0, msg, 0);
80}
81
82/** Size of buffer for storing error messages or extra test output. */
83#define BUFFER_SIZE 4096
84
85/** Buffer for stdout from the test. */
86static char buffer_for_extra_output[BUFFER_SIZE];
87
88/** Buffer for assertion and other error messages. */
89static char buffer_for_error_messages[BUFFER_SIZE];
90
91/** Parse output of a single test.
92 *
93 * @param full_output Full unparsed output.
94 * @param full_output_size Size of @p full_output in bytes.
95 * @param stdio_buffer Where to store normal output from the test.
96 * @param stdio_buffer_size Size of @p stdio_buffer in bytes.
97 * @param error_buffer Where to store error messages from the test.
98 * @param error_buffer_size Size of @p error_buffer in bytes.
99 */
100static void parse_command_output(const char *full_output, size_t full_output_size,
101 char *stdio_buffer, size_t stdio_buffer_size,
102 char *error_buffer, size_t error_buffer_size)
103{
104 memset(stdio_buffer, 0, stdio_buffer_size);
105 memset(error_buffer, 0, error_buffer_size);
106
107 /* Ensure that we do not read past the full_output. */
108 if (full_output[full_output_size - 1] != 0) {
109 /* FIXME: can this happen? */
110 return;
111 }
112
113 while (true) {
114 size_t message_length;
115
116 /* First of all, count number of zero bytes before the text. */
117 size_t cont_zeros_count = 0;
118 while (full_output[0] == 0) {
119 cont_zeros_count++;
120 full_output++;
121 full_output_size--;
122 if (full_output_size == 0) {
123 return;
124 }
125 }
126
127 /* Determine the length of the text after the zeros. */
128 message_length = pcut_str_size(full_output);
129
130 if (cont_zeros_count < 2) {
131 /* Okay, standard I/O. */
132 if (message_length > stdio_buffer_size) {
133 /* TODO: handle gracefully */
134 return;
135 }
136 memcpy(stdio_buffer, full_output, message_length);
137 stdio_buffer += message_length;
138 stdio_buffer_size -= message_length;
139 } else {
140 /* Error message. */
141 if (message_length > error_buffer_size) {
142 /* TODO: handle gracefully */
143 return;
144 }
145 memcpy(error_buffer, full_output, message_length);
146 error_buffer += message_length;
147 error_buffer_size -= message_length;
148 }
149
150 full_output += message_length + 1;
151 full_output_size -= message_length + 1;
152 }
153}
154
155/** Use given set of functions for error reporting.
156 *
157 * @param ops Functions to use.
158 */
159void pcut_report_register_handler(pcut_report_ops_t *ops)
160{
161 report_ops = ops;
162}
163
164/** Initialize the report.
165 *
166 * @param all_items List of all tests that could be run.
167 */
168void pcut_report_init(pcut_item_t *all_items)
169{
170 REPORT_CALL(init, all_items);
171}
172
173/** Report that a test suite was started.
174 *
175 * @param suite Suite that was just started.
176 */
177void pcut_report_suite_start(pcut_item_t *suite)
178{
179 REPORT_CALL(suite_start, suite);
180}
181
182/** Report that a test suite was completed.
183 *
184 * @param suite Suite that just completed.
185 */
186void pcut_report_suite_done(pcut_item_t *suite)
187{
188 REPORT_CALL(suite_done, suite);
189}
190
191/** Report that a test is about to start.
192 *
193 * @param test Test to be run just about now.
194 */
195void pcut_report_test_start(pcut_item_t *test)
196{
197 REPORT_CALL(test_start, test);
198}
199
200/** Report that a test was completed.
201 *
202 * @param test Test that just finished.
203 * @param outcome Outcome of the test.
204 * @param error_message Buffer with error message.
205 * @param teardown_error_message Buffer with error message from a tear-down function.
206 * @param extra_output Extra output from the test (stdout).
207 */
208void pcut_report_test_done(pcut_item_t *test, int outcome,
209 const char *error_message, const char *teardown_error_message,
210 const char *extra_output)
211{
212 REPORT_CALL(test_done, test, outcome, error_message, teardown_error_message,
213 extra_output);
214}
215
216/** Report that a test was completed with unparsed test output.
217 *
218 * @param test Test that just finished
219 * @param outcome Outcome of the test.
220 * @param unparsed_output Buffer with all the output from the test.
221 * @param unparsed_output_size Size of @p unparsed_output in bytes.
222 */
223void pcut_report_test_done_unparsed(pcut_item_t *test, int outcome,
224 const char *unparsed_output, size_t unparsed_output_size)
225{
226
227 parse_command_output(unparsed_output, unparsed_output_size,
228 buffer_for_extra_output, BUFFER_SIZE,
229 buffer_for_error_messages, BUFFER_SIZE);
230
231 pcut_report_test_done(test, outcome, buffer_for_error_messages, NULL, buffer_for_extra_output);
232}
233
234/** Close the report.
235 *
236 */
237void pcut_report_done(void)
238{
239 REPORT_CALL_NO_ARGS(done);
240}
241
Note: See TracBrowser for help on using the repository browser.