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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* test_hashtable.c
*
* Tests Hashtables
*
*/
#include "testutil.h"
#include "testutil_nss.h"
static void *plContext = NULL;
static void
createHashTables(
PKIX_PL_HashTable **ht,
PKIX_PL_HashTable **ht2,
PKIX_PL_HashTable **ht3,
PKIX_PL_HashTable **ht4)
{
PKIX_TEST_STD_VARS();
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Create(1, 0, ht, plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Create(5, 0, ht2, plContext));
/* at most two entries per bucket */
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Create(1, 2, ht4, plContext));
*ht3 = *ht;
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_IncRef((PKIX_PL_Object *)*ht3, plContext));
cleanup:
PKIX_TEST_RETURN();
}
static void
testAdd(
PKIX_PL_HashTable *ht,
PKIX_PL_HashTable *ht2,
PKIX_PL_String **testString,
PKIX_PL_String **testString2,
PKIX_PL_String **testString3,
PKIX_PL_OID **testOID)
{
char *dummyString = "test string 1";
char *dummyString2 = "test string 2";
char *dummyString3 = "test string 3";
char *dummyOID = "2.11.22222.33333";
PKIX_TEST_STD_VARS();
/* Make some dummy objects */
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
PKIX_ESCASCII,
dummyString,
PL_strlen(dummyString),
testString,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
PKIX_ESCASCII,
dummyString2,
PL_strlen(dummyString2),
testString2,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
PKIX_ESCASCII,
dummyString3,
PL_strlen(dummyString3),
testString3,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_OID_Create(dummyOID, testOID, plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Add(ht,
(PKIX_PL_Object *)*testString,
(PKIX_PL_Object *)*testString2,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Add(ht2,
(PKIX_PL_Object *)*testString,
(PKIX_PL_Object *)*testString2,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Add(ht,
(PKIX_PL_Object *)*testString2,
(PKIX_PL_Object *)*testString,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Add(ht2,
(PKIX_PL_Object *)*testString2,
(PKIX_PL_Object *)*testString,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Add(ht,
(PKIX_PL_Object *)*testOID,
(PKIX_PL_Object *)*testOID,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Add(ht2,
(PKIX_PL_Object *)*testOID,
(PKIX_PL_Object *)*testOID,
plContext));
cleanup:
PKIX_TEST_RETURN();
}
static void
testAddFIFO(
PKIX_PL_HashTable *ht,
PKIX_PL_String **testString,
PKIX_PL_String **testString2,
PKIX_PL_String **testString3)
{
PKIX_PL_String *targetString = NULL;
PKIX_Boolean cmpResult;
PKIX_TEST_STD_VARS();
/*
* ht is created as one bucket, two entries per bucket. Since we add
* three items to the ht, we expect the first one to be deleted.
*/
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Add(ht,
(PKIX_PL_Object *)*testString,
(PKIX_PL_Object *)*testString,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Add(ht,
(PKIX_PL_Object *)*testString2,
(PKIX_PL_Object *)*testString2,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Add(ht,
(PKIX_PL_Object *)*testString3,
(PKIX_PL_Object *)*testString3,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Lookup(ht,
(PKIX_PL_Object *)*testString,
(PKIX_PL_Object **)&targetString,
plContext));
if (targetString != NULL) {
testError("HashTable_Lookup retrieved a supposed deleted item");
PKIX_TEST_DECREF_BC(targetString);
}
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Lookup(ht,
(PKIX_PL_Object *)*testString3,
(PKIX_PL_Object **)&targetString,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals(
(PKIX_PL_Object *)targetString,
(PKIX_PL_Object *)*testString3,
&cmpResult,
plContext));
if (cmpResult != PKIX_TRUE) {
testError("HashTable_Lookup failed");
}
PKIX_TEST_DECREF_BC(targetString);
cleanup:
PKIX_TEST_RETURN();
}
static void
testLookup(
PKIX_PL_HashTable *ht,
PKIX_PL_HashTable *ht2,
PKIX_PL_String *testString,
PKIX_PL_String *testString2,
PKIX_PL_String *testString3,
PKIX_PL_OID *testOID)
{
PKIX_PL_String *targetString = NULL;
PKIX_PL_String *targetOID = NULL;
PKIX_Boolean cmpResult;
PKIX_TEST_STD_VARS();
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Lookup(ht,
(PKIX_PL_Object *)testString,
(PKIX_PL_Object **)&targetString,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals(
(PKIX_PL_Object *)targetString,
(PKIX_PL_Object *)testString2,
&cmpResult,
plContext));
if (cmpResult != PKIX_TRUE) {
testError("HashTable_Lookup failed");
}
PKIX_TEST_DECREF_BC(targetString);
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Lookup(ht2,
(PKIX_PL_Object *)testString,
(PKIX_PL_Object **)&targetString,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals(
(PKIX_PL_Object *)targetString,
(PKIX_PL_Object *)testString2,
&cmpResult,
plContext));
if (cmpResult != PKIX_TRUE) {
testError("HashTable_Lookup failed");
}
PKIX_TEST_DECREF_BC(targetString);
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Lookup(ht2,
(PKIX_PL_Object *)testString2,
(PKIX_PL_Object **)&targetString,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals(
(PKIX_PL_Object *)targetString,
(PKIX_PL_Object *)testString,
&cmpResult,
plContext));
if (cmpResult != PKIX_TRUE) {
testError("HashTable_Lookup failed");
}
PKIX_TEST_DECREF_BC(targetString);
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Lookup(ht,
(PKIX_PL_Object *)testOID,
(PKIX_PL_Object **)&targetOID,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString((PKIX_PL_Object *)targetOID, &targetString, plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals(
(PKIX_PL_Object *)targetOID,
(PKIX_PL_Object *)testOID,
&cmpResult,
plContext));
if (cmpResult != PKIX_TRUE) {
testError("HashTable_Lookup failed");
}
PKIX_TEST_DECREF_BC(targetString);
PKIX_TEST_DECREF_BC(targetOID);
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Lookup(ht2,
(PKIX_PL_Object *)testOID,
(PKIX_PL_Object **)&targetOID,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString((PKIX_PL_Object *)targetOID, &targetString, plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Equals(
(PKIX_PL_Object *)targetOID,
(PKIX_PL_Object *)testOID,
&cmpResult,
plContext));
if (cmpResult != PKIX_TRUE) {
testError("HashTable_Lookup failed");
}
PKIX_TEST_DECREF_BC(targetString);
PKIX_TEST_DECREF_BC(targetOID);
(void)printf("Looking up item not in HashTable.\n");
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Lookup(ht,
(PKIX_PL_Object *)testString3,
(PKIX_PL_Object **)&targetString,
plContext));
if (targetString == NULL)
(void)printf("\tCorrectly returned NULL.\n");
else
testError("Hashtable did not return NULL value as expected");
cleanup:
PKIX_TEST_RETURN();
}
static void
testRemove(
PKIX_PL_HashTable *ht,
PKIX_PL_HashTable *ht2,
PKIX_PL_String *testString,
PKIX_PL_String *testString2,
PKIX_PL_OID *testOID)
{
PKIX_TEST_STD_VARS();
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Remove(ht,
(PKIX_PL_Object *)testString,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Remove(ht,
(PKIX_PL_Object *)testOID,
plContext));
PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HashTable_Remove(ht2,
(PKIX_PL_Object *)testString2,
plContext));
PKIX_TEST_EXPECT_ERROR(PKIX_PL_HashTable_Remove(ht,
(PKIX_PL_Object *)testString,
plContext));
cleanup:
PKIX_TEST_RETURN();
}
static void
testDestroy(
PKIX_PL_HashTable *ht,
PKIX_PL_HashTable *ht2,
PKIX_PL_HashTable *ht3,
PKIX_PL_HashTable *ht4)
{
PKIX_TEST_STD_VARS();
PKIX_TEST_DECREF_BC(ht);
PKIX_TEST_DECREF_BC(ht2);
PKIX_TEST_DECREF_BC(ht3);
PKIX_TEST_DECREF_BC(ht4);
cleanup:
PKIX_TEST_RETURN();
}
int
test_hashtable(int argc, char *argv[])
{
PKIX_PL_HashTable *ht, *ht2, *ht3, *ht4;
PKIX_PL_String *testString, *testString2, *testString3;
PKIX_PL_OID *testOID;
PKIX_UInt32 actualMinorVersion;
PKIX_UInt32 j = 0;
PKIX_TEST_STD_VARS();
startTests("HashTables");
PKIX_TEST_EXPECT_NO_ERROR(
PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
subTest("PKIX_PL_HashTable_Create");
createHashTables(&ht, &ht2, &ht3, &ht4);
PKIX_TEST_EQ_HASH_TOSTR_DUP(ht,
ht3,
ht2,
NULL,
HashTable,
PKIX_FALSE);
subTest("PKIX_PL_HashTable_Add");
testAdd(ht, ht2, &testString, &testString2, &testString3, &testOID);
subTest("PKIX_PL_HashTable_ADD - with Bucket Size limit");
testAddFIFO(ht4, &testString, &testString2, &testString3);
subTest("PKIX_PL_HashTable_Lookup");
testLookup(ht, ht2, testString, testString2, testString3, testOID);
subTest("PKIX_PL_HashTable_Remove");
testRemove(ht, ht2, testString, testString2, testOID);
PKIX_TEST_DECREF_BC(testString);
PKIX_TEST_DECREF_BC(testString2);
PKIX_TEST_DECREF_BC(testString3);
PKIX_TEST_DECREF_BC(testOID);
subTest("PKIX_PL_HashTable_Destroy");
testDestroy(ht, ht2, ht3, ht4);
cleanup:
PKIX_Shutdown(plContext);
endTests("BigInt");
return (0);
}
|