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