Signed-off-by: K Shiva Kiran <shiva...@riseup.net>
---
 src/test/test_driver.c       |  49 +++++-
 tests/meson.build            |   1 +
 tests/nwfiltermetadatatest.c | 297 +++++++++++++++++++++++++++++++++++
 3 files changed, 346 insertions(+), 1 deletion(-)
 create mode 100644 tests/nwfiltermetadatatest.c

diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index ffcaa07b3e..14baa82e52 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -10080,7 +10080,6 @@ testConnectGetDomainCapabilities(virConnectPtr conn 
G_GNUC_UNUSED,
     return virDomainCapsFormat(domCaps);
 }
 
-
 static virNWFilterObj *
 testNWFilterObjFromNWFilter(testDriver *driver,
                             const unsigned char *uuid)
@@ -10262,6 +10261,52 @@ testNWFilterGetXMLDesc(virNWFilterPtr nwfilter,
     return ret;
 }
 
+static int
+testNWFilterSetMetadata(virNWFilterPtr nwfilter,
+                        int type,
+                        const char *metadata,
+                        const char *key,
+                        const char *uri,
+                        unsigned int flags)
+{
+    testDriver *driver = nwfilter->conn->privateData;
+    virNWFilterObj *obj = NULL;
+    int ret = -1;
+
+    virCheckFlags(0, -1);
+
+    if (!(obj = testNWFilterObjFromNWFilter(driver, nwfilter->uuid)))
+        return -1;
+
+    ret = virNWFilterObjSetMetadata(obj, type, metadata,
+                                    key, uri, NULL);
+
+    virNWFilterObjUnlock(obj);
+    return ret;
+}
+
+static char *
+testNWFilterGetMetadata(virNWFilterPtr nwfilter,
+                        int type,
+                        const char *uri,
+                        unsigned int flags)
+{
+    testDriver *driver = nwfilter->conn->privateData;
+    virNWFilterObj *obj = NULL;
+    char *ret = NULL;
+
+    virCheckFlags(0, NULL);
+
+    if (!(obj = testNWFilterObjFromNWFilter(driver, nwfilter->uuid)))
+        return NULL;
+
+    ret = virNWFilterObjGetMetadata(obj, type, uri);
+
+    virNWFilterObjUnlock(obj);
+    return ret;
+}
+
+
 /*
  * Test driver
  */
@@ -10546,6 +10591,8 @@ static virNWFilterDriver testNWFilterDriver = {
     .nwfilterDefineXMLFlags = testNWFilterDefineXMLFlags, /* 9.9.0 */
     .nwfilterUndefine = testNWFilterUndefine, /* 9.9.0 */
     .nwfilterGetXMLDesc = testNWFilterGetXMLDesc, /* 9.9.0 */
+    .nwfilterGetMetadata = testNWFilterGetMetadata, /* 9.9.0 */
+    .nwfilterSetMetadata = testNWFilterSetMetadata, /* 9.9.0 */
 };
 
 static virConnectDriver testConnectDriver = {
diff --git a/tests/meson.build b/tests/meson.build
index b235c5f4dd..d3055a9988 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -259,6 +259,7 @@ tests += [
   { 'name': 'interfacexml2xmltest' },
   { 'name': 'metadatatest' },
   { 'name': 'networkmetadatatest' },
+  { 'name': 'nwfiltermetadatatest' },
   { 'name': 'networkxml2xmlupdatetest' },
   { 'name': 'nodedevxml2xmltest' },
   { 'name': 'nwfilterxml2xmltest' },
diff --git a/tests/nwfiltermetadatatest.c b/tests/nwfiltermetadatatest.c
new file mode 100644
index 0000000000..6f2754aa25
--- /dev/null
+++ b/tests/nwfiltermetadatatest.c
@@ -0,0 +1,297 @@
+/*
+ * Copyright (C) 2013 Red Hat, Inc.
+ *
+ * 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
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+
+#include "testutils.h"
+
+#include "virerror.h"
+#include "virxml.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+static const char metadata1[] =
+"<derp xmlns:foobar='http://foo.bar/'>\n"
+"  <bar>foobar</bar>\n"
+"  <foo fooish='blurb'>foofoo</foo>\n"
+"  <foobar:baz>zomg</foobar:baz>\n"
+"</derp>";
+
+
+static const char metadata1_ns[] =
+"<herp:derp xmlns:foobar='http://foo.bar/' xmlns:herp='http://herp.derp/'>\n"
+"  <herp:bar>foobar</herp:bar>\n"
+"  <herp:foo fooish='blurb'>foofoo</herp:foo>\n"
+"  <foobar:baz>zomg</foobar:baz>\n"
+"</herp:derp>";
+
+
+static const char metadata2[] =
+"<foo>\n"
+"  <bar>baz</bar>\n"
+"</foo>";
+
+
+static const char metadata2_ns[] =
+"<blurb:foo xmlns:blurb='http://herp.derp/'>\n"
+"  <blurb:bar>baz</blurb:bar>\n"
+"</blurb:foo>";
+
+
+static char *
+getMetadataFromXML(virNWFilterPtr nwfilter)
+{
+    g_autoptr(xmlDoc) doc = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
+    xmlNodePtr node;
+
+    g_autofree char *xml = NULL;
+
+    if (!(xml = virNWFilterGetXMLDesc(nwfilter, 0)))
+        return NULL;
+
+    if (!(doc = virXMLParseStringCtxt(xml, "(nwfilter_definition)", &ctxt)))
+        return NULL;
+
+    if (!(node = virXPathNode("//metadata/*", ctxt)))
+        return NULL;
+
+    return virXMLNodeToString(node->doc, node);
+}
+
+
+static void
+metadataXMLConvertApostrophe(char *str)
+{
+    do {
+        if (*str == '\"')
+            *str = '\'';
+    } while ((*++str) != '\0');
+}
+
+
+static bool
+verifyMetadata(virNWFilterPtr nwfilter,
+               const char *expectXML,
+               const char *expectAPI,
+               const char *uri)
+{
+    g_autofree char *metadataXML = NULL;
+    g_autofree char *metadataAPI = NULL;
+
+    if (!expectAPI) {
+        if ((metadataAPI = virNWFilterGetMetadata(nwfilter,
+                                                  
VIR_NWFILTER_METADATA_ELEMENT,
+                                                  uri, 0))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           "expected no metadata in API, but got:\n[%s]",
+                           metadataAPI);
+            return false;
+        }
+    } else {
+        if (!(metadataAPI = virNWFilterGetMetadata(nwfilter,
+                                                   
VIR_NWFILTER_METADATA_ELEMENT,
+                                                   uri, 0)))
+            return false;
+
+        metadataXMLConvertApostrophe(metadataAPI);
+
+        if (STRNEQ(metadataAPI, expectAPI)) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           "XML metadata in API doesn't match expected 
metadata: "
+                           "expected:\n[%s]\ngot:\n[%s]",
+                           expectAPI, metadataAPI);
+            return false;
+        }
+
+    }
+
+    if (!expectXML) {
+        if ((metadataXML = getMetadataFromXML(nwfilter))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           "expected no metadata in XML, but got:\n[%s]",
+                           metadataXML);
+            return false;
+        }
+    } else {
+        if (!(metadataXML = getMetadataFromXML(nwfilter)))
+            return false;
+
+        metadataXMLConvertApostrophe(metadataXML);
+
+        if (STRNEQ(metadataXML, expectXML)) {
+            virReportError(VIR_ERR_INTERNAL_ERROR,
+                           "XML in dump doesn't match expected metadata: "
+                           "expected:\n[%s]\ngot:\n[%s]",
+                           expectXML, metadataXML);
+            return false;
+        }
+    }
+
+    return true;
+}
+
+
+struct metadataTest {
+    virConnectPtr conn;
+    virNWFilterPtr nwfilter;
+
+    const char *data;
+    const char *expect;
+    int type;
+    bool fail;
+};
+
+
+static int
+testAssignMetadata(const void *data)
+{
+    const struct metadataTest *test = data;
+
+    if (virNWFilterSetMetadata(test->nwfilter, VIR_NWFILTER_METADATA_ELEMENT,
+                               metadata1, "herp", "http://herp.derp/";, 0) < 0)
+        return -1;
+
+    if (!verifyMetadata(test->nwfilter, metadata1_ns, metadata1, 
"http://herp.derp/";))
+        return -1;
+
+    return 0;
+}
+
+static int
+testRewriteMetadata(const void *data)
+{
+    const struct metadataTest *test = data;
+
+    if (virNWFilterSetMetadata(test->nwfilter, VIR_NWFILTER_METADATA_ELEMENT,
+                               metadata2, "blurb", "http://herp.derp/";, 0) < 0)
+        return -1;
+
+    if (!verifyMetadata(test->nwfilter, metadata2_ns, metadata2, 
"http://herp.derp/";))
+        return -1;
+
+    return 0;
+}
+
+static int
+testEraseMetadata(const void *data)
+{
+    const struct metadataTest *test = data;
+
+    if (virNWFilterSetMetadata(test->nwfilter, VIR_NWFILTER_METADATA_ELEMENT,
+                               NULL, NULL, "http://herp.derp/";, 0) < 0)
+        return -1;
+
+    if (!verifyMetadata(test->nwfilter, NULL, NULL, "http://herp.derp/";))
+        return -1;
+
+    return 0;
+}
+
+static int
+testTextMetadata(const void *data)
+{
+    const struct metadataTest *test = data;
+    g_autofree char *actual = NULL;
+
+    if (virNWFilterSetMetadata(test->nwfilter, test->type, test->data, NULL, 
NULL, 0) < 0) {
+        if (test->fail)
+            return 0;
+        return -1;
+    }
+
+    actual = virNWFilterGetMetadata(test->nwfilter, test->type, NULL, 0);
+
+    if (STRNEQ_NULLABLE(test->expect, actual)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "expected metadata doesn't match actual: "
+                       "expected:'%s'\ngot: '%s'",
+                       NULLSTR(test->data), NULLSTR(actual));
+        return -1;
+    }
+
+    return 0;
+}
+
+#define TEST_TEXT_METADATA(INDEX, TYPE, DATA, EXPECT, FAIL) \
+    do { \
+        test.type = VIR_NWFILTER_METADATA_ ## TYPE; \
+        test.data = DATA; \
+        test.expect = EXPECT; \
+        test.fail = FAIL; \
+ \
+        if (virTestRun("text metadata: " #TYPE " " INDEX " ", \
+                       testTextMetadata, &test) < 0) \
+            ret = EXIT_FAILURE; \
+    } while (0)
+
+#define TEST_TITLE(INDEX, DATA) \
+    TEST_TEXT_METADATA(INDEX, TITLE, DATA, DATA, false)
+#define TEST_TITLE_EXPECT(INDEX, DATA, EXPECT) \
+    TEST_TEXT_METADATA(INDEX, TITLE, DATA, EXPECT, false)
+#define TEST_TITLE_FAIL(INDEX, DATA) \
+    TEST_TEXT_METADATA(INDEX, TITLE, DATA, DATA, true)
+#define TEST_DESCR(INDEX, DATA) \
+    TEST_TEXT_METADATA(INDEX, DESCRIPTION, DATA, DATA, false)
+#define TEST_DESCR_EXPECT(INDEX, DATA, EXPECT) \
+    TEST_TEXT_METADATA(INDEX, DESCRIPTION, DATA, EXPECT, false)
+
+static int
+mymain(void)
+{
+    struct metadataTest test = { 0 };
+    int ret = EXIT_SUCCESS;
+
+    if (!(test.conn = virConnectOpen("test:///default")))
+        return EXIT_FAILURE;
+
+    if (!(test.nwfilter = virNWFilterLookupByName(test.conn, "test-filter"))) {
+        virConnectClose(test.conn);
+        return EXIT_FAILURE;
+    }
+
+    virTestQuiesceLibvirtErrors(false);
+
+    if (virTestRun("Assign metadata ", testAssignMetadata, &test) < 0)
+        ret = EXIT_FAILURE;
+    if (virTestRun("Rewrite Metadata ", testRewriteMetadata, &test) < 0)
+        ret = EXIT_FAILURE;
+    if (virTestRun("Erase metadata ", testEraseMetadata, &test) < 0)
+        ret = EXIT_FAILURE;
+
+    TEST_TITLE("1", "qwert");
+    TEST_TITLE("2", NULL);
+    TEST_TITLE("3", "blah");
+    TEST_TITLE_FAIL("4", "qwe\nrt");
+    TEST_TITLE_EXPECT("5", "", NULL);
+    TEST_TITLE_FAIL("6", "qwert\n");
+    TEST_TITLE_FAIL("7", "\n");
+
+    TEST_DESCR("1", "qwert\nqwert");
+    TEST_DESCR("2", NULL);
+    TEST_DESCR("3", "qwert");
+    TEST_DESCR("4", "\n");
+    TEST_DESCR_EXPECT("5", "", NULL);
+
+    virNWFilterFree(test.nwfilter);
+    virConnectClose(test.conn);
+
+    return ret;
+}
+
+VIR_TEST_MAIN(mymain)
-- 
2.42.0

Reply via email to