D-Bus  1.14.10
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 
41 static unsigned int failures = 0;
42 static unsigned int skipped = 0;
43 static unsigned int tap_test_counter = 0;
44 
45 /*
46  * Output TAP indicating a fatal error, and exit unsuccessfully.
47  */
48 void
49 _dbus_test_fatal (const char *format,
50  ...)
51 {
52  va_list ap;
53 
54  printf ("Bail out! ");
55  va_start (ap, format);
56  vprintf (format, ap);
57  va_end (ap);
58  printf ("\n");
59  fflush (stdout);
60  exit (1);
61 }
62 
63 /*
64  * Output TAP indicating a diagnostic (informational message).
65  */
66 void
67 _dbus_test_diag (const char *format,
68  ...)
69 {
70  va_list ap;
71 
72  printf ("# ");
73  va_start (ap, format);
74  vprintf (format, ap);
75  va_end (ap);
76  printf ("\n");
77  fflush (stdout);
78 }
79 
80 /*
81  * Output TAP indicating that all tests have been skipped, and exit
82  * successfully.
83  *
84  * This is only valid if _dbus_test_ok(), _dbus_test_not_ok(),
85  * etc. have not yet been called.
86  */
87 void
88 _dbus_test_skip_all (const char *format,
89  ...)
90 {
91  va_list ap;
92 
93  _dbus_assert (tap_test_counter == 0);
94 
95  printf ("1..0 # SKIP - ");
96  va_start (ap, format);
97  vprintf (format, ap);
98  va_end (ap);
99  printf ("\n");
100  fflush (stdout);
101  exit (0);
102 }
103 
104 /*
105  * Output TAP indicating that a test has passed, and increment the
106  * test counter.
107  */
108 void
109 _dbus_test_ok (const char *format,
110  ...)
111 {
112  va_list ap;
113 
114  printf ("ok %u - ", ++tap_test_counter);
115  va_start (ap, format);
116  vprintf (format, ap);
117  va_end (ap);
118  printf ("\n");
119  fflush (stdout);
120 }
121 
122 /*
123  * Output TAP indicating that a test has failed (in a way that is not
124  * fatal to the test executable), and increment the test counter.
125  */
126 void
127 _dbus_test_not_ok (const char *format,
128  ...)
129 {
130  va_list ap;
131 
132  printf ("not ok %u - ", ++tap_test_counter);
133  va_start (ap, format);
134  vprintf (format, ap);
135  va_end (ap);
136  printf ("\n");
137  failures++;
138  fflush (stdout);
139 }
140 
141 /*
142  * Output TAP indicating that a test has been skipped (in a way that is
143  * not fatal to the test executable), and increment the test counter.
144  */
145 void
146 _dbus_test_skip (const char *format,
147  ...)
148 {
149  va_list ap;
150 
151  printf ("ok %u # SKIP ", ++tap_test_counter);
152  ++skipped;
153  va_start (ap, format);
154  vprintf (format, ap);
155  va_end (ap);
156  printf ("\n");
157  fflush (stdout);
158 }
159 
160 /*
161  * Shut down libdbus, check that exactly previously_allocated memory
162  * blocks are allocated, and output TAP indicating a test pass or failure.
163  *
164  * Return TRUE if no leaks were detected.
165  */
166 void
167 _dbus_test_check_memleaks (const char *test_name)
168 {
169 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
170  dbus_shutdown ();
171 
172  if (_dbus_get_malloc_blocks_outstanding () == 0)
173  {
174  printf ("ok %u - %s did not leak memory\n", ++tap_test_counter,
175  test_name);
176  }
177  else
178  {
179  printf ("not ok %u - %s leaked %d blocks\n",
180  ++tap_test_counter, test_name,
181  _dbus_get_malloc_blocks_outstanding ());
182  failures++;
183  }
184 #else
185  _dbus_test_skip (
186  "unable to determine whether %s leaked memory (not compiled "
187  "with memory instrumentation)",
188  test_name);
189 #endif
190 }
191 
192 /*
193  * Output TAP indicating that testing has finished and no more tests
194  * are going to be run. Return the Unix-style exit code.
195  */
196 int
197 _dbus_test_done_testing (void)
198 {
199  _dbus_assert (skipped <= tap_test_counter);
200 
201  if (failures == 0)
202  _dbus_test_diag ("%u tests passed (%d skipped)",
203  tap_test_counter - skipped, skipped);
204  else
205  _dbus_test_diag ("%u/%u tests failed (%d skipped)",
206  failures, tap_test_counter - skipped, skipped);
207 
208  printf ("1..%u\n", tap_test_counter);
209  fflush (stdout);
210 
211  if (failures == 0)
212  return 0;
213 
214  return 1;
215 }
#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:888