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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
/*
* SPDX-License-Identifier: ISC
*
* Copyright (C) 2018-2021 Michael Drake <tlsa@netsurf-browser.org>
*/
#include <stdbool.h>
#include <assert.h>
#include <stdio.h>
#include <cyaml/cyaml.h>
#include "ttest.h"
#include "test.h"
/**
* Unit test context data.
*/
typedef struct test_data {
cyaml_data_t **data;
unsigned *seq_count;
const struct cyaml_config *config;
const struct cyaml_schema_value *schema;
} test_data_t;
/**
* Common clean up function to free data loaded by tests.
*
* \param[in] data The unit test context data.
*/
static void cyaml_cleanup(void *data)
{
struct test_data *td = data;
unsigned seq_count = 0;
if (td->seq_count != NULL) {
seq_count = *(td->seq_count);
}
if (td->data != NULL) {
cyaml_free(td->config, td->schema, *(td->data), seq_count);
}
}
/**
* Test loading a non-existent file.
*
* \param[in] report The test report context.
* \param[in] config The CYAML config to use for the test.
* \return true if test passes, false otherwise.
*/
static bool test_file_load_bad_path(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
struct target_struct {
char *cakes;
} *data_tgt = NULL;
static const struct cyaml_schema_field mapping_schema[] = {
CYAML_FIELD_END
};
static const struct cyaml_schema_value top_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER,
struct target_struct, mapping_schema),
};
test_data_t td = {
.data = (cyaml_data_t **) &data_tgt,
.config = config,
.schema = &top_schema,
};
cyaml_err_t err;
ttest_ctx_t tc;
if (!ttest_start(report, __func__, cyaml_cleanup, &td, &tc)) {
return true;
}
err = cyaml_load_file("/cyaml/path/shouldn't/exist.yaml",
config, &top_schema, (cyaml_data_t **) &data_tgt, NULL);
if (err != CYAML_ERR_FILE_OPEN) {
return ttest_fail(&tc, cyaml_strerror(err));
}
return ttest_pass(&tc);
}
/**
* Test loading a non-existent file.
*
* \param[in] report The test report context.
* \param[in] config The CYAML config to use for the test.
* \return true if test passes, false otherwise.
*/
static bool test_file_save_bad_path(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
struct target_struct {
char *cakes;
} *data = NULL;
static const struct cyaml_schema_field mapping_schema[] = {
CYAML_FIELD_END
};
static const struct cyaml_schema_value top_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER,
struct target_struct, mapping_schema),
};
test_data_t td = {
.data = NULL,
.config = config,
.schema = &top_schema,
};
cyaml_err_t err;
ttest_ctx_t tc;
if (!ttest_start(report, __func__, cyaml_cleanup, &td, &tc)) {
return true;
}
err = cyaml_save_file("/cyaml/path/shouldn't/exist.yaml",
config, &top_schema, data, 0);
if (err != CYAML_ERR_FILE_OPEN) {
return ttest_fail(&tc, cyaml_strerror(err));
}
return ttest_pass(&tc);
}
/**
* Test loading the basic YAML file.
*
* \param[in] report The test report context.
* \param[in] config The CYAML config to use for the test.
* \return true if test passes, false otherwise.
*/
static bool test_file_load_basic(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
struct animal {
char *kind;
char **sounds;
unsigned sounds_count;
};
struct target_struct {
struct animal *animals;
unsigned animals_count;
char **cakes;
unsigned cakes_count;
} *data_tgt = NULL;
static const struct cyaml_schema_value sounds_entry_schema = {
CYAML_VALUE_STRING(CYAML_FLAG_POINTER, char, 0, CYAML_UNLIMITED),
};
static const struct cyaml_schema_field animal_mapping_schema[] = {
CYAML_FIELD_STRING_PTR("kind", CYAML_FLAG_POINTER,
struct animal, kind, 0, CYAML_UNLIMITED),
CYAML_FIELD_SEQUENCE("sounds", CYAML_FLAG_POINTER,
struct animal, sounds,
&sounds_entry_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_END
};
static const struct cyaml_schema_value animals_entry_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_DEFAULT,
struct animal, animal_mapping_schema),
};
static const struct cyaml_schema_value cakes_entry_schema = {
CYAML_VALUE_STRING(CYAML_FLAG_POINTER, char, 0, CYAML_UNLIMITED),
};
static const struct cyaml_schema_field mapping_schema[] = {
CYAML_FIELD_SEQUENCE("animals", CYAML_FLAG_POINTER,
struct target_struct, animals,
&animals_entry_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_SEQUENCE("cakes", CYAML_FLAG_POINTER,
struct target_struct, cakes,
&cakes_entry_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_END
};
static const struct cyaml_schema_value top_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER,
struct target_struct, mapping_schema),
};
test_data_t td = {
.data = (cyaml_data_t **) &data_tgt,
.config = config,
.schema = &top_schema,
};
cyaml_err_t err;
ttest_ctx_t tc;
if (!ttest_start(report, __func__, cyaml_cleanup, &td, &tc)) {
return true;
}
err = cyaml_load_file("test/data/basic.yaml", config, &top_schema,
(cyaml_data_t **) &data_tgt, NULL);
if (err != CYAML_OK) {
return ttest_fail(&tc, cyaml_strerror(err));
}
return ttest_pass(&tc);
}
/**
* Test loading and then saving the basic YAML file.
*
* \param[in] report The test report context.
* \param[in] config The CYAML config to use for the test.
* \return true if test passes, false otherwise.
*/
static bool test_file_load_save_basic(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
struct animal {
char *kind;
char **sounds;
unsigned sounds_count;
};
struct target_struct {
struct animal *animals;
unsigned animals_count;
char **cakes;
unsigned cakes_count;
} *data_tgt = NULL;
static const struct cyaml_schema_value sounds_entry_schema = {
CYAML_VALUE_STRING(CYAML_FLAG_POINTER, char, 0, CYAML_UNLIMITED),
};
static const struct cyaml_schema_field animal_mapping_schema[] = {
CYAML_FIELD_STRING_PTR("kind", CYAML_FLAG_POINTER,
struct animal, kind, 0, CYAML_UNLIMITED),
CYAML_FIELD_SEQUENCE("sounds", CYAML_FLAG_POINTER,
struct animal, sounds,
&sounds_entry_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_END
};
static const struct cyaml_schema_value animals_entry_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_DEFAULT,
struct animal, animal_mapping_schema),
};
static const struct cyaml_schema_value cakes_entry_schema = {
CYAML_VALUE_STRING(CYAML_FLAG_POINTER, char, 0, CYAML_UNLIMITED),
};
static const struct cyaml_schema_field mapping_schema[] = {
CYAML_FIELD_SEQUENCE("animals", CYAML_FLAG_POINTER,
struct target_struct, animals,
&animals_entry_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_SEQUENCE("cakes", CYAML_FLAG_POINTER,
struct target_struct, cakes,
&cakes_entry_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_END
};
static const struct cyaml_schema_value top_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER,
struct target_struct, mapping_schema),
};
test_data_t td = {
.data = (cyaml_data_t **) &data_tgt,
.config = config,
.schema = &top_schema,
};
cyaml_err_t err;
ttest_ctx_t tc;
if (!ttest_start(report, __func__, cyaml_cleanup, &td, &tc)) {
return true;
}
err = cyaml_load_file("test/data/basic.yaml", config, &top_schema,
(cyaml_data_t **) &data_tgt, NULL);
if (err != CYAML_OK) {
return ttest_fail(&tc, cyaml_strerror(err));
}
err = cyaml_save_file("build/load_save.yaml", config, &top_schema,
data_tgt, 0);
if (err != CYAML_OK) {
return ttest_fail(&tc, cyaml_strerror(err));
}
return ttest_pass(&tc);
}
/**
* Test loading the basic YAML file, with a mismatching schema.
*
* \param[in] report The test report context.
* \param[in] config The CYAML config to use for the test.
* \return true if test passes, false otherwise.
*/
static bool test_file_load_basic_invalid(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
struct animal {
char *kind;
int *sounds;
unsigned sounds_count;
};
struct target_struct {
struct animal *animals;
unsigned animals_count;
char **cakes;
unsigned cakes_count;
} *data_tgt = NULL;
static const struct cyaml_schema_value sounds_entry_schema = {
/* The data has a string, but we're expecting int here. */
CYAML_VALUE_INT(CYAML_FLAG_DEFAULT, int),
};
static const struct cyaml_schema_field animal_mapping_schema[] = {
CYAML_FIELD_STRING_PTR("kind", CYAML_FLAG_POINTER,
struct animal, kind, 0, CYAML_UNLIMITED),
CYAML_FIELD_SEQUENCE("sounds", CYAML_FLAG_POINTER,
struct animal, sounds,
&sounds_entry_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_END
};
static const struct cyaml_schema_value animals_entry_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_DEFAULT,
struct animal, animal_mapping_schema),
};
static const struct cyaml_schema_value cakes_entry_schema = {
CYAML_VALUE_STRING(CYAML_FLAG_POINTER, char, 0, CYAML_UNLIMITED),
};
static const struct cyaml_schema_field mapping_schema[] = {
CYAML_FIELD_SEQUENCE("animals", CYAML_FLAG_POINTER,
struct target_struct, animals,
&animals_entry_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_SEQUENCE("cakes", CYAML_FLAG_POINTER,
struct target_struct, cakes,
&cakes_entry_schema, 0, CYAML_UNLIMITED),
CYAML_FIELD_END
};
static const struct cyaml_schema_value top_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER,
struct target_struct, mapping_schema),
};
test_data_t td = {
.data = (cyaml_data_t **) &data_tgt,
.config = config,
.schema = &top_schema,
};
cyaml_err_t err;
ttest_ctx_t tc;
if (!ttest_start(report, __func__, cyaml_cleanup, &td, &tc)) {
return true;
}
err = cyaml_load_file("test/data/basic.yaml", config, &top_schema,
(cyaml_data_t **) &data_tgt, NULL);
if (err != CYAML_ERR_INVALID_VALUE) {
return ttest_fail(&tc, cyaml_strerror(err));
}
return ttest_pass(&tc);
}
/**
* Test saving to a file when an erro occurs.
*
* \param[in] report The test report context.
* \param[in] config The CYAML config to use for the test.
* \return true if test passes, false otherwise.
*/
static bool test_file_save_basic_invalid(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
static const struct target_struct {
int value;
} data = {
.value = 9,
};
static const struct cyaml_schema_field mapping_schema[] = {
{
.key = "key",
.value = {
.type = CYAML_INT,
.flags = CYAML_FLAG_DEFAULT,
.data_size = 0,
},
.data_offset = offsetof(struct target_struct, value),
},
CYAML_FIELD_END
};
static const struct cyaml_schema_value top_schema = {
CYAML_VALUE_MAPPING(CYAML_FLAG_POINTER,
struct target_struct, mapping_schema),
};
test_data_t td = {
.config = config,
};
cyaml_err_t err;
ttest_ctx_t tc;
if (!ttest_start(report, __func__, cyaml_cleanup, &td, &tc)) {
return true;
}
err = cyaml_save_file("build/save.yaml", config, &top_schema, &data, 0);
if (err != CYAML_ERR_INVALID_DATA_SIZE) {
return ttest_fail(&tc, cyaml_strerror(err));
}
return ttest_pass(&tc);
}
/**
* Run the YAML file tests.
*
* \param[in] rc The ttest report context.
* \param[in] log_level CYAML log level.
* \param[in] log_fn CYAML logging function, or NULL.
* \return true iff all unit tests pass, otherwise false.
*/
bool file_tests(
ttest_report_ctx_t *rc,
cyaml_log_t log_level,
cyaml_log_fn_t log_fn)
{
bool pass = true;
cyaml_config_t config = {
.log_fn = log_fn,
.mem_fn = cyaml_mem,
.log_level = log_level,
.flags = CYAML_CFG_DEFAULT,
};
ttest_heading(rc, "File loading tests");
pass &= test_file_load_basic(rc, &config);
pass &= test_file_load_save_basic(rc, &config);
/* Since we expect loads of error logging for these tests,
* suppress log output if required log level is greater
* than \ref CYAML_LOG_INFO.
*/
if (log_level > CYAML_LOG_INFO) {
config.log_fn = NULL;
}
pass &= test_file_load_bad_path(rc, &config);
pass &= test_file_save_bad_path(rc, &config);
pass &= test_file_load_basic_invalid(rc, &config);
pass &= test_file_save_basic_invalid(rc, &config);
return pass;
}
|