]> git.ipfire.org Git - thirdparty/dhcp.git/blob - server/tests/hash_unittest.c
[rt25901_atf] Test for 29851 simiplified
[thirdparty/dhcp.git] / server / tests / hash_unittest.c
1 /*
2 * Copyright (c) 2012 by Internet Systems Consortium, Inc. ("ISC")
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 *
16 * Internet Systems Consortium, Inc.
17 * 950 Charter Street
18 * Redwood City, CA 94063
19 * <info@isc.org>
20 * https://www.isc.org/
21 *
22 */
23
24 #include "config.h"
25 #include <atf-c.h>
26 #include <omapip/omapip_p.h>
27 #include "dhcpd.h"
28
29 /*
30 * The following structures are kept here for reference only. As hash functions
31 * are somewhat convoluted, they are copied here for the reference. Original
32 * location is specified. Keep in mind that it may change over time:
33 *
34 * copied from server/omapi.c:49 *
35 * omapi_object_type_t *dhcp_type_lease;
36 * omapi_object_type_t *dhcp_type_pool;
37 * omapi_object_type_t *dhcp_type_class;
38 * omapi_object_type_t *dhcp_type_subclass;
39 * omapi_object_type_t *dhcp_type_host;
40 *
41 * copied from server/salloc.c:138
42 * OMAPI_OBJECT_ALLOC (lease, struct lease, dhcp_type_lease)
43 * OMAPI_OBJECT_ALLOC (class, struct class, dhcp_type_class)
44 * OMAPI_OBJECT_ALLOC (subclass, struct class, dhcp_type_subclass)
45 * OMAPI_OBJECT_ALLOC (pool, struct pool, dhcp_type_pool)
46 * OMAPI_OBJECT_ALLOC (host, struct host_decl, dhcp_type_host)
47 *
48 * copied from server/mdb.c:2686
49 * HASH_FUNCTIONS(lease_ip, const unsigned char *, struct lease, lease_ip_hash_t,
50 * lease_reference, lease_dereference, do_ip4_hash)
51 * HASH_FUNCTIONS(lease_id, const unsigned char *, struct lease, lease_id_hash_t,
52 * lease_reference, lease_dereference, do_id_hash)
53 * HASH_FUNCTIONS (host, const unsigned char *, struct host_decl, host_hash_t,
54 * host_reference, host_dereference, do_string_hash)
55 * HASH_FUNCTIONS (class, const char *, struct class, class_hash_t,
56 * class_reference, class_dereference, do_string_hash)
57 *
58 * copied from server/mdb.c:46
59 * host_hash_t *host_hw_addr_hash;
60 * host_hash_t *host_uid_hash;
61 * host_hash_t *host_name_hash;
62 * lease_id_hash_t *lease_uid_hash;
63 * lease_ip_hash_t *lease_ip_addr_hash;
64 * lease_id_hash_t *lease_hw_addr_hash;
65 */
66
67 /**
68 * @brief sets client-id field in host declaration
69 *
70 * @param host pointer to host declaration
71 * @param uid pointer to client-id data
72 * @param uid_len length of the client-id data
73 *
74 * @return 1 if successful, 0 otherwise
75 */
76 int lease_set_clientid(struct host_decl *host, const unsigned char *uid, int uid_len) {
77
78 /* clean-up this mess and set client-identifier in a sane way */
79 int real_len = uid_len;
80 if (uid_len == 0) {
81 real_len = strlen((const char *)uid) + 1;
82 }
83
84 memset(&host->client_identifier, 0, sizeof(host->client_identifier));
85 host->client_identifier.len = uid_len;
86 if (!buffer_allocate(&host->client_identifier.buffer, real_len, MDL)) {
87 return 0;
88 }
89 host->client_identifier.data = host->client_identifier.buffer->data;
90 memcpy((char *)host->client_identifier.data, uid, real_len);
91
92 return 1;
93 }
94
95 /// @brief executes uid hash test for specified client-ids (2 hosts)
96 ///
97 /// Creates two host structures, adds first host to the uid hash,
98 /// then adds second host to the hash, then removes first host,
99 /// then removed the second. Many checks are performed during all
100 /// operations.
101 ///
102 /// @param clientid1 client-id of the first host
103 /// @param clientid1_len client-id1 length (may be 0 for strings)
104 /// @param clientid2 client-id of the second host
105 /// @param clientid2_len client-id2 length (may be 0 for strings)
106 void lease_hash_test_2hosts(unsigned char clientid1[], size_t clientid1_len,
107 unsigned char clientid2[], size_t clientid2_len) {
108
109 printf("Checking hash operation for 2 hosts: clientid1-len=%lu"
110 "clientid2-len=%lu\n", clientid1_len, clientid2_len);
111
112 dhcp_db_objects_setup ();
113 dhcp_common_objects_setup ();
114
115 /* check that there is actually zero hosts in the hash */
116 /* @todo: host_hash_for_each() */
117
118 struct host_decl *host1 = 0, *host2 = 0;
119 struct host_decl *check = 0;
120
121 /* === step 1: allocate hosts === */
122 ATF_CHECK_MSG(host_allocate(&host1, MDL) == ISC_R_SUCCESS,
123 "Failed to allocate host");
124 ATF_CHECK_MSG(host_allocate(&host2, MDL) == ISC_R_SUCCESS,
125 "Failed to allocate host");
126
127 ATF_CHECK_MSG(host_new_hash(&host_uid_hash, HOST_HASH_SIZE, MDL) != 0,
128 "Unable to create new hash");
129
130 ATF_CHECK_MSG(buffer_allocate(&host1->client_identifier.buffer,
131 clientid1_len, MDL) != 0,
132 "Can't allocate uid buffer for host1");
133
134 ATF_CHECK_MSG(buffer_allocate(&host2->client_identifier.buffer,
135 clientid2_len, MDL) != 0,
136 "Can't allocate uid buffer for host2");
137
138 /* setting up host1->client_identifier is actually not needed */
139 /*
140 ATF_CHECK_MSG(lease_set_clientid(host1, clientid1, actual1_len) != 0,
141 "Failed to set client-id for host1");
142
143 ATF_CHECK_MSG(lease_set_clientid(host2, clientid2, actual2_len) != 0,
144 "Failed to set client-id for host2");
145 */
146
147 ATF_CHECK_MSG(host1->refcnt == 1, "Invalid refcnt for host1");
148 ATF_CHECK_MSG(host2->refcnt == 1, "Invalid refcnt for host2");
149
150 /* verify that our hosts are not in the hash yet */
151 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1,
152 clientid1_len, MDL) == 0,
153 "Host1 is not supposed to be in the uid_hash.");
154
155 ATF_CHECK_MSG(!check, "Host1 is not supposed to be in the uid_hash.");
156
157 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2,
158 clientid2_len, MDL) == 0,
159 "Host2 is not supposed to be in the uid_hash.");
160 ATF_CHECK_MSG(!check, "Host2 is not supposed to be in the uid_hash.");
161
162
163 /* === step 2: add first host to the hash === */
164 host_hash_add(host_uid_hash, clientid1, clientid1_len, host1, MDL);
165
166 /* 2 pointers expected: ours (host1) and the one stored in hash */
167 ATF_CHECK_MSG(host1->refcnt == 2, "Invalid refcnt for host1");
168 /* 1 pointer expected: just ours (host2) */
169 ATF_CHECK_MSG(host2->refcnt == 1, "Invalid refcnt for host2");
170
171 /* verify that host1 is really in the hash and the we can find it */
172 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1,
173 clientid1_len, MDL),
174 "Host1 was supposed to be in the uid_hash.");
175 ATF_CHECK_MSG(check, "Host1 was supposed to be in the uid_hash.");
176
177 /* Hey! That's not the host we were looking for! */
178 ATF_CHECK_MSG(check == host1, "Wrong host returned by host_hash_lookup");
179
180 /* 3 pointers: host1, (stored in hash), check */
181 ATF_CHECK_MSG(host1->refcnt == 3, "Invalid refcnt for host1");
182
183 /* reference count should be increased because we not have a pointer */
184
185 host_dereference(&check, MDL); /* we don't need it now */
186
187 ATF_CHECK_MSG(check == NULL, "check pointer is supposed to be NULL");
188
189 /* 2 pointers: host1, (stored in hash) */
190 ATF_CHECK_MSG(host1->refcnt == 2, "Invalid refcnt for host1");
191
192 /* verify that host2 is not in the hash */
193 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2,
194 clientid2_len, MDL) == 0,
195 "Host2 was not supposed to be in the uid_hash[2].");
196 ATF_CHECK_MSG(check == NULL, "Host2 was not supposed to be in the hash.");
197
198
199 /* === step 3: add second hot to the hash === */
200 host_hash_add(host_uid_hash, clientid2, clientid2_len, host2, MDL);
201
202 /* 2 pointers expected: ours (host1) and the one stored in hash */
203 ATF_CHECK_MSG(host2->refcnt == 2, "Invalid refcnt for host2");
204
205 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2,
206 clientid2_len, MDL),
207 "Host2 was supposed to be in the uid_hash.");
208 ATF_CHECK_MSG(check, "Host2 was supposed to be in the uid_hash.");
209
210 /* Hey! That's not the host we were looking for! */
211 ATF_CHECK_MSG(check == host2, "Wrong host returned by host_hash_lookup");
212
213 /* 3 pointers: host1, (stored in hash), check */
214 ATF_CHECK_MSG(host2->refcnt == 3, "Invalid refcnt for host1");
215
216 host_dereference(&check, MDL); /* we don't need it now */
217
218 /* now we have 2 hosts in the hash */
219
220 /* verify that host1 is still in the hash and the we can find it */
221 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1,
222 clientid1_len, MDL),
223 "Host1 was supposed to be in the uid_hash.");
224 ATF_CHECK_MSG(check, "Host1 was supposed to be in the uid_hash.");
225
226 /* Hey! That's not the host we were looking for! */
227 ATF_CHECK_MSG(check == host1, "Wrong host returned by host_hash_lookup");
228
229 /* 3 pointers: host1, (stored in hash), check */
230 ATF_CHECK_MSG(host1->refcnt == 3, "Invalid refcnt for host1");
231
232 host_dereference(&check, MDL); /* we don't need it now */
233
234
235 /**
236 * @todo check that there is actually two hosts in the hash.
237 * Use host_hash_for_each() for that.
238 */
239
240 /* === step 4: remove first host from the hash === */
241
242 /* delete host from hash */
243 host_hash_delete(host_uid_hash, clientid1, clientid1_len, MDL);
244
245 ATF_CHECK_MSG(host1->refcnt == 1, "Invalid refcnt for host1");
246 ATF_CHECK_MSG(host2->refcnt == 2, "Invalid refcnt for host2");
247
248 /* verify that host1 is no longer in the hash */
249 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1,
250 clientid1_len, MDL) == 0,
251 "Host1 is not supposed to be in the uid_hash.");
252 ATF_CHECK_MSG(!check, "Host1 is not supposed to be in the uid_hash.");
253
254 /* host2 should be still there, though */
255 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2,
256 clientid2_len, MDL),
257 "Host2 was supposed to still be in the uid_hash.");
258 host_dereference(&check, MDL);
259
260 /* === step 5: remove second host from the hash === */
261 host_hash_delete(host_uid_hash, clientid2, clientid2_len, MDL);
262
263 ATF_CHECK_MSG(host1->refcnt == 1, "Invalid refcnt for host1");
264 ATF_CHECK_MSG(host2->refcnt == 1, "Invalid refcnt for host2");
265
266 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2,
267 clientid2_len, MDL) == 0,
268 "Host2 was not supposed to be in the uid_hash anymore.");
269
270 host_dereference(&host1, MDL);
271 host_dereference(&host2, MDL);
272
273 /*
274 * No easy way to check if the host object were actually released.
275 * We could run it in valgrind and check for memory leaks.
276 */
277
278 #if defined (DEBUG_MEMORY_LEAKAGE) && defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
279 /* @todo: Should be called in cleanup */
280 free_everything ();
281 #endif
282 }
283
284 /// @brief executes uid hash test for specified client-ids (3 hosts)
285 ///
286 /// Creates three host structures, adds first host to the uid hash,
287 /// then adds second host to the hash, then removes first host,
288 /// then removed the second. Many checks are performed during all
289 /// operations.
290 ///
291 /// @param clientid1 client-id of the first host
292 /// @param clientid1_len client-id1 length (may be 0 for strings)
293 /// @param clientid2 client-id of the second host
294 /// @param clientid2_len client-id2 length (may be 0 for strings)
295 /// @param clientid3 client-id of the second host
296 /// @param clientid3_len client-id2 length (may be 0 for strings)
297 void lease_hash_test_3hosts(unsigned char clientid1[], size_t clientid1_len,
298 unsigned char clientid2[], size_t clientid2_len,
299 unsigned char clientid3[], size_t clientid3_len) {
300
301 printf("Checking hash operation for 3 hosts: clientid1-len=%lu"
302 " clientid2-len=%lu clientid3-len=%lu\n",
303 clientid1_len, clientid2_len, clientid3_len);
304
305 dhcp_db_objects_setup ();
306 dhcp_common_objects_setup ();
307
308 /* check that there is actually zero hosts in the hash */
309 /* @todo: host_hash_for_each() */
310
311 struct host_decl *host1 = 0, *host2 = 0, *host3 = 0;
312 struct host_decl *check = 0;
313
314 /* === step 1: allocate hosts === */
315 ATF_CHECK_MSG(host_allocate(&host1, MDL) == ISC_R_SUCCESS,
316 "Failed to allocate host");
317 ATF_CHECK_MSG(host_allocate(&host2, MDL) == ISC_R_SUCCESS,
318 "Failed to allocate host");
319 ATF_CHECK_MSG(host_allocate(&host3, MDL) == ISC_R_SUCCESS,
320 "Failed to allocate host");
321
322 ATF_CHECK_MSG(host_new_hash(&host_uid_hash, HOST_HASH_SIZE, MDL) != 0,
323 "Unable to create new hash");
324
325 ATF_CHECK_MSG(buffer_allocate(&host1->client_identifier.buffer,
326 clientid1_len, MDL) != 0,
327 "Can't allocate uid buffer for host1");
328 ATF_CHECK_MSG(buffer_allocate(&host2->client_identifier.buffer,
329 clientid2_len, MDL) != 0,
330 "Can't allocate uid buffer for host2");
331 ATF_CHECK_MSG(buffer_allocate(&host3->client_identifier.buffer,
332 clientid3_len, MDL) != 0,
333 "Can't allocate uid buffer for host3");
334
335 /* verify that our hosts are not in the hash yet */
336 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1,
337 clientid1_len, MDL) == 0,
338 "Host1 is not supposed to be in the uid_hash.");
339
340 ATF_CHECK_MSG(!check, "Host1 is not supposed to be in the uid_hash.");
341
342 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2,
343 clientid2_len, MDL) == 0,
344 "Host2 is not supposed to be in the uid_hash.");
345 ATF_CHECK_MSG(!check, "Host2 is not supposed to be in the uid_hash.");
346
347 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid3,
348 clientid3_len, MDL) == 0,
349 "Host3 is not supposed to be in the uid_hash.");
350 ATF_CHECK_MSG(!check, "Host3 is not supposed to be in the uid_hash.");
351
352 /* === step 2: add hosts to the hash === */
353 host_hash_add(host_uid_hash, clientid1, clientid1_len, host1, MDL);
354 host_hash_add(host_uid_hash, clientid2, clientid2_len, host2, MDL);
355 host_hash_add(host_uid_hash, clientid3, clientid3_len, host3, MDL);
356
357 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1,
358 clientid1_len, MDL),
359 "Host1 was supposed to be in the uid_hash.");
360 /* Hey! That's not the host we were looking for! */
361 ATF_CHECK_MSG(check == host1, "Wrong host returned by host_hash_lookup");
362 host_dereference(&check, MDL); /* we don't need it now */
363
364 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2,
365 clientid2_len, MDL),
366 "Host2 was supposed to be in the uid_hash.");
367 ATF_CHECK_MSG(check, "Host2 was supposed to be in the uid_hash.");
368 /* Hey! That's not the host we were looking for! */
369 ATF_CHECK_MSG(check == host2, "Wrong host returned by host_hash_lookup");
370 host_dereference(&check, MDL); /* we don't need it now */
371
372 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid3,
373 clientid3_len, MDL),
374 "Host3 was supposed to be in the uid_hash.");
375 ATF_CHECK_MSG(check, "Host3 was supposed to be in the uid_hash.");
376 /* Hey! That's not the host we were looking for! */
377 ATF_CHECK_MSG(check == host3, "Wrong host returned by host_hash_lookup");
378 host_dereference(&check, MDL); /* we don't need it now */
379
380 /* === step 4: remove first host from the hash === */
381
382 /* delete host from hash */
383 host_hash_delete(host_uid_hash, clientid1, clientid1_len, MDL);
384
385 /* verify that host1 is no longer in the hash */
386 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid1,
387 clientid1_len, MDL) == 0,
388 "Host1 is not supposed to be in the uid_hash.");
389 ATF_CHECK_MSG(!check, "Host1 is not supposed to be in the uid_hash.");
390
391 /* host2 and host3 should be still there, though */
392 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2,
393 clientid2_len, MDL),
394 "Host2 was supposed to still be in the uid_hash.");
395 host_dereference(&check, MDL);
396 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid3,
397 clientid3_len, MDL),
398 "Host3 was supposed to still be in the uid_hash.");
399 host_dereference(&check, MDL);
400
401 /* === step 5: remove second host from the hash === */
402 host_hash_delete(host_uid_hash, clientid2, clientid2_len, MDL);
403
404 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid2,
405 clientid2_len, MDL) == 0,
406 "Host2 was not supposed to be in the uid_hash anymore.");
407 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid3,
408 clientid3_len, MDL),
409 "Host3 was supposed to still be in the uid_hash.");
410 host_dereference(&check, MDL);
411
412 /* === step 6: remove the last (third) host from the hash === */
413 host_hash_delete(host_uid_hash, clientid3, clientid3_len, MDL);
414
415 ATF_CHECK_MSG(host_hash_lookup(&check, host_uid_hash, clientid3,
416 clientid3_len, MDL) == 0,
417 "Host3 was not supposed to be in the uid_hash anymore.");
418 host_dereference(&check, MDL);
419
420
421 host_dereference(&host1, MDL);
422 host_dereference(&host2, MDL);
423 host_dereference(&host3, MDL);
424
425 /*
426 * No easy way to check if the host object were actually released.
427 * We could run it in valgrind and check for memory leaks.
428 */
429
430 #if defined (DEBUG_MEMORY_LEAKAGE) && defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
431 /* @todo: Should be called in cleanup */
432 free_everything ();
433 #endif
434 }
435
436 void lease_hash_test_3hosts_negative(unsigned char clientid1[], size_t clientid1_len,
437 unsigned char clientid2[], size_t clientid2_len,
438 unsigned char clientid3[], size_t clientid3_len) {
439
440 printf("Checking negative hash operation for 3 hosts: clientid1-len=%lu"
441 " clientid2-len=%lu clientid3-len=%lu\n",
442 clientid1_len, clientid2_len, clientid3_len);
443
444 dhcp_db_objects_setup ();
445 dhcp_common_objects_setup ();
446
447 /* check that there is actually zero hosts in the hash */
448 /* @todo: host_hash_for_each() */
449
450 struct host_decl *host1 = 0, *host2 = 0, *host3 = 0;
451 struct host_decl *check = 0;
452
453 /* === step 1: allocate hosts === */
454 ATF_CHECK_MSG(host_allocate(&host1, MDL) == ISC_R_SUCCESS,
455 "Failed to allocate host");
456 ATF_CHECK_MSG(host_allocate(&host2, MDL) == ISC_R_SUCCESS,
457 "Failed to allocate host");
458 ATF_CHECK_MSG(host_allocate(&host3, MDL) == ISC_R_SUCCESS,
459 "Failed to allocate host");
460
461 ATF_CHECK_MSG(host_new_hash(&host_uid_hash, HOST_HASH_SIZE, MDL) != 0,
462 "Unable to create new hash");
463
464 /* === step 2: add hosts to the hash === */
465 host_hash_add(host_uid_hash, clientid1, clientid1_len, host1, MDL);
466 host_hash_delete(host_uid_hash, clientid1, clientid1_len, MDL);
467 host_hash_add(host_uid_hash, clientid2, clientid2_len, host2, MDL);
468 host_hash_delete(host_uid_hash, clientid2, clientid2_len, MDL);
469 host_hash_delete(host_uid_hash, clientid2, clientid2_len, MDL);
470
471 host_hash_delete(host_uid_hash, clientid1, clientid1_len, MDL);
472 host_hash_add(host_uid_hash, clientid3, clientid3_len, host3, MDL);
473 host_hash_delete(host_uid_hash, clientid3, clientid3_len, MDL);
474
475 host_dereference(&host1, MDL);
476 host_dereference(&host2, MDL);
477 host_dereference(&host3, MDL);
478
479 /*
480 * No easy way to check if the host object were actually released.
481 * We could run it in valgrind and check for memory leaks.
482 */
483
484 #if defined (DEBUG_MEMORY_LEAKAGE) && defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT)
485 /* @todo: Should be called in cleanup */
486 free_everything ();
487 #endif
488 }
489
490
491
492 ATF_TC(lease_hash_basic_2hosts);
493
494 ATF_TC_HEAD(lease_hash_basic_2hosts, tc) {
495 atf_tc_set_md_var(tc, "descr", "Basic lease hash tests");
496 /*
497 * The following functions are tested:
498 * host_allocate(), host_new_hash(), buffer_allocate(), host_hash_lookup()
499 * host_hash_add(), host_hash_delete()
500 */
501 }
502
503 ATF_TC_BODY(lease_hash_basic_2hosts, tc) {
504
505 unsigned char clientid1[] = { 0x1, 0x2, 0x3 };
506 unsigned char clientid2[] = { 0xff, 0xfe };
507
508 lease_hash_test_2hosts(clientid1, sizeof(clientid1),
509 clientid2, sizeof(clientid2));
510 }
511
512
513 ATF_TC(lease_hash_string_2hosts);
514
515 ATF_TC_HEAD(lease_hash_string_2hosts, tc) {
516 atf_tc_set_md_var(tc, "descr", "string-based lease hash tests");
517 /*
518 * The following functions are tested:
519 * host_allocate(), host_new_hash(), buffer_allocate(), host_hash_lookup()
520 * host_hash_add(), host_hash_delete()
521 */
522 }
523
524 ATF_TC_BODY(lease_hash_string_2hosts, tc) {
525
526 unsigned char clientid1[] = "Alice";
527 unsigned char clientid2[] = "Bob";
528
529 lease_hash_test_2hosts(clientid1, 0, clientid2, 0);
530 }
531
532
533 ATF_TC(lease_hash_negative1);
534
535 ATF_TC_HEAD(lease_hash_negative1, tc) {
536 atf_tc_set_md_var(tc, "descr", "Negative tests for lease hash");
537 }
538
539 ATF_TC_BODY(lease_hash_negative1, tc) {
540
541 unsigned char clientid1[] = { 0x1 };
542 unsigned char clientid2[] = { 0x0 };
543
544 lease_hash_test_2hosts(clientid1, 0, clientid2, 1);
545 }
546
547
548 ATF_TC(lease_hash_negative2);
549
550 ATF_TC_HEAD(lease_hash_negative2, tc) {
551 atf_tc_set_md_var(tc, "descr", "Negative tests for lease hash");
552 }
553
554 ATF_TC_BODY(lease_hash_negative2, tc) {
555
556 unsigned char clientid1[] = { 0x0 };
557 unsigned char clientid2[] = { 0x0 };
558 unsigned char clientid3[] = { 0x0 };
559
560 lease_hash_test_3hosts_negative(clientid1, 1, clientid2, 0, clientid3, 1);
561 }
562
563
564
565 ATF_TC(lease_hash_string_3hosts);
566
567 ATF_TC_HEAD(lease_hash_string_3hosts, tc) {
568 atf_tc_set_md_var(tc, "descr", "string-based lease hash tests");
569 /*
570 * The following functions are tested:
571 * host_allocate(), host_new_hash(), buffer_allocate(), host_hash_lookup()
572 * host_hash_add(), host_hash_delete()
573 */
574 }
575
576 ATF_TC_BODY(lease_hash_string_3hosts, tc) {
577
578 unsigned char clientid1[] = "Alice";
579 unsigned char clientid2[] = "Bob";
580 unsigned char clientid3[] = "Charlie";
581
582 lease_hash_test_3hosts(clientid1, 0, clientid2, 0, clientid3, 0);
583 }
584
585
586 ATF_TC(lease_hash_basic_3hosts);
587
588 ATF_TC_HEAD(lease_hash_basic_3hosts, tc) {
589 atf_tc_set_md_var(tc, "descr", "Basic lease hash tests");
590 /*
591 * The following functions are tested:
592 * host_allocate(), host_new_hash(), buffer_allocate(), host_hash_lookup()
593 * host_hash_add(), host_hash_delete()
594 */
595 }
596
597 ATF_TC_BODY(lease_hash_basic_3hosts, tc) {
598
599 unsigned char clientid1[] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9 };
600 unsigned char clientid2[] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 };
601 unsigned char clientid3[] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 };
602
603 lease_hash_test_3hosts(clientid1, sizeof(clientid1),
604 clientid2, sizeof(clientid2),
605 clientid3, sizeof(clientid3));
606 }
607
608 /* this test is a direct reproduction of 29851 issue */
609 ATF_TC(uid_hash_rt29851);
610
611 ATF_TC_HEAD(uid_hash_rt29851, tc) {
612 atf_tc_set_md_var(tc, "descr", "Uid hash tests");
613
614 /* this test should last less than millisecond. If its execution
615 is longer than 3 second, we hit infinite loop. */
616 atf_tc_set_md_var(tc, "timeout", "3");
617 }
618
619 ATF_TC_BODY(uid_hash_rt29851, tc) {
620
621 unsigned char clientid1[] = { 0x0 };
622 unsigned char clientid2[] = { 0x0 };
623 unsigned char clientid3[] = { 0x0 };
624
625 int clientid1_len = 1;
626 int clientid2_len = 1;
627 int clientid3_len = 0;
628
629 struct lease *lease1 = 0, *lease2 = 0, *lease3 = 0;
630
631 dhcp_db_objects_setup ();
632 dhcp_common_objects_setup ();
633
634 ATF_CHECK(lease_id_new_hash(&lease_uid_hash, LEASE_HASH_SIZE, MDL));
635
636 ATF_CHECK(lease_allocate (&lease1, MDL) == ISC_R_SUCCESS);
637 ATF_CHECK(lease_allocate (&lease2, MDL) == ISC_R_SUCCESS);
638 ATF_CHECK(lease_allocate (&lease3, MDL) == ISC_R_SUCCESS);
639
640 lease1->uid = clientid1;
641 lease2->uid = clientid2;
642 lease3->uid = clientid3;
643
644 lease1->uid_len = clientid1_len;
645 lease2->uid_len = clientid2_len;
646 lease3->uid_len = clientid3_len;
647
648 uid_hash_add(lease1);
649 // uid_hash_delete(lease2); // not necessary for actual issue repro
650 uid_hash_add(lease3);
651
652 // lease2->uid_len = 0; // not necessary for actual issue repro
653 // uid_hash_delete(lease2); // not necessary for actual issue repro
654 // uid_hash_delete(lease3); // not necessary for actual issue repro
655 uid_hash_delete(lease1);
656
657 // lease2->uid_len = 1; // not necessary for actual issue repro
658 uid_hash_add(lease1);
659 uid_hash_delete(lease2);
660 }
661
662
663
664
665
666
667 ATF_TP_ADD_TCS(tp) {
668 ATF_TP_ADD_TC(tp, lease_hash_basic_2hosts);
669 ATF_TP_ADD_TC(tp, lease_hash_basic_3hosts);
670 ATF_TP_ADD_TC(tp, lease_hash_string_2hosts);
671 ATF_TP_ADD_TC(tp, lease_hash_string_3hosts);
672 ATF_TP_ADD_TC(tp, lease_hash_negative1);
673 ATF_TP_ADD_TC(tp, lease_hash_negative2);
674 ATF_TP_ADD_TC(tp, uid_hash_rt29851);
675 return (atf_no_error());
676 }