2016-06-07 16:54:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 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 "viralloc.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virxml.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
VIR_LOG_INIT("tests.schematest");
|
|
|
|
|
|
|
|
struct testSchemaData {
|
|
|
|
virXMLValidatorPtr validator;
|
2017-03-28 11:28:02 +00:00
|
|
|
const char *schema;
|
2016-06-07 16:54:43 +00:00
|
|
|
const char *xml_path;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
testSchemaFile(const void *args)
|
|
|
|
{
|
|
|
|
const struct testSchemaData *data = args;
|
2019-03-07 08:40:54 +00:00
|
|
|
bool shouldFail = virStringHasSuffix(data->xml_path, "-invalid.xml");
|
2016-06-07 16:54:43 +00:00
|
|
|
xmlDocPtr xml = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!(xml = virXMLParseFile(data->xml_path)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virXMLValidatorValidate(data->validator, xml) < 0) {
|
|
|
|
if (!shouldFail)
|
|
|
|
goto cleanup;
|
|
|
|
} else {
|
|
|
|
if (shouldFail)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
xmlFreeDoc(xml);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
testSchemaDir(const char *schema,
|
|
|
|
virXMLValidatorPtr validator,
|
|
|
|
const char *dir_path)
|
|
|
|
{
|
|
|
|
DIR *dir = NULL;
|
|
|
|
struct dirent *ent;
|
|
|
|
int ret = 0;
|
|
|
|
int rc;
|
|
|
|
struct testSchemaData data = {
|
|
|
|
.validator = validator,
|
|
|
|
};
|
|
|
|
|
2019-11-19 10:55:05 +00:00
|
|
|
if (virDirOpen(&dir, dir_path) < 0) {
|
|
|
|
virTestPropagateLibvirtError();
|
2016-06-07 16:54:43 +00:00
|
|
|
return -1;
|
2019-11-19 10:55:05 +00:00
|
|
|
}
|
2016-06-07 16:54:43 +00:00
|
|
|
|
|
|
|
while ((rc = virDirRead(dir, &ent, dir_path)) > 0) {
|
2019-11-19 10:16:34 +00:00
|
|
|
g_autofree char *test_name = NULL;
|
|
|
|
g_autofree char *xml_path = NULL;
|
|
|
|
|
2019-03-07 08:40:54 +00:00
|
|
|
if (!virStringHasSuffix(ent->d_name, ".xml"))
|
2016-06-07 16:54:43 +00:00
|
|
|
continue;
|
2018-12-17 13:17:26 +00:00
|
|
|
if (ent->d_name[0] == '.')
|
|
|
|
continue;
|
2016-06-07 16:54:43 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
xml_path = g_strdup_printf("%s/%s", dir_path, ent->d_name);
|
2016-06-07 16:54:43 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
test_name = g_strdup_printf("Checking %s against %s", ent->d_name, schema);
|
2016-06-07 16:54:43 +00:00
|
|
|
|
|
|
|
data.xml_path = xml_path;
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun(test_name, testSchemaFile, &data) < 0)
|
2016-06-07 16:54:43 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:55:05 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
virTestPropagateLibvirtError();
|
2016-06-07 16:54:43 +00:00
|
|
|
ret = -1;
|
2019-11-19 10:55:05 +00:00
|
|
|
}
|
2016-06-07 16:54:43 +00:00
|
|
|
|
2016-06-21 10:40:29 +00:00
|
|
|
VIR_DIR_CLOSE(dir);
|
2016-06-07 16:54:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2016-08-02 07:11:33 +00:00
|
|
|
testSchemaDirs(const char *schema, virXMLValidatorPtr validator, ...)
|
2016-06-07 16:54:43 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int ret = 0;
|
|
|
|
const char *dir;
|
|
|
|
|
2016-08-02 07:11:33 +00:00
|
|
|
va_start(args, validator);
|
2016-06-07 16:54:43 +00:00
|
|
|
|
|
|
|
while ((dir = va_arg(args, char *))) {
|
2019-11-19 10:16:34 +00:00
|
|
|
g_autofree char *dir_path = g_strdup_printf("%s/%s", abs_srcdir, dir);
|
2016-06-08 09:04:04 +00:00
|
|
|
if (testSchemaDir(schema, validator, dir_path) < 0)
|
2016-06-07 16:54:43 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 07:11:33 +00:00
|
|
|
static int
|
|
|
|
testSchemaGrammar(const void *opaque)
|
|
|
|
{
|
2017-03-28 11:28:02 +00:00
|
|
|
struct testSchemaData *data = (struct testSchemaData *) opaque;
|
2019-11-19 10:16:34 +00:00
|
|
|
g_autofree char *schema_path = NULL;
|
2016-08-02 07:11:33 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
schema_path = g_strdup_printf("%s/docs/schemas/%s", abs_top_srcdir,
|
|
|
|
data->schema);
|
2016-08-02 07:11:33 +00:00
|
|
|
|
|
|
|
if (!(data->validator = virXMLValidatorInit(schema_path)))
|
2019-11-19 10:16:34 +00:00
|
|
|
return -1;
|
2016-08-02 07:11:33 +00:00
|
|
|
|
2019-11-19 10:16:34 +00:00
|
|
|
return 0;
|
2016-08-02 07:11:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-07 16:54:43 +00:00
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2017-03-28 11:28:02 +00:00
|
|
|
struct testSchemaData data;
|
2016-08-02 07:11:33 +00:00
|
|
|
|
2016-08-12 10:19:44 +00:00
|
|
|
memset(&data, 0, sizeof(data));
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define DO_TEST_DIR(sch, ...) \
|
|
|
|
do { \
|
|
|
|
data.schema = sch; \
|
|
|
|
if (virTestRun("test schema grammar file: " sch, \
|
|
|
|
testSchemaGrammar, &data) == 0) { \
|
|
|
|
/* initialize the validator even if the schema test \
|
|
|
|
* was skipped because of VIR_TEST_RANGE */ \
|
|
|
|
if (!data.validator && testSchemaGrammar(&data) < 0) { \
|
|
|
|
ret = -1; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
if (testSchemaDirs(sch, data.validator, __VA_ARGS__, NULL) < 0) \
|
|
|
|
ret = -1; \
|
|
|
|
\
|
|
|
|
virXMLValidatorFree(data.validator); \
|
|
|
|
data.validator = NULL; \
|
|
|
|
} else { \
|
|
|
|
ret = -1; \
|
|
|
|
} \
|
2016-08-02 07:11:33 +00:00
|
|
|
} while (0)
|
2016-06-07 16:54:43 +00:00
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define DO_TEST_FILE(sch, xmlfile) \
|
|
|
|
do { \
|
|
|
|
data.schema = sch; \
|
|
|
|
data.xml_path = abs_srcdir "/" xmlfile; \
|
|
|
|
if (virTestRun("test schema grammar file: " sch, \
|
|
|
|
testSchemaGrammar, &data) == 0) { \
|
|
|
|
/* initialize the validator even if the schema test \
|
|
|
|
* was skipped because of VIR_TEST_RANGE */ \
|
|
|
|
if (!data.validator && testSchemaGrammar(&data) < 0) { \
|
|
|
|
ret = -1; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
if (virTestRun("Checking " xmlfile " against " sch, \
|
|
|
|
testSchemaFile, &data) < 0) \
|
|
|
|
ret = -1; \
|
|
|
|
\
|
|
|
|
virXMLValidatorFree(data.validator); \
|
|
|
|
data.validator = NULL; \
|
|
|
|
} else { \
|
|
|
|
ret = -1; \
|
|
|
|
} \
|
2017-03-28 11:28:02 +00:00
|
|
|
} while (0)
|
|
|
|
|
2018-03-28 22:20:24 +00:00
|
|
|
DO_TEST_DIR("capability.rng", "capabilityschemadata", "vircaps2xmldata");
|
2019-06-14 13:03:04 +00:00
|
|
|
DO_TEST_DIR("domain.rng", "domainschemadata",
|
2019-07-03 06:36:09 +00:00
|
|
|
"qemuxml2argvdata", "xmconfigdata",
|
2019-07-03 06:25:15 +00:00
|
|
|
"qemuxml2xmloutdata", "lxcxml2xmldata",
|
2017-03-28 11:28:02 +00:00
|
|
|
"lxcxml2xmloutdata", "bhyvexml2argvdata", "genericxml2xmlindata",
|
2017-11-29 17:43:29 +00:00
|
|
|
"genericxml2xmloutdata", "xlconfigdata", "libxlxml2domconfigdata",
|
2017-03-28 11:28:02 +00:00
|
|
|
"qemuhotplugtestdomains");
|
2019-10-22 12:36:41 +00:00
|
|
|
DO_TEST_DIR("domaincaps.rng", "domaincapsdata");
|
2018-06-12 03:12:21 +00:00
|
|
|
DO_TEST_DIR("domaincheckpoint.rng", "qemudomaincheckpointxml2xmlin",
|
|
|
|
"qemudomaincheckpointxml2xmlout");
|
2019-07-05 18:24:40 +00:00
|
|
|
DO_TEST_DIR("domainsnapshot.rng", "qemudomainsnapshotxml2xmlin",
|
|
|
|
"qemudomainsnapshotxml2xmlout");
|
2017-03-28 11:28:02 +00:00
|
|
|
DO_TEST_DIR("interface.rng", "interfaceschemadata");
|
|
|
|
DO_TEST_DIR("network.rng", "../src/network", "networkxml2xmlin",
|
|
|
|
"networkxml2xmlout", "networkxml2confdata");
|
2018-08-31 14:21:34 +00:00
|
|
|
DO_TEST_DIR("networkport.rng", "virnetworkportxml2xmldata");
|
2017-03-28 11:28:02 +00:00
|
|
|
DO_TEST_DIR("nodedev.rng", "nodedevschemadata");
|
2019-07-19 17:31:20 +00:00
|
|
|
DO_TEST_DIR("nwfilter.rng", "nwfilterxml2xmlout", "../src/nwfilter");
|
2018-05-15 17:00:16 +00:00
|
|
|
DO_TEST_DIR("nwfilterbinding.rng", "virnwfilterbindingxml2xmldata");
|
2017-03-28 11:28:02 +00:00
|
|
|
DO_TEST_DIR("secret.rng", "secretxml2xmlin");
|
2019-02-07 17:29:15 +00:00
|
|
|
DO_TEST_DIR("storagepoolcaps.rng", "storagepoolcapsschemadata");
|
2017-03-28 11:28:02 +00:00
|
|
|
DO_TEST_DIR("storagepool.rng", "storagepoolxml2xmlin", "storagepoolxml2xmlout",
|
|
|
|
"storagepoolschemadata");
|
|
|
|
DO_TEST_DIR("storagevol.rng", "storagevolxml2xmlin", "storagevolxml2xmlout",
|
|
|
|
"storagevolschemadata");
|
2016-06-07 16:54:43 +00:00
|
|
|
|
2017-04-05 07:51:51 +00:00
|
|
|
DO_TEST_FILE("../news.rng", "../docs/news.xml");
|
2017-03-28 11:35:05 +00:00
|
|
|
|
2016-06-07 16:54:43 +00:00
|
|
|
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain)
|