Add parameter validation utility
[babeltrace.git] / src / plugins / common / param-validation / param-validation.c
1 /*
2 * Copyright 2019 EfficiOS Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #include "param-validation.h"
24
25 #include <babeltrace2/babeltrace.h>
26 #include <glib.h>
27 #include <inttypes.h>
28
29 #include "common/common.h"
30
31 struct bt_param_validation_context {
32 gchar *error;
33 GArray *scope_stack;
34 };
35
36 struct validate_ctx_stack_element {
37 enum {
38 VALIDATE_CTX_STACK_ELEMENT_MAP,
39 VALIDATE_CTX_STACK_ELEMENT_ARRAY,
40 } type;
41
42 union {
43 const char *map_key_name;
44 uint64_t array_index;
45 };
46 };
47
48 static
49 void validate_ctx_push_map_scope(
50 struct bt_param_validation_context *ctx,
51 const char *key)
52 {
53 struct validate_ctx_stack_element stack_element = {
54 .type = VALIDATE_CTX_STACK_ELEMENT_MAP,
55 .map_key_name = key,
56 };
57
58 g_array_append_val(ctx->scope_stack, stack_element);
59 }
60
61 static
62 void validate_ctx_push_array_scope(
63 struct bt_param_validation_context *ctx, uint64_t index)
64 {
65 struct validate_ctx_stack_element stack_element = {
66 .type = VALIDATE_CTX_STACK_ELEMENT_ARRAY,
67 .array_index = index,
68 };
69
70 g_array_append_val(ctx->scope_stack, stack_element);
71 }
72
73 static
74 void validate_ctx_pop_scope(struct bt_param_validation_context *ctx)
75 {
76 BT_ASSERT(ctx->scope_stack->len > 0);
77
78 g_array_remove_index_fast(ctx->scope_stack, ctx->scope_stack->len - 1);
79 }
80
81 static
82 void append_scope_to_string(GString *str,
83 const struct validate_ctx_stack_element *elem,
84 bool first)
85 {
86 switch (elem->type) {
87 case VALIDATE_CTX_STACK_ELEMENT_MAP:
88 if (!first) {
89 g_string_append_c(str, '.');
90 }
91
92 g_string_append(str, elem->map_key_name);
93 break;
94 case VALIDATE_CTX_STACK_ELEMENT_ARRAY:
95 g_string_append_printf(str, "[%" PRIu64 "]", elem->array_index);
96 break;
97 default:
98 abort();
99 }
100 }
101
102 enum bt_param_validation_status bt_param_validation_error(
103 struct bt_param_validation_context *ctx,
104 const char *format, ...) {
105 va_list ap;
106 enum bt_param_validation_status status;
107
108 GString *str = g_string_new(NULL);
109 if (!str) {
110 status = BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR;
111 goto end;
112 }
113
114 if (ctx->scope_stack->len > 0) {
115 guint i;
116
117 g_string_assign(str, "Error validating parameter `");
118
119 append_scope_to_string(str, &g_array_index(ctx->scope_stack,
120 struct validate_ctx_stack_element, 0), true);
121
122 for (i = 1; i < ctx->scope_stack->len; i++) {
123 append_scope_to_string(str,
124 &g_array_index(ctx->scope_stack,
125 struct validate_ctx_stack_element, i), false);
126 }
127
128 g_string_append(str, "`: ");
129 } else {
130 g_string_assign(str, "Error validating parameters: ");
131 }
132
133 va_start(ap, format);
134 g_string_append_vprintf(str, format, ap);
135 va_end(ap);
136
137 ctx->error = g_string_free(str, FALSE);
138 status = BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR;
139
140 end:
141 return status;
142 }
143
144 struct validate_map_value_data
145 {
146 GPtrArray *available_keys;
147 enum bt_param_validation_status status;
148 struct bt_param_validation_context *ctx;
149 };
150
151 static
152 enum bt_param_validation_status validate_value(
153 const bt_value *value,
154 const struct bt_param_validation_value_descr *descr,
155 struct bt_param_validation_context *ctx);
156
157 static
158 bt_bool validate_map_value_entry(const char *key,
159 const bt_value *value, void *v_data)
160 {
161 struct validate_map_value_data *data = v_data;
162 const struct bt_param_validation_map_value_entry_descr *candidate;
163 guint i;
164
165 /* Check if this key is in the available keys. */
166 for (i = 0; i < data->available_keys->len; i++) {
167 candidate = g_ptr_array_index(data->available_keys, i);
168
169 if (g_str_equal(key, candidate->key)) {
170 break;
171 }
172 }
173
174 if (i < data->available_keys->len) {
175 /* Key was found in available keys. */
176 g_ptr_array_remove_index_fast(data->available_keys, i);
177
178 /* Push key name as the scope. */
179 validate_ctx_push_map_scope(data->ctx, key);
180
181 /* Validate the value of the entry. */
182 data->status = validate_value(value, &candidate->value_descr,
183 data->ctx);
184
185 validate_ctx_pop_scope(data->ctx);
186 } else {
187 data->status = bt_param_validation_error(data->ctx,
188 "unexpected key `%s`.", key);
189 }
190
191 /* Continue iterating if everything is good so far. */
192 return data->status == BT_PARAM_VALIDATION_STATUS_OK;
193 }
194
195 static
196 enum bt_param_validation_status validate_map_value(
197 const struct bt_param_validation_map_value_descr *descr,
198 const bt_value *map,
199 struct bt_param_validation_context *ctx) {
200 enum bt_param_validation_status status;
201 struct validate_map_value_data data;
202 bt_value_map_foreach_entry_const_status foreach_entry_status;
203 GPtrArray *available_keys = NULL;
204 const struct bt_param_validation_map_value_entry_descr *descr_iter;
205 guint i;
206
207 BT_ASSERT(bt_value_get_type(map) == BT_VALUE_TYPE_MAP);
208
209 available_keys = g_ptr_array_new();
210 if (!available_keys) {
211 status = BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR;
212 goto end;
213 }
214
215 for (descr_iter = descr->entries; descr_iter->key; descr_iter++) {
216 g_ptr_array_add(available_keys, (gpointer) descr_iter);
217 }
218
219 /* Initialize `status` to OK, in case the map is empty. */
220 data.status = BT_PARAM_VALIDATION_STATUS_OK;
221 data.available_keys = available_keys;
222 data.ctx = ctx;
223
224 foreach_entry_status = bt_value_map_foreach_entry_const(map,
225 validate_map_value_entry, &data);
226 if (foreach_entry_status == BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_MEMORY_ERROR) {
227 status = BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR;
228 goto end;
229 }
230
231 if (data.status != BT_PARAM_VALIDATION_STATUS_OK) {
232 status = data.status;
233 goto end;
234 }
235
236 for (i = 0; i < data.available_keys->len; i++) {
237 const struct bt_param_validation_map_value_entry_descr *entry =
238 g_ptr_array_index(data.available_keys, i);
239
240 if (!entry->is_optional) {
241 status = bt_param_validation_error(ctx,
242 "missing mandatory entry `%s`",
243 entry->key);
244 goto end;
245 }
246 }
247
248 status = BT_PARAM_VALIDATION_STATUS_OK;
249
250 end:
251 g_ptr_array_free(available_keys, TRUE);
252 return status;
253 }
254
255 static
256 enum bt_param_validation_status validate_array_value(
257 const struct bt_param_validation_array_value_descr *descr,
258 const bt_value *array,
259 struct bt_param_validation_context *ctx) {
260 enum bt_param_validation_status status;
261 uint64_t i;
262
263 BT_ASSERT(bt_value_get_type(array) == BT_VALUE_TYPE_ARRAY);
264
265 if (bt_value_array_get_length(array) < descr->min_length) {
266 status = bt_param_validation_error(ctx,
267 "array is smaller than the minimum length: "
268 "array-length=%" PRIu64 ", min-length=%" PRIu64,
269 bt_value_array_get_length(array),
270 descr->min_length);
271 goto end;
272 }
273
274 if (bt_value_array_get_length(array) > descr->max_length) {
275 status = bt_param_validation_error(ctx,
276 "array is larger than the maximum length: "
277 "array-length=%" PRIu64 ", max-length=%" PRIu64,
278 bt_value_array_get_length(array),
279 descr->max_length);
280 goto end;
281 }
282
283 for (i = 0; i < bt_value_array_get_length(array); i++) {
284 const bt_value *element =
285 bt_value_array_borrow_element_by_index_const(array, i);
286
287 validate_ctx_push_array_scope(ctx, i);
288
289 status = validate_value(element, descr->element_type, ctx);
290
291 validate_ctx_pop_scope(ctx);
292
293 if (status != BT_PARAM_VALIDATION_STATUS_OK) {
294 goto end;
295 }
296 }
297
298 status = BT_PARAM_VALIDATION_STATUS_OK;
299
300 end:
301 return status;
302 }
303
304 static
305 enum bt_param_validation_status validate_string_value(
306 const struct bt_param_validation_string_value_descr *descr,
307 const bt_value *string,
308 struct bt_param_validation_context *ctx) {
309 enum bt_param_validation_status status;
310 const char *s = bt_value_string_get(string);
311 gchar *joined_choices = NULL;
312
313 BT_ASSERT(bt_value_get_type(string) == BT_VALUE_TYPE_STRING);
314
315 if (descr->choices) {
316 const char **choice;
317
318 for (choice = descr->choices; *choice; choice++) {
319 if (strcmp(s, *choice) == 0) {
320 break;
321 }
322 }
323
324 if (!*choice) {
325 /*
326 * g_strjoinv takes a gchar **, but it doesn't modify
327 * the array of the strings (yet).
328 */
329 joined_choices = g_strjoinv(", ", (gchar **) descr->choices);
330 if (!joined_choices) {
331 status = BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR;
332 goto end;
333 }
334
335 status = bt_param_validation_error(ctx,
336 "string is not amongst the available choices: "
337 "string=%s, choices=[%s]", s, joined_choices);
338 goto end;
339 }
340 }
341
342 status = BT_PARAM_VALIDATION_STATUS_OK;
343 end:
344 g_free(joined_choices);
345
346 return status;
347 }
348
349 static
350 enum bt_param_validation_status validate_value(
351 const bt_value *value,
352 const struct bt_param_validation_value_descr *descr,
353 struct bt_param_validation_context *ctx) {
354 enum bt_param_validation_status status;
355
356 /* If there is a custom validation func, we call it and ignore the rest. */
357 if (descr->validation_func) {
358 status = descr->validation_func(value, ctx);
359
360 if (status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
361 BT_ASSERT(ctx->error);
362 }
363
364 goto end;
365 }
366
367 if (bt_value_get_type(value) != descr->type) {
368 bt_param_validation_error(ctx,
369 "unexpected type: expected-type=%s, actual-type=%s",
370 bt_common_value_type_string(descr->type),
371 bt_common_value_type_string(bt_value_get_type(value)));
372 status = BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR;
373 goto end;
374 }
375
376 switch (bt_value_get_type(value)) {
377 case BT_VALUE_TYPE_MAP:
378 status = validate_map_value(&descr->map, value, ctx);
379 break;
380 case BT_VALUE_TYPE_ARRAY:
381 status = validate_array_value(&descr->array, value, ctx);
382 break;
383 case BT_VALUE_TYPE_STRING:
384 status = validate_string_value(&descr->string, value, ctx);
385 break;
386 default:
387 status = BT_PARAM_VALIDATION_STATUS_OK;
388 break;
389 }
390
391 end:
392 return status;
393 }
394
395 enum bt_param_validation_status bt_param_validation_validate(
396 const bt_value *params,
397 const struct bt_param_validation_map_value_entry_descr *entries,
398 gchar **error) {
399 struct bt_param_validation_context ctx;
400 struct bt_param_validation_map_value_descr map_value_descr;
401 enum bt_param_validation_status status;
402
403 ctx.error = NULL;
404 ctx.scope_stack = g_array_new(FALSE, FALSE,
405 sizeof(struct validate_ctx_stack_element));
406 g_ptr_array_new_with_free_func(g_free);
407 if (!ctx.scope_stack) {
408 status = BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR;
409 goto end;
410 }
411
412 map_value_descr.entries = entries;
413
414 status = validate_map_value(&map_value_descr, params, &ctx);
415
416 end:
417 *error = ctx.error;
418 ctx.error = NULL;
419
420 return status;
421 }
This page took 0.037992 seconds and 4 git commands to generate.