xref: /freebsd/contrib/netbsd-tests/lib/libc/t_cdb.c (revision a0ee8cc6)
1 /*	$NetBSD: t_cdb.c,v 1.1 2012/09/27 00:38:57 joerg Exp $	*/
2 /*-
3  * Copyright (c) 2012 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Joerg Sonnenberger.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: t_cdb.c,v 1.1 2012/09/27 00:38:57 joerg Exp $");
36 
37 #include <atf-c.h>
38 #include <assert.h>
39 #include <cdbr.h>
40 #include <cdbw.h>
41 #include <fcntl.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #define	MAXKEYS	16384
47 
48 static const char database_name[] = "test.cdb";
49 
50 uint32_t keys[MAXKEYS];
51 
52 static int
53 cmp_keys(const void *a_, const void *b_)
54 {
55 	uint32_t a = *(const uint32_t *)a_;
56 	uint32_t b = *(const uint32_t *)b_;
57 
58 	return a > b ? 1 : (a < b ? 1 : 0);
59 }
60 
61 static void
62 init_keys(size_t len)
63 {
64 	uint32_t sorted_keys[MAXKEYS];
65 	size_t i;
66 
67 	assert(len <= MAXKEYS);
68 
69 	if (len == 0)
70 		return;
71 
72 	do {
73 		for (i = 0; i < len; ++i)
74 			sorted_keys[i] = keys[i] = arc4random();
75 
76 		qsort(sorted_keys, len, sizeof(*sorted_keys), cmp_keys);
77 		for (i = 1; i < len; ++i) {
78 			if (sorted_keys[i - 1] == sorted_keys[i])
79 				break;
80 		}
81 	} while (i != len);
82 }
83 
84 static void
85 write_database(size_t len)
86 {
87 	struct cdbw *db;
88 	int fd;
89 	size_t i;
90 	uint32_t buf[2];
91 
92 	ATF_REQUIRE((db = cdbw_open()) != NULL);
93 	ATF_REQUIRE((fd = creat(database_name, S_IRUSR|S_IWUSR)) != -1);
94 	for (i = 0; i < len; ++i) {
95 		buf[0] = i;
96 		buf[1] = keys[i];
97 		ATF_REQUIRE(cdbw_put(db, &keys[i], sizeof(keys[i]),
98 		    buf, sizeof(buf)) == 0);
99 	}
100 	ATF_REQUIRE(cdbw_output(db, fd, "test database", arc4random) == 0);
101 	cdbw_close(db);
102 	ATF_REQUIRE(close(fd) == 0);
103 }
104 
105 static void
106 check_database(size_t len)
107 {
108 	struct cdbr *db;
109 	size_t i, data_len;
110 	const void *data;
111 	uint32_t buf[2];
112 
113 	ATF_REQUIRE((db = cdbr_open(database_name, CDBR_DEFAULT)) != NULL);
114 	ATF_REQUIRE_EQ(cdbr_entries(db), len);
115 	for (i = 0; i < len; ++i) {
116 		ATF_REQUIRE(cdbr_find(db, &keys[i], sizeof(keys[i]),
117 		    &data, &data_len) != -1);
118 		ATF_REQUIRE_EQ(data_len, sizeof(buf));
119 		memcpy(buf, data, sizeof(buf));
120 		ATF_REQUIRE_EQ(buf[0], i);
121 		ATF_REQUIRE_EQ(buf[1], keys[i]);
122 	}
123 	cdbr_close(db);
124 }
125 
126 ATF_TC_WITH_CLEANUP(cdb);
127 
128 ATF_TC_HEAD(cdb, tc)
129 {
130 
131 	atf_tc_set_md_var(tc, "descr", "Test cdb(5) reading and writing");
132 }
133 
134 ATF_TC_BODY(cdb, tc)
135 {
136 	size_t i, sizes[] = { 0, 16, 64, 1024, 2048 };
137 	for (i = 0; i < __arraycount(sizes); ++i) {
138 		init_keys(sizes[i]);
139 		write_database(sizes[i]);
140 		check_database(sizes[i]);
141 		unlink(database_name);
142 	}
143 }
144 
145 ATF_TC_CLEANUP(cdb, tc)
146 {
147 
148 	unlink(database_name);
149 }
150 
151 ATF_TP_ADD_TCS(tp)
152 {
153 
154 	ATF_TP_ADD_TC(tp, cdb);
155 
156 	return atf_no_error();
157 }
158 
159