2 * Copyright (C) 2016 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
22 #include "testutils.h"
29 #define VIR_FROM_THIS VIR_FROM_NONE
31 VIR_LOG_INIT("tests.schematest");
33 struct testSchemaData
{
34 virXMLValidatorPtr validator
;
41 testSchemaFile(const void *args
)
43 const struct testSchemaData
*data
= args
;
44 bool shouldFail
= virStringHasSuffix(data
->xml_path
, "-invalid.xml");
48 if (!(xml
= virXMLParseFile(data
->xml_path
)))
51 if (virXMLValidatorValidate(data
->validator
, xml
) < 0) {
67 testSchemaDir(const char *schema
,
68 virXMLValidatorPtr validator
,
75 char *test_name
= NULL
;
76 char *xml_path
= NULL
;
77 struct testSchemaData data
= {
78 .validator
= validator
,
81 if (virDirOpen(&dir
, dir_path
) < 0)
84 while ((rc
= virDirRead(dir
, &ent
, dir_path
)) > 0) {
85 if (!virStringHasSuffix(ent
->d_name
, ".xml"))
87 if (ent
->d_name
[0] == '.')
90 if (virAsprintf(&xml_path
, "%s/%s", dir_path
, ent
->d_name
) < 0)
93 if (virAsprintf(&test_name
, "Checking %s against %s",
94 ent
->d_name
, schema
) < 0)
97 data
.xml_path
= xml_path
;
98 if (virTestRun(test_name
, testSchemaFile
, &data
) < 0)
117 testSchemaDirs(const char *schema
, virXMLValidatorPtr validator
, ...)
121 char *dir_path
= NULL
;
124 va_start(args
, validator
);
126 while ((dir
= va_arg(args
, char *))) {
127 if (virAsprintf(&dir_path
, "%s/%s", abs_srcdir
, dir
) < 0) {
131 if (testSchemaDir(schema
, validator
, dir_path
) < 0)
144 testSchemaGrammar(const void *opaque
)
146 struct testSchemaData
*data
= (struct testSchemaData
*) opaque
;
150 if (virAsprintf(&schema_path
, "%s/docs/schemas/%s",
151 abs_top_srcdir
, data
->schema
) < 0)
154 if (!(data
->validator
= virXMLValidatorInit(schema_path
)))
160 VIR_FREE(schema_path
);
169 struct testSchemaData data
;
171 memset(&data
, 0, sizeof(data
));
173 #define DO_TEST_DIR(sch, ...) \
176 if (virTestRun("test schema grammar file: " sch, \
177 testSchemaGrammar, &data) == 0) { \
178 /* initialize the validator even if the schema test \
179 * was skipped because of VIR_TEST_RANGE */ \
180 if (!data.validator && testSchemaGrammar(&data) < 0) { \
184 if (testSchemaDirs(sch, data.validator, __VA_ARGS__, NULL) < 0) \
187 virXMLValidatorFree(data.validator); \
188 data.validator = NULL; \
194 #define DO_TEST_FILE(sch, xmlfile) \
197 data.xml_path = abs_srcdir "/" xmlfile; \
198 if (virTestRun("test schema grammar file: " sch, \
199 testSchemaGrammar, &data) == 0) { \
200 /* initialize the validator even if the schema test \
201 * was skipped because of VIR_TEST_RANGE */ \
202 if (!data.validator && testSchemaGrammar(&data) < 0) { \
206 if (virTestRun("Checking " xmlfile " against " sch, \
207 testSchemaFile, &data) < 0) \
210 virXMLValidatorFree(data.validator); \
211 data.validator = NULL; \
217 DO_TEST_DIR("capability.rng", "capabilityschemadata", "vircaps2xmldata");
218 DO_TEST_DIR("domain.rng", "domainschemadata",
219 "qemuxml2argvdata", "xmconfigdata",
220 "qemuxml2xmloutdata", "lxcxml2xmldata",
221 "lxcxml2xmloutdata", "bhyvexml2argvdata", "genericxml2xmlindata",
222 "genericxml2xmloutdata", "xlconfigdata", "libxlxml2domconfigdata",
223 "qemuhotplugtestdomains");
224 DO_TEST_DIR("domainbackup.rng", "domainbackupxml2xmlin",
225 "domainbackupxml2xmlout");
226 DO_TEST_DIR("domaincaps.rng", "domaincapsschemadata");
227 DO_TEST_DIR("domaincheckpoint.rng", "qemudomaincheckpointxml2xmlin",
228 "qemudomaincheckpointxml2xmlout");
229 DO_TEST_DIR("domainsnapshot.rng", "qemudomainsnapshotxml2xmlin",
230 "qemudomainsnapshotxml2xmlout");
231 DO_TEST_DIR("interface.rng", "interfaceschemadata");
232 DO_TEST_DIR("network.rng", "../src/network", "networkxml2xmlin",
233 "networkxml2xmlout", "networkxml2confdata");
234 DO_TEST_DIR("networkport.rng", "virnetworkportxml2xmldata");
235 DO_TEST_DIR("nodedev.rng", "nodedevschemadata");
236 DO_TEST_DIR("nwfilter.rng", "nwfilterxml2xmlout", "../src/nwfilter");
237 DO_TEST_DIR("nwfilterbinding.rng", "virnwfilterbindingxml2xmldata");
238 DO_TEST_DIR("secret.rng", "secretxml2xmlin");
239 DO_TEST_DIR("storagepoolcaps.rng", "storagepoolcapsschemadata");
240 DO_TEST_DIR("storagepool.rng", "storagepoolxml2xmlin", "storagepoolxml2xmlout",
241 "storagepoolschemadata");
242 DO_TEST_DIR("storagevol.rng", "storagevolxml2xmlin", "storagevolxml2xmlout",
243 "storagevolschemadata");
245 DO_TEST_FILE("../news.rng", "../docs/news.xml");
247 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
250 VIR_TEST_MAIN(mymain
)