1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#include <autoconf.h>
14#include <sel4test/gen_config.h>
15
16#include <stdbool.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <assert.h>
21
22#include <sel4test/test.h>
23#include <sel4test/testutil.h>
24
25#include <utils/util.h>
26
27/* buffer for test stdout size */
28#define STDOUT_CACHE 50000
29
30
31/* buffer stdout here during a test if we're outputting xml */
32static char current_stdout_bank[STDOUT_CACHE];
33/* index we are up to in the buffer */
34static int buf_index = 0;
35/* is the buffer currently enabled? otherwise just printf */
36static bool do_buffer_printf = false;
37
38/* global to track if the current test has passed */
39static test_result_t current_test_result = SUCCESS;
40
41#undef printf
42void sel4test_printf(const char *string)
43{
44    if (!do_buffer_printf) {
45        printf("%s", string);
46    } else if (buf_index < STDOUT_CACHE && buf_index >= 0) {
47        size_t len = STDOUT_CACHE - buf_index;
48        snprintf(current_stdout_bank + buf_index, len, "%s", string);
49        /* NULL terminate the destination in case 'string' was too long. */
50        current_stdout_bank[STDOUT_CACHE - 1] = '\0';
51        buf_index += strlen(string);
52    }
53}
54
55void sel4test_start_printf_buffer(void)
56{
57    /* only enable the printf buffer if we are buffering output */
58    if (config_set(CONFIG_PRINT_XML)) {
59        buf_index = 0;
60        do_buffer_printf = true;
61    }
62}
63
64void sel4test_end_printf_buffer(void)
65{
66    do_buffer_printf = false;
67    if (buf_index != 0) {
68        printf("\t\t<system-out>%s</system-out>\n", current_stdout_bank);
69        buf_index = 0;
70    }
71
72}
73
74void _sel4test_report_error(const char *error, const char *file, int line)
75{
76    if (config_set(CONFIG_PRINT_XML)) {
77        printf("\t\t<error>%s at line %d of file %s</error>\n", error, line, file);
78    } else {
79        printf("\tError: %s at line %d of file %s\n", error, line, file);
80    }
81    current_test_result = FAILURE;
82}
83
84void _sel4test_failure(const char *failure, const char *file, int line)
85{
86    if (config_set(CONFIG_PRINT_XML)) {
87        printf("\t\t<failure type=\"failure\">%s at line %d of file %s</failure>\n", failure, line, file);
88    } else {
89        printf("\tFailure: %s at line %d of file %s\n", failure, line, file);
90    }
91    current_test_result = FAILURE;
92}
93
94void _sel4test_abort(const char *failure, const char *file, int line)
95{
96    if (config_set(CONFIG_PRINT_XML)) {
97        printf("\t\t<failure type=\"failure\">%s at line %d of file %s</failure>\n", failure, line, file);
98    } else {
99        printf("\tFailure: %s at line %d of file %s\n", failure, line, file);
100    }
101    current_test_result = ABORT;
102}
103
104void sel4test_reset(void)
105{
106    current_test_result = SUCCESS;
107
108#ifdef CONFIG_ENABLE_BENCHMARKS
109    seL4_BenchmarkResetLog();
110#endif /* CONFIG_ENABLE_BENCHMARKS */
111}
112
113test_result_t sel4test_get_result(void)
114{
115    return current_test_result;
116}
117