提交 88a2dc1f 编写于 作者: D Daniel P. Berrange

Support passing dict by reference for dbus messages

Currently DBus dict values must be passed inline

   virDBusMessageEncode("a{ss}",
                        3,
                        "key1", "val1",
                        "key2", "val2",
                        "key3", "val3");
   virDBusMessageDecode("a{ss}",
                        3,
                        &key1, &val1,
                        &key2, &val2,
                        &key3, &val3);

This allows them to be passed by reference

   const char **dictin = {
      "key1", "val1",
      "key2", "val2",
      "key3", "val3"
   };
   char **dictout;
   size_t ndictout;

   virDBusMessageEncode("a&{ss}",
                        ARRAY_CARDINALITY(dict) / 2,
                        dictin);
   virDBusMessageDecode("a&{ss}",
                        &ndictout,
                        &dictout);
Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
上级 c7542573
此差异已折叠。
......@@ -208,6 +208,20 @@ virStringFreeListCount(char **strings,
}
size_t virStringListLen(const char **strings)
{
size_t i = 0;
if (!strings)
return 0;
while (strings[i] != NULL)
i++;
return i;
}
bool
virStringArrayHasString(char **strings, const char *needle)
{
......
......@@ -44,6 +44,8 @@ char *virStringJoin(const char **strings,
void virStringFreeList(char **strings);
void virStringFreeListCount(char **strings, size_t count);
size_t virStringListLen(const char **strings);
bool virStringArrayHasString(char **strings, const char *needle);
char *virArgvToString(const char *const *argv);
......
......@@ -228,6 +228,99 @@ static int testMessageArray(const void *args ATTRIBUTE_UNUSED)
return ret;
}
static int testMessageEmptyArrayRef(const void *args ATTRIBUTE_UNUSED)
{
DBusMessage *msg = NULL;
int ret = -1;
const char *in_strv1[] = {};
size_t out_nstrv1;
char **out_strv1 = NULL;
if (!(msg = dbus_message_new_method_call("org.libvirt.test",
"/org/libvirt/test",
"org.libvirt.test.astrochicken",
"cluck"))) {
VIR_DEBUG("Failed to allocate method call");
goto cleanup;
}
if (virDBusMessageEncode(msg,
"a&s",
0, in_strv1) < 0) {
VIR_DEBUG("Failed to encode arguments");
goto cleanup;
}
if (virDBusMessageDecode(msg,
"a&s",
&out_nstrv1, &out_strv1) < 0) {
VIR_DEBUG("Failed to decode arguments");
goto cleanup;
}
if (out_nstrv1 != 0) {
fprintf(stderr, "Expected 0 string, but got %zu\n",
out_nstrv1);
goto cleanup;
}
ret = 0;
cleanup:
dbus_message_unref(msg);
return ret;
}
static int testMessageSingleArrayRef(const void *args ATTRIBUTE_UNUSED)
{
DBusMessage *msg = NULL;
int ret = -1;
const char *in_strv1[] = {
"Fishfood",
};
char **out_strv1 = NULL;
size_t out_nstrv1 = 0;
if (!(msg = dbus_message_new_method_call("org.libvirt.test",
"/org/libvirt/test",
"org.libvirt.test.astrochicken",
"cluck"))) {
VIR_DEBUG("Failed to allocate method call");
goto cleanup;
}
if (virDBusMessageEncode(msg,
"a&s",
1, in_strv1) < 0) {
VIR_DEBUG("Failed to encode arguments");
goto cleanup;
}
if (virDBusMessageDecode(msg,
"a&s",
&out_nstrv1, &out_strv1) < 0) {
VIR_DEBUG("Failed to decode arguments");
goto cleanup;
}
if (out_nstrv1 != 1) {
fprintf(stderr, "Expected 1 string, but got %zu\n",
out_nstrv1);
goto cleanup;
}
VERIFY_STR("strv1[0]", in_strv1[0], out_strv1[0], "%s");
ret = 0;
cleanup:
if (out_strv1)
VIR_FREE(out_strv1[0]);
dbus_message_unref(msg);
return ret;
}
static int testMessageArrayRef(const void *args ATTRIBUTE_UNUSED)
{
DBusMessage *msg = NULL;
......@@ -426,7 +519,7 @@ static int testMessageDict(const void *args ATTRIBUTE_UNUSED)
}
if (virDBusMessageEncode(msg,
"sa{si}s",
"(sa{si}s)",
in_str1,
3,
in_key1, in_int32a,
......@@ -438,7 +531,7 @@ static int testMessageDict(const void *args ATTRIBUTE_UNUSED)
}
if (virDBusMessageDecode(msg,
"sa{si}s",
"(sa{si}s)",
&out_str1,
3,
&out_key1, &out_int32a,
......@@ -471,6 +564,119 @@ static int testMessageDict(const void *args ATTRIBUTE_UNUSED)
return ret;
}
static int testMessageDictRef(const void *args ATTRIBUTE_UNUSED)
{
DBusMessage *msg = NULL;
int ret = -1;
const char *in_str1 = "Hello";
const char *in_strv1[] = {
"Fruit1", "Apple",
"Fruit2", "Orange",
"Fruit3", "Kiwi",
};
const char *in_str2 = "World";
char *out_str1 = NULL;
size_t out_nint32 = 0;
char **out_strv1 = NULL;
char *out_str2 = NULL;
if (!(msg = dbus_message_new_method_call("org.libvirt.test",
"/org/libvirt/test",
"org.libvirt.test.astrochicken",
"cluck"))) {
VIR_DEBUG("Failed to allocate method call");
goto cleanup;
}
if (virDBusMessageEncode(msg,
"(sa&{ss}s)",
in_str1,
3, in_strv1,
in_str2) < 0) {
VIR_DEBUG("Failed to encode arguments");
goto cleanup;
}
if (virDBusMessageDecode(msg,
"(sa&{ss}s)",
&out_str1,
&out_nint32,
&out_strv1,
&out_str2) < 0) {
VIR_DEBUG("Failed to decode arguments: '%s'", virGetLastErrorMessage());
goto cleanup;
}
VERIFY_STR("str1", in_str1, out_str1, "%s");
VERIFY_STR("strv1[0]", in_strv1[0], out_strv1[0], "%s");
VERIFY_STR("strv1[1]", in_strv1[1], out_strv1[1], "%s");
VERIFY_STR("strv1[2]", in_strv1[2], out_strv1[2], "%s");
VERIFY_STR("strv1[3]", in_strv1[3], out_strv1[3], "%s");
VERIFY_STR("strv1[4]", in_strv1[4], out_strv1[4], "%s");
VERIFY_STR("strv1[5]", in_strv1[5], out_strv1[5], "%s");
VERIFY_STR("str2", in_str2, out_str2, "%s");
ret = 0;
cleanup:
VIR_FREE(out_str1);
VIR_FREE(out_str2);
if (out_strv1) {
VIR_FREE(out_strv1[0]);
VIR_FREE(out_strv1[1]);
VIR_FREE(out_strv1[2]);
VIR_FREE(out_strv1[3]);
VIR_FREE(out_strv1[4]);
VIR_FREE(out_strv1[5]);
}
VIR_FREE(out_strv1);
dbus_message_unref(msg);
return ret;
}
static int testMessageEmptyDictRef(const void *args ATTRIBUTE_UNUSED)
{
DBusMessage *msg = NULL;
int ret = -1;
const char *in_strv1[] = {};
size_t out_nint32 = 0;
char **out_strv1 = NULL;
if (!(msg = dbus_message_new_method_call("org.libvirt.test",
"/org/libvirt/test",
"org.libvirt.test.astrochicken",
"cluck"))) {
VIR_DEBUG("Failed to allocate method call");
goto cleanup;
}
if (virDBusMessageEncode(msg,
"a&{ss}",
0, in_strv1) < 0) {
VIR_DEBUG("Failed to encode arguments");
goto cleanup;
}
if (virDBusMessageDecode(msg,
"a&{ss}",
&out_nint32,
&out_strv1) < 0) {
VIR_DEBUG("Failed to decode arguments: '%s'", virGetLastErrorMessage());
goto cleanup;
}
if (out_nint32 != 0) {
fprintf(stderr, "Unexpected dict entries\n");
goto cleanup;
}
ret = 0;
cleanup:
dbus_message_unref(msg);
return ret;
}
static int
mymain(void)
......@@ -483,12 +689,20 @@ mymain(void)
ret = -1;
if (virtTestRun("Test message array ", testMessageArray, NULL) < 0)
ret = -1;
if (virtTestRun("Test message array empty ref ", testMessageEmptyArrayRef, NULL) < 0)
ret = -1;
if (virtTestRun("Test message array single ref ", testMessageSingleArrayRef, NULL) < 0)
ret = -1;
if (virtTestRun("Test message array ref ", testMessageArrayRef, NULL) < 0)
ret = -1;
if (virtTestRun("Test message struct ", testMessageStruct, NULL) < 0)
ret = -1;
if (virtTestRun("Test message dict ", testMessageDict, NULL) < 0)
ret = -1;
if (virtTestRun("Test message dict empty ref ", testMessageEmptyDictRef, NULL) < 0)
ret = -1;
if (virtTestRun("Test message dict ref ", testMessageDictRef, NULL) < 0)
ret = -1;
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册