2 * Copyright (c) EfficiOS Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; under version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "param-parse/param-parse.h"
20 #include "plugins/common/param-validation/param-validation.h"
27 enum bt_param_validation_status
run_test(
28 const char *params_str
,
29 const struct bt_param_validation_map_value_entry_descr
*entries
,
30 const char *test_name
,
31 const char *expected_error
)
33 GString
*err
= g_string_new(NULL
);
34 const bt_value
*params
;
35 enum bt_param_validation_status status
;
36 gchar
*validate_error
= NULL
;
39 fprintf(stderr
, "Failed to allocated a GString.\n");
43 params
= bt_param_parse(params_str
, err
);
46 fprintf(stderr
, "Could not parse params: `%s`, %s\n",
47 params_str
, err
->str
);
51 status
= bt_param_validation_validate(params
, entries
, &validate_error
);
56 /* We expect a failure. */
57 ok(status
== BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
,
58 "%s: validation fails", test_name
);
59 ok(validate_error
, "%s: error string is not NULL", test_name
);
61 fmt
= "%s: error string contains expected string";
62 if (validate_error
&& strstr(validate_error
, expected_error
)) {
66 diag("could not find `%s` in `%s`", expected_error
, validate_error
);
69 g_free(validate_error
);
71 /* We expect a success. */
72 ok(status
== BT_PARAM_VALIDATION_STATUS_OK
, "%s: validation succeeds", test_name
);
73 ok(!validate_error
, "%s: error string is NULL", test_name
);
76 bt_value_put_ref(params
);
77 g_string_free(err
, TRUE
);
83 void test_map_valid(void)
85 const struct bt_param_validation_map_value_entry_descr entries
[] = {
86 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_SIGNED_INTEGER
} },
87 { "fenouil", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_STRING
} },
88 { "panais", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL
, { .type
= BT_VALUE_TYPE_BOOL
} },
89 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
92 run_test("carotte=2,fenouil=\"miam\"", entries
, "valid map", NULL
);
96 void test_map_missing_key(void)
98 const struct bt_param_validation_map_value_entry_descr entries
[] = {
99 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_SIGNED_INTEGER
} },
100 { "tomate", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_SIGNED_INTEGER
} },
101 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
104 run_test("carotte=2", entries
, "missing key in map",
105 "Error validating parameters: missing mandatory entry `tomate`");
109 void test_map_unexpected_key(void)
111 const struct bt_param_validation_map_value_entry_descr entries
[] = {
112 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_SIGNED_INTEGER
} },
113 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
116 run_test("tomate=2", entries
, "unexpected key in map", "unexpected key `tomate`");
120 void test_map_invalid_entry_value_type(void)
122 const struct bt_param_validation_map_value_entry_descr entries
[] = {
123 { "carottes", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_SIGNED_INTEGER
} },
124 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
127 run_test("carottes=\"orange\"", entries
, "map entry with unexpected type",
128 "Error validating parameter `carottes`: unexpected type: expected-type=SIGNED_INTEGER, actual-type=STRING");
132 void test_nested_error(void)
134 const struct bt_param_validation_map_value_entry_descr poireau_entries
[] = {
135 { "navet", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_SIGNED_INTEGER
} },
136 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
,
139 const struct bt_param_validation_map_value_entry_descr carottes_elem_entries
[] = {
140 { "poireau", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_MAP
, .map
= {
141 .entries
= poireau_entries
,
143 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
,
146 const struct bt_param_validation_value_descr carottes_elem
= {
147 .type
= BT_VALUE_TYPE_MAP
,
149 .entries
= carottes_elem_entries
,
153 const struct bt_param_validation_map_value_entry_descr entries
[] = {
154 { "carottes", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
156 .max_length
= BT_PARAM_VALIDATION_INFINITE
,
157 .element_type
= &carottes_elem
,
159 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
162 run_test("carottes=[{poireau={navet=7}}, {poireau={}}]", entries
, "error nested in maps and arrays",
163 "Error validating parameter `carottes[1].poireau`: missing mandatory entry `navet`");
167 void test_array_valid(void)
169 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
171 const struct bt_param_validation_map_value_entry_descr entries
[] = {
172 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
175 .element_type
= &carotte_elem
,
177 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
180 run_test("carotte=[true, false, true]", entries
, "valid array", NULL
);
184 void test_array_empty_valid(void)
186 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
188 const struct bt_param_validation_map_value_entry_descr entries
[] = {
189 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
192 .element_type
= &carotte_elem
,
194 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
197 run_test("carotte=[]", entries
, "valid empty array", NULL
);
201 void test_array_invalid_too_small(void)
203 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
205 const struct bt_param_validation_map_value_entry_descr entries
[] = {
206 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
209 .element_type
= &carotte_elem
,
211 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
214 run_test("carotte=[]", entries
, "array too small",
215 "Error validating parameter `carotte`: array is smaller than the minimum length: array-length=0, min-length=1");
219 void test_array_invalid_too_large(void)
221 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
223 const struct bt_param_validation_map_value_entry_descr entries
[] = {
224 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
227 .element_type
= &carotte_elem
,
229 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
232 run_test("carotte=[true, false, false]", entries
, "array too large",
233 "Error validating parameter `carotte`: array is larger than the maximum length: array-length=3, max-length=2");
237 void test_array_invalid_elem_type(void)
239 const struct bt_param_validation_value_descr carotte_elem
= { .type
= BT_VALUE_TYPE_BOOL
, {} };
241 const struct bt_param_validation_map_value_entry_descr entries
[] = {
242 { "carotte", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_ARRAY
, .array
= {
245 .element_type
= &carotte_elem
,
247 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
250 run_test("carotte=[true, false, 2]", entries
, "array with invalid element type",
251 "Error validating parameter `carotte[2]`: unexpected type: expected-type=BOOL, actual-type=SIGNED_INTEGER");
255 void test_string_valid_without_choices(void)
257 const struct bt_param_validation_map_value_entry_descr entries
[] = {
258 { "haricot", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { .type
= BT_VALUE_TYPE_STRING
, { } } },
259 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
262 run_test("haricot=\"vert\"", entries
, "valid string without choices", NULL
);
266 void test_string_valid_with_choices(void)
268 const char *haricot_choices
[] = {"vert", "jaune", "rouge", NULL
};
269 const struct bt_param_validation_map_value_entry_descr entries
[] = {
270 { "haricot", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_STRING
, .string
= {
271 .choices
= haricot_choices
,
273 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
276 run_test("haricot=\"jaune\"", entries
, "valid string with choices", NULL
);
280 void test_string_invalid_choice(void)
282 const char *haricot_choices
[] = {"vert", "jaune", "rouge", NULL
};
283 const struct bt_param_validation_map_value_entry_descr entries
[] = {
284 { "haricot", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, { BT_VALUE_TYPE_STRING
, .string
= {
285 .choices
= haricot_choices
,
287 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
290 run_test("haricot=\"violet\"", entries
, "string with invalid choice",
291 "Error validating parameter `haricot`: string is not amongst the available choices: string=violet, choices=[vert, jaune, rouge]");
295 enum bt_param_validation_status
custom_validation_func_valid(
296 const bt_value
*value
,
297 struct bt_param_validation_context
*context
)
299 ok(bt_value_get_type(value
) == BT_VALUE_TYPE_UNSIGNED_INTEGER
,
300 "type of value passed to custom function is as expected");
301 ok(bt_value_integer_unsigned_get(value
) == 1234,
302 "value passed to custom function is as expected");
303 return BT_PARAM_VALIDATION_STATUS_OK
;
307 void test_custom_validation_func_valid(void)
309 const struct bt_param_validation_map_value_entry_descr entries
[] = {
310 { "navet", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, {
311 .validation_func
= custom_validation_func_valid
,
313 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
316 run_test("navet=+1234", entries
, "custom validation function with valid value", NULL
);
320 enum bt_param_validation_status
custom_validation_func_invalid(
321 const bt_value
*value
,
322 struct bt_param_validation_context
*context
)
324 return bt_param_validation_error(context
, "wrooooong");
328 void test_custom_validation_func_invalid(void)
330 const struct bt_param_validation_map_value_entry_descr entries
[] = {
331 { "navet", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY
, {
332 .validation_func
= custom_validation_func_invalid
,
334 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
337 run_test("navet=+1234", entries
, "custom validation function with invalid value",
338 "Error validating parameter `navet`: wrooooong");
347 test_map_missing_key();
348 test_map_unexpected_key();
349 test_map_invalid_entry_value_type();
352 test_array_empty_valid();
354 test_array_invalid_too_small();
355 test_array_invalid_too_large();
356 test_array_invalid_elem_type();
358 test_string_valid_without_choices();
359 test_string_valid_with_choices();
361 test_string_invalid_choice();
363 test_custom_validation_func_valid();
364 test_custom_validation_func_invalid();
368 return exit_status();