File: test_dbus_message.c

package info (click to toggle)
libnih 1.0.3-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,824 kB
  • sloc: ansic: 188,730; sh: 11,217; makefile: 1,117; yacc: 291; xml: 239; sed: 16
file content (180 lines) | stat: -rw-r--r-- 4,170 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* libnih
 *
 * test_dbus_message.c - test suite for nih-dbus/dbus_message.c
 *
 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
 * Copyright © 2009 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * 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 Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <nih/test.h>
#include <nih-dbus/test_dbus.h>

#include <dbus/dbus.h>

#include <sys/types.h>
#include <sys/wait.h>

#include <signal.h>
#include <stdlib.h>

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/timer.h>
#include <nih/signal.h>
#include <nih/main.h>

#include <nih-dbus/dbus_connection.h>
#include <nih-dbus/dbus_object.h>

#include <nih-dbus/dbus_message.h>


void
test_message_new (void)
{
	NihDBusMessage *msg;
	pid_t           dbus_pid;
	DBusConnection *conn;
	DBusMessage *   message;

	/* Check that we can create a new DBus message structure, and that
	 * it references the connection and message.
	 */
	TEST_FUNCTION ("nih_dbus_message_new");
	TEST_DBUS (dbus_pid);
	TEST_DBUS_OPEN (conn);

	message = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_CALL);

	TEST_ALLOC_FAIL {
		msg = nih_dbus_message_new (NULL, conn, message);

		if (test_alloc_failed) {
			TEST_EQ_P (msg, NULL);

			continue;
		}

		TEST_ALLOC_SIZE (msg, sizeof (NihDBusMessage));
		TEST_EQ_P (msg->connection, conn);
		TEST_EQ_P (msg->message, message);

		nih_free (msg);
	}

	dbus_message_unref (message);

	TEST_DBUS_CLOSE (conn);
	TEST_DBUS_END (dbus_pid);

	dbus_shutdown ();
}


void
test_message_error (void)
{
	pid_t           dbus_pid;
	DBusConnection *server_conn;
	DBusConnection *client_conn;
	DBusMessage *   method_call;
	DBusMessage *   reply;
	dbus_uint32_t   serial;
	NihDBusMessage *message = NULL;
	int             ret;
	DBusError       error;

	/* Check that an error returned outside the handler with the
	 * nih_dbus_message_error() function is returned to the sender
	 * with the right details.
	 */
	TEST_FUNCTION ("nih_dbus_message_error");
	TEST_DBUS (dbus_pid);
	TEST_DBUS_OPEN (server_conn);
	TEST_DBUS_OPEN (client_conn);

	TEST_ALLOC_FAIL {
		method_call = dbus_message_new_method_call (
			dbus_bus_get_unique_name (server_conn),
			"/com/netsplit/Nih",
			"com.netsplit.Nih.Glue",
			"ReturnError");

		dbus_connection_send (client_conn, method_call, &serial);
		dbus_connection_flush (client_conn);
		dbus_message_unref (method_call);

		TEST_DBUS_MESSAGE (server_conn, method_call);
		assert (dbus_message_get_serial (method_call) == serial);

		TEST_ALLOC_SAFE {
			message = nih_new (NULL, NihDBusMessage);
			message->connection = client_conn;
			message->message = method_call;
		}

		ret = nih_dbus_message_error (message,
					      "com.netsplit.Nih.Test.MyError",
					      "this is a %s %d", "test", 1234);

		if (test_alloc_failed) {
			TEST_LT (ret, 0);

			nih_free (message);
			dbus_message_unref (method_call);
			continue;
		}

		TEST_EQ (ret, 0);

		nih_free (message);
		dbus_message_unref (method_call);

		TEST_DBUS_MESSAGE (client_conn, reply);
		TEST_EQ (dbus_message_get_type (reply),
			 DBUS_MESSAGE_TYPE_ERROR);
		TEST_EQ (dbus_message_get_reply_serial (reply), serial);

		dbus_error_init (&error);
		dbus_set_error_from_message (&error, reply);

		TEST_EQ_STR (error.name,
			     "com.netsplit.Nih.Test.MyError");
		TEST_EQ_STR (error.message,
			     "this is a test 1234");

		dbus_error_free (&error);
		dbus_message_unref (reply);
	}


	TEST_DBUS_CLOSE (client_conn);
	TEST_DBUS_CLOSE (server_conn);
	TEST_DBUS_END (dbus_pid);

	dbus_shutdown ();
}


int
main (int   argc,
      char *argv[])
{
	test_message_new ();
	test_message_error ();

	return 0;
}