Bug 15571: Clean up GUID-less connections correctly (Scott James Remnant)
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "test.h"
25 #include "activation-helper.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <dbus/dbus-internals.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 (const char *name)
41 {
42   dbus_shutdown ();
43   
44   printf ("%s: checking for memleaks\n", name);
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 static void
54 test_post_hook (const char *name)
55 {
56   check_memleaks (name);
57 }
58 #endif /* DBUS_BUILD_TESTS */
59
60
61 #ifdef ACTIVATION_LAUNCHER_DO_OOM
62
63 /* returns true if good things happen, or if we get OOM */
64 static dbus_bool_t
65 bus_activation_helper_oom_test (void *data)
66 {
67   const char *service;
68   DBusError error;
69   dbus_bool_t retval;
70
71   service = (const char *) data;
72   retval = TRUE;
73
74   dbus_error_init (&error);
75   if (!run_launch_helper (service, &error))
76     {
77       _DBUS_ASSERT_ERROR_IS_SET (&error);
78       /* we failed, but a OOM is good */
79       if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
80         {
81           _dbus_warn ("FAILED SELF TEST: Error: %s\n", error.message);
82           retval = FALSE;
83         }
84       dbus_error_free (&error);
85     }
86   else
87     {
88       /* we succeeded, yay! */
89       _DBUS_ASSERT_ERROR_IS_CLEAR (&error);
90     }
91   return retval;
92 }
93
94 #endif
95
96 int
97 main (int argc, char **argv)
98 {
99 #ifdef DBUS_BUILD_TESTS
100   const char *dir;
101   DBusString config_file;
102
103   if (argc > 1)
104     dir = argv[1];
105   else
106     dir = _dbus_getenv ("DBUS_TEST_DATA");
107
108   if (dir == NULL)
109     {
110       fprintf (stderr, "Must specify test data directory as argv[1] or in DBUS_TEST_DATA env variable\n");
111       return 1;
112     }
113
114   printf ("%s: Running launch helper OOM checks\n", argv[0]);
115
116   if (!_dbus_string_init (&config_file))
117     return 1;
118   if (!_dbus_string_append (&config_file, dir))
119     return 1;
120   if (!_dbus_string_append (&config_file, "/valid-config-files-system/debug-allow-all-pass.conf"))
121     return 1;
122
123   /* use a config file that will actually work... */
124   _dbus_setenv ("TEST_LAUNCH_HELPER_CONFIG",
125                 _dbus_string_get_const_data (&config_file));
126
127   _dbus_string_free (&config_file);
128
129   if (!_dbus_test_oom_handling ("dbus-daemon-launch-helper",
130                                 bus_activation_helper_oom_test,
131                                 "org.freedesktop.DBus.TestSuiteEchoService"))
132     die ("OOM failed");
133
134   test_post_hook (argv[0]);
135
136   printf ("%s: Success\n", argv[0]);
137
138   return 0;
139 #else /* DBUS_BUILD_TESTS */
140
141   printf ("Not compiled with test support\n");
142   
143   return 0;
144 #endif
145 }
146