[rename] renamed kdbus related macros
[platform/upstream/dbus.git] / bus / test-launch-helper.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* test-main.c  main() for the OOM check of the launch helper
3  *
4  * Copyright (C) 2007 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 "test.h"
26 #include "activation-helper.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <dbus/dbus-internals.h>
31 #include <dbus/dbus-misc.h>
32
33 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
34 static void
35 die (const char *failure)
36 {
37   fprintf (stderr, "Unit test failed: %s\n", failure);
38   exit (1);
39 }
40
41 static void
42 check_memleaks (const char *name)
43 {
44   dbus_shutdown ();
45   
46   printf ("%s: checking for memleaks\n", name);
47   if (_dbus_get_malloc_blocks_outstanding () != 0)
48     {
49       _dbus_warn ("%d dbus_malloc blocks were not freed\n",
50                   _dbus_get_malloc_blocks_outstanding ());
51       die ("memleaks");
52     }
53 }
54
55 static void
56 test_post_hook (const char *name)
57 {
58   check_memleaks (name);
59 }
60 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
61
62
63 #ifdef ACTIVATION_LAUNCHER_DO_OOM
64
65 /* returns true if good things happen, or if we get OOM */
66 static dbus_bool_t
67 bus_activation_helper_oom_test (void *data)
68 {
69   const char *service;
70   DBusError error;
71   dbus_bool_t retval;
72
73   service = (const char *) data;
74   retval = TRUE;
75
76   dbus_error_init (&error);
77   if (!run_launch_helper (service, &error))
78     {
79       _DBUS_ASSERT_ERROR_IS_SET (&error);
80       /* we failed, but a OOM is good */
81       if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
82         {
83           _dbus_warn ("FAILED SELF TEST: Error: %s\n", error.message);
84           retval = FALSE;
85         }
86       dbus_error_free (&error);
87     }
88   else
89     {
90       /* we succeeded, yay! */
91       _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
92     }
93   return retval;
94 }
95
96 #endif
97
98 int
99 main (int argc, char **argv)
100 {
101 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
102   const char *dir;
103   DBusString config_file;
104
105   if (argc > 1)
106     dir = argv[1];
107   else
108     dir = _dbus_getenv ("DBUS_TEST_DATA");
109
110   if (dir == NULL)
111     {
112       fprintf (stderr, "Must specify test data directory as argv[1] or in DBUS_TEST_DATA env variable\n");
113       return 1;
114     }
115
116   printf ("%s: Running launch helper OOM checks\n", argv[0]);
117
118   if (!_dbus_string_init (&config_file))
119     return 1;
120   if (!_dbus_string_append (&config_file, dir))
121     return 1;
122   if (!_dbus_string_append (&config_file, "/valid-config-files-system/debug-allow-all-pass.conf"))
123     return 1;
124
125   /* use a config file that will actually work... */
126   dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG",
127                _dbus_string_get_const_data (&config_file));
128
129   _dbus_string_free (&config_file);
130
131   if (!_dbus_test_oom_handling ("dbus-daemon-launch-helper",
132                                 bus_activation_helper_oom_test,
133                                 "org.freedesktop.DBus.TestSuiteEchoService"))
134     die ("OOM failed");
135
136   test_post_hook (argv[0]);
137
138   printf ("%s: Success\n", argv[0]);
139
140   return 0;
141 #else /* DBUS_ENABLE_EMBEDDED_TESTS */
142
143   printf ("Not compiled with test support\n");
144   
145   return 0;
146 #endif
147 }
148