1 /* $NetBSD: test_alloc.c,v 1.1.1.3 2014/07/12 11:57:48 spz Exp $ */
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
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
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 */
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.
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");
51 atf_tc_fail("buffer_dereference() did not NULL-out buffer");
55 * Check an actual buffer.
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");
65 atf_tc_fail("buffer_dereference() did not NULL-out buffer");
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
) {
88 if (!buffer_allocate(&a
, 100, MDL
)) {
89 atf_tc_fail("failed on allocate 100 bytes");
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
107 * Confirm we work under normal circumstances.
110 if (!buffer_reference(&b
, a
, MDL
)) {
111 atf_tc_fail("buffer_reference() failed");
115 atf_tc_fail("incorrect pointer returned");
118 if (b
->refcnt
!= 2) {
119 atf_tc_fail("incorrect refcnt");
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
157 * @TODO: we should confirm we get an error message here.
160 if (buffer_dereference(&a
, MDL
)) {
161 atf_tc_fail("succeeded on an error input");
165 * Confirm we work under normal circumstances.
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");
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.
184 if (!buffer_allocate(&a
, 100, MDL
)) {
185 atf_tc_fail("failed on allocate");
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");
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
) {
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
));
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
);
236 atf_tc_fail("incorrect length");
239 if (a
.data
!= NULL
) {
240 atf_tc_fail("incorrect data");
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");
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
));
276 a
.data
= (const unsigned char *)str
;
280 * Forget and confirm we've forgotten.
282 data_string_forget(&a
, MDL
);
285 atf_tc_fail("incorrect length");
287 if (a
.data
!= NULL
) {
288 atf_tc_fail("incorrect data");
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
));
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");
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
));
361 a
.data
= (const unsigned char *)str
;
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");
386 data_string_forget(&b
, MDL
);
387 data_string_forget(&a
, MDL
);
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());