Remove building with NOCRYPTO option
[minix.git] / external / bsd / dhcp / dist / common / tests / test_alloc.c
blob1385632c02c36d651866b523601bb6c812be3d8e
1 /* $NetBSD: test_alloc.c,v 1.1.1.3 2014/07/12 11:57:48 spz Exp $ */
2 /*
3 * Copyright (c) 2007,2009,2012 by Internet Systems Consortium, Inc. ("ISC")
5 * We test the functions provided in alloc.c here. These are very
6 * basic functions, and it is very important that they work correctly.
8 * You can see two different styles of testing:
10 * - In the first, we have a single test for each function that tests
11 * all of the possible ways it can operate. (This is the case for
12 * the buffer tests.)
14 * - In the second, we have a separate test for each of the ways a
15 * function can operate. (This is the case for the data_string
16 * tests.)
18 * The advantage of a single test per function is that you have fewer
19 * tests, and less duplicated and extra code. The advantage of having
20 * a separate test is that each test is simpler. Plus if you need to
21 * allow certain tests to fail for some reason (known bugs that are
22 * hard to fix for example), then
25 /** @TODO: dmalloc() test */
27 #include "config.h"
28 #include <atf-c.h>
29 #include "dhcpd.h"
31 ATF_TC(buffer_allocate);
33 ATF_TC_HEAD(buffer_allocate, tc) {
34 atf_tc_set_md_var(tc, "descr", "buffer_allocate basic test");
37 ATF_TC_BODY(buffer_allocate, tc) {
38 struct buffer *buf = 0;
41 * Check a 0-length buffer.
43 buf = NULL;
44 if (!buffer_allocate(&buf, 0, MDL)) {
45 atf_tc_fail("failed on 0-len buffer");
47 if (!buffer_dereference(&buf, MDL)) {
48 atf_tc_fail("buffer_dereference() failed");
50 if (buf != NULL) {
51 atf_tc_fail("buffer_dereference() did not NULL-out buffer");
55 * Check an actual buffer.
57 buf = NULL;
58 if (!buffer_allocate(&buf, 100, MDL)) {
59 atf_tc_fail("failed on allocate 100 bytes\n");
61 if (!buffer_dereference(&buf, MDL)) {
62 atf_tc_fail("buffer_dereference() failed");
64 if (buf != NULL) {
65 atf_tc_fail("buffer_dereference() did not NULL-out buffer");
69 * Okay, we're happy.
71 atf_tc_pass();
74 ATF_TC(buffer_reference);
76 ATF_TC_HEAD(buffer_reference, tc) {
77 atf_tc_set_md_var(tc, "descr", "buffer_reference basic test");
80 ATF_TC_BODY(buffer_reference, tc) {
82 struct buffer *a, *b;
85 * Create a buffer.
87 a = NULL;
88 if (!buffer_allocate(&a, 100, MDL)) {
89 atf_tc_fail("failed on allocate 100 bytes");
92 /**
93 * Confirm buffer_reference() doesn't work if we pass in NULL.
95 * @TODO: we should confirm we get an error message here.
97 if (buffer_reference(NULL, a, MDL)) {
98 atf_tc_fail("succeeded on an error input");
102 * @TODO: we should confirm we get an error message if we pass
103 * a non-NULL target.
107 * Confirm we work under normal circumstances.
109 b = NULL;
110 if (!buffer_reference(&b, a, MDL)) {
111 atf_tc_fail("buffer_reference() failed");
114 if (b != a) {
115 atf_tc_fail("incorrect pointer returned");
118 if (b->refcnt != 2) {
119 atf_tc_fail("incorrect refcnt");
123 * Clean up.
125 if (!buffer_dereference(&b, MDL)) {
126 atf_tc_fail("buffer_dereference() failed");
128 if (!buffer_dereference(&a, MDL)) {
129 atf_tc_fail("buffer_dereference() failed");
135 ATF_TC(buffer_dereference);
137 ATF_TC_HEAD(buffer_dereference, tc) {
138 atf_tc_set_md_var(tc, "descr", "buffer_dereference basic test");
141 ATF_TC_BODY(buffer_dereference, tc) {
142 struct buffer *a, *b;
145 * Confirm buffer_dereference() doesn't work if we pass in NULL.
147 * TODO: we should confirm we get an error message here.
149 if (buffer_dereference(NULL, MDL)) {
150 atf_tc_fail("succeeded on an error input");
154 * Confirm buffer_dereference() doesn't work if we pass in
155 * a pointer to NULL.
157 * @TODO: we should confirm we get an error message here.
159 a = NULL;
160 if (buffer_dereference(&a, MDL)) {
161 atf_tc_fail("succeeded on an error input");
165 * Confirm we work under normal circumstances.
167 a = NULL;
168 if (!buffer_allocate(&a, 100, MDL)) {
169 atf_tc_fail("failed on allocate");
171 if (!buffer_dereference(&a, MDL)) {
172 atf_tc_fail("buffer_dereference() failed");
174 if (a != NULL) {
175 atf_tc_fail("non-null buffer after buffer_dereference()");
179 * Confirm we get an error from negative refcnt.
181 * @TODO: we should confirm we get an error message here.
183 a = NULL;
184 if (!buffer_allocate(&a, 100, MDL)) {
185 atf_tc_fail("failed on allocate");
187 b = NULL;
188 if (!buffer_reference(&b, a, MDL)) {
189 atf_tc_fail("buffer_reference() failed");
191 a->refcnt = 0; /* purposely set to invalid value */
192 if (buffer_dereference(&a, MDL)) {
193 atf_tc_fail("buffer_dereference() succeeded on error input");
195 a->refcnt = 2;
196 if (!buffer_dereference(&b, MDL)) {
197 atf_tc_fail("buffer_dereference() failed");
199 if (!buffer_dereference(&a, MDL)) {
200 atf_tc_fail("buffer_dereference() failed");
204 ATF_TC(data_string_forget);
206 ATF_TC_HEAD(data_string_forget, tc) {
207 atf_tc_set_md_var(tc, "descr", "data_string_forget basic test");
210 ATF_TC_BODY(data_string_forget, tc) {
211 struct buffer *buf;
212 struct data_string a;
213 const char *str = "Lorem ipsum dolor sit amet turpis duis.";
216 * Create the string we want to forget.
218 memset(&a, 0, sizeof(a));
219 a.len = strlen(str);
220 buf = NULL;
221 if (!buffer_allocate(&buf, a.len, MDL)) {
222 atf_tc_fail("out of memory");
224 if (!buffer_reference(&a.buffer, buf, MDL)) {
225 atf_tc_fail("buffer_reference() failed");
227 a.data = a.buffer->data;
228 memcpy(a.buffer->data, str, a.len);
231 * Forget and confirm we've forgotten.
233 data_string_forget(&a, MDL);
235 if (a.len != 0) {
236 atf_tc_fail("incorrect length");
239 if (a.data != NULL) {
240 atf_tc_fail("incorrect data");
242 if (a.terminated) {
243 atf_tc_fail("incorrect terminated");
245 if (a.buffer != NULL) {
246 atf_tc_fail("incorrect buffer");
248 if (buf->refcnt != 1) {
249 atf_tc_fail("too many references to buf");
253 * Clean up buffer.
255 if (!buffer_dereference(&buf, MDL)) {
256 atf_tc_fail("buffer_reference() failed");
260 ATF_TC(data_string_forget_nobuf);
262 ATF_TC_HEAD(data_string_forget_nobuf, tc) {
263 atf_tc_set_md_var(tc, "descr", "data_string_forget test, "
264 "data_string without buffer");
267 ATF_TC_BODY(data_string_forget_nobuf, tc) {
268 struct data_string a;
269 const char *str = "Lorem ipsum dolor sit amet massa nunc.";
272 * Create the string we want to forget.
274 memset(&a, 0, sizeof(a));
275 a.len = strlen(str);
276 a.data = (const unsigned char *)str;
277 a.terminated = 1;
280 * Forget and confirm we've forgotten.
282 data_string_forget(&a, MDL);
284 if (a.len != 0) {
285 atf_tc_fail("incorrect length");
287 if (a.data != NULL) {
288 atf_tc_fail("incorrect data");
290 if (a.terminated) {
291 atf_tc_fail("incorrect terminated");
293 if (a.buffer != NULL) {
294 atf_tc_fail("incorrect buffer");
298 ATF_TC(data_string_copy);
300 ATF_TC_HEAD(data_string_copy, tc) {
301 atf_tc_set_md_var(tc, "descr", "data_string_copy basic test");
304 ATF_TC_BODY(data_string_copy, tc) {
305 struct data_string a, b;
306 const char *str = "Lorem ipsum dolor sit amet orci aliquam.";
309 * Create the string we want to copy.
311 memset(&a, 0, sizeof(a));
312 a.len = strlen(str);
313 if (!buffer_allocate(&a.buffer, a.len, MDL)) {
314 atf_tc_fail("out of memory");
316 a.data = a.buffer->data;
317 memcpy(a.buffer->data, str, a.len);
320 * Copy the string, and confirm it works.
322 memset(&b, 0, sizeof(b));
323 data_string_copy(&b, &a, MDL);
325 if (b.len != a.len) {
326 atf_tc_fail("incorrect length");
328 if (b.data != a.data) {
329 atf_tc_fail("incorrect data");
331 if (b.terminated != a.terminated) {
332 atf_tc_fail("incorrect terminated");
334 if (b.buffer != a.buffer) {
335 atf_tc_fail("incorrect buffer");
339 * Clean up.
341 data_string_forget(&b, MDL);
342 data_string_forget(&a, MDL);
345 ATF_TC(data_string_copy_nobuf);
347 ATF_TC_HEAD(data_string_copy_nobuf, tc) {
348 atf_tc_set_md_var(tc, "descr", "data_string_copy test, "
349 "data_string without buffer");
352 ATF_TC_BODY(data_string_copy_nobuf, tc) {
353 struct data_string a, b;
354 const char *str = "Lorem ipsum dolor sit amet cras amet.";
357 * Create the string we want to copy.
359 memset(&a, 0, sizeof(a));
360 a.len = strlen(str);
361 a.data = (const unsigned char *)str;
362 a.terminated = 1;
365 * Copy the string, and confirm it works.
367 memset(&b, 0, sizeof(b));
368 data_string_copy(&b, &a, MDL);
370 if (b.len != a.len) {
371 atf_tc_fail("incorrect length");
373 if (b.data != a.data) {
374 atf_tc_fail("incorrect data");
376 if (b.terminated != a.terminated) {
377 atf_tc_fail("incorrect terminated");
379 if (b.buffer != a.buffer) {
380 atf_tc_fail("incorrect buffer");
384 * Clean up.
386 data_string_forget(&b, MDL);
387 data_string_forget(&a, MDL);
391 ATF_TP_ADD_TCS(tp)
393 ATF_TP_ADD_TC(tp, buffer_allocate);
394 ATF_TP_ADD_TC(tp, buffer_reference);
395 ATF_TP_ADD_TC(tp, buffer_dereference);
396 ATF_TP_ADD_TC(tp, data_string_forget);
397 ATF_TP_ADD_TC(tp, data_string_forget_nobuf);
398 ATF_TP_ADD_TC(tp, data_string_copy);
399 ATF_TP_ADD_TC(tp, data_string_copy_nobuf);
401 return (atf_no_error());