Skip to content

Commit

Permalink
Test: Errors: Add bad schema tests for default values
Browse files Browse the repository at this point in the history
  • Loading branch information
tlsa committed May 29, 2023
1 parent 54be5e3 commit 95afbb8
Showing 1 changed file with 133 additions and 1 deletion.
134 changes: 133 additions & 1 deletion test/units/errs.c
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,63 @@ static bool test_err_load_schema_bad_type(
return ttest_pass(&tc);
}

/**
* Test loading with schema default value with bad type.
*
* \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_err_load_schema_bad_type_default(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
static const unsigned char yaml[] =
"{}\n";
struct target_struct {
int value;
} *data_tgt = NULL;
static const struct cyaml_schema_field mapping_schema[] = {
{
.key = "key",
.value = {
.type = 99999,
.flags = CYAML_FLAG_OPTIONAL,
.data_size = sizeof(data_tgt->value),
},
.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 = {
.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_data(yaml, YAML_LEN(yaml), config, &top_schema,
(cyaml_data_t **) &data_tgt, NULL);
if (err != CYAML_ERR_BAD_TYPE_IN_SCHEMA) {
return ttest_fail(&tc, cyaml_strerror(err));
}

if (data_tgt != NULL) {
return ttest_fail(&tc, "Data non-NULL on error.");
}

return ttest_pass(&tc);
}

/**
* Test saving with schema with bad type.
*
Expand Down Expand Up @@ -1809,6 +1866,80 @@ static bool test_err_load_schema_bad_data_size_9(
return ttest_pass(&tc);
}

/**
* Test loading with schema with data size (9) for sequence count.
*
* \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_err_load_schema_bad_data_size_10(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
static const int missing[] = { 1, 2, 3, 4 };
static const unsigned char yaml[] =
"{}\n";
struct target_struct {
int value[4];
unsigned value_count;
} *data_tgt = NULL;
static const struct cyaml_schema_value entry_schema = {
CYAML_VALUE_INT(CYAML_FLAG_DEFAULT, int),
};
static const struct cyaml_schema_field mapping_schema[] = {
{
.key = "key",
.value = {
.type = CYAML_SEQUENCE,
.flags = CYAML_FLAG_OPTIONAL,
.data_size = sizeof(*(data_tgt->value)),
.sequence = {
.min = 0,
.max = 4,
.entry = &entry_schema,
.missing = missing,
.missing_count =
CYAML_ARRAY_LEN(missing),
},
},
.data_offset = offsetof(struct target_struct, value),
.count_size = 9,
.count_offset = offsetof(
struct target_struct,
value_count),
},
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_data(yaml, YAML_LEN(yaml), config, &top_schema,
(cyaml_data_t **) &data_tgt, NULL);
if (err != CYAML_ERR_INVALID_DATA_SIZE) {
return ttest_fail(&tc, cyaml_strerror(err));
}

if (data_tgt != NULL) {
return ttest_fail(&tc, "Data non-NULL on error.");
}

return ttest_pass(&tc);
}

/**
* Test saving with schema with data size (0).
*
Expand Down Expand Up @@ -2212,7 +2343,6 @@ static bool test_err_save_schema_bad_data_size_7(
.entry = &entry_schema,
.min = 0,
.max = 10,

},
},
.data_offset = offsetof(struct target_struct, seq),
Expand Down Expand Up @@ -8313,9 +8443,11 @@ bool errs_tests(
pass &= test_err_save_schema_bad_data_size_7(rc, &config);
pass &= test_err_save_schema_bad_data_size_8(rc, &config);
pass &= test_err_copy_schema_bad_data_size_1(rc, &config);
pass &= test_err_load_schema_bad_data_size_10(rc, &config);
pass &= test_err_load_schema_sequence_min_max(rc, &config);
pass &= test_err_save_schema_sequence_min_max(rc, &config);
pass &= test_err_copy_schema_sequence_min_max(rc, &config);
pass &= test_err_load_schema_bad_type_default(rc, &config);
pass &= test_err_load_schema_bad_data_size_float(rc, &config);
pass &= test_err_load_schema_sequence_in_sequence(rc, &config);
pass &= test_err_save_schema_sequence_in_sequence(rc, &config);
Expand Down

0 comments on commit 95afbb8

Please sign in to comment.