3
* Copyright (C) 2003 Philip Blundell <philb@gnu.org>
4
* Copyright (C) 2013 Canonical, Ltd.
6
* Originally dbus-send.c from the dbus package. It has been heavily modified
7
* to work within the regression test framework.
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31
#include "dbus_common.h"
33
const char *type_to_name(int message_type)
35
switch (message_type) {
36
case DBUS_MESSAGE_TYPE_SIGNAL:
38
case DBUS_MESSAGE_TYPE_METHOD_CALL:
40
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
41
return "method return";
42
case DBUS_MESSAGE_TYPE_ERROR:
45
return "(unknown message type)";
49
void log_message(int log_fd, const char *prefix, DBusMessage * message)
51
const char *sender = NULL;
52
const char *destination = NULL;
53
const char *unique = "(UNIQUE)";
59
message_type = dbus_message_get_type(message);
60
sender = dbus_message_get_sender(message);
61
destination = dbus_message_get_destination(message);
63
/* Remove unique (random) names from the logs since they make it
64
* impossible to do simple log comparisons between two different test
67
if (sender && sender[0] == ':')
69
if (destination && destination[0] == ':')
72
dprintf(log_fd, "%s%s sender=%s -> dest=%s",
73
prefix, type_to_name(message_type),
74
sender ? sender : "(null)",
75
destination ? destination : "(null)");
77
switch (message_type) {
78
case DBUS_MESSAGE_TYPE_METHOD_CALL:
79
case DBUS_MESSAGE_TYPE_SIGNAL:
80
dprintf(log_fd, " path=%s; interface=%s; member=%s\n",
81
dbus_message_get_path(message),
82
dbus_message_get_interface(message),
83
dbus_message_get_member(message));
86
case DBUS_MESSAGE_TYPE_ERROR:
87
dprintf(log_fd, " error_name=%s\n",
88
dbus_message_get_error_name(message));
92
dprintf(log_fd, "\n");
97
void append_arg(DBusMessageIter * iter, int type, const char *value)
101
dbus_uint32_t uint32;
103
dbus_uint64_t uint64;
107
dbus_bool_t v_BOOLEAN;
109
/* FIXME - we are ignoring OOM returns on all these functions */
112
byte = strtoul(value, NULL, 0);
113
dbus_message_iter_append_basic(iter, DBUS_TYPE_BYTE, &byte);
116
case DBUS_TYPE_DOUBLE:
117
d = strtod(value, NULL);
118
dbus_message_iter_append_basic(iter, DBUS_TYPE_DOUBLE, &d);
121
case DBUS_TYPE_INT16:
122
int16 = strtol(value, NULL, 0);
123
dbus_message_iter_append_basic(iter, DBUS_TYPE_INT16, &int16);
126
case DBUS_TYPE_UINT16:
127
uint16 = strtoul(value, NULL, 0);
128
dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT16, &uint16);
131
case DBUS_TYPE_INT32:
132
int32 = strtol(value, NULL, 0);
133
dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &int32);
136
case DBUS_TYPE_UINT32:
137
uint32 = strtoul(value, NULL, 0);
138
dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &uint32);
141
case DBUS_TYPE_INT64:
142
int64 = strtoll(value, NULL, 0);
143
dbus_message_iter_append_basic(iter, DBUS_TYPE_INT64, &int64);
146
case DBUS_TYPE_UINT64:
147
uint64 = strtoull(value, NULL, 0);
148
dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT64, &uint64);
151
case DBUS_TYPE_STRING:
152
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &value);
155
case DBUS_TYPE_OBJECT_PATH:
156
dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
160
case DBUS_TYPE_BOOLEAN:
161
if (strcmp(value, "true") == 0) {
163
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN,
165
} else if (strcmp(value, "false") == 0) {
167
dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN,
171
"FAIL: Expected \"true\" or \"false\" instead of \"%s\"\n",
178
fprintf(stderr, "FAIL: Unsupported data type %c\n", (char)type);
183
void append_array(DBusMessageIter * iter, int type, const char *value)
186
char *dupval = strdup(value);
188
val = strtok(dupval, ",");
189
while (val != NULL) {
190
append_arg(iter, type, val);
191
val = strtok(NULL, ",");
197
append_dict(DBusMessageIter * iter, int keytype, int valtype, const char *value)
200
char *dupval = strdup(value);
202
val = strtok(dupval, ",");
203
while (val != NULL) {
204
DBusMessageIter subiter;
206
dbus_message_iter_open_container(iter,
207
DBUS_TYPE_DICT_ENTRY,
210
append_arg(&subiter, keytype, val);
211
val = strtok(NULL, ",");
213
fprintf(stderr, "FAIL: Malformed dictionary\n");
216
append_arg(&subiter, valtype, val);
218
dbus_message_iter_close_container(iter, &subiter);
219
val = strtok(NULL, ",");
224
int type_from_name(const char *arg)
227
if (!strcmp(arg, "string"))
228
type = DBUS_TYPE_STRING;
229
else if (!strcmp(arg, "int16"))
230
type = DBUS_TYPE_INT16;
231
else if (!strcmp(arg, "uint16"))
232
type = DBUS_TYPE_UINT16;
233
else if (!strcmp(arg, "int32"))
234
type = DBUS_TYPE_INT32;
235
else if (!strcmp(arg, "uint32"))
236
type = DBUS_TYPE_UINT32;
237
else if (!strcmp(arg, "int64"))
238
type = DBUS_TYPE_INT64;
239
else if (!strcmp(arg, "uint64"))
240
type = DBUS_TYPE_UINT64;
241
else if (!strcmp(arg, "double"))
242
type = DBUS_TYPE_DOUBLE;
243
else if (!strcmp(arg, "byte"))
244
type = DBUS_TYPE_BYTE;
245
else if (!strcmp(arg, "boolean"))
246
type = DBUS_TYPE_BOOLEAN;
247
else if (!strcmp(arg, "objpath"))
248
type = DBUS_TYPE_OBJECT_PATH;
250
fprintf(stderr, "FAIL: Unknown type \"%s\"\n", arg);