Refactor: test: wrapper for enable_ust_lttng_channel
[lttng-tools.git] / tests / regression / tools / mi / validate_xml.c
1 /*
2 * Copyright (C) 2014 Jonathan Rajotte <jonathan.r.julien@gmail.com>
3 *
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; version 2.1 of
7 * the License.
8 *
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.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /*
20 * This script validate and xml from an xsd.
21 * argv[1] Path of the xsd
22 * argv[2] Path to the XML to be validated
23 */
24
25 #define _GNU_SOURCE
26 #include <assert.h>
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <inttypes.h>
32 #include <dirent.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #include <libxml/xmlschemas.h>
38 #include <libxml/parser.h>
39
40 #include <lttng/lttng-error.h>
41
42 struct validation_ctx {
43 xmlSchemaParserCtxtPtr parser_ctx;
44 xmlSchemaPtr schema;
45 xmlSchemaValidCtxtPtr schema_validation_ctx;
46 };
47
48 enum command_err_code {
49 CMD_SUCCESS = 0,
50 CMD_ERROR
51 };
52
53 static
54 void xml_error_handler(void *ctx, const char *format, ...)
55 {
56 char *err_msg;
57 va_list args;
58 int ret;
59
60 va_start(args, format);
61 ret = vasprintf(&err_msg, format, args);
62 va_end(args);
63 if (ret == -1) {
64 fprintf(stderr, "ERR: %s\n",
65 "String allocation failed in xml error handle");
66 return;
67 }
68
69 fprintf(stderr, "XML Error: %s\n", err_msg);
70 free(err_msg);
71 }
72
73 static
74 void fini_validation_ctx(
75 struct validation_ctx *ctx)
76 {
77 if (ctx->parser_ctx) {
78 xmlSchemaFreeParserCtxt(ctx->parser_ctx);
79 }
80
81 if (ctx->schema) {
82 xmlSchemaFree(ctx->schema);
83 }
84
85 if (ctx->schema_validation_ctx) {
86 xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx);
87 }
88
89 memset(ctx, 0, sizeof(struct validation_ctx));
90 }
91
92 static
93 int init_validation_ctx(
94 struct validation_ctx *ctx, char *xsd_path)
95 {
96 int ret;
97
98 if (!xsd_path) {
99 ret = -LTTNG_ERR_NOMEM;
100 goto end;
101 }
102
103 ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path);
104 if (!ctx->parser_ctx) {
105 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
106 goto end;
107 }
108 xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler,
109 xml_error_handler, NULL);
110
111 ctx->schema = xmlSchemaParse(ctx->parser_ctx);
112 if (!ctx->schema) {
113 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
114 goto end;
115 }
116
117 ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema);
118 if (!ctx->schema_validation_ctx) {
119 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
120 goto end;
121 }
122
123 xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler,
124 xml_error_handler, NULL);
125 ret = 0;
126
127 end:
128 if (ret) {
129 fini_validation_ctx(ctx);
130 }
131 return ret;
132 }
133
134 static int validate_xml(const char *xml_file_path, struct validation_ctx *ctx)
135 {
136 int ret;
137 xmlDocPtr doc = NULL;
138
139 assert(xml_file_path);
140 assert(ctx);
141
142 /* Open the document */
143 doc = xmlParseFile(xml_file_path);
144 if (!doc) {
145 ret = LTTNG_ERR_MI_IO_FAIL;
146 goto end;
147 }
148
149 /* Validate against the validation ctx (xsd) */
150 ret = xmlSchemaValidateDoc(ctx->schema_validation_ctx, doc);
151 if (ret) {
152 fprintf(stderr, "ERR: %s\n", "XML is not valid againt provided XSD");
153 ret = CMD_ERROR;
154 goto end;
155 }
156
157 ret = CMD_SUCCESS;
158 end:
159 return ret;
160
161
162 }
163 int main(int argc, char **argv, char *env[])
164 {
165 int ret;
166 struct validation_ctx ctx = { 0 };
167
168 /* Check if we have all argument */
169 if (argc < 3) {
170 fprintf(stderr, "ERR: %s\n", "Missing arguments");
171 ret = CMD_ERROR;
172 goto end;
173 }
174
175 /* Check if xsd file exist */
176 ret = access(argv[1], F_OK);
177 if (ret < 0) {
178 fprintf(stderr, "ERR: %s\n", "Xsd path not valid");
179 goto end;
180 }
181
182 /* Check if xml to validate exist */
183 ret = access(argv[2], F_OK);
184 if (ret < 0) {
185 fprintf(stderr, "ERR: %s\n", "XML path not valid");
186 goto end;
187 }
188
189 /* initialize the validation ctx */
190 ret = init_validation_ctx(&ctx, argv[1]);
191 if (ret) {
192 goto end;
193 }
194
195 ret = validate_xml(argv[2], &ctx);
196
197 fini_validation_ctx(&ctx);
198
199 end:
200 return ret;
201 }
This page took 0.036731 seconds and 4 git commands to generate.