1#include <config.h>
2#include "../test-utils.h"
3
4static void
5die (const char *message, ...)
6{
7  va_list args;
8  va_start (args, message);
9  vfprintf (stderr, message, args);
10  va_end (args);
11  exit (1);
12}
13
14typedef struct TestServiceData TestServiceData;
15
16struct TestServiceData
17{
18  DBusLoop *loop;
19  char *private_addr;
20};
21
22static void
23new_connection_callback (DBusServer     *server,
24                         DBusConnection *new_connection,
25                         void           *data)
26{
27  TestServiceData *testdata = data;
28
29  if (!test_connection_setup (testdata->loop, new_connection))
30    dbus_connection_close (new_connection);
31}
32
33static DBusHandlerResult
34filter_session_message (DBusConnection     *connection,
35                        DBusMessage        *message,
36                        void               *user_data)
37{
38  TestServiceData *testdata = user_data;
39
40  if (dbus_message_is_method_call (message,
41                                   "org.freedesktop.DBus.TestSuite.PrivServer",
42                                   "GetPrivateAddress"))
43    {
44       DBusMessage *reply;
45       reply = dbus_message_new_method_return (message);
46       dbus_message_append_args (reply, DBUS_TYPE_STRING,
47                                 &(testdata->private_addr), DBUS_TYPE_INVALID);
48       dbus_connection_send (connection, reply, NULL);
49       dbus_message_unref (reply);
50       return DBUS_HANDLER_RESULT_HANDLED;
51    }
52  else if (dbus_message_is_method_call (message,
53                                   "org.freedesktop.DBus.TestSuite.PrivServer",
54                                   "Quit"))
55    {
56      fprintf (stderr, "server exiting loop\n");
57      _dbus_loop_quit (testdata->loop);
58      return DBUS_HANDLER_RESULT_HANDLED;
59    }
60  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
61}
62
63int
64main (int argc, char *argv[])
65{
66  DBusServer *server;
67  DBusError error;
68  DBusLoop *loop;
69  DBusConnection *session;
70  TestServiceData *testdata;
71
72  dbus_error_init (&error);
73
74  loop = _dbus_loop_new ();
75
76  testdata = dbus_new (TestServiceData, 1);
77  testdata->loop = loop;
78
79  session = dbus_bus_get (DBUS_BUS_SESSION, &error);
80  if (!session)
81    die ("couldn't access session bus");
82
83  test_connection_setup (loop, session);
84
85  dbus_bus_request_name (session, "org.freedesktop.DBus.TestSuite.PrivServer", 0, &error);
86  if (dbus_error_is_set (&error))
87    die ("couldn't request name: %s", error.message);
88
89  if (!dbus_connection_add_filter (session, filter_session_message, testdata, NULL))
90    die ("couldn't add filter");
91
92#ifdef DBUS_CMAKE
93  server = dbus_server_listen (TEST_LISTEN, &error);
94#else
95  server = dbus_server_listen ("unix:tmpdir=/tmp", &error);
96#endif
97  if (!server)
98    die (error.message);
99  testdata->private_addr = dbus_server_get_address (server);
100  printf ("test server listening on %s\n", testdata->private_addr);
101
102  dbus_server_set_new_connection_function (server, new_connection_callback,
103                                           testdata, NULL);
104
105  if (!test_server_setup (loop, server))
106    die ("server setup failed");
107
108  fprintf (stderr, "server running mainloop\n");
109  _dbus_loop_run (loop);
110  fprintf (stderr, "server mainloop quit\n");
111
112  test_server_shutdown (loop, server);
113
114  test_connection_shutdown (loop, session);
115
116  dbus_connection_unref (session);
117
118  _dbus_loop_unref (loop);
119
120  dbus_free (testdata);
121
122  return 0;
123}
124