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
|
/*
* SPDX-License-Identifier: ISC
*
* Copyright (C) 2018-2021 Michael Drake <tlsa@netsurf-browser.org>
*/
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <cyaml/cyaml.h>
#include "../../src/mem.h"
#include "../../src/util.h"
#include "ttest.h"
#include "test.h"
/**
* Test cyaml memory functions.
*
* \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_util_memory_funcs(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
ttest_ctx_t tc;
unsigned char *mem, *tmp;
if (!ttest_start(report, __func__, NULL, NULL, &tc)) return true;
/* Create test allocation */
mem = cyaml__alloc(config, 0xff, true);
if (mem == NULL) {
return ttest_fail(&tc, "Memory allocation failed.");
}
/* Check allocation was zeroed. */
for (unsigned i = 0; i < 0x7f; i++) {
if (mem[i] != 0) {
return ttest_fail(&tc, "Allocation not cleaned.");
}
}
/* Set our own known values */
for (unsigned i = 0; i < 0xff; i++) {
mem[i] = 0xff;
}
/* Shrink allocation */
tmp = cyaml__realloc(config, mem, 0xff, 0x7f, true);
if (tmp == NULL) {
return ttest_fail(&tc, "Allocation shrink failed.");
}
mem = tmp;
/* Check for our own values. */
for (unsigned i = 0; i < 0x7f; i++) {
if (mem[i] != 0xff) {
return ttest_fail(&tc, "Data trampled by realloc.");
}
}
/* Free test allocation. */
cyaml__free(config, mem);
return ttest_pass(&tc);
}
/**
* Test cyaml memory functions.
*
* \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_util_strdup(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
ttest_ctx_t tc;
const char *orig = "Hello!";
char *copy;
if (!ttest_start(report, __func__, NULL, NULL, &tc)) return true;
/* Create test allocation */
copy = cyaml__strdup(config, orig, NULL);
if (copy == NULL) {
return ttest_fail(&tc, "Memory allocation failed.");
}
/* Confirm string duplication. */
if (strcmp(orig, copy) != 0) {
return ttest_fail(&tc, "String not copied correctly.");
}
/* Free test allocation. */
cyaml__free(config, copy);
return ttest_pass(&tc);
}
/**
* Test cyaml memory functions.
*
* \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_util_strdup_len(
ttest_report_ctx_t *report,
const cyaml_config_t *config)
{
ttest_ctx_t tc;
const char *orig = "Hello!";
char *copy;
size_t len;
if (!ttest_start(report, __func__, NULL, NULL, &tc)) return true;
/* Create test allocation */
copy = cyaml__strdup(config, orig, &len);
if (copy == NULL) {
return ttest_fail(&tc, "Memory allocation failed.");
}
/* Confirm string duplication. */
if (strcmp(orig, copy) != 0) {
return ttest_fail(&tc, "String not copied correctly.");
}
/* Confirm string length. */
if (strlen(orig) != len) {
return ttest_fail(&tc, "Bad string length.");
}
/* Free test allocation. */
cyaml__free(config, copy);
return ttest_pass(&tc);
}
/**
* Test invalid state machine state.
*
* \param[in] report The test report context.
* \return true if test passes, false otherwise.
*/
static bool test_util_state_invalid(
ttest_report_ctx_t *report)
{
ttest_ctx_t tc;
if (!ttest_start(report, __func__, NULL, NULL, &tc)) return true;
if (strcmp(cyaml__state_to_str(CYAML_STATE__COUNT),
"<invalid>") != 0) {
return ttest_fail(&tc, "Wrong string for invalid state.");
}
if (strcmp(cyaml__state_to_str(CYAML_STATE__COUNT + 1),
"<invalid>") != 0) {
return ttest_fail(&tc, "Wrong string for invalid state.");
}
return ttest_pass(&tc);
}
/**
* Test CYAML_OK error code.
*
* \param[in] report The test report context.
* \return true if test passes, false otherwise.
*/
static bool test_util_err_success_zero(
ttest_report_ctx_t *report)
{
ttest_ctx_t tc;
if (!ttest_start(report, __func__, NULL, NULL, &tc)) return true;
if (CYAML_OK != 0) {
return ttest_fail(&tc, "CYAML_OK value not zero");
}
return ttest_pass(&tc);
}
/**
* Test valid cyaml_strerror strings.
*
* \param[in] report The test report context.
* \return true if test passes, false otherwise.
*/
static bool test_util_err_strings_valid(
ttest_report_ctx_t *report)
{
ttest_ctx_t tc;
if (!ttest_start(report, __func__, NULL, NULL, &tc)) return true;
if (cyaml_strerror(CYAML_OK) == NULL) {
return ttest_fail(&tc, "CYAML_OK string is NULL");
}
if (strcmp(cyaml_strerror(CYAML_OK), "Success") != 0) {
return ttest_fail(&tc, "CYAML_OK string not 'Success'");
}
for (unsigned i = 1; i <= CYAML_ERR__COUNT; i++) {
if (cyaml_strerror(i) == NULL) {
return ttest_fail(&tc, "Error code %u string is NULL",
i);
}
for (unsigned j = 0; j < i; j++) {
if (strcmp(cyaml_strerror(j), cyaml_strerror(i)) == 0) {
return ttest_fail(&tc, "Two error codes "
"have same string (%u and %u)",
i, j);
}
}
}
return ttest_pass(&tc);
}
/**
* Test invalid cyaml_strerror strings.
*
* \param[in] report The test report context.
* \return true if test passes, false otherwise.
*/
static bool test_util_err_strings_invalid(
ttest_report_ctx_t *report)
{
ttest_ctx_t tc;
if (!ttest_start(report, __func__, NULL, NULL, &tc)) return true;
if (strcmp(cyaml_strerror(CYAML_ERR__COUNT),
"Invalid error code") != 0) {
return ttest_fail(&tc, "CYAML_ERR__COUNT string not "
"'Invalid error code'");
}
if (strcmp(cyaml_strerror(CYAML_ERR__COUNT + 1),
"Invalid error code") != 0) {
return ttest_fail(&tc, "CYAML_ERR__COUNT + 1 string not "
"'Invalid error code'");
}
if (strcmp(cyaml_strerror((cyaml_err_t)-1), "Invalid error code") != 0) {
return ttest_fail(&tc, "-1 string not 'Invalid error code'");
}
return ttest_pass(&tc);
}
/**
* Run the CYAML util unit 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 util_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, "Memory utility functions");
pass &= test_util_strdup(rc, &config);
pass &= test_util_strdup_len(rc, &config);
pass &= test_util_memory_funcs(rc, &config);
ttest_heading(rc, "Error code tests");
pass &= test_util_state_invalid(rc);
pass &= test_util_err_success_zero(rc);
pass &= test_util_err_strings_valid(rc);
pass &= test_util_err_strings_invalid(rc);
return pass;
}
|