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