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

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

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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