Signed-off-by: K Shiva Kiran <shiva_kr@xxxxxxxxxx> --- src/conf/nwfilter_conf.c | 5 +- src/conf/nwfilter_conf.h | 3 + src/test/test_driver.c | 240 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 246 insertions(+), 2 deletions(-) diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c index d03f78af4d..344bddcd68 100644 --- a/src/conf/nwfilter_conf.c +++ b/src/conf/nwfilter_conf.c @@ -2509,7 +2509,7 @@ virNWFilterIsAllowedChain(const char *chainname) } -static virNWFilterDef * +virNWFilterDef * virNWFilterDefParseXML(xmlXPathContextPtr ctxt) { g_autoptr(virNWFilterDef) ret = NULL; @@ -2660,6 +2660,9 @@ virNWFilterSaveConfig(const char *configDir, char uuidstr[VIR_UUID_STRING_BUFLEN]; g_autofree char *configFile = NULL; + if (!configDir) + return 0; + if (!(xml = virNWFilterDefFormat(def))) return -1; diff --git a/src/conf/nwfilter_conf.h b/src/conf/nwfilter_conf.h index 34de6eab3d..6e13eae262 100644 --- a/src/conf/nwfilter_conf.h +++ b/src/conf/nwfilter_conf.h @@ -552,6 +552,9 @@ virNWFilterDefParse(const char *xmlStr, const char *filename, unsigned int flags); +virNWFilterDef * +virNWFilterDefParseXML(xmlXPathContextPtr ctxt); + typedef int (*virNWFilterTriggerRebuildCallback)(void *opaque); int diff --git a/src/test/test_driver.c b/src/test/test_driver.c index e87d7cfd44..ffcaa07b3e 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -63,6 +63,8 @@ #include "virhostcpu.h" #include "virdomaincheckpointobjlist.h" #include "virdomainsnapshotobjlist.h" +#include "nwfilter_conf.h" +#include "virnwfilterobj.h" #include "virkeycode.h" #include "virutil.h" @@ -114,6 +116,7 @@ struct _testDriver { /* immutable pointer, self-locking APIs */ virDomainObjList *domains; virNetworkObjList *networks; + virNWFilterObjList *nwfilters; virObjectEventState *eventState; }; typedef struct _testDriver testDriver; @@ -158,6 +161,7 @@ testDriverDispose(void *obj) virObjectUnref(driver->domains); virNodeDeviceObjListFree(driver->devs); virObjectUnref(driver->networks); + virObjectUnref(driver->nwfilters); virObjectUnref(driver->ifaces); virObjectUnref(driver->pools); virObjectUnref(driver->eventState); @@ -465,6 +469,7 @@ testDriverNew(void) !(ret->ifaces = virInterfaceObjListNew()) || !(ret->domains = virDomainObjListNew()) || !(ret->networks = virNetworkObjListNew()) || + !(ret->nwfilters = virNWFilterObjListNew()) || !(ret->devs = virNodeDeviceObjListNew()) || !(ret->pools = virStoragePoolObjListNew())) goto error; @@ -519,6 +524,10 @@ static const char *defaultConnXML = " </ip>" "</network>" "" +"<filter name='test-filter' chain='ipv4' priority='1'>" +" <uuid>aafaaaaf-7aca-7007-bbca-dda97dd1adfc</uuid>" +"</filter>" +"" "<interface type=\"ethernet\" name=\"eth1\">" " <start mode=\"onboot\"/>" " <mac address=\"aa:bb:cc:dd:ee:ff\"/>" @@ -1113,6 +1122,40 @@ testParseNetworks(testDriver *privconn, } +static int +testParseNWFilters(testDriver *privconn, + const char *file, + xmlXPathContextPtr ctxt) +{ + VIR_XPATH_NODE_AUTORESTORE(ctxt) + int num; + size_t i; + virNWFilterObj *obj; + g_autofree xmlNodePtr *nodes = NULL; + + num = virXPathNodeSet("/node/filter", ctxt, &nodes); + if (num < 0) + return -1; + + for (i = 0; i < num; i++) { + g_autoptr(virNWFilterDef) def = NULL; + + if (!(ctxt->node = testParseXMLDocFromFile(nodes[i], file))) + return -1; + + if (!(def = virNWFilterDefParseXML(ctxt))) + return -1; + + if (!(obj = virNWFilterObjListAssignDef(privconn->nwfilters, def))) + return -1; + def = NULL; + + virNWFilterObjUnlock(obj); + } + return 0; +} + + static int testParseInterfaces(testDriver *privconn, const char *file, @@ -1320,6 +1363,8 @@ testOpenParse(testDriver *privconn, return -1; if (testParseNetworks(privconn, file, ctxt) < 0) return -1; + if (testParseNWFilters(privconn, file, ctxt) < 0) + return -1; if (testParseInterfaces(privconn, file, ctxt) < 0) return -1; if (testParseStorage(privconn, file, ctxt) < 0) @@ -10036,6 +10081,187 @@ testConnectGetDomainCapabilities(virConnectPtr conn G_GNUC_UNUSED, } +static virNWFilterObj * +testNWFilterObjFromNWFilter(testDriver *driver, + const unsigned char *uuid) +{ + virNWFilterObj *obj; + char uuidstr[VIR_UUID_STRING_BUFLEN]; + + if (!(obj = virNWFilterObjListFindByUUID(driver->nwfilters, uuid))) { + virUUIDFormat(uuid, uuidstr); + virReportError(VIR_ERR_NO_NWFILTER, + _("no nwfilter with matching uuid '%1$s'"), uuidstr); + } + return obj; +} + +static virNWFilterPtr +testNWFilterLookupByUUID(virConnectPtr conn, + const unsigned char *uuid) +{ + testDriver *driver = conn->privateData; + virNWFilterObj *obj = NULL; + virNWFilterDef *def; + virNWFilterPtr nwfilter = NULL; + + if (!(obj = testNWFilterObjFromNWFilter(driver, uuid))) + return NULL; + + def = virNWFilterObjGetDef(obj); + nwfilter = virGetNWFilter(conn, def->name, def->uuid); + + virNWFilterObjUnlock(obj); + return nwfilter; +} + + +static virNWFilterPtr +testNWFilterLookupByName(virConnectPtr conn, + const char *name) +{ + testDriver *driver = conn->privateData; + virNWFilterObj *obj = NULL; + virNWFilterDef *def; + virNWFilterPtr nwfilter = NULL; + + if (!(obj = virNWFilterObjListFindByName(driver->nwfilters, name))) { + virReportError(VIR_ERR_NO_NWFILTER, + _("no nwfilter with matching name '%1$s'"), + name); + return NULL; + } + def = virNWFilterObjGetDef(obj); + nwfilter = virGetNWFilter(conn, def->name, def->uuid); + + virNWFilterObjUnlock(obj); + return nwfilter; +} + +static int +testNWFilterConnectNumOfNWFilters(virConnectPtr conn) +{ + testDriver *driver = conn->privateData; + return virNWFilterObjListNumOfNWFilters(driver->nwfilters, + conn, NULL); +} + + +static int +testNWFilterConnectListNWFilters(virConnectPtr conn, + char **const names, + int maxnames) +{ + testDriver *driver = conn->privateData; + return virNWFilterObjListGetNames(driver->nwfilters, conn, + NULL, names, maxnames); +} + + +static int +testNWFilterConnectListAllNWFilters(virConnectPtr conn, + virNWFilterPtr **nwfilters, + unsigned int flags) +{ + testDriver *driver = conn->privateData; + virCheckFlags(0, -1); + return virNWFilterObjListExport(conn, driver->nwfilters, + nwfilters, NULL); +} + + +static virNWFilterPtr +testNWFilterDefineXMLFlags(virConnectPtr conn, + const char *xml, + unsigned int flags) +{ + testDriver *driver = conn->privateData; + virNWFilterDef *def; + virNWFilterObj *obj = NULL; + virNWFilterDef *objdef; + virNWFilterPtr nwfilter = NULL; + + virCheckFlags(VIR_NWFILTER_DEFINE_VALIDATE, NULL); + + if (!(def = virNWFilterDefParse(xml, NULL, flags))) + goto cleanup; + + + if (!(obj = virNWFilterObjListAssignDef(driver->nwfilters, def))) + goto cleanup; + + def = NULL; + objdef = virNWFilterObjGetDef(obj); + + nwfilter = virGetNWFilter(conn, objdef->name, objdef->uuid); + + cleanup: + virNWFilterDefFree(def); + if (obj) + virNWFilterObjUnlock(obj); + return nwfilter; +} + + +static virNWFilterPtr +testNWFilterDefineXML(virConnectPtr conn, + const char *xml) +{ + return testNWFilterDefineXMLFlags(conn, xml, 0); +} + + +static int +testNWFilterUndefine(virNWFilterPtr nwfilter) +{ + testDriver *driver = nwfilter->conn->privateData; + virNWFilterObj *obj; + int ret = -1; + + if (!(obj = testNWFilterObjFromNWFilter(driver, nwfilter->uuid))) + goto cleanup; + + if (virNWFilterObjTestUnassignDef(obj) < 0) { + virReportError(VIR_ERR_OPERATION_INVALID, + "%s", + _("nwfilter is in use")); + goto cleanup; + } + + virNWFilterObjListRemove(driver->nwfilters, obj); + obj = NULL; + ret = 0; + + cleanup: + if (obj) + virNWFilterObjUnlock(obj); + + return ret; +} + + +static char * +testNWFilterGetXMLDesc(virNWFilterPtr nwfilter, + unsigned int flags) +{ + testDriver *driver = nwfilter->conn->privateData; + virNWFilterObj *obj = NULL; + virNWFilterDef *def = NULL; + char *ret = NULL; + + virCheckFlags(0, NULL); + + if (!(obj = testNWFilterObjFromNWFilter(driver, nwfilter->uuid))) + return NULL; + + def = virNWFilterObjGetDef(obj); + + ret = virNWFilterDefFormat(def); + + virNWFilterObjUnlock(obj); + return ret; +} + /* * Test driver */ @@ -10310,6 +10536,18 @@ static virNodeDeviceDriver testNodeDeviceDriver = { .nodeDeviceDestroy = testNodeDeviceDestroy, /* 0.7.3 */ }; +static virNWFilterDriver testNWFilterDriver = { + .connectNumOfNWFilters = testNWFilterConnectNumOfNWFilters, /* 9.9.0 */ + .connectListNWFilters = testNWFilterConnectListNWFilters, /* 9.9.0 */ + .connectListAllNWFilters = testNWFilterConnectListAllNWFilters, /* 9.9.0 */ + .nwfilterLookupByName = testNWFilterLookupByName, /* 9.9.0 */ + .nwfilterLookupByUUID = testNWFilterLookupByUUID, /* 9.9.0 */ + .nwfilterDefineXML = testNWFilterDefineXML, /* 9.9.0 */ + .nwfilterDefineXMLFlags = testNWFilterDefineXMLFlags, /* 9.9.0 */ + .nwfilterUndefine = testNWFilterUndefine, /* 9.9.0 */ + .nwfilterGetXMLDesc = testNWFilterGetXMLDesc, /* 9.9.0 */ +}; + static virConnectDriver testConnectDriver = { .localOnly = true, .uriSchemes = (const char *[]){ "test", NULL }, @@ -10317,7 +10555,7 @@ static virConnectDriver testConnectDriver = { .interfaceDriver = &testInterfaceDriver, .networkDriver = &testNetworkDriver, .nodeDeviceDriver = &testNodeDeviceDriver, - .nwfilterDriver = NULL, + .nwfilterDriver = &testNWFilterDriver, .secretDriver = NULL, .storageDriver = &testStorageDriver, }; -- 2.42.0