dbus-test.c 4.93 KB
Newer Older
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 3
/* dbus-test.c  Program to run all tests
 *
4
 * Copyright (C) 2002, 2003, 2004, 2005  Red Hat Inc.
5
 *
6
 * Licensed under the Academic Free License version 2.1
7 8 9 10 11 12 13 14 15 16 17 18 19
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * 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-test.h"
26
#include "dbus-sysdeps.h"
27
#include "dbus-internals.h"
28
#include <stdio.h>
29 30
#include <stdlib.h>

31
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
32 33 34
static void
die (const char *failure)
{
35
  fprintf (stderr, "Unit test failed: %s\n", failure);
36 37
  exit (1);
}
38 39 40 41 42 43

static void
check_memleaks (void)
{
  dbus_shutdown ();

44
  printf ("%s: checking for memleaks\n", "test-dbus");
45 46 47 48 49 50 51 52
  if (_dbus_get_malloc_blocks_outstanding () != 0)
    {
      _dbus_warn ("%d dbus_malloc blocks were not freed\n",
                  _dbus_get_malloc_blocks_outstanding ());
      die ("memleaks");
    }
}

53 54 55 56 57 58 59 60 61 62
typedef dbus_bool_t (*TestFunc)(void);
typedef dbus_bool_t (*TestDataFunc)(const char *data);

static void
run_test (const char             *test_name,
	  const char             *specific_test,
	  TestFunc                test)
{
  if (!specific_test || strcmp (specific_test, test_name) == 0)
    {
63
      printf ("%s: running %s tests\n", "test-dbus", test_name);
64 65 66
      if (!test ())
	die (test_name);

67 68
      check_memleaks ();
    }
69 70 71 72 73 74 75 76 77 78
}

static void
run_data_test (const char             *test_name,
	       const char             *specific_test,
	       TestDataFunc            test,
	       const char             *test_data_dir)
{
  if (!specific_test || strcmp (specific_test, test_name) == 0)
    {
79
      printf ("%s: running %s tests\n", "test-dbus", test_name);
80 81 82
      if (!test (test_data_dir))
	die (test_name);

83 84
      check_memleaks ();
    }
85 86
}

87
#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
88

89 90 91 92 93 94
/**
 * An exported symbol to be run in order to execute
 * unit tests. Should not be used by
 * any app other than our test app, this symbol
 * won't exist in some builds of the library.
 * (with --enable-tests=no)
95 96
 *
 * @param test_data_dir the directory with test data (test/data normally)
97
 * @param specific_test run specific test or #NULL to run all tests
98 99
 */
void
100
dbus_internal_do_not_use_run_tests (const char *test_data_dir, const char *specific_test)
101
{
102
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
103 104 105
  if (!_dbus_threads_init_debug ())
    die ("debug threads init");
  
106
  if (test_data_dir == NULL)
107
    test_data_dir = _dbus_getenv ("DBUS_TEST_DATA");
108 109 110 111 112

  if (test_data_dir != NULL)
    printf ("Test data in %s\n", test_data_dir);
  else
    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
  run_test ("marshal-recursive", specific_test, _dbus_marshal_recursive_test);
133

134
  run_test ("byteswap", specific_test, _dbus_marshal_byteswap_test);
135
  
136
  run_test ("memory", specific_test, _dbus_memory_test);
137

138
#if 1
139
  run_test ("mem-pool", specific_test, _dbus_mem_pool_test);
140
#endif
141
  
142
  run_test ("list", specific_test, _dbus_list_test);
143

144
  run_test ("marshal-validate", specific_test, _dbus_marshal_validate_test);
145

146
  run_data_test ("message", specific_test, _dbus_message_test, test_data_dir);
147
  
148
  run_test ("hash", specific_test, _dbus_hash_test);
149

150
#if !defined(DBUS_WINCE)
151
  run_data_test ("spawn", specific_test, _dbus_spawn_test, test_data_dir);
152
#endif
153
  
154
  run_data_test ("credentials", specific_test, _dbus_credentials_test, test_data_dir);
155 156

#ifdef DBUS_UNIX
157
  run_data_test ("userdb", specific_test, _dbus_userdb_test, test_data_dir);
158 159

  run_test ("transport-unix", specific_test, _dbus_transport_unix_test);
160
#endif
161
  
162
  run_test ("keyring", specific_test, _dbus_keyring_test);
163

164
  run_data_test ("sha", specific_test, _dbus_sha_test, test_data_dir);
165
  
166
  run_data_test ("auth", specific_test, _dbus_auth_test, test_data_dir);
167

168
  printf ("%s: completed successfully\n", "test-dbus");
169 170 171
#else
  printf ("Not compiled with unit tests, not running any\n");
#endif
172
}
173