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