2 * Copyright (C) 2013, 2014 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
21 #define LIBVIRT_VIRDBUSPRIV_H_ALLOW
22 #include "virdbuspriv.h"
24 #include "testutils.h"
26 VIR_LOG_INIT("tests.dbustest");
28 #define VERIFY(typname, valorig, valnew, fmt) \
30 VIR_DEBUG("Compare " typname " '" fmt "' to '" \
31 fmt "'", valorig, valnew); \
32 if (valorig != valnew) { \
33 fprintf(stderr, "Failed to round-trip " typname " '" \
34 fmt "' to '" fmt "'\n", valorig, valnew); \
39 #define VERIFY_STR(typname, valorig, valnew, fmt) \
41 VIR_DEBUG("Compare " typname " '" fmt "' to '" \
42 fmt "'", valorig, valnew); \
43 if (STRNEQ(valorig, valnew)) { \
44 fprintf(stderr, "Failed to round-trip " typname " '" \
45 fmt "' to '" fmt "'\n", valorig, valnew); \
50 static int testMessageSimple(const void *args ATTRIBUTE_UNUSED
)
52 DBusMessage
*msg
= NULL
;
54 unsigned char in_byte
= 200, out_byte
= 0;
55 bool in_bool
= true, out_bool
= false;
56 short in_int16
= 0xfefe, out_int16
= 0;
57 unsigned short in_uint16
= 32000, out_uint16
= 0;
58 int in_int32
= 100000000, out_int32
= 0;
59 unsigned int in_uint32
= 200000000, out_uint32
= 0;
60 long long in_int64
= 1000000000000LL, out_int64
= 0;
61 unsigned long long in_uint64
= 2000000000000LL, out_uint64
= 0;
62 double in_double
= 3.14159265359, out_double
= 0;
63 const char *in_string
= "Hello World";
64 char *out_string
= NULL
;
65 const char *in_objectpath
= "/org/libvirt/test";
66 char *out_objectpath
= NULL
;
67 const char *in_signature
= "ybnqiuxtdsog";
68 char *out_signature
= NULL
;
70 if (!(msg
= dbus_message_new_method_call("org.libvirt.test",
72 "org.libvirt.test.astrochicken",
74 VIR_DEBUG("Failed to allocate method call");
78 if (virDBusMessageEncode(msg
,
85 in_objectpath
, in_signature
) < 0) {
86 VIR_DEBUG("Failed to encode arguments");
90 if (virDBusMessageDecode(msg
,
93 &out_int16
, &out_uint16
,
94 &out_int32
, &out_uint32
,
95 &out_int64
, &out_uint64
,
96 &out_double
, &out_string
,
97 &out_objectpath
, &out_signature
) < 0) {
98 VIR_DEBUG("Failed to decode arguments");
102 VERIFY("byte", in_byte
, out_byte
, "%d");
103 VERIFY("bool", in_bool
, out_bool
, "%d");
104 VERIFY("int16", in_int16
, out_int16
, "%d");
105 VERIFY("uint16", in_int16
, out_int16
, "%d");
106 VERIFY("int32", in_int32
, out_int32
, "%d");
107 VERIFY("uint32", in_int32
, out_int32
, "%d");
108 VERIFY("int64", in_int64
, out_int64
, "%lld");
109 VERIFY("uint64", in_int64
, out_int64
, "%lld");
110 VERIFY("double", in_double
, out_double
, "%lf");
111 VERIFY_STR("string", in_string
, out_string
, "%s");
112 VERIFY_STR("objectpath", in_objectpath
, out_objectpath
, "%s");
113 VERIFY_STR("signature", in_signature
, out_signature
, "%s");
118 VIR_FREE(out_string
);
119 VIR_FREE(out_signature
);
120 VIR_FREE(out_objectpath
);
121 virDBusMessageUnref(msg
);
126 static int testMessageVariant(const void *args ATTRIBUTE_UNUSED
)
128 DBusMessage
*msg
= NULL
;
130 const char *in_str1
= "Hello";
131 int in_int32
= 100000000, out_int32
= 0;
132 const char *in_str2
= "World";
133 char *out_str1
= NULL
, *out_str2
= NULL
;
135 if (!(msg
= dbus_message_new_method_call("org.libvirt.test",
137 "org.libvirt.test.astrochicken",
139 VIR_DEBUG("Failed to allocate method call");
143 if (virDBusMessageEncode(msg
,
148 VIR_DEBUG("Failed to encode arguments");
152 if (virDBusMessageDecode(msg
,
157 VIR_DEBUG("Failed to decode arguments");
162 VERIFY_STR("str1", in_str1
, out_str1
, "%s");
163 VERIFY("int32", in_int32
, out_int32
, "%d");
164 VERIFY_STR("str2", in_str2
, out_str2
, "%s");
171 virDBusMessageUnref(msg
);
175 static int testMessageArray(const void *args ATTRIBUTE_UNUSED
)
177 DBusMessage
*msg
= NULL
;
179 const char *in_str1
= "Hello";
180 int in_int32a
= 1000000000, out_int32a
= 0;
181 int in_int32b
= 2000000000, out_int32b
= 0;
182 int in_int32c
= -2000000000, out_int32c
= 0;
183 bool in_bool
[] = { true, false, true }, out_bool
[] = { false, true, false};
184 const char *in_str2
= "World";
185 char *out_str1
= NULL
, *out_str2
= NULL
;
187 if (!(msg
= dbus_message_new_method_call("org.libvirt.test",
189 "org.libvirt.test.astrochicken",
191 VIR_DEBUG("Failed to allocate method call");
195 if (virDBusMessageEncode(msg
,
198 3, in_int32a
, in_int32b
, in_int32c
,
199 3, in_bool
[0], in_bool
[1], in_bool
[2],
201 VIR_DEBUG("Failed to encode arguments");
205 if (virDBusMessageDecode(msg
,
208 3, &out_int32a
, &out_int32b
, &out_int32c
,
209 3, &out_bool
[0], &out_bool
[1], &out_bool
[2],
211 VIR_DEBUG("Failed to decode arguments");
216 VERIFY_STR("str1", in_str1
, out_str1
, "%s");
217 VERIFY("int32a", in_int32a
, out_int32a
, "%d");
218 VERIFY("int32b", in_int32b
, out_int32b
, "%d");
219 VERIFY("int32c", in_int32c
, out_int32c
, "%d");
220 VERIFY("bool[0]", in_bool
[0], out_bool
[0], "%d");
221 VERIFY("bool[1]", in_bool
[1], out_bool
[1], "%d");
222 VERIFY("bool[2]", in_bool
[2], out_bool
[2], "%d");
223 VERIFY_STR("str2", in_str2
, out_str2
, "%s");
230 virDBusMessageUnref(msg
);
234 static int testMessageEmptyArrayRef(const void *args ATTRIBUTE_UNUSED
)
236 DBusMessage
*msg
= NULL
;
238 const char *in_strv1
[] = {};
240 char **out_strv1
= NULL
;
242 if (!(msg
= dbus_message_new_method_call("org.libvirt.test",
244 "org.libvirt.test.astrochicken",
246 VIR_DEBUG("Failed to allocate method call");
250 if (virDBusMessageEncode(msg
,
253 VIR_DEBUG("Failed to encode arguments");
257 if (virDBusMessageDecode(msg
,
259 &out_nstrv1
, &out_strv1
) < 0) {
260 VIR_DEBUG("Failed to decode arguments");
265 if (out_nstrv1
!= 0) {
266 fprintf(stderr
, "Expected 0 string, but got %zu\n",
274 virDBusMessageUnref(msg
);
278 static int testMessageSingleArrayRef(const void *args ATTRIBUTE_UNUSED
)
280 DBusMessage
*msg
= NULL
;
282 const char *in_strv1
[] = {
285 char **out_strv1
= NULL
;
286 size_t out_nstrv1
= 0;
288 if (!(msg
= dbus_message_new_method_call("org.libvirt.test",
290 "org.libvirt.test.astrochicken",
292 VIR_DEBUG("Failed to allocate method call");
296 if (virDBusMessageEncode(msg
,
299 VIR_DEBUG("Failed to encode arguments");
303 if (virDBusMessageDecode(msg
,
305 &out_nstrv1
, &out_strv1
) < 0) {
306 VIR_DEBUG("Failed to decode arguments");
311 if (out_nstrv1
!= 1) {
312 fprintf(stderr
, "Expected 1 string, but got %zu\n",
316 VERIFY_STR("strv1[0]", in_strv1
[0], out_strv1
[0], "%s");
322 VIR_FREE(out_strv1
[0]);
324 virDBusMessageUnref(msg
);
328 static int testMessageArrayRef(const void *args ATTRIBUTE_UNUSED
)
330 DBusMessage
*msg
= NULL
;
332 const char *in_str1
= "Hello";
334 100000000, 2000000000, -2000000000
336 bool in_bool
[] = { true, false, true };
337 const char *in_strv1
[] = {
340 const char *in_strv2
[] = {
343 int *out_int32
= NULL
;
344 size_t out_nint32
= 0;
345 bool *out_bool
= NULL
;
346 size_t out_nbool
= 0;
347 char **out_strv1
= NULL
;
348 char **out_strv2
= NULL
;
349 size_t out_nstrv1
= 0;
350 size_t out_nstrv2
= 0;
351 const char *in_str2
= "World";
352 char *out_str1
= NULL
, *out_str2
= NULL
;
355 if (!(msg
= dbus_message_new_method_call("org.libvirt.test",
357 "org.libvirt.test.astrochicken",
359 VIR_DEBUG("Failed to allocate method call");
363 if (virDBusMessageEncode(msg
,
371 VIR_DEBUG("Failed to encode arguments");
375 if (virDBusMessageDecode(msg
,
378 &out_nstrv1
, &out_strv1
,
379 &out_nint32
, &out_int32
,
380 &out_nbool
, &out_bool
,
381 &out_nstrv2
, &out_strv2
,
383 VIR_DEBUG("Failed to decode arguments");
388 VERIFY_STR("str1", in_str1
, out_str1
, "%s");
389 if (out_nstrv1
!= 1) {
390 fprintf(stderr
, "Expected 1 string, but got %zu\n",
394 VERIFY_STR("strv1[0]", in_strv1
[0], out_strv1
[0], "%s");
396 if (out_nint32
!= 3) {
397 fprintf(stderr
, "Expected 3 integers, but got %zu\n",
401 VERIFY("int32a", in_int32
[0], out_int32
[0], "%d");
402 VERIFY("int32b", in_int32
[1], out_int32
[1], "%d");
403 VERIFY("int32c", in_int32
[2], out_int32
[2], "%d");
405 if (out_nbool
!= 3) {
406 fprintf(stderr
, "Expected 3 bools, but got %zu\n",
410 VERIFY("bool[0]", in_bool
[0], out_bool
[0], "%d");
411 VERIFY("bool[1]", in_bool
[1], out_bool
[1], "%d");
412 VERIFY("bool[2]", in_bool
[2], out_bool
[2], "%d");
414 if (out_nstrv2
!= 2) {
415 fprintf(stderr
, "Expected 2 strings, but got %zu\n",
419 VERIFY_STR("strv2[0]", in_strv2
[0], out_strv2
[0], "%s");
420 VERIFY_STR("strv2[1]", in_strv2
[1], out_strv2
[1], "%s");
422 VERIFY_STR("str2", in_str2
, out_str2
, "%s");
431 for (i
= 0; i
< out_nstrv1
; i
++)
432 VIR_FREE(out_strv1
[i
]);
434 for (i
= 0; i
< out_nstrv2
; i
++)
435 VIR_FREE(out_strv2
[i
]);
437 virDBusMessageUnref(msg
);
441 static int testMessageStruct(const void *args ATTRIBUTE_UNUSED
)
443 DBusMessage
*msg
= NULL
;
445 unsigned char in_byte
= 200, out_byte
= 0;
446 bool in_bool
= true, out_bool
= false;
447 short in_int16
= 12000, out_int16
= 0;
448 unsigned short in_uint16
= 32000, out_uint16
= 0;
449 int in_int32
= 100000000, out_int32
= 0;
450 unsigned int in_uint32
= 200000000, out_uint32
= 0;
451 long long in_int64
= -1000000000000LL, out_int64
= 0;
452 unsigned long long in_uint64
= 2000000000000LL, out_uint64
= 0;
453 double in_double
= 3.14159265359, out_double
= 0;
454 const char *in_string
= "Hello World";
455 char *out_string
= NULL
;
456 const char *in_objectpath
= "/org/libvirt/test";
457 char *out_objectpath
= NULL
;
458 const char *in_signature
= "ybnqiuxtdsog";
459 char *out_signature
= NULL
;
461 if (!(msg
= dbus_message_new_method_call("org.libvirt.test",
463 "org.libvirt.test.astrochicken",
465 VIR_DEBUG("Failed to allocate method call");
469 if (virDBusMessageEncode(msg
,
475 in_double
, in_string
,
476 in_objectpath
, in_signature
) < 0) {
477 VIR_DEBUG("Failed to encode arguments");
481 if (virDBusMessageDecode(msg
,
483 &out_byte
, &out_bool
,
484 &out_int16
, &out_uint16
,
485 &out_int32
, &out_uint32
,
486 &out_int64
, &out_uint64
,
487 &out_double
, &out_string
,
488 &out_objectpath
, &out_signature
) < 0) {
489 VIR_DEBUG("Failed to decode arguments");
493 VERIFY("byte", in_byte
, out_byte
, "%d");
494 VERIFY("bool", in_bool
, out_bool
, "%d");
495 VERIFY("int16", in_int16
, out_int16
, "%d");
496 VERIFY("uint16", in_int16
, out_int16
, "%d");
497 VERIFY("int32", in_int32
, out_int32
, "%d");
498 VERIFY("uint32", in_int32
, out_int32
, "%d");
499 VERIFY("int64", in_int64
, out_int64
, "%lld");
500 VERIFY("uint64", in_int64
, out_int64
, "%lld");
501 VERIFY("double", in_double
, out_double
, "%lf");
502 VERIFY_STR("string", in_string
, out_string
, "%s");
503 VERIFY_STR("objectpath", in_objectpath
, out_objectpath
, "%s");
504 VERIFY_STR("signature", in_signature
, out_signature
, "%s");
509 VIR_FREE(out_string
);
510 VIR_FREE(out_signature
);
511 VIR_FREE(out_objectpath
);
512 virDBusMessageUnref(msg
);
517 static int testMessageDict(const void *args ATTRIBUTE_UNUSED
)
519 DBusMessage
*msg
= NULL
;
521 const char *in_str1
= "Hello";
522 int in_int32a
= 100000000, out_int32a
= 0;
523 const char *in_key1
= "turnover";
524 int in_int32b
= 200000000, out_int32b
= 0;
525 const char *in_key2
= "revenue";
526 int in_int32c
= 300000000, out_int32c
= 0;
527 const char *in_key3
= "debt";
528 const char *in_str2
= "World";
529 char *out_str1
= NULL
, *out_str2
= NULL
;
530 char *out_key1
= NULL
, *out_key2
= NULL
, *out_key3
= NULL
;
532 if (!(msg
= dbus_message_new_method_call("org.libvirt.test",
534 "org.libvirt.test.astrochicken",
536 VIR_DEBUG("Failed to allocate method call");
540 if (virDBusMessageEncode(msg
,
548 VIR_DEBUG("Failed to encode arguments");
552 if (virDBusMessageDecode(msg
,
556 &out_key1
, &out_int32a
,
557 &out_key2
, &out_int32b
,
558 &out_key3
, &out_int32c
,
560 VIR_DEBUG("Failed to decode arguments");
565 VERIFY_STR("str1", in_str1
, out_str1
, "%s");
566 VERIFY("int32a", in_int32a
, out_int32a
, "%d");
567 VERIFY("int32b", in_int32b
, out_int32b
, "%d");
568 VERIFY("int32c", in_int32c
, out_int32c
, "%d");
569 VERIFY_STR("key1", in_key1
, out_key1
, "%s");
570 VERIFY_STR("key1", in_key2
, out_key2
, "%s");
571 VERIFY_STR("key1", in_key3
, out_key3
, "%s");
572 VERIFY_STR("str2", in_str2
, out_str2
, "%s");
582 virDBusMessageUnref(msg
);
586 static int testMessageDictRef(const void *args ATTRIBUTE_UNUSED
)
588 DBusMessage
*msg
= NULL
;
590 const char *in_str1
= "Hello";
591 const char *in_strv1
[] = {
596 const char *in_str2
= "World";
597 char *out_str1
= NULL
;
598 size_t out_nint32
= 0;
599 char **out_strv1
= NULL
;
600 char *out_str2
= NULL
;
602 if (!(msg
= dbus_message_new_method_call("org.libvirt.test",
604 "org.libvirt.test.astrochicken",
606 VIR_DEBUG("Failed to allocate method call");
610 if (virDBusMessageEncode(msg
,
615 VIR_DEBUG("Failed to encode arguments");
619 if (virDBusMessageDecode(msg
,
625 VIR_DEBUG("Failed to decode arguments: '%s'", virGetLastErrorMessage());
630 VERIFY_STR("str1", in_str1
, out_str1
, "%s");
631 VERIFY_STR("strv1[0]", in_strv1
[0], out_strv1
[0], "%s");
632 VERIFY_STR("strv1[1]", in_strv1
[1], out_strv1
[1], "%s");
633 VERIFY_STR("strv1[2]", in_strv1
[2], out_strv1
[2], "%s");
634 VERIFY_STR("strv1[3]", in_strv1
[3], out_strv1
[3], "%s");
635 VERIFY_STR("strv1[4]", in_strv1
[4], out_strv1
[4], "%s");
636 VERIFY_STR("strv1[5]", in_strv1
[5], out_strv1
[5], "%s");
637 VERIFY_STR("str2", in_str2
, out_str2
, "%s");
645 VIR_FREE(out_strv1
[0]);
646 VIR_FREE(out_strv1
[1]);
647 VIR_FREE(out_strv1
[2]);
648 VIR_FREE(out_strv1
[3]);
649 VIR_FREE(out_strv1
[4]);
650 VIR_FREE(out_strv1
[5]);
653 virDBusMessageUnref(msg
);
657 static int testMessageEmptyDictRef(const void *args ATTRIBUTE_UNUSED
)
659 DBusMessage
*msg
= NULL
;
661 const char *in_strv1
[] = {};
662 size_t out_nint32
= 0;
663 char **out_strv1
= NULL
;
665 if (!(msg
= dbus_message_new_method_call("org.libvirt.test",
667 "org.libvirt.test.astrochicken",
669 VIR_DEBUG("Failed to allocate method call");
673 if (virDBusMessageEncode(msg
,
676 VIR_DEBUG("Failed to encode arguments");
680 if (virDBusMessageDecode(msg
,
684 VIR_DEBUG("Failed to decode arguments: '%s'", virGetLastErrorMessage());
688 if (out_nint32
!= 0) {
689 fprintf(stderr
, "Unexpected dict entries\n");
696 virDBusMessageUnref(msg
);
705 if (virTestRun("Test message simple ", testMessageSimple
, NULL
) < 0)
707 if (virTestRun("Test message variant ", testMessageVariant
, NULL
) < 0)
709 if (virTestRun("Test message array ", testMessageArray
, NULL
) < 0)
711 if (virTestRun("Test message array empty ref ", testMessageEmptyArrayRef
, NULL
) < 0)
713 if (virTestRun("Test message array single ref ", testMessageSingleArrayRef
, NULL
) < 0)
715 if (virTestRun("Test message array ref ", testMessageArrayRef
, NULL
) < 0)
717 if (virTestRun("Test message struct ", testMessageStruct
, NULL
) < 0)
719 if (virTestRun("Test message dict ", testMessageDict
, NULL
) < 0)
721 if (virTestRun("Test message dict empty ref ", testMessageEmptyDictRef
, NULL
) < 0)
723 if (virTestRun("Test message dict ref ", testMessageDictRef
, NULL
) < 0)
725 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
728 VIR_TEST_MAIN(mymain
)