Commit | Line | Data |
---|---|---|
d0d4e0ed SM |
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> | |
c4f23e30 | 28 | #include <stdbool.h> |
d0d4e0ed SM |
29 | |
30 | #include "common/common.h" | |
31 | ||
32 | struct bt_param_validation_context { | |
33 | gchar *error; | |
34 | GArray *scope_stack; | |
35 | }; | |
36 | ||
37 | struct validate_ctx_stack_element { | |
38 | enum { | |
39 | VALIDATE_CTX_STACK_ELEMENT_MAP, | |
40 | VALIDATE_CTX_STACK_ELEMENT_ARRAY, | |
41 | } type; | |
42 | ||
43 | union { | |
44 | const char *map_key_name; | |
45 | uint64_t array_index; | |
46 | }; | |
47 | }; | |
48 | ||
49 | static | |
50 | void validate_ctx_push_map_scope( | |
51 | struct bt_param_validation_context *ctx, | |
52 | const char *key) | |
53 | { | |
54 | struct validate_ctx_stack_element stack_element = { | |
55 | .type = VALIDATE_CTX_STACK_ELEMENT_MAP, | |
56 | .map_key_name = key, | |
57 | }; | |
58 | ||
59 | g_array_append_val(ctx->scope_stack, stack_element); | |
60 | } | |
61 | ||
62 | static | |
63 | void validate_ctx_push_array_scope( | |
64 | struct bt_param_validation_context *ctx, uint64_t index) | |
65 | { | |
66 | struct validate_ctx_stack_element stack_element = { | |
67 | .type = VALIDATE_CTX_STACK_ELEMENT_ARRAY, | |
68 | .array_index = index, | |
69 | }; | |
70 | ||
71 | g_array_append_val(ctx->scope_stack, stack_element); | |
72 | } | |
73 | ||
74 | static | |
75 | void validate_ctx_pop_scope(struct bt_param_validation_context *ctx) | |
76 | { | |
77 | BT_ASSERT(ctx->scope_stack->len > 0); | |
78 | ||
79 | g_array_remove_index_fast(ctx->scope_stack, ctx->scope_stack->len - 1); | |
80 | } | |
81 | ||
82 | static | |
83 | void append_scope_to_string(GString *str, | |
84 | const struct validate_ctx_stack_element *elem, | |
85 | bool first) | |
86 | { | |
87 | switch (elem->type) { | |
88 | case VALIDATE_CTX_STACK_ELEMENT_MAP: | |
89 | if (!first) { | |
90 | g_string_append_c(str, '.'); | |
91 | } | |
92 | ||
93 | g_string_append(str, elem->map_key_name); | |
94 | break; | |
95 | case VALIDATE_CTX_STACK_ELEMENT_ARRAY: | |
96 | g_string_append_printf(str, "[%" PRIu64 "]", elem->array_index); | |
97 | break; | |
98 | default: | |
99 | abort(); | |
100 | } | |
101 | } | |
102 | ||
103 | enum bt_param_validation_status bt_param_validation_error( | |
104 | struct bt_param_validation_context *ctx, | |
105 | const char *format, ...) { | |
106 | va_list ap; | |
107 | enum bt_param_validation_status status; | |
108 | ||
109 | GString *str = g_string_new(NULL); | |
110 | if (!str) { | |
111 | status = BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR; | |
112 | goto end; | |
113 | } | |
114 | ||
115 | if (ctx->scope_stack->len > 0) { | |
116 | guint i; | |
117 | ||
118 | g_string_assign(str, "Error validating parameter `"); | |
119 | ||
120 | append_scope_to_string(str, &g_array_index(ctx->scope_stack, | |
121 | struct validate_ctx_stack_element, 0), true); | |
122 | ||
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); | |
127 | } | |
128 | ||
129 | g_string_append(str, "`: "); | |
130 | } else { | |
131 | g_string_assign(str, "Error validating parameters: "); | |
132 | } | |
133 | ||
134 | va_start(ap, format); | |
135 | g_string_append_vprintf(str, format, ap); | |
136 | va_end(ap); | |
137 | ||
138 | ctx->error = g_string_free(str, FALSE); | |
139 | status = BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR; | |
140 | ||
141 | end: | |
142 | return status; | |
143 | } | |
144 | ||
145 | struct validate_map_value_data | |
146 | { | |
147 | GPtrArray *available_keys; | |
148 | enum bt_param_validation_status status; | |
149 | struct bt_param_validation_context *ctx; | |
150 | }; | |
151 | ||
152 | static | |
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); | |
157 | ||
158 | static | |
159 | bt_bool validate_map_value_entry(const char *key, | |
160 | const bt_value *value, void *v_data) | |
161 | { | |
162 | struct validate_map_value_data *data = v_data; | |
5a039e2a | 163 | const struct bt_param_validation_map_value_entry_descr *entry = NULL; |
d0d4e0ed SM |
164 | guint i; |
165 | ||
166 | /* Check if this key is in the available keys. */ | |
167 | for (i = 0; i < data->available_keys->len; i++) { | |
5a039e2a SM |
168 | const struct bt_param_validation_map_value_entry_descr *candidate = |
169 | g_ptr_array_index(data->available_keys, i); | |
d0d4e0ed SM |
170 | |
171 | if (g_str_equal(key, candidate->key)) { | |
5a039e2a | 172 | entry = candidate; |
d0d4e0ed SM |
173 | break; |
174 | } | |
175 | } | |
176 | ||
5a039e2a | 177 | if (entry) { |
d0d4e0ed SM |
178 | /* Key was found in available keys. */ |
179 | g_ptr_array_remove_index_fast(data->available_keys, i); | |
180 | ||
181 | /* Push key name as the scope. */ | |
182 | validate_ctx_push_map_scope(data->ctx, key); | |
183 | ||
184 | /* Validate the value of the entry. */ | |
5a039e2a | 185 | data->status = validate_value(value, &entry->value_descr, |
d0d4e0ed SM |
186 | data->ctx); |
187 | ||
188 | validate_ctx_pop_scope(data->ctx); | |
189 | } else { | |
190 | data->status = bt_param_validation_error(data->ctx, | |
191 | "unexpected key `%s`.", key); | |
192 | } | |
193 | ||
194 | /* Continue iterating if everything is good so far. */ | |
195 | return data->status == BT_PARAM_VALIDATION_STATUS_OK; | |
196 | } | |
197 | ||
198 | static | |
199 | enum bt_param_validation_status validate_map_value( | |
200 | const struct bt_param_validation_map_value_descr *descr, | |
201 | const bt_value *map, | |
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; | |
208 | guint i; | |
209 | ||
210 | BT_ASSERT(bt_value_get_type(map) == BT_VALUE_TYPE_MAP); | |
211 | ||
212 | available_keys = g_ptr_array_new(); | |
213 | if (!available_keys) { | |
214 | status = BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR; | |
215 | goto end; | |
216 | } | |
217 | ||
218 | for (descr_iter = descr->entries; descr_iter->key; descr_iter++) { | |
219 | g_ptr_array_add(available_keys, (gpointer) descr_iter); | |
220 | } | |
221 | ||
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; | |
225 | data.ctx = ctx; | |
226 | ||
227 | foreach_entry_status = bt_value_map_foreach_entry_const(map, | |
228 | validate_map_value_entry, &data); | |
605adc18 SM |
229 | if (foreach_entry_status == BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_INTERRUPTED) { |
230 | BT_ASSERT(data.status != BT_PARAM_VALIDATION_STATUS_OK); | |
d0d4e0ed SM |
231 | status = data.status; |
232 | goto end; | |
233 | } | |
234 | ||
605adc18 SM |
235 | BT_ASSERT(data.status == BT_PARAM_VALIDATION_STATUS_OK); |
236 | ||
d0d4e0ed SM |
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); | |
240 | ||
241 | if (!entry->is_optional) { | |
242 | status = bt_param_validation_error(ctx, | |
243 | "missing mandatory entry `%s`", | |
244 | entry->key); | |
245 | goto end; | |
246 | } | |
247 | } | |
248 | ||
249 | status = BT_PARAM_VALIDATION_STATUS_OK; | |
250 | ||
251 | end: | |
252 | g_ptr_array_free(available_keys, TRUE); | |
253 | return status; | |
254 | } | |
255 | ||
256 | static | |
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; | |
262 | uint64_t i; | |
263 | ||
264 | BT_ASSERT(bt_value_get_type(array) == BT_VALUE_TYPE_ARRAY); | |
265 | ||
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), | |
271 | descr->min_length); | |
272 | goto end; | |
273 | } | |
274 | ||
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), | |
280 | descr->max_length); | |
281 | goto end; | |
282 | } | |
283 | ||
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); | |
287 | ||
288 | validate_ctx_push_array_scope(ctx, i); | |
289 | ||
290 | status = validate_value(element, descr->element_type, ctx); | |
291 | ||
292 | validate_ctx_pop_scope(ctx); | |
293 | ||
294 | if (status != BT_PARAM_VALIDATION_STATUS_OK) { | |
295 | goto end; | |
296 | } | |
297 | } | |
298 | ||
299 | status = BT_PARAM_VALIDATION_STATUS_OK; | |
300 | ||
301 | end: | |
302 | return status; | |
303 | } | |
304 | ||
305 | static | |
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; | |
313 | ||
314 | BT_ASSERT(bt_value_get_type(string) == BT_VALUE_TYPE_STRING); | |
315 | ||
316 | if (descr->choices) { | |
317 | const char **choice; | |
318 | ||
319 | for (choice = descr->choices; *choice; choice++) { | |
320 | if (strcmp(s, *choice) == 0) { | |
321 | break; | |
322 | } | |
323 | } | |
324 | ||
325 | if (!*choice) { | |
326 | /* | |
327 | * g_strjoinv takes a gchar **, but it doesn't modify | |
328 | * the array of the strings (yet). | |
329 | */ | |
330 | joined_choices = g_strjoinv(", ", (gchar **) descr->choices); | |
331 | if (!joined_choices) { | |
332 | status = BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR; | |
333 | goto end; | |
334 | } | |
335 | ||
336 | status = bt_param_validation_error(ctx, | |
337 | "string is not amongst the available choices: " | |
338 | "string=%s, choices=[%s]", s, joined_choices); | |
339 | goto end; | |
340 | } | |
341 | } | |
342 | ||
343 | status = BT_PARAM_VALIDATION_STATUS_OK; | |
344 | end: | |
345 | g_free(joined_choices); | |
346 | ||
347 | return status; | |
348 | } | |
349 | ||
350 | static | |
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; | |
356 | ||
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); | |
360 | ||
361 | if (status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { | |
362 | BT_ASSERT(ctx->error); | |
363 | } | |
364 | ||
365 | goto end; | |
366 | } | |
367 | ||
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; | |
374 | goto end; | |
375 | } | |
376 | ||
377 | switch (bt_value_get_type(value)) { | |
378 | case BT_VALUE_TYPE_MAP: | |
379 | status = validate_map_value(&descr->map, value, ctx); | |
380 | break; | |
381 | case BT_VALUE_TYPE_ARRAY: | |
382 | status = validate_array_value(&descr->array, value, ctx); | |
383 | break; | |
384 | case BT_VALUE_TYPE_STRING: | |
385 | status = validate_string_value(&descr->string, value, ctx); | |
386 | break; | |
387 | default: | |
388 | status = BT_PARAM_VALIDATION_STATUS_OK; | |
389 | break; | |
390 | } | |
391 | ||
392 | end: | |
393 | return status; | |
394 | } | |
395 | ||
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, | |
399 | gchar **error) { | |
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; | |
403 | ||
8839434e SM |
404 | memset(&ctx, '\0', sizeof(ctx)); |
405 | ||
d0d4e0ed SM |
406 | ctx.scope_stack = g_array_new(FALSE, FALSE, |
407 | sizeof(struct validate_ctx_stack_element)); | |
d0d4e0ed SM |
408 | if (!ctx.scope_stack) { |
409 | status = BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR; | |
410 | goto end; | |
411 | } | |
412 | ||
413 | map_value_descr.entries = entries; | |
414 | ||
415 | status = validate_map_value(&map_value_descr, params, &ctx); | |
416 | ||
417 | end: | |
418 | *error = ctx.error; | |
419 | ctx.error = NULL; | |
420 | ||
8839434e SM |
421 | if (ctx.scope_stack) { |
422 | g_array_free(ctx.scope_stack, TRUE); | |
423 | } | |
424 | ||
d0d4e0ed SM |
425 | return status; |
426 | } |