Tests: validate_xml: leak of xml document instance
[lttng-tools.git] / tests / utils / xml-utils / validate_xml.cpp
CommitLineData
68270f0f 1/*
4b2b86f2 2 * Copyright (C) 2014 EfficiOS Inc.
68270f0f 3 *
9d16b343 4 * SPDX-License-Identifier: LGPL-2.1-only
68270f0f 5 *
68270f0f
JRJ
6 */
7
8 /*
9 * This script validate and xml from an xsd.
10 * argv[1] Path of the xsd
11 * argv[2] Path to the XML to be validated
12 */
13
68270f0f
JRJ
14#include <ctype.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <inttypes.h>
19#include <dirent.h>
20#include <unistd.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23
24#include <libxml/xmlschemas.h>
25#include <libxml/parser.h>
26
27#include <lttng/lttng-error.h>
c9e313bc 28#include <common/macros.hpp>
d22ad5f8 29
68270f0f
JRJ
30struct validation_ctx {
31 xmlSchemaParserCtxtPtr parser_ctx;
32 xmlSchemaPtr schema;
33 xmlSchemaValidCtxtPtr schema_validation_ctx;
34};
35
36enum command_err_code {
37 CMD_SUCCESS = 0,
38 CMD_ERROR
39};
40
d22ad5f8 41static ATTR_FORMAT_PRINTF(2, 3)
f46376a1
MJ
42void xml_error_handler(void *ctx __attribute__((unused)),
43 const char *format, ...)
68270f0f
JRJ
44{
45 char *err_msg;
46 va_list args;
47 int ret;
48
49 va_start(args, format);
50 ret = vasprintf(&err_msg, format, args);
51 va_end(args);
52 if (ret == -1) {
53 fprintf(stderr, "ERR: %s\n",
54 "String allocation failed in xml error handle");
55 return;
56 }
57
58 fprintf(stderr, "XML Error: %s\n", err_msg);
59 free(err_msg);
60}
61
62static
63void fini_validation_ctx(
64 struct validation_ctx *ctx)
65{
66 if (ctx->parser_ctx) {
67 xmlSchemaFreeParserCtxt(ctx->parser_ctx);
68 }
69
70 if (ctx->schema) {
71 xmlSchemaFree(ctx->schema);
72 }
73
74 if (ctx->schema_validation_ctx) {
75 xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx);
76 }
77
78 memset(ctx, 0, sizeof(struct validation_ctx));
79}
80
81static
82int init_validation_ctx(
83 struct validation_ctx *ctx, char *xsd_path)
84{
85 int ret;
86
87 if (!xsd_path) {
88 ret = -LTTNG_ERR_NOMEM;
89 goto end;
90 }
91
92 ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path);
93 if (!ctx->parser_ctx) {
94 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
95 goto end;
96 }
97 xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler,
98 xml_error_handler, NULL);
99
100 ctx->schema = xmlSchemaParse(ctx->parser_ctx);
101 if (!ctx->schema) {
102 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
103 goto end;
104 }
105
106 ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema);
107 if (!ctx->schema_validation_ctx) {
108 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
109 goto end;
110 }
111
112 xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler,
113 xml_error_handler, NULL);
114 ret = 0;
115
116end:
117 if (ret) {
118 fini_validation_ctx(ctx);
119 }
120 return ret;
121}
122
123static int validate_xml(const char *xml_file_path, struct validation_ctx *ctx)
124{
125 int ret;
126 xmlDocPtr doc = NULL;
127
a0377dfe
FD
128 LTTNG_ASSERT(xml_file_path);
129 LTTNG_ASSERT(ctx);
68270f0f
JRJ
130
131 /* Open the document */
132 doc = xmlParseFile(xml_file_path);
133 if (!doc) {
134 ret = LTTNG_ERR_MI_IO_FAIL;
135 goto end;
136 }
137
138 /* Validate against the validation ctx (xsd) */
139 ret = xmlSchemaValidateDoc(ctx->schema_validation_ctx, doc);
140 if (ret) {
141 fprintf(stderr, "ERR: %s\n", "XML is not valid againt provided XSD");
142 ret = CMD_ERROR;
143 goto end;
144 }
145
146 ret = CMD_SUCCESS;
147end:
21699d0f
JG
148 if (doc) {
149 xmlFreeDoc(doc);
150 }
68270f0f 151 return ret;
68270f0f 152}
21699d0f 153
f46376a1 154int main(int argc, char **argv)
68270f0f
JRJ
155{
156 int ret;
1c9a0b0e 157 struct validation_ctx ctx = {};
68270f0f
JRJ
158
159 /* Check if we have all argument */
160 if (argc < 3) {
161 fprintf(stderr, "ERR: %s\n", "Missing arguments");
162 ret = CMD_ERROR;
163 goto end;
164 }
165
166 /* Check if xsd file exist */
167 ret = access(argv[1], F_OK);
168 if (ret < 0) {
169 fprintf(stderr, "ERR: %s\n", "Xsd path not valid");
170 goto end;
171 }
172
173 /* Check if xml to validate exist */
174 ret = access(argv[2], F_OK);
175 if (ret < 0) {
176 fprintf(stderr, "ERR: %s\n", "XML path not valid");
177 goto end;
178 }
179
180 /* initialize the validation ctx */
181 ret = init_validation_ctx(&ctx, argv[1]);
182 if (ret) {
183 goto end;
184 }
185
186 ret = validate_xml(argv[2], &ctx);
187
188 fini_validation_ctx(&ctx);
189
190end:
191 return ret;
192}
This page took 0.049355 seconds and 4 git commands to generate.