X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=tests%2Fregression%2Ftools%2Fmi%2Fvalidate_xml.c;fp=tests%2Fregression%2Ftools%2Fmi%2Fvalidate_xml.c;h=e3c6350902d444dce687afdcd6a76fadb7705707;hb=68270f0f604eefdc89583950a7cfa02fe7a0cab5;hp=0000000000000000000000000000000000000000;hpb=99e88aea851afcb1c9388469c68c7e1fe2b8982a;p=lttng-tools.git diff --git a/tests/regression/tools/mi/validate_xml.c b/tests/regression/tools/mi/validate_xml.c new file mode 100644 index 000000000..e3c635090 --- /dev/null +++ b/tests/regression/tools/mi/validate_xml.c @@ -0,0 +1,201 @@ +/* + * Copyright (C) 2014 Jonathan Rajotte + * + * 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; version 2.1 of + * the License. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + + /* + * This script validate and xml from an xsd. + * argv[1] Path of the xsd + * argv[2] Path to the XML to be validated + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +struct validation_ctx { + xmlSchemaParserCtxtPtr parser_ctx; + xmlSchemaPtr schema; + xmlSchemaValidCtxtPtr schema_validation_ctx; +}; + +enum command_err_code { + CMD_SUCCESS = 0, + CMD_ERROR +}; + +static +void xml_error_handler(void *ctx, const char *format, ...) +{ + char *err_msg; + va_list args; + int ret; + + va_start(args, format); + ret = vasprintf(&err_msg, format, args); + va_end(args); + if (ret == -1) { + fprintf(stderr, "ERR: %s\n", + "String allocation failed in xml error handle"); + return; + } + + fprintf(stderr, "XML Error: %s\n", err_msg); + free(err_msg); +} + +static +void fini_validation_ctx( + struct validation_ctx *ctx) +{ + if (ctx->parser_ctx) { + xmlSchemaFreeParserCtxt(ctx->parser_ctx); + } + + if (ctx->schema) { + xmlSchemaFree(ctx->schema); + } + + if (ctx->schema_validation_ctx) { + xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx); + } + + memset(ctx, 0, sizeof(struct validation_ctx)); +} + +static +int init_validation_ctx( + struct validation_ctx *ctx, char *xsd_path) +{ + int ret; + + if (!xsd_path) { + ret = -LTTNG_ERR_NOMEM; + goto end; + } + + ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path); + if (!ctx->parser_ctx) { + ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; + goto end; + } + xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler, + xml_error_handler, NULL); + + ctx->schema = xmlSchemaParse(ctx->parser_ctx); + if (!ctx->schema) { + ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; + goto end; + } + + ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema); + if (!ctx->schema_validation_ctx) { + ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; + goto end; + } + + xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler, + xml_error_handler, NULL); + ret = 0; + +end: + if (ret) { + fini_validation_ctx(ctx); + } + return ret; +} + +static int validate_xml(const char *xml_file_path, struct validation_ctx *ctx) +{ + int ret; + xmlDocPtr doc = NULL; + + assert(xml_file_path); + assert(ctx); + + /* Open the document */ + doc = xmlParseFile(xml_file_path); + if (!doc) { + ret = LTTNG_ERR_MI_IO_FAIL; + goto end; + } + + /* Validate against the validation ctx (xsd) */ + ret = xmlSchemaValidateDoc(ctx->schema_validation_ctx, doc); + if (ret) { + fprintf(stderr, "ERR: %s\n", "XML is not valid againt provided XSD"); + ret = CMD_ERROR; + goto end; + } + + ret = CMD_SUCCESS; +end: + return ret; + + +} +int main(int argc, char **argv, char *env[]) +{ + int ret; + struct validation_ctx ctx; + + /* Check if we have all argument */ + if (argc < 3) { + fprintf(stderr, "ERR: %s\n", "Missing arguments"); + ret = CMD_ERROR; + goto end; + } + + /* Check if xsd file exist */ + ret = access(argv[1], F_OK); + if (ret < 0) { + fprintf(stderr, "ERR: %s\n", "Xsd path not valid"); + goto end; + } + + /* Check if xml to validate exist */ + ret = access(argv[2], F_OK); + if (ret < 0) { + fprintf(stderr, "ERR: %s\n", "XML path not valid"); + goto end; + } + + /* initialize the validation ctx */ + ret = init_validation_ctx(&ctx, argv[1]); + if (ret) { + goto end; + } + + ret = validate_xml(argv[2], &ctx); + + fini_validation_ctx(&ctx); + +end: + return ret; +}