1 /* $NetBSD: symtab_test.c,v 1.1.1.4 2014/12/10 03:34:44 christos Exp $ */
2
3 /*
4 * Copyright (C) 2011-2013 Internet Systems Consortium, Inc. ("ISC")
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /* Id */
20
21 /*! \file */
22
23 #include <config.h>
24
25 #include <atf-c.h>
26
27 #include <unistd.h>
28
29 #include <isc/symtab.h>
30 #include <isc/print.h>
31
32 #include "isctest.h"
33
34 static void
undefine(char * key,unsigned int type,isc_symvalue_t value,void * arg)35 undefine(char *key, unsigned int type, isc_symvalue_t value, void *arg) {
36 UNUSED(arg);
37
38 ATF_REQUIRE_EQ(type, 1);
39 isc_mem_free(mctx, key);
40 isc_mem_free(mctx, value.as_pointer);
41 }
42
43 /*
44 * Individual unit tests
45 */
46
47 ATF_TC(symtab_grow);
ATF_TC_HEAD(symtab_grow,tc)48 ATF_TC_HEAD(symtab_grow, tc) {
49 atf_tc_set_md_var(tc, "descr", "symbol table growth");
50 }
ATF_TC_BODY(symtab_grow,tc)51 ATF_TC_BODY(symtab_grow, tc) {
52 isc_result_t result;
53 isc_symtab_t *st = NULL;
54 isc_symvalue_t value;
55 isc_symexists_t policy = isc_symexists_reject;
56 int i;
57
58 UNUSED(tc);
59
60 result = isc_test_begin(NULL, ISC_TRUE);
61 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
62
63 result = isc_symtab_create(mctx, 3, undefine, NULL, ISC_FALSE, &st);
64 ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
65 ATF_REQUIRE(st != NULL);
66
67 /* Nothing should be in the table yet */
68
69 /*
70 * Put 1024 entries in the table (this should necessate
71 * regrowing the hash table several times
72 */
73 for (i = 0; i < 1024; i++) {
74 char str[16], *key;
75
76 snprintf(str, sizeof(str), "%04x", i);
77 key = isc_mem_strdup(mctx, str);
78 ATF_REQUIRE(key != NULL);
79 value.as_pointer = isc_mem_strdup(mctx, str);
80 ATF_REQUIRE(value.as_pointer != NULL);
81 result = isc_symtab_define(st, key, 1, value, policy);
82 ATF_CHECK_EQ(result, ISC_R_SUCCESS);
83 if (result != ISC_R_SUCCESS)
84 undefine(key, 1, value, NULL);
85 }
86
87 /*
88 * Try to put them in again; this should fail
89 */
90 for (i = 0; i < 1024; i++) {
91 char str[16], *key;
92
93 snprintf(str, sizeof(str), "%04x", i);
94 key = isc_mem_strdup(mctx, str);
95 ATF_REQUIRE(key != NULL);
96 value.as_pointer = isc_mem_strdup(mctx, str);
97 ATF_REQUIRE(value.as_pointer != NULL);
98 result = isc_symtab_define(st, key, 1, value, policy);
99 ATF_CHECK_EQ(result, ISC_R_EXISTS);
100 undefine(key, 1, value, NULL);
101 }
102
103 /*
104 * Retrieve them; this should succeed
105 */
106 for (i = 0; i < 1024; i++) {
107 char str[16];
108
109 snprintf(str, sizeof(str), "%04x", i);
110 result = isc_symtab_lookup(st, str, 0, &value);
111 ATF_CHECK_EQ(result, ISC_R_SUCCESS);
112 ATF_CHECK_STREQ(str, (char *)value.as_pointer);
113 }
114
115 /*
116 * Undefine them
117 */
118 for (i = 0; i < 1024; i++) {
119 char str[16];
120
121 snprintf(str, sizeof(str), "%04x", i);
122 result = isc_symtab_undefine(st, str, 1);
123 ATF_CHECK_EQ(result, ISC_R_SUCCESS);
124 }
125
126 /*
127 * Retrieve them again; this should fail
128 */
129 for (i = 0; i < 1024; i++) {
130 char str[16];
131
132 snprintf(str, sizeof(str), "%04x", i);
133 result = isc_symtab_lookup(st, str, 0, &value);
134 ATF_CHECK_EQ(result, ISC_R_NOTFOUND);
135 }
136
137 isc_symtab_destroy(&st);
138 isc_test_end();
139 }
140
141 /*
142 * Main
143 */
ATF_TP_ADD_TCS(tp)144 ATF_TP_ADD_TCS(tp) {
145 ATF_TP_ADD_TC(tp, symtab_grow);
146
147 return (atf_no_error());
148 }
149
150