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