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