D-Bus 1.13.18
dbus-test-tap.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-test-tap — TAP helpers for "embedded tests"
3 *
4 * Copyright © 2017 Collabora Ltd.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction,
9 * including without limitation the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27#include <config.h>
28#include "dbus/dbus-test-tap.h"
29
30/*
31 * TAP, the Test Anything Protocol, is a text-based syntax for test-cases
32 * to report results to test harnesses.
33 *
34 * See <http://testanything.org/> for details of the syntax, which
35 * will not be explained here.
36 */
37
38#include <stdio.h>
39#include <stdlib.h>
40
41static unsigned int failures = 0;
42static unsigned int tap_test_counter = 0;
43
44/*
45 * Output TAP indicating a fatal error, and exit unsuccessfully.
46 */
47void
48_dbus_test_fatal (const char *format,
49 ...)
50{
51 va_list ap;
52
53 printf ("Bail out! ");
54 va_start (ap, format);
55 vprintf (format, ap);
56 va_end (ap);
57 printf ("\n");
58 fflush (stdout);
59 exit (1);
60}
61
62/*
63 * Output TAP indicating a diagnostic (informational message).
64 */
65void
66_dbus_test_diag (const char *format,
67 ...)
68{
69 va_list ap;
70
71 printf ("# ");
72 va_start (ap, format);
73 vprintf (format, ap);
74 va_end (ap);
75 printf ("\n");
76 fflush (stdout);
77}
78
79/*
80 * Output TAP indicating that all tests have been skipped, and exit
81 * successfully.
82 *
83 * This is only valid if _dbus_test_ok(), _dbus_test_not_ok(),
84 * etc. have not yet been called.
85 */
86void
87_dbus_test_skip_all (const char *format,
88 ...)
89{
90 va_list ap;
91
92 _dbus_assert (tap_test_counter == 0);
93
94 printf ("1..0 # SKIP - ");
95 va_start (ap, format);
96 vprintf (format, ap);
97 va_end (ap);
98 printf ("\n");
99 fflush (stdout);
100 exit (0);
101}
102
103/*
104 * Output TAP indicating that a test has passed, and increment the
105 * test counter.
106 */
107void
108_dbus_test_ok (const char *format,
109 ...)
110{
111 va_list ap;
112
113 printf ("ok %u - ", ++tap_test_counter);
114 va_start (ap, format);
115 vprintf (format, ap);
116 va_end (ap);
117 printf ("\n");
118 fflush (stdout);
119}
120
121/*
122 * Output TAP indicating that a test has failed (in a way that is not
123 * fatal to the test executable), and increment the test counter.
124 */
125void
126_dbus_test_not_ok (const char *format,
127 ...)
128{
129 va_list ap;
130
131 printf ("not ok %u - ", ++tap_test_counter);
132 va_start (ap, format);
133 vprintf (format, ap);
134 va_end (ap);
135 printf ("\n");
136 failures++;
137 fflush (stdout);
138}
139
140/*
141 * Output TAP indicating that a test has been skipped (in a way that is
142 * not fatal to the test executable), and increment the test counter.
143 */
144void
145_dbus_test_skip (const char *format,
146 ...)
147{
148 va_list ap;
149
150 printf ("ok %u # SKIP ", ++tap_test_counter);
151 va_start (ap, format);
152 vprintf (format, ap);
153 va_end (ap);
154 printf ("\n");
155 fflush (stdout);
156}
157
158/*
159 * Shut down libdbus, check that exactly previously_allocated memory
160 * blocks are allocated, and output TAP indicating a test pass or failure.
161 *
162 * Return TRUE if no leaks were detected.
163 */
164void
165_dbus_test_check_memleaks (const char *test_name)
166{
167#ifdef DBUS_ENABLE_EMBEDDED_TESTS
168 dbus_shutdown ();
169
170 if (_dbus_get_malloc_blocks_outstanding () == 0)
171 {
172 printf ("ok %u - %s did not leak memory\n", ++tap_test_counter,
173 test_name);
174 }
175 else
176 {
177 printf ("not ok %u - %s leaked %d blocks\n",
178 ++tap_test_counter, test_name,
179 _dbus_get_malloc_blocks_outstanding ());
180 failures++;
181 }
182#else
183 _dbus_test_skip (
184 "unable to determine whether %s leaked memory (not compiled "
185 "with memory instrumentation)",
186 test_name);
187#endif
188}
189
190/*
191 * Output TAP indicating that testing has finished and no more tests
192 * are going to be run. Return the Unix-style exit code.
193 */
194int
195_dbus_test_done_testing (void)
196{
197 if (failures == 0)
198 _dbus_test_diag ("%u tests passed", tap_test_counter);
199 else
200 _dbus_test_diag ("%u/%u tests failed", failures, tap_test_counter);
201
202 printf ("1..%u\n", tap_test_counter);
203 fflush (stdout);
204
205 if (failures == 0)
206 return 0;
207
208 return 1;
209}
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
void dbus_shutdown(void)
Frees all memory allocated internally by libdbus and reverses the effects of dbus_threads_init().
Definition: dbus-memory.c:900