Bug 28460 - Refactored dbus configuration access.
[platform/upstream/dbus.git] / dbus / dbus-test.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <config.h>
25 #include "dbus-config.h"
26 #include "dbus-test.h"
27 #include "dbus-sysdeps.h"
28 #include "dbus-internals.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #ifdef DBUS_BUILD_TESTS
33 static void
34 die (const char *failure)
35 {
36   fprintf (stderr, "Unit test failed: %s\n", failure);
37   exit (1);
38 }
39
40 static void
41 check_memleaks (void)
42 {
43   dbus_shutdown ();
44
45   printf ("%s: checking for memleaks\n", "dbus-test");
46   if (_dbus_get_malloc_blocks_outstanding () != 0)
47     {
48       _dbus_warn ("%d dbus_malloc blocks were not freed\n",
49                   _dbus_get_malloc_blocks_outstanding ());
50       die ("memleaks");
51     }
52 }
53
54 typedef dbus_bool_t (*TestFunc)(void);
55 typedef dbus_bool_t (*TestDataFunc)(const char *data);
56
57 static void
58 run_test (const char             *test_name,
59           const char             *specific_test,
60           TestFunc                test)
61 {
62   if (!specific_test || strcmp (specific_test, test_name) == 0)
63     {
64       printf ("%s: running %s tests\n", "dbus-test", test_name);
65       if (!test ())
66         die (test_name);
67     }
68
69   check_memleaks ();
70 }
71
72 static void
73 run_data_test (const char             *test_name,
74                const char             *specific_test,
75                TestDataFunc            test,
76                const char             *test_data_dir)
77 {
78   if (!specific_test || strcmp (specific_test, test_name) == 0)
79     {
80       printf ("%s: running %s tests\n", "dbus-test", test_name);
81       if (!test (test_data_dir))
82         die (test_name);
83     }
84
85   check_memleaks ();
86 }
87
88 #endif /* DBUS_BUILD_TESTS */
89
90 /**
91  * An exported symbol to be run in order to execute
92  * unit tests. Should not be used by
93  * any app other than our test app, this symbol
94  * won't exist in some builds of the library.
95  * (with --enable-tests=no)
96  *
97  * @param test_data_dir the directory with test data (test/data normally)
98  */
99 void
100 dbus_internal_do_not_use_run_tests (const char *test_data_dir, const char *specific_test)
101 {
102 #ifdef DBUS_BUILD_TESTS
103   if (!_dbus_threads_init_debug ())
104     die ("debug threads init");
105   
106   if (test_data_dir == NULL)
107     test_data_dir = _dbus_config_test_data ();
108
109   if (test_data_dir != NULL)
110     printf ("Test data in %s\n", test_data_dir);
111   else
112     printf ("No test data!\n");
113
114   run_test ("string", specific_test, _dbus_string_test);
115   
116   run_test ("sysdeps", specific_test, _dbus_sysdeps_test);
117   
118   run_test ("data-slot", specific_test, _dbus_data_slot_test);
119
120   run_test ("misc", specific_test, _dbus_misc_test);
121   
122   run_test ("address", specific_test, _dbus_address_test);
123
124   run_test ("server", specific_test, _dbus_server_test);
125
126   run_test ("object-tree", specific_test, _dbus_object_tree_test);
127
128   run_test ("signature", specific_test, _dbus_signature_test);
129   
130   run_test ("marshalling", specific_test, _dbus_marshal_test);
131
132 #if 0
133   printf ("%s: running recursive marshalling tests\n", "dbus-test");
134   if (!_dbus_marshal_recursive_test ())
135     die ("recursive marshal");
136
137   check_memleaks ();
138 #else
139   _dbus_warn ("recursive marshal tests disabled\n");
140 #endif
141
142   run_test ("byteswap", specific_test, _dbus_marshal_byteswap_test);
143   
144   run_test ("memory", specific_test, _dbus_memory_test);
145
146 #if 1
147   run_test ("mem-pool", specific_test, _dbus_mem_pool_test);
148 #endif
149   
150   run_test ("list", specific_test, _dbus_list_test);
151
152   run_test ("marshal-validate", specific_test, _dbus_marshal_validate_test);
153
154   run_test ("marshal-header", specific_test, _dbus_marshal_header_test);
155   
156   run_data_test ("message", specific_test, _dbus_message_test, test_data_dir);
157   
158   run_test ("hash", specific_test, _dbus_hash_test);
159
160 #if !defined(DBUS_WINCE)
161   run_data_test ("spawn", specific_test, _dbus_spawn_test, test_data_dir);
162 #endif
163   
164   run_data_test ("credentials", specific_test, _dbus_credentials_test, test_data_dir);
165
166 #ifdef DBUS_UNIX
167   run_data_test ("userdb", specific_test, _dbus_userdb_test, test_data_dir);
168 #endif
169   
170   run_test ("keyring", specific_test, _dbus_keyring_test);
171   
172 #if 0
173   printf ("%s: running md5 tests\n", "dbus-test");
174   if (!_dbus_md5_test ())
175     die ("md5");
176
177   check_memleaks ();
178 #endif
179   
180   run_data_test ("sha", specific_test, _dbus_sha_test, test_data_dir);
181   
182   run_data_test ("auth", specific_test, _dbus_auth_test, test_data_dir);
183
184   run_data_test ("pending-call", specific_test, _dbus_pending_call_test, test_data_dir);
185   
186   printf ("%s: completed successfully\n", "dbus-test");
187 #else
188   printf ("Not compiled with unit tests, not running any\n");
189 #endif
190 }
191