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