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