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>
29 #include "common/common.h"
31 struct bt_param_validation_context
{
36 struct validate_ctx_stack_element
{
38 VALIDATE_CTX_STACK_ELEMENT_MAP
,
39 VALIDATE_CTX_STACK_ELEMENT_ARRAY
,
43 const char *map_key_name
;
49 void validate_ctx_push_map_scope(
50 struct bt_param_validation_context
*ctx
,
53 struct validate_ctx_stack_element stack_element
= {
54 .type
= VALIDATE_CTX_STACK_ELEMENT_MAP
,
58 g_array_append_val(ctx
->scope_stack
, stack_element
);
62 void validate_ctx_push_array_scope(
63 struct bt_param_validation_context
*ctx
, uint64_t index
)
65 struct validate_ctx_stack_element stack_element
= {
66 .type
= VALIDATE_CTX_STACK_ELEMENT_ARRAY
,
70 g_array_append_val(ctx
->scope_stack
, stack_element
);
74 void validate_ctx_pop_scope(struct bt_param_validation_context
*ctx
)
76 BT_ASSERT(ctx
->scope_stack
->len
> 0);
78 g_array_remove_index_fast(ctx
->scope_stack
, ctx
->scope_stack
->len
- 1);
82 void append_scope_to_string(GString
*str
,
83 const struct validate_ctx_stack_element
*elem
,
87 case VALIDATE_CTX_STACK_ELEMENT_MAP
:
89 g_string_append_c(str
, '.');
92 g_string_append(str
, elem
->map_key_name
);
94 case VALIDATE_CTX_STACK_ELEMENT_ARRAY
:
95 g_string_append_printf(str
, "[%" PRIu64
"]", elem
->array_index
);
102 enum bt_param_validation_status
bt_param_validation_error(
103 struct bt_param_validation_context
*ctx
,
104 const char *format
, ...) {
106 enum bt_param_validation_status status
;
108 GString
*str
= g_string_new(NULL
);
110 status
= BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
;
114 if (ctx
->scope_stack
->len
> 0) {
117 g_string_assign(str
, "Error validating parameter `");
119 append_scope_to_string(str
, &g_array_index(ctx
->scope_stack
,
120 struct validate_ctx_stack_element
, 0), true);
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);
128 g_string_append(str
, "`: ");
130 g_string_assign(str
, "Error validating parameters: ");
133 va_start(ap
, format
);
134 g_string_append_vprintf(str
, format
, ap
);
137 ctx
->error
= g_string_free(str
, FALSE
);
138 status
= BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
;
144 struct validate_map_value_data
146 GPtrArray
*available_keys
;
147 enum bt_param_validation_status status
;
148 struct bt_param_validation_context
*ctx
;
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
);
158 bt_bool
validate_map_value_entry(const char *key
,
159 const bt_value
*value
, void *v_data
)
161 struct validate_map_value_data
*data
= v_data
;
162 const struct bt_param_validation_map_value_entry_descr
*candidate
;
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
);
169 if (g_str_equal(key
, candidate
->key
)) {
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
);
178 /* Push key name as the scope. */
179 validate_ctx_push_map_scope(data
->ctx
, key
);
181 /* Validate the value of the entry. */
182 data
->status
= validate_value(value
, &candidate
->value_descr
,
185 validate_ctx_pop_scope(data
->ctx
);
187 data
->status
= bt_param_validation_error(data
->ctx
,
188 "unexpected key `%s`.", key
);
191 /* Continue iterating if everything is good so far. */
192 return data
->status
== BT_PARAM_VALIDATION_STATUS_OK
;
196 enum bt_param_validation_status
validate_map_value(
197 const struct bt_param_validation_map_value_descr
*descr
,
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
;
207 BT_ASSERT(bt_value_get_type(map
) == BT_VALUE_TYPE_MAP
);
209 available_keys
= g_ptr_array_new();
210 if (!available_keys
) {
211 status
= BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
;
215 for (descr_iter
= descr
->entries
; descr_iter
->key
; descr_iter
++) {
216 g_ptr_array_add(available_keys
, (gpointer
) descr_iter
);
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
;
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
;
231 if (data
.status
!= BT_PARAM_VALIDATION_STATUS_OK
) {
232 status
= data
.status
;
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
);
240 if (!entry
->is_optional
) {
241 status
= bt_param_validation_error(ctx
,
242 "missing mandatory entry `%s`",
248 status
= BT_PARAM_VALIDATION_STATUS_OK
;
251 g_ptr_array_free(available_keys
, TRUE
);
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
;
263 BT_ASSERT(bt_value_get_type(array
) == BT_VALUE_TYPE_ARRAY
);
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
),
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
),
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
);
287 validate_ctx_push_array_scope(ctx
, i
);
289 status
= validate_value(element
, descr
->element_type
, ctx
);
291 validate_ctx_pop_scope(ctx
);
293 if (status
!= BT_PARAM_VALIDATION_STATUS_OK
) {
298 status
= BT_PARAM_VALIDATION_STATUS_OK
;
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
;
313 BT_ASSERT(bt_value_get_type(string
) == BT_VALUE_TYPE_STRING
);
315 if (descr
->choices
) {
318 for (choice
= descr
->choices
; *choice
; choice
++) {
319 if (strcmp(s
, *choice
) == 0) {
326 * g_strjoinv takes a gchar **, but it doesn't modify
327 * the array of the strings (yet).
329 joined_choices
= g_strjoinv(", ", (gchar
**) descr
->choices
);
330 if (!joined_choices
) {
331 status
= BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
;
335 status
= bt_param_validation_error(ctx
,
336 "string is not amongst the available choices: "
337 "string=%s, choices=[%s]", s
, joined_choices
);
342 status
= BT_PARAM_VALIDATION_STATUS_OK
;
344 g_free(joined_choices
);
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
;
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
);
360 if (status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
) {
361 BT_ASSERT(ctx
->error
);
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
;
376 switch (bt_value_get_type(value
)) {
377 case BT_VALUE_TYPE_MAP
:
378 status
= validate_map_value(&descr
->map
, value
, ctx
);
380 case BT_VALUE_TYPE_ARRAY
:
381 status
= validate_array_value(&descr
->array
, value
, ctx
);
383 case BT_VALUE_TYPE_STRING
:
384 status
= validate_string_value(&descr
->string
, value
, ctx
);
387 status
= BT_PARAM_VALIDATION_STATUS_OK
;
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
,
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
;
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
;
412 map_value_descr
.entries
= entries
;
414 status
= validate_map_value(&map_value_descr
, params
, &ctx
);