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