diff --git a/tests/Makefile.am b/tests/Makefile.am
index effa259e3a453f89ee9353614696209801289c53..4627f5d2ba2085c84683f23f76779e8e67b0a447 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -282,7 +282,9 @@ test_programs += qemuxml2argvtest qemuxml2xmltest \
qemumonitortest qemumonitorjsontest qemuhotplugtest \
qemuagenttest qemucapabilitiestest qemucaps2xmltest \
qemumemlocktest \
- qemucommandutiltest
+ qemucommandutiltest \
+ qemublocktest \
+ $(NULL)
test_helpers += qemucapsprobe
test_libraries += libqemumonitortestutils.la \
libqemutestdriver.la \
@@ -668,6 +670,15 @@ qemuhotplugtest_SOURCES = \
$(NULL)
qemuhotplugtest_LDADD = libqemumonitortestutils.la $(qemu_LDADDS) $(LDADDS)
+qemublocktest_SOURCES = \
+ qemublocktest.c testutils.h testutils.c
+qemublocktest_LDADD = $(LDADDS) \
+ ../src/libvirt_conf.la \
+ ../src/libvirt_util.la \
+ $(qemu_LDADDS) \
+ ../gnulib/lib/libgnu.la \
+ $(NULL)
+
domainsnapshotxml2xmltest_SOURCES = \
domainsnapshotxml2xmltest.c testutilsqemu.c testutilsqemu.h \
testutils.c testutils.h
@@ -686,6 +697,7 @@ EXTRA_DIST += qemuxml2argvtest.c qemuxml2xmltest.c qemuargv2xmltest.c \
qemuagenttest.c qemucapabilitiestest.c \
qemucaps2xmltest.c qemucommandutiltest.c \
qemumemlocktest.c qemucpumock.c testutilshostcpus.h \
+ qemublocktest.c \
$(QEMUMONITORTESTUTILS_SOURCES)
endif ! WITH_QEMU
diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
new file mode 100644
index 0000000000000000000000000000000000000000..d1665756abb7369926183a27140084dab955cb71
--- /dev/null
+++ b/tests/qemublocktest.c
@@ -0,0 +1,190 @@
+/*
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see
+ * .
+ */
+
+#include
+
+#include
+
+#include "testutils.h"
+#include "virstoragefile.h"
+#include "virstring.h"
+#include "virlog.h"
+#include "qemu/qemu_block.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+VIR_LOG_INIT("tests.storagetest");
+
+struct testBackingXMLjsonXMLdata {
+ int type;
+ const char *xml;
+};
+
+static int
+testBackingXMLjsonXML(const void *args)
+{
+ const struct testBackingXMLjsonXMLdata *data = args;
+ xmlDocPtr xml = NULL;
+ xmlXPathContextPtr ctxt = NULL;
+ virBuffer buf = VIR_BUFFER_INITIALIZER;
+ virStorageSourcePtr xmlsrc = NULL;
+ virStorageSourcePtr jsonsrc = NULL;
+ virJSONValuePtr backendprops = NULL;
+ virJSONValuePtr wrapper = NULL;
+ char *propsstr = NULL;
+ char *protocolwrapper = NULL;
+ char *actualxml = NULL;
+ int ret = -1;
+
+ if (VIR_ALLOC(xmlsrc) < 0)
+ return -1;
+
+ xmlsrc->type = data->type;
+
+ if (!(xml = virXMLParseStringCtxt(data->xml, "(test storage source XML)", &ctxt)))
+ goto cleanup;
+
+ if (virDomainDiskSourceParse(ctxt->node, ctxt, xmlsrc, 0) < 0) {
+ fprintf(stderr, "failed to parse disk source xml\n");
+ goto cleanup;
+ }
+
+ if (!(backendprops = qemuBlockStorageSourceGetBackendProps(xmlsrc))) {
+ fprintf(stderr, "failed to format disk source json\n");
+ goto cleanup;
+ }
+
+ if (virJSONValueObjectCreate(&wrapper, "a:file", backendprops, NULL) < 0)
+ goto cleanup;
+
+ backendprops = NULL;
+
+ if (!(propsstr = virJSONValueToString(wrapper, false)))
+ goto cleanup;
+
+ if (virAsprintf(&protocolwrapper, "json:%s", propsstr) < 0)
+ goto cleanup;
+
+ if (!(jsonsrc = virStorageSourceNewFromBackingAbsolute(protocolwrapper))) {
+ fprintf(stderr, "failed to parse disk json\n");
+ goto cleanup;
+ }
+
+ if (virDomainDiskSourceFormat(&buf, jsonsrc, 0, 0) < 0 ||
+ !(actualxml = virBufferContentAndReset(&buf))) {
+ fprintf(stderr, "failed to format disk source xml\n");
+ goto cleanup;
+ }
+
+ if (STRNEQ(actualxml, data->xml)) {
+ fprintf(stderr, "\n expected storage source xml:\n'%s'\n"
+ "actual storage source xml:\n%s\n"
+ "intermediate json:\n%s\n",
+ data->xml, actualxml, protocolwrapper);
+ goto cleanup;
+ }
+
+ ret = 0;
+
+ cleanup:
+ virStorageSourceFree(xmlsrc);
+ virStorageSourceFree(jsonsrc);
+ VIR_FREE(propsstr);
+ VIR_FREE(protocolwrapper);
+ VIR_FREE(actualxml);
+ virJSONValueFree(backendprops);
+ virJSONValueFree(wrapper);
+ virBufferFreeAndReset(&buf);
+ xmlXPathFreeContext(ctxt);
+ xmlFreeDoc(xml);
+
+ return ret;
+}
+
+
+static int
+mymain(void)
+{
+ int ret = 0;
+ struct testBackingXMLjsonXMLdata data;
+
+ virTestCounterReset("qemu storage source xml->json->xml ");
+
+#define TEST_JSON_FORMAT(tpe, xmlstr) \
+ do { \
+ data.type = tpe; \
+ data.xml = xmlstr; \
+ if (virTestRun(virTestCounterNext(), testBackingXMLjsonXML, &data) < 0) \
+ ret = -1; \
+ } while (0)
+
+#define TEST_JSON_FORMAT_NET(xmlstr)\
+ TEST_JSON_FORMAT(VIR_STORAGE_TYPE_NETWORK, xmlstr)
+
+ TEST_JSON_FORMAT(VIR_STORAGE_TYPE_FILE, "\n");
+
+ /* type VIR_STORAGE_TYPE_BLOCK is not tested since it parses back to 'file' */
+ /* type VIR_STORAGE_TYPE_DIR it is a 'format' driver in qemu */
+
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+ TEST_JSON_FORMAT_NET("\n");
+
+ return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+VIR_TEST_MAIN(mymain)