| 1 | /* |
| 2 | * Copyright (c) EfficiOS Inc. |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 12 | * |
| 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. |
| 16 | */ |
| 17 | |
| 18 | #include "tap/tap.h" |
| 19 | #include "param-parse/param-parse.h" |
| 20 | #include "plugins/common/param-validation/param-validation.h" |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | static |
| 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) |
| 32 | { |
| 33 | GString *err = g_string_new(NULL); |
| 34 | const bt_value *params; |
| 35 | enum bt_param_validation_status status; |
| 36 | gchar *validate_error = NULL; |
| 37 | |
| 38 | if (!err) { |
| 39 | fprintf(stderr, "Failed to allocated a GString.\n"); |
| 40 | abort(); |
| 41 | } |
| 42 | |
| 43 | params = bt_param_parse(params_str, err); |
| 44 | |
| 45 | if (!params) { |
| 46 | fprintf(stderr, "Could not parse params: `%s`, %s\n", |
| 47 | params_str, err->str); |
| 48 | abort(); |
| 49 | } |
| 50 | |
| 51 | status = bt_param_validation_validate(params, entries, &validate_error); |
| 52 | |
| 53 | if (expected_error) { |
| 54 | const char *fmt; |
| 55 | |
| 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); |
| 60 | |
| 61 | fmt = "%s: error string contains expected string"; |
| 62 | if (validate_error && strstr(validate_error, expected_error)) { |
| 63 | pass(fmt, test_name); |
| 64 | } else { |
| 65 | fail(fmt, test_name); |
| 66 | diag("could not find `%s` in `%s`", expected_error, validate_error); |
| 67 | } |
| 68 | |
| 69 | g_free(validate_error); |
| 70 | } else { |
| 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); |
| 74 | } |
| 75 | |
| 76 | bt_value_put_ref(params); |
| 77 | g_string_free(err, TRUE); |
| 78 | |
| 79 | return status; |
| 80 | } |
| 81 | |
| 82 | static |
| 83 | void test_map_valid(void) |
| 84 | { |
| 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 |
| 90 | }; |
| 91 | |
| 92 | run_test("carotte=2,fenouil=\"miam\"", entries, "valid map", NULL); |
| 93 | } |
| 94 | |
| 95 | static |
| 96 | void test_map_missing_key(void) |
| 97 | { |
| 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 |
| 102 | }; |
| 103 | |
| 104 | run_test("carotte=2", entries, "missing key in map", |
| 105 | "Error validating parameters: missing mandatory entry `tomate`"); |
| 106 | } |
| 107 | |
| 108 | static |
| 109 | void test_map_unexpected_key(void) |
| 110 | { |
| 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 |
| 114 | }; |
| 115 | |
| 116 | run_test("tomate=2", entries, "unexpected key in map", "unexpected key `tomate`"); |
| 117 | } |
| 118 | |
| 119 | static |
| 120 | void test_map_invalid_entry_value_type(void) |
| 121 | { |
| 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 |
| 125 | }; |
| 126 | |
| 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"); |
| 129 | } |
| 130 | |
| 131 | static |
| 132 | void test_nested_error(void) |
| 133 | { |
| 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, |
| 137 | }; |
| 138 | |
| 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, |
| 142 | } } }, |
| 143 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END, |
| 144 | }; |
| 145 | |
| 146 | const struct bt_param_validation_value_descr carottes_elem = { |
| 147 | .type = BT_VALUE_TYPE_MAP, |
| 148 | .map = { |
| 149 | .entries = carottes_elem_entries, |
| 150 | } |
| 151 | }; |
| 152 | |
| 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 = { |
| 155 | .min_length = 0, |
| 156 | .max_length = BT_PARAM_VALIDATION_INFINITE, |
| 157 | .element_type = &carottes_elem, |
| 158 | } } }, |
| 159 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
| 160 | }; |
| 161 | |
| 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`"); |
| 164 | } |
| 165 | |
| 166 | static |
| 167 | void test_array_valid(void) |
| 168 | { |
| 169 | const struct bt_param_validation_value_descr carotte_elem = { .type = BT_VALUE_TYPE_BOOL, {} }; |
| 170 | |
| 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 = { |
| 173 | .min_length = 2, |
| 174 | .max_length = 22, |
| 175 | .element_type = &carotte_elem, |
| 176 | } } }, |
| 177 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
| 178 | }; |
| 179 | |
| 180 | run_test("carotte=[true, false, true]", entries, "valid array", NULL); |
| 181 | } |
| 182 | |
| 183 | static |
| 184 | void test_array_empty_valid(void) |
| 185 | { |
| 186 | const struct bt_param_validation_value_descr carotte_elem = { .type = BT_VALUE_TYPE_BOOL, {} }; |
| 187 | |
| 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 = { |
| 190 | .min_length = 0, |
| 191 | .max_length = 2, |
| 192 | .element_type = &carotte_elem, |
| 193 | } } }, |
| 194 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
| 195 | }; |
| 196 | |
| 197 | run_test("carotte=[]", entries, "valid empty array", NULL); |
| 198 | } |
| 199 | |
| 200 | static |
| 201 | void test_array_invalid_too_small(void) |
| 202 | { |
| 203 | const struct bt_param_validation_value_descr carotte_elem = { .type = BT_VALUE_TYPE_BOOL, {} }; |
| 204 | |
| 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 = { |
| 207 | .min_length = 1, |
| 208 | .max_length = 100, |
| 209 | .element_type = &carotte_elem, |
| 210 | } } }, |
| 211 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
| 212 | }; |
| 213 | |
| 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"); |
| 216 | } |
| 217 | |
| 218 | static |
| 219 | void test_array_invalid_too_large(void) |
| 220 | { |
| 221 | const struct bt_param_validation_value_descr carotte_elem = { .type = BT_VALUE_TYPE_BOOL, {} }; |
| 222 | |
| 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 = { |
| 225 | .min_length = 2, |
| 226 | .max_length = 2, |
| 227 | .element_type = &carotte_elem, |
| 228 | } } }, |
| 229 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
| 230 | }; |
| 231 | |
| 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"); |
| 234 | } |
| 235 | |
| 236 | static |
| 237 | void test_array_invalid_elem_type(void) |
| 238 | { |
| 239 | const struct bt_param_validation_value_descr carotte_elem = { .type = BT_VALUE_TYPE_BOOL, {} }; |
| 240 | |
| 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 = { |
| 243 | .min_length = 3, |
| 244 | .max_length = 3, |
| 245 | .element_type = &carotte_elem, |
| 246 | } } }, |
| 247 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
| 248 | }; |
| 249 | |
| 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"); |
| 252 | } |
| 253 | |
| 254 | static |
| 255 | void test_string_valid_without_choices(void) |
| 256 | { |
| 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 |
| 260 | }; |
| 261 | |
| 262 | run_test("haricot=\"vert\"", entries, "valid string without choices", NULL); |
| 263 | } |
| 264 | |
| 265 | static |
| 266 | void test_string_valid_with_choices(void) |
| 267 | { |
| 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, |
| 272 | } } }, |
| 273 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
| 274 | }; |
| 275 | |
| 276 | run_test("haricot=\"jaune\"", entries, "valid string with choices", NULL); |
| 277 | } |
| 278 | |
| 279 | static |
| 280 | void test_string_invalid_choice(void) |
| 281 | { |
| 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, |
| 286 | } } }, |
| 287 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
| 288 | }; |
| 289 | |
| 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]"); |
| 292 | } |
| 293 | |
| 294 | static |
| 295 | enum bt_param_validation_status custom_validation_func_valid( |
| 296 | const bt_value *value, |
| 297 | struct bt_param_validation_context *context) |
| 298 | { |
| 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; |
| 304 | } |
| 305 | |
| 306 | static |
| 307 | void test_custom_validation_func_valid(void) |
| 308 | { |
| 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, |
| 312 | } }, |
| 313 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
| 314 | }; |
| 315 | |
| 316 | run_test("navet=+1234", entries, "custom validation function with valid value", NULL); |
| 317 | } |
| 318 | |
| 319 | static |
| 320 | enum bt_param_validation_status custom_validation_func_invalid( |
| 321 | const bt_value *value, |
| 322 | struct bt_param_validation_context *context) |
| 323 | { |
| 324 | return bt_param_validation_error(context, "wrooooong"); |
| 325 | } |
| 326 | |
| 327 | static |
| 328 | void test_custom_validation_func_invalid(void) |
| 329 | { |
| 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, |
| 333 | } }, |
| 334 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
| 335 | }; |
| 336 | |
| 337 | run_test("navet=+1234", entries, "custom validation function with invalid value", |
| 338 | "Error validating parameter `navet`: wrooooong"); |
| 339 | } |
| 340 | |
| 341 | int main(void) |
| 342 | { |
| 343 | plan_tests(41); |
| 344 | |
| 345 | test_map_valid(); |
| 346 | |
| 347 | test_map_missing_key(); |
| 348 | test_map_unexpected_key(); |
| 349 | test_map_invalid_entry_value_type(); |
| 350 | |
| 351 | test_array_valid(); |
| 352 | test_array_empty_valid(); |
| 353 | |
| 354 | test_array_invalid_too_small(); |
| 355 | test_array_invalid_too_large(); |
| 356 | test_array_invalid_elem_type(); |
| 357 | |
| 358 | test_string_valid_without_choices(); |
| 359 | test_string_valid_with_choices(); |
| 360 | |
| 361 | test_string_invalid_choice(); |
| 362 | |
| 363 | test_custom_validation_func_valid(); |
| 364 | test_custom_validation_func_invalid(); |
| 365 | |
| 366 | test_nested_error(); |
| 367 | |
| 368 | return exit_status(); |
| 369 | } |