| 1 | #include <cgreen/breadcrumb.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdint.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | #include <errno.h> |
| 7 | |
| 8 | #include "utils.h" |
| 9 | |
| 10 | #include "xml_reporter_internal.h" |
| 11 | |
| 12 | #ifdef __ANDROID__ |
| 13 | #include "cgreen/internal/android_headers/androidcompat.h" |
| 14 | #endif // #ifdef __ANDROID__ |
| 15 | |
| 16 | typedef struct { |
| 17 | XmlPrinter *printer; |
| 18 | int segment_count; |
| 19 | } XmlMemo; |
| 20 | |
| 21 | |
| 22 | static void xml_reporter_start_suite(TestReporter *reporter, const char *name, |
| 23 | int count); |
| 24 | static void xml_reporter_start_test(TestReporter *reporter, const char *name); |
| 25 | static void xml_reporter_finish_test(TestReporter *reporter, const char *filename, |
| 26 | int line, const char *message); |
| 27 | static void xml_reporter_finish_suite(TestReporter *reporter, const char *filename, |
| 28 | int line); |
| 29 | static void xml_show_skip(TestReporter *reporter, const char *file, int line); |
| 30 | static void xml_show_fail(TestReporter *reporter, const char *file, int line, |
| 31 | const char *message, va_list arguments); |
| 32 | static void xml_show_incomplete(TestReporter *reporter, const char *filename, |
| 33 | int line, const char *message, va_list arguments); |
| 34 | |
| 35 | |
| 36 | void set_xml_reporter_printer(TestReporter *reporter, XmlPrinter *new_printer) { |
| 37 | XmlMemo *memo = (XmlMemo *)reporter->memo; |
| 38 | memo->printer = new_printer; |
| 39 | } |
| 40 | |
| 41 | static const char *file_prefix; |
| 42 | |
| 43 | TestReporter *create_xml_reporter(const char *prefix) { |
| 44 | TestReporter *reporter; |
| 45 | XmlMemo *memo; |
| 46 | |
| 47 | reporter = create_reporter(); |
| 48 | if (reporter == NULL) { |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | memo = (XmlMemo *) malloc(sizeof(XmlMemo)); |
| 53 | if (memo == NULL) { |
| 54 | destroy_reporter(reporter); |
| 55 | return NULL; |
| 56 | } |
| 57 | memo->printer = fprintf; |
| 58 | reporter->memo = memo; |
| 59 | |
| 60 | file_prefix = prefix; |
| 61 | reporter->start_suite = &xml_reporter_start_suite; |
| 62 | reporter->start_test = &xml_reporter_start_test; |
| 63 | reporter->show_fail = &xml_show_fail; |
| 64 | reporter->show_skip = &xml_show_skip; |
| 65 | reporter->show_incomplete = &xml_show_incomplete; |
| 66 | reporter->finish_test = &xml_reporter_finish_test; |
| 67 | reporter->finish_suite = &xml_reporter_finish_suite; |
| 68 | return reporter; |
| 69 | } |
| 70 | |
| 71 | static int file_stack_p = 0; |
| 72 | static FILE *file_stack[100]; |
| 73 | |
| 74 | static char *indent(TestReporter *reporter) { |
| 75 | static char buffer[1000]; |
| 76 | int depth = get_breadcrumb_depth(reporter->breadcrumb); |
| 77 | |
| 78 | memset(buffer, '\0', depth+1); |
| 79 | memset(buffer, '\t', depth); |
| 80 | return buffer; |
| 81 | } |
| 82 | |
| 83 | static void print_path_separator_if_needed(XmlMemo *memo, int *more_segments) { |
| 84 | if (*more_segments > 0) { |
| 85 | memo->printer(file_stack[file_stack_p-1], "/"); |
| 86 | (*more_segments)--; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void print_path_segment_walker(const char *segment, void *void_memo) { |
| 91 | XmlMemo *memo = (XmlMemo *)void_memo; |
| 92 | |
| 93 | memo->printer(file_stack[file_stack_p-1], "%s", segment); |
| 94 | print_path_separator_if_needed(memo, &memo->segment_count); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | #ifndef PATH_MAX |
| 99 | #define PATH_MAX 4096 |
| 100 | #endif |
| 101 | static char suite_path[PATH_MAX]; |
| 102 | |
| 103 | static void strcat_path_segment(const char *segment, void *more_segments) { |
| 104 | (void)more_segments; |
| 105 | if (suite_path[0] != '\0') strcat(suite_path, "-"); |
| 106 | strncat(suite_path, segment, sizeof(suite_path)-strlen(suite_path)-1); |
| 107 | } |
| 108 | |
| 109 | static void add_suite_name(const char *suite_name) { |
| 110 | if (suite_path[0] != '\0') |
| 111 | strcat(suite_path, "-"); |
| 112 | strncat(suite_path, suite_name, sizeof(suite_path)-strlen(suite_path)-1); |
| 113 | } |
| 114 | |
| 115 | static void xml_reporter_start_suite(TestReporter *reporter, const char *suitename, int count) { |
| 116 | char filename[PATH_MAX]; |
| 117 | int segment_decrementer = reporter->breadcrumb->depth; |
| 118 | XmlMemo *memo = (XmlMemo *)reporter->memo; |
| 119 | FILE *out; |
| 120 | |
| 121 | (void)count; /* UNUSED */ |
| 122 | |
| 123 | reporter->passes = 0; |
| 124 | reporter->failures = 0; |
| 125 | reporter->skips = 0; |
| 126 | reporter->exceptions = 0; |
| 127 | |
| 128 | suite_path[0] = '\0'; |
| 129 | walk_breadcrumb(reporter->breadcrumb, strcat_path_segment, &segment_decrementer); |
| 130 | add_suite_name(suitename); |
| 131 | |
| 132 | if (snprintf(filename, sizeof(filename), "%s-%s.xml", file_prefix, suite_path) < 0) |
| 133 | PANIC("Error when creating output filename"); |
| 134 | |
| 135 | if (memo->printer == fprintf) { |
| 136 | // If we're really printing to files, then open one... |
| 137 | out = fopen(filename, "w"); |
| 138 | if (!out) { |
| 139 | memo->printer(stderr, "could not open %s: %s\r\n", filename, strerror(errno)); |
| 140 | exit(EXIT_FAILURE); |
| 141 | } |
| 142 | } else |
| 143 | out = stdout; |
| 144 | |
| 145 | file_stack[file_stack_p++] = out; |
| 146 | memo->printer(out, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"); |
| 147 | memo->printer(out, indent(reporter)); |
| 148 | memo->printer(out, "<testsuite name=\"%s\">\n", suite_path); |
| 149 | reporter_start_suite(reporter, suitename, 0); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /* Accumulate output from the actual test (the "<testcase>" nodes) in |
| 154 | a file since the tests usually are run in a child processes, so |
| 155 | there is no simple way to save output from it and then use it in |
| 156 | the parent (start_test() and finish_test() are run from parent) */ |
| 157 | |
| 158 | static char *output = NULL; |
| 159 | static FILE *child_output_tmpfile; |
| 160 | |
| 161 | static void xml_reporter_start_test(TestReporter *reporter, const char *testname) { |
| 162 | XmlMemo *memo = (XmlMemo *)reporter->memo; |
| 163 | FILE *out = file_stack[file_stack_p-1]; |
| 164 | |
| 165 | memo->printer(out, indent(reporter)); |
| 166 | memo->printer(out, "<testcase classname=\""); |
| 167 | memo->segment_count = reporter->breadcrumb->depth - 1; |
| 168 | walk_breadcrumb(reporter->breadcrumb, print_path_segment_walker, memo); |
| 169 | |
| 170 | // Don't terminate the XML-node now so that we can add the duration later |
| 171 | // But then we need to accumulate subsequent output to report later |
| 172 | memo->printer(out, "\" name=\"%s\"", testname); |
| 173 | reporter_start_test(reporter, testname); |
| 174 | output = strdup(""); |
| 175 | |
| 176 | child_output_tmpfile = tmpfile(); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static char *concat(char *head, const char *tail) { |
| 181 | char *result = realloc(head, strlen(head)+strlen(tail)+1); |
| 182 | return strcat(result, tail); |
| 183 | } |
| 184 | |
| 185 | static void xml_show_skip(TestReporter *reporter, const char *file, int line) { |
| 186 | (void)file; |
| 187 | (void)line; |
| 188 | |
| 189 | output = concat(output, indent(reporter)); |
| 190 | output = concat(output, "\t<skipped />\n"); |
| 191 | |
| 192 | fseek(child_output_tmpfile,0,SEEK_SET); |
| 193 | fputs(output, child_output_tmpfile); |
| 194 | } |
| 195 | |
| 196 | static void xml_concat_escaped_message(const char *message, va_list arguments) { |
| 197 | char buffer[1000]; |
| 198 | vsnprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), message, arguments); |
| 199 | |
| 200 | size_t current_char_position = 0; |
| 201 | for (; current_char_position < strlen(buffer); current_char_position++) { |
| 202 | switch (buffer[current_char_position]) { |
| 203 | case '"': |
| 204 | output = concat(output, """); |
| 205 | break; |
| 206 | case '&': |
| 207 | output = concat(output, "&"); |
| 208 | break; |
| 209 | case '<': |
| 210 | output = concat(output, "<"); |
| 211 | break; |
| 212 | case '>': |
| 213 | output = concat(output, ">"); |
| 214 | break; |
| 215 | case '\'': |
| 216 | output = concat(output, "'"); |
| 217 | break; |
| 218 | default: { |
| 219 | char single_char[2] = {0}; |
| 220 | single_char[0] = buffer[current_char_position]; |
| 221 | output = concat(output, single_char); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | static void xml_show_fail(TestReporter *reporter, const char *file, int line, const char *message, va_list arguments) { |
| 228 | char buffer[1000]; |
| 229 | |
| 230 | output = concat(output, indent(reporter)); |
| 231 | output = concat(output, "<failure message=\""); |
| 232 | |
| 233 | xml_concat_escaped_message(message, arguments); |
| 234 | output = concat(output, "\">\n"); |
| 235 | output = concat(output, indent(reporter)); |
| 236 | |
| 237 | snprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), |
| 238 | "\t<location file=\"%s\" line=\"%d\"/>\n", file, line); |
| 239 | output = concat(output, buffer); |
| 240 | output = concat(output, indent(reporter)); |
| 241 | output = concat(output, "</failure>\n"); |
| 242 | |
| 243 | fseek(child_output_tmpfile,0,SEEK_SET); |
| 244 | fputs(output, child_output_tmpfile); |
| 245 | } |
| 246 | |
| 247 | static void xml_show_incomplete(TestReporter *reporter, const char *filename, int line, const char *message, va_list arguments) { |
| 248 | char buffer[1000]; |
| 249 | |
| 250 | output = concat(output, indent(reporter)); |
| 251 | output = concat(output, "<error type=\"Fatal\" message=\""); |
| 252 | vsnprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), |
| 253 | message ? message: "Test terminated unexpectedly, likely from a non-standard exception or Posix signal", arguments); |
| 254 | output = concat(output, buffer); |
| 255 | output = concat(output, "\">\n"); |
| 256 | output = concat(output, indent(reporter)); |
| 257 | snprintf(buffer, sizeof(buffer)/sizeof(buffer[0]),"\t<location file=\"%s\" line=\"%d\"/>\n", filename, line); |
| 258 | output = concat(output, buffer); |
| 259 | output = concat(output, indent(reporter)); |
| 260 | output = concat(output, "</error>\n"); |
| 261 | |
| 262 | fseek(child_output_tmpfile,0,SEEK_SET); |
| 263 | fputs(output, child_output_tmpfile); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | static void transfer_output_from(FILE *tmpfile, XmlPrinter printer, FILE *out) { |
| 268 | fseek(tmpfile, 0, SEEK_SET); |
| 269 | char buffer[1000]; |
| 270 | while (fgets(buffer, 1000, tmpfile) != NULL) |
| 271 | printer(out, "%s", buffer); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | static void xml_reporter_finish_test(TestReporter *reporter, const char *filename, int line, const char *message) { |
| 276 | XmlMemo *memo = (XmlMemo *)reporter->memo; |
| 277 | FILE *out = file_stack[file_stack_p-1]; |
| 278 | |
| 279 | reporter_finish_test(reporter, filename, line, message); |
| 280 | memo->printer(out, " time=\"%.5f\">\n", (double)reporter->duration/(double)1000); |
| 281 | if (output && strlen(output) == 0) { |
| 282 | free(output); |
| 283 | output = NULL; |
| 284 | } |
| 285 | |
| 286 | transfer_output_from(child_output_tmpfile, memo->printer, out); |
| 287 | |
| 288 | memo->printer(out, indent(reporter)); |
| 289 | memo->printer(out, "</testcase>\n"); |
| 290 | fflush(out); |
| 291 | } |
| 292 | |
| 293 | static void xml_reporter_finish_suite(TestReporter *reporter, const char *filename, int line) { |
| 294 | XmlMemo *memo = (XmlMemo *)reporter->memo; |
| 295 | FILE *out = file_stack[--file_stack_p]; |
| 296 | |
| 297 | reporter_finish_suite(reporter, filename, line); |
| 298 | |
| 299 | reporter->total_passes += reporter->passes; |
| 300 | reporter->total_failures += reporter->failures; |
| 301 | reporter->total_skips += reporter->skips; |
| 302 | reporter->total_exceptions += reporter->exceptions; |
| 303 | |
| 304 | // TODO: Here we should backpatch the time for the suite but that's not |
| 305 | // exactly necessary as Jenkins, at least, seems to sum it up automatically |
| 306 | memo->printer(out, indent(reporter)); |
| 307 | memo->printer(out, "</testsuite>\n"); |
| 308 | if (file_stack_p != 0) |
| 309 | fclose(out); |
| 310 | } |