From 665344da01cabd7af7bb1e92cea0ead0d415a79b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1n=20Tomko?= Date: Fri, 14 Feb 2014 15:44:59 +0100 Subject: [PATCH] Add tests for secret XML parsing also validate it against the RNG schema. --- tests/Makefile.am | 12 ++- tests/secretschematest | 10 ++ .../ephemeral-usage-volume.xml | 7 ++ tests/secretxml2xmlin/usage-ceph.xml | 7 ++ tests/secretxml2xmlin/usage-iscsi.xml | 7 ++ tests/secretxml2xmlin/usage-volume.xml | 7 ++ tests/secretxml2xmltest.c | 98 +++++++++++++++++++ 7 files changed, 147 insertions(+), 1 deletion(-) create mode 100755 tests/secretschematest create mode 100644 tests/secretxml2xmlin/ephemeral-usage-volume.xml create mode 100644 tests/secretxml2xmlin/usage-ceph.xml create mode 100644 tests/secretxml2xmlin/usage-iscsi.xml create mode 100644 tests/secretxml2xmlin/usage-volume.xml create mode 100644 tests/secretxml2xmltest.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 9bb61ca709..0c72cc2489 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -95,6 +95,7 @@ EXTRA_DIST = \ qemuxml2argvdata \ qemuxml2xmloutdata \ qemuxmlnsdata \ + secretxml2xmlin \ securityselinuxlabeldata \ schematestutils.sh \ sexpr2xmldata \ @@ -258,6 +259,8 @@ test_programs += cputest test_programs += metadatatest +test_programs += secretxml2xmltest + test_scripts = \ capabilityschematest \ interfaceschematest \ @@ -267,7 +270,8 @@ test_scripts = \ domainschematest \ nodedevschematest \ nwfilterschematest \ - domainsnapshotschematest + domainsnapshotschematest \ + secretschematest if WITH_LIBVIRTD test_scripts += \ @@ -612,6 +616,12 @@ nwfilterxml2xmltest_SOURCES = \ testutils.c testutils.h nwfilterxml2xmltest_LDADD = $(LDADDS) +secretxml2xmltest_SOURCES = \ + secretxml2xmltest.c \ + testutils.c testutils.h +secretxml2xmltest_LDADD = $(LDADDS) + + if WITH_STORAGE storagevolxml2argvtest_SOURCES = \ storagevolxml2argvtest.c \ diff --git a/tests/secretschematest b/tests/secretschematest new file mode 100755 index 0000000000..f64d1a3e15 --- /dev/null +++ b/tests/secretschematest @@ -0,0 +1,10 @@ +#!/bin/sh + +: ${srcdir=.} +. $srcdir/test-lib.sh +. $abs_srcdir/schematestutils.sh + +DIRS="secretxml2xmlin" +SCHEMA="secret.rng" + +check_schema "$DIRS" "$SCHEMA" diff --git a/tests/secretxml2xmlin/ephemeral-usage-volume.xml b/tests/secretxml2xmlin/ephemeral-usage-volume.xml new file mode 100644 index 0000000000..e273c57686 --- /dev/null +++ b/tests/secretxml2xmlin/ephemeral-usage-volume.xml @@ -0,0 +1,7 @@ + + 22e1353d-c27e-4d6d-bf15-465053e6ba0b + Ephemeral Private Secret + + /var/lib/libvirt/images/image.img + + diff --git a/tests/secretxml2xmlin/usage-ceph.xml b/tests/secretxml2xmlin/usage-ceph.xml new file mode 100644 index 0000000000..e880293a63 --- /dev/null +++ b/tests/secretxml2xmlin/usage-ceph.xml @@ -0,0 +1,7 @@ + + f52a81b2-424e-490c-823d-6bd4235bc572 + Ceph secret + + CephCephCephCeph + + diff --git a/tests/secretxml2xmlin/usage-iscsi.xml b/tests/secretxml2xmlin/usage-iscsi.xml new file mode 100644 index 0000000000..bfc94722e0 --- /dev/null +++ b/tests/secretxml2xmlin/usage-iscsi.xml @@ -0,0 +1,7 @@ + + 27f25d34-aea6-4e2a-be85-fa2c18380be8 + iSCSI secret + + iscsitarget + + diff --git a/tests/secretxml2xmlin/usage-volume.xml b/tests/secretxml2xmlin/usage-volume.xml new file mode 100644 index 0000000000..e273c57686 --- /dev/null +++ b/tests/secretxml2xmlin/usage-volume.xml @@ -0,0 +1,7 @@ + + 22e1353d-c27e-4d6d-bf15-465053e6ba0b + Ephemeral Private Secret + + /var/lib/libvirt/images/image.img + + diff --git a/tests/secretxml2xmltest.c b/tests/secretxml2xmltest.c new file mode 100644 index 0000000000..be9ef64b2f --- /dev/null +++ b/tests/secretxml2xmltest.c @@ -0,0 +1,98 @@ +#include + +#include + +#include "internal.h" +#include "testutils.h" +#include "secret_conf.h" + +#define VIR_FROM_THIS VIR_FROM_NONE + +static int +testCompareXMLToXMLFiles(const char *inxml, const char *outxml) +{ + char *inXmlData = NULL; + char *outXmlData = NULL; + char *actual = NULL; + int ret = -1; + virSecretDefPtr secret = NULL; + + if (virtTestLoadFile(inxml, &inXmlData) < 0) + goto fail; + if (virtTestLoadFile(outxml, &outXmlData) < 0) + goto fail; + + if (!(secret = virSecretDefParseString(inXmlData))) + goto fail; + + if (!(actual = virSecretDefFormat(secret))) + goto fail; + + if (STRNEQ(outXmlData, actual)) { + virtTestDifference(stderr, outXmlData, actual); + goto fail; + } + + ret = 0; + +fail: + VIR_FREE(inXmlData); + VIR_FREE(outXmlData); + VIR_FREE(actual); + virSecretDefFree(secret); + return ret; +} + +struct testInfo { + const char *name; + bool different; +}; + +static int +testCompareXMLToXMLHelper(const void *data) +{ + int result = -1; + char *inxml = NULL; + char *outxml = NULL; + const struct testInfo *info = data; + + if (virAsprintf(&inxml, "%s/secretxml2xmlin/%s.xml", + abs_srcdir, info->name) < 0 || + virAsprintf(&outxml, "%s/secretxml2xml%s/%s.xml", + abs_srcdir, + info->different ? "out" : "in", + info->name) < 0) { + goto cleanup; + } + + result = testCompareXMLToXMLFiles(inxml, outxml); + +cleanup: + VIR_FREE(inxml); + VIR_FREE(outxml); + + return result; +} + +static int +mymain(void) +{ + int ret = 0; + +#define DO_TEST(name) \ + do { \ + const struct testInfo info = {name, false}; \ + if (virtTestRun("Secret XML->XML " name, \ + testCompareXMLToXMLHelper, &info) < 0) \ + ret = -1; \ + } while (0) + + DO_TEST("ephemeral-usage-volume"); + DO_TEST("usage-volume"); + DO_TEST("usage-ceph"); + DO_TEST("usage-iscsi"); + + return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} + +VIRT_TEST_MAIN(mymain)