| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <inttypes.h> |
| 15 |
#include <sched.h> /* IWYU pragma: keep */ |
| 16 |
#include <setjmp.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <stddef.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
#define UNIT_TESTING |
| 24 |
#include <cmocka.h> |
| 25 |
|
| 26 |
#include <isc/symtab.h> |
| 27 |
#include <isc/util.h> |
| 28 |
|
| 29 |
#include <tests/isc.h> |
| 30 |
|
| 31 |
static void |
| 32 |
undefine(char *key, unsigned int type, isc_symvalue_t value, void *arg) { |
| 33 |
UNUSED(arg); |
| 34 |
|
| 35 |
assert_int_equal(type, 1); |
| 36 |
isc_mem_free(mctx, key); |
| 37 |
isc_mem_free(mctx, value.as_pointer); |
| 38 |
} |
| 39 |
|
| 40 |
/* test symbol table growth */ |
| 41 |
ISC_RUN_TEST_IMPL(symtab_grow) { |
| 42 |
isc_result_t result; |
| 43 |
isc_symtab_t *st = NULL; |
| 44 |
isc_symvalue_t value; |
| 45 |
isc_symexists_t policy = isc_symexists_reject; |
| 46 |
int i; |
| 47 |
|
| 48 |
UNUSED(state); |
| 49 |
|
| 50 |
result = isc_symtab_create(mctx, 3, undefine, NULL, false, &st); |
| 51 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 52 |
assert_non_null(st); |
| 53 |
|
| 54 |
/* Nothing should be in the table yet */ |
| 55 |
|
| 56 |
/* |
| 57 |
* Put 1024 entries in the table (this should necessate |
| 58 |
* regrowing the hash table several times |
| 59 |
*/ |
| 60 |
for (i = 0; i < 1024; i++) { |
| 61 |
char str[16], *key; |
| 62 |
|
| 63 |
snprintf(str, sizeof(str), "%04x", i); |
| 64 |
key = isc_mem_strdup(mctx, str); |
| 65 |
assert_non_null(key); |
| 66 |
value.as_pointer = isc_mem_strdup(mctx, str); |
| 67 |
assert_non_null(value.as_pointer); |
| 68 |
result = isc_symtab_define(st, key, 1, value, policy); |
| 69 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 70 |
if (result != ISC_R_SUCCESS) { |
| 71 |
undefine(key, 1, value, NULL); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
* Try to put them in again; this should fail |
| 77 |
*/ |
| 78 |
for (i = 0; i < 1024; i++) { |
| 79 |
char str[16], *key; |
| 80 |
|
| 81 |
snprintf(str, sizeof(str), "%04x", i); |
| 82 |
key = isc_mem_strdup(mctx, str); |
| 83 |
assert_non_null(key); |
| 84 |
value.as_pointer = isc_mem_strdup(mctx, str); |
| 85 |
assert_non_null(value.as_pointer); |
| 86 |
result = isc_symtab_define(st, key, 1, value, policy); |
| 87 |
assert_int_equal(result, ISC_R_EXISTS); |
| 88 |
undefine(key, 1, value, NULL); |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* Retrieve them; this should succeed |
| 93 |
*/ |
| 94 |
for (i = 0; i < 1024; i++) { |
| 95 |
char str[16]; |
| 96 |
|
| 97 |
snprintf(str, sizeof(str), "%04x", i); |
| 98 |
result = isc_symtab_lookup(st, str, 0, &value); |
| 99 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 100 |
assert_string_equal(str, (char *)value.as_pointer); |
| 101 |
} |
| 102 |
|
| 103 |
/* |
| 104 |
* Undefine them |
| 105 |
*/ |
| 106 |
for (i = 0; i < 1024; i++) { |
| 107 |
char str[16]; |
| 108 |
|
| 109 |
snprintf(str, sizeof(str), "%04x", i); |
| 110 |
result = isc_symtab_undefine(st, str, 1); |
| 111 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* Retrieve them again; this should fail |
| 116 |
*/ |
| 117 |
for (i = 0; i < 1024; i++) { |
| 118 |
char str[16]; |
| 119 |
|
| 120 |
snprintf(str, sizeof(str), "%04x", i); |
| 121 |
result = isc_symtab_lookup(st, str, 0, &value); |
| 122 |
assert_int_equal(result, ISC_R_NOTFOUND); |
| 123 |
} |
| 124 |
|
| 125 |
isc_symtab_destroy(&st); |
| 126 |
} |
| 127 |
|
| 128 |
ISC_TEST_LIST_START |
| 129 |
|
| 130 |
ISC_TEST_ENTRY(symtab_grow) |
| 131 |
|
| 132 |
ISC_TEST_LIST_END |
| 133 |
|
| 134 |
ISC_TEST_MAIN |