gkdbus: Fix underflow and unreachable code bug
[platform/upstream/glib.git] / gio / tests / gdbus-test-fixture.c
1
2 #include "gdbus-object-manager-example/objectmanager-gen.h"
3
4 /* ---------------------------------------------------------------------------------------------------- */
5
6 /* The fixture contains a GTestDBus object and
7  * a proxy to the service we're going to be testing.
8  */
9 typedef struct {
10   GTestDBus *dbus;
11   GDBusObjectManager *manager;
12 } TestFixture;
13
14 static void
15 fixture_setup (TestFixture *fixture, gconstpointer unused)
16 {
17   GError *error = NULL;
18   gchar *relative, *servicesdir;
19
20   /* Create the global dbus-daemon for this test suite
21    */
22   fixture->dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
23
24   /* Add the private directory with our in-tree service files.
25    */
26   relative = g_test_build_filename (G_TEST_BUILT, "services", NULL);
27   servicesdir = g_canonicalize_filename (relative, NULL);
28   g_free (relative);
29
30   g_test_dbus_add_service_dir (fixture->dbus, servicesdir);
31   g_free (servicesdir);
32
33   /* Start the private D-Bus daemon
34    */
35   g_test_dbus_up (fixture->dbus);
36
37   /* Create the proxy that we're going to test
38    */
39   fixture->manager =
40     example_object_manager_client_new_for_bus_sync (G_BUS_TYPE_SESSION,
41                                                     G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
42                                                     "org.gtk.GDBus.Examples.ObjectManager",
43                                                     "/example/Animals",
44                                                     NULL, /* GCancellable */
45                                                     &error);
46   if (fixture->manager == NULL)
47     g_error ("Error getting object manager client: %s", error->message);
48 }
49
50 static void
51 fixture_teardown (TestFixture *fixture, gconstpointer unused)
52 {
53   /* Tear down the proxy
54    */
55   if (fixture->manager)
56     g_object_unref (fixture->manager);
57
58   /* Stop the private D-Bus daemon
59    */
60   g_test_dbus_down (fixture->dbus);
61   g_object_unref (fixture->dbus);
62 }
63
64 /* The gdbus-example-objectmanager-server exports 10 objects,
65  * to test the server has actually activated, let's ensure
66  * that 10 objects exist.
67  */
68 static void
69 test_gtest_dbus (TestFixture *fixture, gconstpointer unused)
70 {
71   GList *objects;
72
73   objects = g_dbus_object_manager_get_objects (fixture->manager);
74
75   g_assert_cmpint (g_list_length (objects), ==, 10);
76   g_list_free_full (objects, g_object_unref);
77 }
78
79 int
80 main (int   argc,
81       char *argv[])
82 {
83   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
84
85   /* This test simply ensures that we can bring the GTestDBus up and down a hand
86    * full of times in a row, each time successfully activating the in-tree service
87    */
88   g_test_add ("/GTestDBus/Cycle1", TestFixture, NULL,
89               fixture_setup, test_gtest_dbus, fixture_teardown);
90   g_test_add ("/GTestDBus/Cycle2", TestFixture, NULL,
91               fixture_setup, test_gtest_dbus, fixture_teardown);
92   g_test_add ("/GTestDBus/Cycle3", TestFixture, NULL,
93               fixture_setup, test_gtest_dbus, fixture_teardown);
94   g_test_add ("/GTestDBus/Cycle4", TestFixture, NULL,
95               fixture_setup, test_gtest_dbus, fixture_teardown);
96   g_test_add ("/GTestDBus/Cycle5", TestFixture, NULL,
97               fixture_setup, test_gtest_dbus, fixture_teardown);
98   
99   return g_test_run ();
100 }