fa1401046e23b2d9acf32734edcdd0010e0d652c
[babeltrace.git] / tests / param-validation / test-param-validation.c
1 /*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) EfficiOS Inc.
5 */
6
7 #include "tap/tap.h"
8 #include "param-parse/param-parse.h"
9 #include "plugins/common/param-validation/param-validation.h"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 static
16 enum bt_param_validation_status run_test(
17 const char *params_str,
18 const struct bt_param_validation_map_value_entry_descr *entries,
19 const char *test_name,
20 const char *expected_error)
21 {
22 GString *err = g_string_new(NULL);
23 const bt_value *params;
24 enum bt_param_validation_status status;
25 gchar *validate_error = NULL;
26
27 if (!err) {
28 fprintf(stderr, "Failed to allocated a GString.\n");
29 abort();
30 }
31
32 params = bt_param_parse(params_str, err);
33
34 if (!params) {
35 fprintf(stderr, "Could not parse params: `%s`, %s\n",
36 params_str, err->str);
37 abort();
38 }
39
40 status = bt_param_validation_validate(params, entries, &validate_error);
41
42 if (expected_error) {
43 /* We expect a failure. */
44 ok(status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR,
45 "%s: validation fails", test_name);
46 ok(validate_error, "%s: error string is not NULL", test_name);
47
48 #define BT_FMT "%s: error string contains expected string"
49 if (validate_error && strstr(validate_error, expected_error)) {
50 pass(BT_FMT, test_name);
51 } else {
52 fail(BT_FMT, test_name);
53 diag("could not find `%s` in `%s`", expected_error, validate_error);
54 }
55 #undef BT_FMT
56
57 g_free(validate_error);
58 } else {
59 /* We expect a success. */
60 ok(status == BT_PARAM_VALIDATION_STATUS_OK, "%s: validation succeeds", test_name);
61 ok(!validate_error, "%s: error string is NULL", test_name);
62 }
63
64 bt_value_put_ref(params);
65 g_string_free(err, TRUE);
66
67 return status;
68 }
69
70 static
71 void test_map_valid(void)
72 {
73 const struct bt_param_validation_map_value_entry_descr entries[] = {
74 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { .type = BT_VALUE_TYPE_SIGNED_INTEGER } },
75 { "fenouil", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_STRING } },
76 { "panais", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
77 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
78 };
79
80 run_test("carotte=2,fenouil=\"miam\"", entries, "valid map", NULL);
81 }
82
83 static
84 void test_map_missing_key(void)
85 {
86 const struct bt_param_validation_map_value_entry_descr entries[] = {
87 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { .type = BT_VALUE_TYPE_SIGNED_INTEGER } },
88 { "tomate", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { .type = BT_VALUE_TYPE_SIGNED_INTEGER } },
89 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
90 };
91
92 run_test("carotte=2", entries, "missing key in map",
93 "Error validating parameters: missing mandatory entry `tomate`");
94 }
95
96 static
97 void test_map_unexpected_key(void)
98 {
99 const struct bt_param_validation_map_value_entry_descr entries[] = {
100 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { .type = BT_VALUE_TYPE_SIGNED_INTEGER } },
101 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
102 };
103
104 run_test("tomate=2", entries, "unexpected key in map", "unexpected key `tomate`");
105 }
106
107 static
108 void test_map_invalid_entry_value_type(void)
109 {
110 const struct bt_param_validation_map_value_entry_descr entries[] = {
111 { "carottes", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { .type = BT_VALUE_TYPE_SIGNED_INTEGER } },
112 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
113 };
114
115 run_test("carottes=\"orange\"", entries, "map entry with unexpected type",
116 "Error validating parameter `carottes`: unexpected type: expected-type=SIGNED_INTEGER, actual-type=STRING");
117 }
118
119 static
120 void test_nested_error(void)
121 {
122 const struct bt_param_validation_map_value_entry_descr poireau_entries[] = {
123 { "navet", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { .type = BT_VALUE_TYPE_SIGNED_INTEGER } },
124 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END,
125 };
126
127 const struct bt_param_validation_map_value_entry_descr carottes_elem_entries[] = {
128 { "poireau", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { BT_VALUE_TYPE_MAP, .map = {
129 .entries = poireau_entries,
130 } } },
131 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END,
132 };
133
134 const struct bt_param_validation_value_descr carottes_elem = {
135 .type = BT_VALUE_TYPE_MAP,
136 .map = {
137 .entries = carottes_elem_entries,
138 }
139 };
140
141 const struct bt_param_validation_map_value_entry_descr entries[] = {
142 { "carottes", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { BT_VALUE_TYPE_ARRAY, .array = {
143 .min_length = 0,
144 .max_length = BT_PARAM_VALIDATION_INFINITE,
145 .element_type = &carottes_elem,
146 } } },
147 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
148 };
149
150 run_test("carottes=[{poireau={navet=7}}, {poireau={}}]", entries, "error nested in maps and arrays",
151 "Error validating parameter `carottes[1].poireau`: missing mandatory entry `navet`");
152 }
153
154 static
155 void test_array_valid(void)
156 {
157 const struct bt_param_validation_value_descr carotte_elem = { .type = BT_VALUE_TYPE_BOOL, {} };
158
159 const struct bt_param_validation_map_value_entry_descr entries[] = {
160 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { BT_VALUE_TYPE_ARRAY, .array = {
161 .min_length = 2,
162 .max_length = 22,
163 .element_type = &carotte_elem,
164 } } },
165 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
166 };
167
168 run_test("carotte=[true, false, true]", entries, "valid array", NULL);
169 }
170
171 static
172 void test_array_empty_valid(void)
173 {
174 const struct bt_param_validation_value_descr carotte_elem = { .type = BT_VALUE_TYPE_BOOL, {} };
175
176 const struct bt_param_validation_map_value_entry_descr entries[] = {
177 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { BT_VALUE_TYPE_ARRAY, .array = {
178 .min_length = 0,
179 .max_length = 2,
180 .element_type = &carotte_elem,
181 } } },
182 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
183 };
184
185 run_test("carotte=[]", entries, "valid empty array", NULL);
186 }
187
188 static
189 void test_array_invalid_too_small(void)
190 {
191 const struct bt_param_validation_value_descr carotte_elem = { .type = BT_VALUE_TYPE_BOOL, {} };
192
193 const struct bt_param_validation_map_value_entry_descr entries[] = {
194 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { BT_VALUE_TYPE_ARRAY, .array = {
195 .min_length = 1,
196 .max_length = 100,
197 .element_type = &carotte_elem,
198 } } },
199 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
200 };
201
202 run_test("carotte=[]", entries, "array too small",
203 "Error validating parameter `carotte`: array is smaller than the minimum length: array-length=0, min-length=1");
204 }
205
206 static
207 void test_array_invalid_too_large(void)
208 {
209 const struct bt_param_validation_value_descr carotte_elem = { .type = BT_VALUE_TYPE_BOOL, {} };
210
211 const struct bt_param_validation_map_value_entry_descr entries[] = {
212 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { BT_VALUE_TYPE_ARRAY, .array = {
213 .min_length = 2,
214 .max_length = 2,
215 .element_type = &carotte_elem,
216 } } },
217 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
218 };
219
220 run_test("carotte=[true, false, false]", entries, "array too large",
221 "Error validating parameter `carotte`: array is larger than the maximum length: array-length=3, max-length=2");
222 }
223
224 static
225 void test_array_invalid_elem_type(void)
226 {
227 const struct bt_param_validation_value_descr carotte_elem = { .type = BT_VALUE_TYPE_BOOL, {} };
228
229 const struct bt_param_validation_map_value_entry_descr entries[] = {
230 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { BT_VALUE_TYPE_ARRAY, .array = {
231 .min_length = 3,
232 .max_length = 3,
233 .element_type = &carotte_elem,
234 } } },
235 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
236 };
237
238 run_test("carotte=[true, false, 2]", entries, "array with invalid element type",
239 "Error validating parameter `carotte[2]`: unexpected type: expected-type=BOOL, actual-type=SIGNED_INTEGER");
240 }
241
242 static
243 void test_string_valid_without_choices(void)
244 {
245 const struct bt_param_validation_map_value_entry_descr entries[] = {
246 { "haricot", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { .type = BT_VALUE_TYPE_STRING, { } } },
247 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
248 };
249
250 run_test("haricot=\"vert\"", entries, "valid string without choices", NULL);
251 }
252
253 static
254 void test_string_valid_with_choices(void)
255 {
256 const char *haricot_choices[] = {"vert", "jaune", "rouge", NULL};
257 const struct bt_param_validation_map_value_entry_descr entries[] = {
258 { "haricot", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { BT_VALUE_TYPE_STRING, .string = {
259 .choices = haricot_choices,
260 } } },
261 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
262 };
263
264 run_test("haricot=\"jaune\"", entries, "valid string with choices", NULL);
265 }
266
267 static
268 void test_string_invalid_choice(void)
269 {
270 const char *haricot_choices[] = {"vert", "jaune", "rouge", NULL};
271 const struct bt_param_validation_map_value_entry_descr entries[] = {
272 { "haricot", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, { BT_VALUE_TYPE_STRING, .string = {
273 .choices = haricot_choices,
274 } } },
275 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
276 };
277
278 run_test("haricot=\"violet\"", entries, "string with invalid choice",
279 "Error validating parameter `haricot`: string is not amongst the available choices: string=violet, choices=[vert, jaune, rouge]");
280 }
281
282 static
283 enum bt_param_validation_status custom_validation_func_valid(
284 const bt_value *value,
285 struct bt_param_validation_context *context)
286 {
287 ok(bt_value_get_type(value) == BT_VALUE_TYPE_UNSIGNED_INTEGER,
288 "type of value passed to custom function is as expected");
289 ok(bt_value_integer_unsigned_get(value) == 1234,
290 "value passed to custom function is as expected");
291 return BT_PARAM_VALIDATION_STATUS_OK;
292 }
293
294 static
295 void test_custom_validation_func_valid(void)
296 {
297 const struct bt_param_validation_map_value_entry_descr entries[] = {
298 { "navet", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, {
299 .validation_func = custom_validation_func_valid,
300 } },
301 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
302 };
303
304 run_test("navet=+1234", entries, "custom validation function with valid value", NULL);
305 }
306
307 static
308 enum bt_param_validation_status custom_validation_func_invalid(
309 const bt_value *value,
310 struct bt_param_validation_context *context)
311 {
312 return bt_param_validation_error(context, "wrooooong");
313 }
314
315 static
316 void test_custom_validation_func_invalid(void)
317 {
318 const struct bt_param_validation_map_value_entry_descr entries[] = {
319 { "navet", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, {
320 .validation_func = custom_validation_func_invalid,
321 } },
322 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
323 };
324
325 run_test("navet=+1234", entries, "custom validation function with invalid value",
326 "Error validating parameter `navet`: wrooooong");
327 }
328
329 int main(void)
330 {
331 plan_tests(41);
332
333 test_map_valid();
334
335 test_map_missing_key();
336 test_map_unexpected_key();
337 test_map_invalid_entry_value_type();
338
339 test_array_valid();
340 test_array_empty_valid();
341
342 test_array_invalid_too_small();
343 test_array_invalid_too_large();
344 test_array_invalid_elem_type();
345
346 test_string_valid_without_choices();
347 test_string_valid_with_choices();
348
349 test_string_invalid_choice();
350
351 test_custom_validation_func_valid();
352 test_custom_validation_func_invalid();
353
354 test_nested_error();
355
356 return exit_status();
357 }
This page took 0.036035 seconds and 3 git commands to generate.