2005-01-27 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-test.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-test.c  Program to run all tests
3  *
4  * Copyright (C) 2002, 2003, 2004, 2005  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <config.h>
25 #include "dbus-test.h"
26 #include "dbus-sysdeps.h"
27 #include "dbus-internals.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #ifdef DBUS_BUILD_TESTS
32 static void
33 die (const char *failure)
34 {
35   fprintf (stderr, "Unit test failed: %s\n", failure);
36   exit (1);
37 }
38
39 static void
40 check_memleaks (void)
41 {
42   dbus_shutdown ();
43
44   printf ("%s: checking for memleaks\n", "dbus-test");
45   if (_dbus_get_malloc_blocks_outstanding () != 0)
46     {
47       _dbus_warn ("%d dbus_malloc blocks were not freed\n",
48                   _dbus_get_malloc_blocks_outstanding ());
49       die ("memleaks");
50     }
51 }
52
53 #endif /* DBUS_BUILD_TESTS */
54
55 /**
56  * An exported symbol to be run in order to execute
57  * unit tests. Should not be used by
58  * any app other than our test app, this symbol
59  * won't exist in some builds of the library.
60  * (with --enable-tests=no)
61  *
62  * @param test_data_dir the directory with test data (test/data normally)
63  */
64 void
65 dbus_internal_do_not_use_run_tests (const char *test_data_dir)
66 {
67 #ifdef DBUS_BUILD_TESTS
68   if (!_dbus_threads_init_debug ())
69     die ("debug threads init");
70   
71   if (test_data_dir == NULL)
72     test_data_dir = _dbus_getenv ("DBUS_TEST_DATA");
73
74   if (test_data_dir != NULL)
75     printf ("Test data in %s\n", test_data_dir);
76   else
77     printf ("No test data!\n");
78
79   printf ("%s: running string tests\n", "dbus-test");
80   if (!_dbus_string_test ())
81     die ("strings");
82
83   check_memleaks ();
84   
85   printf ("%s: running sysdeps tests\n", "dbus-test");
86   if (!_dbus_sysdeps_test ())
87     die ("sysdeps");
88
89   check_memleaks ();
90   
91   printf ("%s: running data slot tests\n", "dbus-test");
92   if (!_dbus_data_slot_test ())
93     die ("dataslot");
94
95   check_memleaks ();
96   
97   printf ("%s: running address parse tests\n", "dbus-test");
98   if (!_dbus_address_test ())
99     die ("address parsing");
100
101   check_memleaks ();
102
103   printf ("%s: running server listen tests\n", "dbus-test");
104   if (!_dbus_server_test ())
105     die ("server listen");
106
107   check_memleaks ();
108
109   printf ("%s: running object tree tests\n", "dbus-test");
110   if (!_dbus_object_tree_test ())
111     die ("object tree");
112   
113   check_memleaks ();
114   
115   printf ("%s: running marshalling tests\n", "dbus-test");
116   if (!_dbus_marshal_test ())
117     die ("marshalling");
118
119   check_memleaks ();
120
121 #if 1
122   printf ("%s: running recursive marshalling tests\n", "dbus-test");
123   if (!_dbus_marshal_recursive_test ())
124     die ("recursive marshal");
125
126   check_memleaks ();
127 #else
128   _dbus_warn ("recursive marshal tests disabled\n");
129 #endif
130
131   printf ("%s: running byteswap tests\n", "dbus-test");
132   if (!_dbus_marshal_byteswap_test ())
133     die ("byteswap marshaled data");
134
135   check_memleaks ();
136   
137   printf ("%s: running memory tests\n", "dbus-test");
138   if (!_dbus_memory_test ())
139     die ("memory");
140   
141   check_memleaks ();
142
143 #if 1
144   printf ("%s: running memory pool tests\n", "dbus-test");
145   if (!_dbus_mem_pool_test ())
146     die ("memory pools");
147
148   check_memleaks ();
149 #endif
150   
151   printf ("%s: running linked list tests\n", "dbus-test");
152   if (!_dbus_list_test ())
153     die ("lists");
154
155   check_memleaks ();
156
157   printf ("%s: running validation tests\n", "dbus-test");
158   if (!_dbus_marshal_validate_test ())
159     die ("validation");
160
161   check_memleaks ();
162
163   printf ("%s: running header marshal tests\n", "dbus-test");
164   if (!_dbus_marshal_header_test ())
165     die ("header marshal");
166
167   check_memleaks ();
168   
169   printf ("%s: running message tests\n", "dbus-test");
170   if (!_dbus_message_test (test_data_dir))
171     die ("messages");
172
173   check_memleaks ();
174   
175   printf ("%s: running hash table tests\n", "dbus-test");
176   if (!_dbus_hash_test ())
177     die ("hash tables");
178
179   check_memleaks ();
180
181   printf ("%s: running spawn tests\n", "dbus-test");
182   if (!_dbus_spawn_test (test_data_dir))
183     die ("spawn");
184
185   check_memleaks ();
186   
187   printf ("%s: running user database tests\n", "dbus-test");
188   if (!_dbus_userdb_test (test_data_dir))
189     die ("user database");
190
191   check_memleaks ();
192   
193   printf ("%s: running keyring tests\n", "dbus-test");
194   if (!_dbus_keyring_test ())
195     die ("keyring");
196
197   check_memleaks ();
198   
199 #if 0
200   printf ("%s: running md5 tests\n", "dbus-test");
201   if (!_dbus_md5_test ())
202     die ("md5");
203
204   check_memleaks ();
205 #endif
206   
207   printf ("%s: running SHA-1 tests\n", "dbus-test");
208   if (!_dbus_sha_test (test_data_dir))
209     die ("SHA-1");
210
211   check_memleaks ();
212   
213   printf ("%s: running auth tests\n", "dbus-test");
214   if (!_dbus_auth_test (test_data_dir))
215     die ("auth");
216
217   check_memleaks ();
218
219   printf ("%s: running pending call tests\n", "dbus-test");
220   if (!_dbus_pending_call_test (test_data_dir))
221     die ("auth");
222
223   check_memleaks ();
224   
225   printf ("%s: completed successfully\n", "dbus-test");
226 #else
227   printf ("Not compiled with unit tests, not running any\n");
228 #endif
229 }