1/*	$NetBSD: symtab_test.c,v 1.2 2024/02/21 22:52:51 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/print.h>
29#include <isc/symtab.h>
30#include <isc/util.h>
31
32#include <tests/isc.h>
33
34static void
35undefine(char *key, unsigned int type, isc_symvalue_t value, void *arg) {
36	UNUSED(arg);
37
38	assert_int_equal(type, 1);
39	isc_mem_free(mctx, key);
40	isc_mem_free(mctx, value.as_pointer);
41}
42
43/* test symbol table growth */
44ISC_RUN_TEST_IMPL(symtab_grow) {
45	isc_result_t result;
46	isc_symtab_t *st = NULL;
47	isc_symvalue_t value;
48	isc_symexists_t policy = isc_symexists_reject;
49	int i;
50
51	UNUSED(state);
52
53	result = isc_symtab_create(mctx, 3, undefine, NULL, false, &st);
54	assert_int_equal(result, ISC_R_SUCCESS);
55	assert_non_null(st);
56
57	/* Nothing should be in the table yet */
58
59	/*
60	 * Put 1024 entries in the table (this should necessate
61	 * regrowing the hash table several times
62	 */
63	for (i = 0; i < 1024; i++) {
64		char str[16], *key;
65
66		snprintf(str, sizeof(str), "%04x", i);
67		key = isc_mem_strdup(mctx, str);
68		assert_non_null(key);
69		value.as_pointer = isc_mem_strdup(mctx, str);
70		assert_non_null(value.as_pointer);
71		result = isc_symtab_define(st, key, 1, value, policy);
72		assert_int_equal(result, ISC_R_SUCCESS);
73		if (result != ISC_R_SUCCESS) {
74			undefine(key, 1, value, NULL);
75		}
76	}
77
78	/*
79	 * Try to put them in again; this should fail
80	 */
81	for (i = 0; i < 1024; i++) {
82		char str[16], *key;
83
84		snprintf(str, sizeof(str), "%04x", i);
85		key = isc_mem_strdup(mctx, str);
86		assert_non_null(key);
87		value.as_pointer = isc_mem_strdup(mctx, str);
88		assert_non_null(value.as_pointer);
89		result = isc_symtab_define(st, key, 1, value, policy);
90		assert_int_equal(result, ISC_R_EXISTS);
91		undefine(key, 1, value, NULL);
92	}
93
94	/*
95	 * Retrieve them; this should succeed
96	 */
97	for (i = 0; i < 1024; i++) {
98		char str[16];
99
100		snprintf(str, sizeof(str), "%04x", i);
101		result = isc_symtab_lookup(st, str, 0, &value);
102		assert_int_equal(result, ISC_R_SUCCESS);
103		assert_string_equal(str, (char *)value.as_pointer);
104	}
105
106	/*
107	 * Undefine them
108	 */
109	for (i = 0; i < 1024; i++) {
110		char str[16];
111
112		snprintf(str, sizeof(str), "%04x", i);
113		result = isc_symtab_undefine(st, str, 1);
114		assert_int_equal(result, ISC_R_SUCCESS);
115	}
116
117	/*
118	 * Retrieve them again; this should fail
119	 */
120	for (i = 0; i < 1024; i++) {
121		char str[16];
122
123		snprintf(str, sizeof(str), "%04x", i);
124		result = isc_symtab_lookup(st, str, 0, &value);
125		assert_int_equal(result, ISC_R_NOTFOUND);
126	}
127
128	isc_symtab_destroy(&st);
129}
130
131ISC_TEST_LIST_START
132
133ISC_TEST_ENTRY(symtab_grow)
134
135ISC_TEST_LIST_END
136
137ISC_TEST_MAIN
138