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
);
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
);
50 fmt
= "%s: error string contains expected string";
51 if (validate_error
&& strstr(validate_error
, expected_error
)) {
55 diag("could not find `%s` in `%s`", expected_error
, validate_error
);
58 g_free(validate_error
);
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
);
65 bt_value_put_ref(params
);
66 g_string_free(err
, TRUE
);
72 void test_map_valid(void)
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
81 run_test("carotte=2,fenouil=\"miam\"", entries
, "valid map", NULL
);
85 void test_map_missing_key(void)
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
93 run_test("carotte=2", entries
, "missing key in map",
94 "Error validating parameters: missing mandatory entry `tomate`");
98 void test_map_unexpected_key(void)
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
105 run_test("tomate=2", entries
, "unexpected key in map", "unexpected key `tomate`");
109 void test_map_invalid_entry_value_type(void)
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
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");
121 void test_nested_error(void)
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
,
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
,
132 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
,
135 const struct bt_param_validation_value_descr carottes_elem
= {
136 .type
= BT_VALUE_TYPE_MAP
,
138 .entries
= carottes_elem_entries
,
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
= {
145 .max_length
= BT_PARAM_VALIDATION_INFINITE
,
146 .element_type
= &carottes_elem
,
148 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
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`");
156 void test_array_valid(void)
158 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
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
= {
164 .element_type
= &carotte_elem
,
166 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
169 run_test("carotte=[true, false, true]", entries
, "valid array", NULL
);
173 void test_array_empty_valid(void)
175 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
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
= {
181 .element_type
= &carotte_elem
,
183 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
186 run_test("carotte=[]", entries
, "valid empty array", NULL
);
190 void test_array_invalid_too_small(void)
192 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
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
= {
198 .element_type
= &carotte_elem
,
200 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
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");
208 void test_array_invalid_too_large(void)
210 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
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
= {
216 .element_type
= &carotte_elem
,
218 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
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");
226 void test_array_invalid_elem_type(void)
228 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
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
= {
234 .element_type
= &carotte_elem
,
236 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
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");
244 void test_string_valid_without_choices(void)
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
251 run_test("haricot=\"vert\"", entries
, "valid string without choices", NULL
);
255 void test_string_valid_with_choices(void)
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
,
262 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
265 run_test("haricot=\"jaune\"", entries
, "valid string with choices", NULL
);
269 void test_string_invalid_choice(void)
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
,
276 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
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]");
284 enum bt_param_validation_status
custom_validation_func_valid(
285 const bt_value
*value
,
286 struct bt_param_validation_context
*context
)
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
;
296 void test_custom_validation_func_valid(void)
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
,
302 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
305 run_test("navet=+1234", entries
, "custom validation function with valid value", NULL
);
309 enum bt_param_validation_status
custom_validation_func_invalid(
310 const bt_value
*value
,
311 struct bt_param_validation_context
*context
)
313 return bt_param_validation_error(context
, "wrooooong");
317 void test_custom_validation_func_invalid(void)
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
,
323 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
326 run_test("navet=+1234", entries
, "custom validation function with invalid value",
327 "Error validating parameter `navet`: wrooooong");
336 test_map_missing_key();
337 test_map_unexpected_key();
338 test_map_invalid_entry_value_type();
341 test_array_empty_valid();
343 test_array_invalid_too_small();
344 test_array_invalid_too_large();
345 test_array_invalid_elem_type();
347 test_string_valid_without_choices();
348 test_string_valid_with_choices();
350 test_string_invalid_choice();
352 test_custom_validation_func_valid();
353 test_custom_validation_func_invalid();
357 return exit_status();