2 * Copyright 2019 EfficiOS Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 #include "param-validation.h"
25 #include <babeltrace2/babeltrace.h>
30 #include "common/common.h"
32 struct bt_param_validation_context
{
37 struct validate_ctx_stack_element
{
39 VALIDATE_CTX_STACK_ELEMENT_MAP
,
40 VALIDATE_CTX_STACK_ELEMENT_ARRAY
,
44 const char *map_key_name
;
50 void validate_ctx_push_map_scope(
51 struct bt_param_validation_context
*ctx
,
54 struct validate_ctx_stack_element stack_element
= {
55 .type
= VALIDATE_CTX_STACK_ELEMENT_MAP
,
59 g_array_append_val(ctx
->scope_stack
, stack_element
);
63 void validate_ctx_push_array_scope(
64 struct bt_param_validation_context
*ctx
, uint64_t index
)
66 struct validate_ctx_stack_element stack_element
= {
67 .type
= VALIDATE_CTX_STACK_ELEMENT_ARRAY
,
71 g_array_append_val(ctx
->scope_stack
, stack_element
);
75 void validate_ctx_pop_scope(struct bt_param_validation_context
*ctx
)
77 BT_ASSERT(ctx
->scope_stack
->len
> 0);
79 g_array_remove_index_fast(ctx
->scope_stack
, ctx
->scope_stack
->len
- 1);
83 void append_scope_to_string(GString
*str
,
84 const struct validate_ctx_stack_element
*elem
,
88 case VALIDATE_CTX_STACK_ELEMENT_MAP
:
90 g_string_append_c(str
, '.');
93 g_string_append(str
, elem
->map_key_name
);
95 case VALIDATE_CTX_STACK_ELEMENT_ARRAY
:
96 g_string_append_printf(str
, "[%" PRIu64
"]", elem
->array_index
);
103 enum bt_param_validation_status
bt_param_validation_error(
104 struct bt_param_validation_context
*ctx
,
105 const char *format
, ...) {
107 enum bt_param_validation_status status
;
109 GString
*str
= g_string_new(NULL
);
111 status
= BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
;
115 if (ctx
->scope_stack
->len
> 0) {
118 g_string_assign(str
, "Error validating parameter `");
120 append_scope_to_string(str
, &g_array_index(ctx
->scope_stack
,
121 struct validate_ctx_stack_element
, 0), true);
123 for (i
= 1; i
< ctx
->scope_stack
->len
; i
++) {
124 append_scope_to_string(str
,
125 &g_array_index(ctx
->scope_stack
,
126 struct validate_ctx_stack_element
, i
), false);
129 g_string_append(str
, "`: ");
131 g_string_assign(str
, "Error validating parameters: ");
134 va_start(ap
, format
);
135 g_string_append_vprintf(str
, format
, ap
);
138 ctx
->error
= g_string_free(str
, FALSE
);
139 status
= BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
;
145 struct validate_map_value_data
147 GPtrArray
*available_keys
;
148 enum bt_param_validation_status status
;
149 struct bt_param_validation_context
*ctx
;
153 enum bt_param_validation_status
validate_value(
154 const bt_value
*value
,
155 const struct bt_param_validation_value_descr
*descr
,
156 struct bt_param_validation_context
*ctx
);
159 bt_bool
validate_map_value_entry(const char *key
,
160 const bt_value
*value
, void *v_data
)
162 struct validate_map_value_data
*data
= v_data
;
163 const struct bt_param_validation_map_value_entry_descr
*entry
= NULL
;
166 /* Check if this key is in the available keys. */
167 for (i
= 0; i
< data
->available_keys
->len
; i
++) {
168 const struct bt_param_validation_map_value_entry_descr
*candidate
=
169 g_ptr_array_index(data
->available_keys
, i
);
171 if (g_str_equal(key
, candidate
->key
)) {
178 /* Key was found in available keys. */
179 g_ptr_array_remove_index_fast(data
->available_keys
, i
);
181 /* Push key name as the scope. */
182 validate_ctx_push_map_scope(data
->ctx
, key
);
184 /* Validate the value of the entry. */
185 data
->status
= validate_value(value
, &entry
->value_descr
,
188 validate_ctx_pop_scope(data
->ctx
);
190 data
->status
= bt_param_validation_error(data
->ctx
,
191 "unexpected key `%s`.", key
);
194 /* Continue iterating if everything is good so far. */
195 return data
->status
== BT_PARAM_VALIDATION_STATUS_OK
;
199 enum bt_param_validation_status
validate_map_value(
200 const struct bt_param_validation_map_value_descr
*descr
,
202 struct bt_param_validation_context
*ctx
) {
203 enum bt_param_validation_status status
;
204 struct validate_map_value_data data
;
205 bt_value_map_foreach_entry_const_status foreach_entry_status
;
206 GPtrArray
*available_keys
= NULL
;
207 const struct bt_param_validation_map_value_entry_descr
*descr_iter
;
210 BT_ASSERT(bt_value_get_type(map
) == BT_VALUE_TYPE_MAP
);
212 available_keys
= g_ptr_array_new();
213 if (!available_keys
) {
214 status
= BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
;
218 for (descr_iter
= descr
->entries
; descr_iter
->key
; descr_iter
++) {
219 g_ptr_array_add(available_keys
, (gpointer
) descr_iter
);
222 /* Initialize `status` to OK, in case the map is empty. */
223 data
.status
= BT_PARAM_VALIDATION_STATUS_OK
;
224 data
.available_keys
= available_keys
;
227 foreach_entry_status
= bt_value_map_foreach_entry_const(map
,
228 validate_map_value_entry
, &data
);
229 if (foreach_entry_status
== BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_INTERRUPTED
) {
230 BT_ASSERT(data
.status
!= BT_PARAM_VALIDATION_STATUS_OK
);
231 status
= data
.status
;
235 BT_ASSERT(data
.status
== BT_PARAM_VALIDATION_STATUS_OK
);
237 for (i
= 0; i
< data
.available_keys
->len
; i
++) {
238 const struct bt_param_validation_map_value_entry_descr
*entry
=
239 g_ptr_array_index(data
.available_keys
, i
);
241 if (!entry
->is_optional
) {
242 status
= bt_param_validation_error(ctx
,
243 "missing mandatory entry `%s`",
249 status
= BT_PARAM_VALIDATION_STATUS_OK
;
252 g_ptr_array_free(available_keys
, TRUE
);
257 enum bt_param_validation_status
validate_array_value(
258 const struct bt_param_validation_array_value_descr
*descr
,
259 const bt_value
*array
,
260 struct bt_param_validation_context
*ctx
) {
261 enum bt_param_validation_status status
;
264 BT_ASSERT(bt_value_get_type(array
) == BT_VALUE_TYPE_ARRAY
);
266 if (bt_value_array_get_length(array
) < descr
->min_length
) {
267 status
= bt_param_validation_error(ctx
,
268 "array is smaller than the minimum length: "
269 "array-length=%" PRIu64
", min-length=%" PRIu64
,
270 bt_value_array_get_length(array
),
275 if (bt_value_array_get_length(array
) > descr
->max_length
) {
276 status
= bt_param_validation_error(ctx
,
277 "array is larger than the maximum length: "
278 "array-length=%" PRIu64
", max-length=%" PRIu64
,
279 bt_value_array_get_length(array
),
284 for (i
= 0; i
< bt_value_array_get_length(array
); i
++) {
285 const bt_value
*element
=
286 bt_value_array_borrow_element_by_index_const(array
, i
);
288 validate_ctx_push_array_scope(ctx
, i
);
290 status
= validate_value(element
, descr
->element_type
, ctx
);
292 validate_ctx_pop_scope(ctx
);
294 if (status
!= BT_PARAM_VALIDATION_STATUS_OK
) {
299 status
= BT_PARAM_VALIDATION_STATUS_OK
;
306 enum bt_param_validation_status
validate_string_value(
307 const struct bt_param_validation_string_value_descr
*descr
,
308 const bt_value
*string
,
309 struct bt_param_validation_context
*ctx
) {
310 enum bt_param_validation_status status
;
311 const char *s
= bt_value_string_get(string
);
312 gchar
*joined_choices
= NULL
;
314 BT_ASSERT(bt_value_get_type(string
) == BT_VALUE_TYPE_STRING
);
316 if (descr
->choices
) {
319 for (choice
= descr
->choices
; *choice
; choice
++) {
320 if (strcmp(s
, *choice
) == 0) {
327 * g_strjoinv takes a gchar **, but it doesn't modify
328 * the array of the strings (yet).
330 joined_choices
= g_strjoinv(", ", (gchar
**) descr
->choices
);
331 if (!joined_choices
) {
332 status
= BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
;
336 status
= bt_param_validation_error(ctx
,
337 "string is not amongst the available choices: "
338 "string=%s, choices=[%s]", s
, joined_choices
);
343 status
= BT_PARAM_VALIDATION_STATUS_OK
;
345 g_free(joined_choices
);
351 enum bt_param_validation_status
validate_value(
352 const bt_value
*value
,
353 const struct bt_param_validation_value_descr
*descr
,
354 struct bt_param_validation_context
*ctx
) {
355 enum bt_param_validation_status status
;
357 /* If there is a custom validation func, we call it and ignore the rest. */
358 if (descr
->validation_func
) {
359 status
= descr
->validation_func(value
, ctx
);
361 if (status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
362 BT_ASSERT(ctx
->error
);
368 if (bt_value_get_type(value
) != descr
->type
) {
369 bt_param_validation_error(ctx
,
370 "unexpected type: expected-type=%s, actual-type=%s",
371 bt_common_value_type_string(descr
->type
),
372 bt_common_value_type_string(bt_value_get_type(value
)));
373 status
= BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
;
377 switch (bt_value_get_type(value
)) {
378 case BT_VALUE_TYPE_MAP
:
379 status
= validate_map_value(&descr
->map
, value
, ctx
);
381 case BT_VALUE_TYPE_ARRAY
:
382 status
= validate_array_value(&descr
->array
, value
, ctx
);
384 case BT_VALUE_TYPE_STRING
:
385 status
= validate_string_value(&descr
->string
, value
, ctx
);
388 status
= BT_PARAM_VALIDATION_STATUS_OK
;
396 enum bt_param_validation_status
bt_param_validation_validate(
397 const bt_value
*params
,
398 const struct bt_param_validation_map_value_entry_descr
*entries
,
400 struct bt_param_validation_context ctx
;
401 struct bt_param_validation_map_value_descr map_value_descr
;
402 enum bt_param_validation_status status
;
404 memset(&ctx
, '\0', sizeof(ctx
));
406 ctx
.scope_stack
= g_array_new(FALSE
, FALSE
,
407 sizeof(struct validate_ctx_stack_element
));
408 if (!ctx
.scope_stack
) {
409 status
= BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
;
413 map_value_descr
.entries
= entries
;
415 status
= validate_map_value(&map_value_descr
, params
, &ctx
);
421 if (ctx
.scope_stack
) {
422 g_array_free(ctx
.scope_stack
, TRUE
);