2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) EfficiOS Inc.
8 #include "param-parse/param-parse.h"
9 #include "plugins/common/param-validation/param-validation.h"
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
)
22 GString
*err
= g_string_new(NULL
);
23 const bt_value
*params
;
24 enum bt_param_validation_status status
;
25 gchar
*validate_error
= NULL
;
28 fprintf(stderr
, "Failed to allocated a GString.\n");
32 params
= bt_param_parse(params_str
, err
);
35 fprintf(stderr
, "Could not parse params: `%s`, %s\n",
36 params_str
, err
->str
);
40 status
= bt_param_validation_validate(params
, entries
, &validate_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
);
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
);
52 fail(BT_FMT
, test_name
);
53 diag("could not find `%s` in `%s`", expected_error
, validate_error
);
57 g_free(validate_error
);
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
);
64 bt_value_put_ref(params
);
65 g_string_free(err
, TRUE
);
71 void test_map_valid(void)
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
80 run_test("carotte=2,fenouil=\"miam\"", entries
, "valid map", NULL
);
84 void test_map_missing_key(void)
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
92 run_test("carotte=2", entries
, "missing key in map",
93 "Error validating parameters: missing mandatory entry `tomate`");
97 void test_map_unexpected_key(void)
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
104 run_test("tomate=2", entries
, "unexpected key in map", "unexpected key `tomate`");
108 void test_map_invalid_entry_value_type(void)
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
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");
120 void test_nested_error(void)
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
,
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
,
131 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
,
134 const struct bt_param_validation_value_descr carottes_elem
= {
135 .type
= BT_VALUE_TYPE_MAP
,
137 .entries
= carottes_elem_entries
,
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
= {
144 .max_length
= BT_PARAM_VALIDATION_INFINITE
,
145 .element_type
= &carottes_elem
,
147 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
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`");
155 void test_array_valid(void)
157 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
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
= {
163 .element_type
= &carotte_elem
,
165 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
168 run_test("carotte=[true, false, true]", entries
, "valid array", NULL
);
172 void test_array_empty_valid(void)
174 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
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
= {
180 .element_type
= &carotte_elem
,
182 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
185 run_test("carotte=[]", entries
, "valid empty array", NULL
);
189 void test_array_invalid_too_small(void)
191 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
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
= {
197 .element_type
= &carotte_elem
,
199 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
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");
207 void test_array_invalid_too_large(void)
209 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
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
= {
215 .element_type
= &carotte_elem
,
217 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
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");
225 void test_array_invalid_elem_type(void)
227 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
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
= {
233 .element_type
= &carotte_elem
,
235 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
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");
243 void test_string_valid_without_choices(void)
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
250 run_test("haricot=\"vert\"", entries
, "valid string without choices", NULL
);
254 void test_string_valid_with_choices(void)
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
,
261 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
264 run_test("haricot=\"jaune\"", entries
, "valid string with choices", NULL
);
268 void test_string_invalid_choice(void)
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
,
275 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
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]");
283 enum bt_param_validation_status
custom_validation_func_valid(
284 const bt_value
*value
,
285 struct bt_param_validation_context
*context
)
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
;
295 void test_custom_validation_func_valid(void)
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
,
301 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
304 run_test("navet=+1234", entries
, "custom validation function with valid value", NULL
);
308 enum bt_param_validation_status
custom_validation_func_invalid(
309 const bt_value
*value
,
310 struct bt_param_validation_context
*context
)
312 return bt_param_validation_error(context
, "wrooooong");
316 void test_custom_validation_func_invalid(void)
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
,
322 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
325 run_test("navet=+1234", entries
, "custom validation function with invalid value",
326 "Error validating parameter `navet`: wrooooong");
335 test_map_missing_key();
336 test_map_unexpected_key();
337 test_map_invalid_entry_value_type();
340 test_array_empty_valid();
342 test_array_invalid_too_small();
343 test_array_invalid_too_large();
344 test_array_invalid_elem_type();
346 test_string_valid_without_choices();
347 test_string_valid_with_choices();
349 test_string_invalid_choice();
351 test_custom_validation_func_valid();
352 test_custom_validation_func_invalid();
356 return exit_status();