aboutsummaryrefslogtreecommitdiff
path: root/common/tests/test_alloc.c
blob: c0e1b9af7315d50fb4d9f8ee45f27e4f355ec200 (plain)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * We test the functions provided in alloc.c here. These are very 
 * basic functions, and it is very important that they work correctly.
 *
 * You can see two different styles of testing:
 *
 * - In the first, we have a single test for each function that tests
 *   all of the possible ways it can operate. (This is the case for
 *   the buffer tests.)
 *
 * - In the second, we have a separate test for each of the ways a
 *   function can operate. (This is the case for the data_string
 *   tests.)
 *
 * The advantage of a single test per function is that you have fewer
 * tests, and less duplicated and extra code. The advantage of having
 * a separate test is that each test is simpler. Plus if you need to
 * allow certain tests to fail for some reason (known bugs that are
 * hard to fix for example), then 
 */

/* TODO: dmalloc() test */

#include "config.h"
#include "t_api.h"

#include "dhcpd.h"

static void test_buffer_allocate(void);
static void test_buffer_reference(void);
static void test_buffer_dereference(void);
static void test_data_string_forget(void);
static void test_data_string_forget_nobuf(void);
static void test_data_string_copy(void);
static void test_data_string_copy_nobuf(void);

/*
 * T_testlist is a list of tests that are invoked.
 */
testspec_t T_testlist[] = {
	{ test_buffer_allocate,
		"buffer_allocate()" },
	{ test_buffer_reference,
		"buffer_reference()" },
	{ test_buffer_dereference,
		"buffer_dereference()" },
	{ test_data_string_forget,
		"data_string_forget()" },
	{ test_data_string_forget_nobuf,
		"data_string_forget(), no buffer" },
	{ test_data_string_copy,
		"data_string_copy()" },
	{ test_data_string_copy_nobuf,
		"data_string_copy(), no buffer" },
	{ NULL,	NULL }
};

static void
test_buffer_allocate(void) {
	static const char *test_desc = 
		"buffer_allocate basic test";

	struct buffer *buf;

	t_assert("buffer_allocate", 1, T_REQUIRED, "%s", test_desc);

	/*
	 * Check a 0-length buffer.
	 */
	buf = NULL;
	if (!buffer_allocate(&buf, 0, MDL)) {
		t_info("failed on 0-len buffer\n");
		t_result(T_FAIL);
		return;
	}
	if (!buffer_dereference(&buf, MDL)) {
		t_info("buffer_dereference() failed\n");
		t_result(T_FAIL);
		return;
	}
	if (buf != NULL) {
		t_info("buffer_dereference() did not NULL-out buffer\n");
		t_result(T_FAIL);
		return;
	}

	/*
	 * Check an actual buffer.
	 */
	buf = NULL;
	if (!buffer_allocate(&buf, 100, MDL)) {
		t_info("failed on allocate\n");
		t_result(T_FAIL);
		return;
	}
	if (!buffer_dereference(&buf, MDL)) {
		t_info("buffer_dereference() failed\n");
		t_result(T_FAIL);
		return;
	}
	if (buf != NULL) {
		t_info("buffer_dereference() did not NULL-out buffer\n");
		t_result(T_FAIL);
		return;
	}

	/*
	 * Okay, we're happy.
	 */
	t_result(T_PASS);
}

static void
test_buffer_reference(void) {
	static const char *test_desc = 
		"buffer_reference basic test";
	int result = T_PASS;

	struct buffer *a, *b;

	t_assert("buffer_reference", 1, T_REQUIRED, "%s", test_desc);

	/*
	 * Create a buffer.
	 */
	a = NULL;
	if (!buffer_allocate(&a, 100, MDL)) {
		t_info("failed on allocate\n");
		t_result(T_FAIL);
		return;
	}

	/*
	 * Confirm buffer_reference() doesn't work if we pass in NULL.
	 *
	 * TODO: we should confirm we get an error message here.
	 */
	if (buffer_reference(NULL, a, MDL)) {
		t_info("succeeded on an error input\n");
		t_result(T_FAIL);
		return;
	}

	/*
	 * TODO: we should confirm we get an error message if we pass
	 *       a non-NULL target.
	 */

	/*
	 * Confirm we work under normal circumstances.
	 */
	b = NULL;
	if (!buffer_reference(&b, a, MDL)) {
		t_info("buffer_reference() failed\n");
		t_result(T_FAIL);
		return;
	}

	if (b != a) {
		t_info("incorrect pointer\n");
		result = T_FAIL;
	}
	if (b->refcnt != 2) {
		t_info("incorrect refcnt\n");
		result = T_FAIL;
	}

	/*
	 * Clean up.
	 */
	if (!buffer_dereference(&b, MDL)) {
		t_info("buffer_dereference() failed\n");
		t_result(T_FAIL);
		return;
	}
	if (!buffer_dereference(&a, MDL)) {
		t_info("buffer_dereference() failed\n");
		t_result(T_FAIL);
		return;
	}

	t_result(result);
}

static void
test_buffer_dereference(void) {
	static const char *test_desc = 
		"buffer_dereference basic test";

	struct buffer *a, *b;

	t_assert("buffer_dereference", 1, T_REQUIRED, "%s", test_desc);

	/*
	 * Confirm buffer_dereference() doesn't work if we pass in NULL.
	 *
	 * TODO: we should confirm we get an error message here.
	 */
	if (buffer_dereference(NULL, MDL)) {
		t_info("succeeded on an error input\n");
		t_result(T_FAIL);
		return;
	}

	/*
	 * Confirm buffer_dereference() doesn't work if we pass in 
	 * a pointer to NULL.
	 *
	 * TODO: we should confirm we get an error message here.
	 */
	a = NULL;
	if (buffer_dereference(&a, MDL)) {
		t_info("succeeded on an error input\n");
		t_result(T_FAIL);
		return;
	}

	/*
	 * Confirm we work under normal circumstances.
	 */
	a = NULL;
	if (!buffer_allocate(&a, 100, MDL)) {
		t_info("failed on allocate\n");
		t_result(T_FAIL);
		return;
	}
	if (!buffer_dereference(&a, MDL)) {
		t_info("buffer_dereference() failed\n");
		t_result(T_FAIL);
		return;
	}
	if (a != NULL) {
		t_info("non-null buffer after buffer_dereference()\n");
		t_result(T_FAIL);
		return;
	}

	/*
	 * Confirm we get an error from negative refcnt.
	 *
	 * TODO: we should confirm we get an error message here.
	 */
	a = NULL;
	if (!buffer_allocate(&a, 100, MDL)) {
		t_info("failed on allocate\n");
		t_result(T_FAIL);
		return;
	}
	b = NULL;
	if (!buffer_reference(&b, a, MDL)) {
		t_info("buffer_reference() failed\n");
		t_result(T_FAIL);
		return;
	}
	a->refcnt = 0;	/* purposely set to invalid value */
	if (buffer_dereference(&a, MDL)) {
		t_info("buffer_dereference() succeeded on error input\n");
		t_result(T_FAIL);
		return;
	}
	a->refcnt = 2;
	if (!buffer_dereference(&b, MDL)) {
		t_info("buffer_dereference() failed\n");
		t_result(T_FAIL);
		return;
	}
	if (!buffer_dereference(&a, MDL)) {
		t_info("buffer_dereference() failed\n");
		t_result(T_FAIL);
		return;
	}

	t_result(T_PASS);
}

static void
test_data_string_forget(void) {
	static const char *test_desc = 
		"data_string_forget basic test";
	int result = T_PASS;

	struct buffer *buf;
	struct data_string a;
	const char *str = "Lorem ipsum dolor sit amet turpis duis.";

	t_assert("data_string_forget", 1, T_REQUIRED, "%s", test_desc);

	/* 
	 * Create the string we want to forget.
	 */
	memset(&a, 0, sizeof(a));
	a.len = strlen(str);
	buf = NULL;
	if (!buffer_allocate(&buf, a.len, MDL)) {
		t_info("out of memory\n");
		t_result(T_FAIL);
		return;
	}
	if (!buffer_reference(&a.buffer, buf, MDL)) {
		t_info("buffer_reference() failed\n");
		t_result(T_FAIL);
		return;
	}
	a.data = a.buffer->data;
	memcpy(a.buffer->data, str, a.len);

	/*
	 * Forget and confirm we've forgotten.
	 */
	data_string_forget(&a, MDL);

	if (a.len != 0) {
		t_info("incorrect length\n");
		result = T_FAIL;
	}
	if (a.data != NULL) {
		t_info("incorrect data\n");
		result = T_FAIL;
	}
	if (a.terminated) {
		t_info("incorrect terminated\n");
		result = T_FAIL;
	}
	if (a.buffer != NULL) {
		t_info("incorrect buffer\n");
		result = T_FAIL;
	}
	if (buf->refcnt != 1) {
		t_info("too many references to buf\n");
		result = T_FAIL;
	}

	/*
	 * Clean up buffer.
	 */
	if (!buffer_dereference(&buf, MDL)) {
		t_info("buffer_reference() failed\n");
		t_result(T_FAIL);
		return;
	}

	t_result(result);
}

static void
test_data_string_forget_nobuf(void) {
	static const char *test_desc = 
		"data_string_forget test, data_string without buffer";
	int result = T_PASS;

	struct data_string a;
	const char *str = "Lorem ipsum dolor sit amet massa nunc.";

	t_assert("data_string_forget, no buffer", 1, T_REQUIRED, "%s", test_desc);

	/* 
	 * Create the string we want to forget.
	 */
	memset(&a, 0, sizeof(a));
	a.len = strlen(str);
	a.data = (const unsigned char *)str;
	a.terminated = 1;

	/*
	 * Forget and confirm we've forgotten.
	 */
	data_string_forget(&a, MDL);

	if (a.len != 0) {
		t_info("incorrect length\n");
		result = T_FAIL;
	}
	if (a.data != NULL) {
		t_info("incorrect data\n");
		result = T_FAIL;
	}
	if (a.terminated) {
		t_info("incorrect terminated\n");
		result = T_FAIL;
	}
	if (a.buffer != NULL) {
		t_info("incorrect buffer\n");
		result = T_FAIL;
	}

	t_result(result);
}

static void
test_data_string_copy(void) {
	static const char *test_desc = 
		"data_string_copy basic test";
	int result = T_PASS;

	struct data_string a, b;
	const char *str = "Lorem ipsum dolor sit amet orci aliquam.";

	t_assert("data_string_copy", 1, T_REQUIRED, "%s", test_desc);


	/* 
	 * Create the string we want to copy.
	 */
	memset(&a, 0, sizeof(a));
	a.len = strlen(str);
	if (!buffer_allocate(&a.buffer, a.len, MDL)) {
		t_info("out of memory\n");
		t_result(T_FAIL);
		return;
	}
	a.data = a.buffer->data;
	memcpy(a.buffer->data, str, a.len);

	/*
	 * Copy the string, and confirm it works.
	 */
	memset(&b, 0, sizeof(b));
	data_string_copy(&b, &a, MDL);

	if (b.len != a.len) {
		t_info("incorrect length\n");
		result = T_FAIL;
	}
	if (b.data != a.data) {
		t_info("incorrect data\n");
		result = T_FAIL;
	}
	if (b.terminated != a.terminated) {
		t_info("incorrect terminated\n");
		result = T_FAIL;
	}
	if (b.buffer != a.buffer) {
		t_info("incorrect buffer\n");
		result = T_FAIL;
	}

	/*
	 * Clean up.
	 */
	data_string_forget(&b, MDL);
	data_string_forget(&a, MDL);

	t_result(result);
}

static void
test_data_string_copy_nobuf(void) {
	static const char *test_desc = 
		"data_string_copy test, data_string without buffer";
	int result = T_PASS;

	struct data_string a, b;
	const char *str = "Lorem ipsum dolor sit amet cras amet.";

	t_assert("data_string_copy, no buffer", 1, T_REQUIRED, "%s",
		 test_desc);


	/* 
	 * Create the string we want to copy.
	 */
	memset(&a, 0, sizeof(a));
	a.len = strlen(str);
	a.data = (const unsigned char *)str;
	a.terminated = 1;

	/*
	 * Copy the string, and confirm it works.
	 */
	memset(&b, 0, sizeof(b));
	data_string_copy(&b, &a, MDL);

	if (b.len != a.len) {
		t_info("incorrect length\n");
		result = T_FAIL;
	}
	if (b.data != a.data) {
		t_info("incorrect data\n");
		result = T_FAIL;
	}
	if (b.terminated != a.terminated) {
		t_info("incorrect terminated\n");
		result = T_FAIL;
	}
	if (b.buffer != a.buffer) {
		t_info("incorrect buffer\n");
		result = T_FAIL;
	}

	/*
	 * Clean up.
	 */
	data_string_forget(&b, MDL);
	data_string_forget(&a, MDL);

	t_result(result);
}