xref: /freebsd/contrib/netbsd-tests/lib/libc/t_cdb.c (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1 /*	$NetBSD: t_cdb.c,v 1.2 2017/01/10 22:24:29 christos 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.2 2017/01/10 22:24:29 christos Exp $");
36 
37 #include <atf-c.h>
38 
39 #include <sys/stat.h>
40 
41 #include <assert.h>
42 #include <cdbr.h>
43 #include <cdbw.h>
44 #include <fcntl.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #define	MAXKEYS	16384
50 
51 static const char database_name[] = "test.cdb";
52 
53 uint32_t keys[MAXKEYS];
54 
55 static int
56 cmp_keys(const void *a_, const void *b_)
57 {
58 	uint32_t a = *(const uint32_t *)a_;
59 	uint32_t b = *(const uint32_t *)b_;
60 
61 	return a > b ? 1 : (a < b ? 1 : 0);
62 }
63 
64 static void
65 init_keys(size_t len)
66 {
67 	uint32_t sorted_keys[MAXKEYS];
68 	size_t i;
69 
70 	assert(len <= MAXKEYS);
71 
72 	if (len == 0)
73 		return;
74 
75 	do {
76 		for (i = 0; i < len; ++i)
77 			sorted_keys[i] = keys[i] = arc4random();
78 
79 		qsort(sorted_keys, len, sizeof(*sorted_keys), cmp_keys);
80 		for (i = 1; i < len; ++i) {
81 			if (sorted_keys[i - 1] == sorted_keys[i])
82 				break;
83 		}
84 	} while (i != len);
85 }
86 
87 static void
88 write_database(size_t len)
89 {
90 	struct cdbw *db;
91 	int fd;
92 	size_t i;
93 	uint32_t buf[2];
94 
95 	ATF_REQUIRE((db = cdbw_open()) != NULL);
96 	ATF_REQUIRE((fd = creat(database_name, S_IRUSR|S_IWUSR)) != -1);
97 	for (i = 0; i < len; ++i) {
98 		buf[0] = i;
99 		buf[1] = keys[i];
100 		ATF_REQUIRE(cdbw_put(db, &keys[i], sizeof(keys[i]),
101 		    buf, sizeof(buf)) == 0);
102 	}
103 	ATF_REQUIRE(cdbw_output(db, fd, "test database", arc4random) == 0);
104 	cdbw_close(db);
105 	ATF_REQUIRE(close(fd) == 0);
106 }
107 
108 static void
109 check_database(size_t len)
110 {
111 	struct cdbr *db;
112 	size_t i, data_len;
113 	const void *data;
114 	uint32_t buf[2];
115 
116 	ATF_REQUIRE((db = cdbr_open(database_name, CDBR_DEFAULT)) != NULL);
117 	ATF_REQUIRE_EQ(cdbr_entries(db), len);
118 	for (i = 0; i < len; ++i) {
119 		ATF_REQUIRE(cdbr_find(db, &keys[i], sizeof(keys[i]),
120 		    &data, &data_len) != -1);
121 		ATF_REQUIRE_EQ(data_len, sizeof(buf));
122 		memcpy(buf, data, sizeof(buf));
123 		ATF_REQUIRE_EQ(buf[0], i);
124 		ATF_REQUIRE_EQ(buf[1], keys[i]);
125 	}
126 	cdbr_close(db);
127 }
128 
129 ATF_TC_WITH_CLEANUP(cdb);
130 
131 ATF_TC_HEAD(cdb, tc)
132 {
133 
134 	atf_tc_set_md_var(tc, "descr", "Test cdb(5) reading and writing");
135 }
136 
137 ATF_TC_BODY(cdb, tc)
138 {
139 	size_t i, sizes[] = { 0, 16, 64, 1024, 2048 };
140 	for (i = 0; i < __arraycount(sizes); ++i) {
141 		init_keys(sizes[i]);
142 		write_database(sizes[i]);
143 		check_database(sizes[i]);
144 		unlink(database_name);
145 	}
146 }
147 
148 ATF_TC_CLEANUP(cdb, tc)
149 {
150 
151 	unlink(database_name);
152 }
153 
154 ATF_TP_ADD_TCS(tp)
155 {
156 
157 	ATF_TP_ADD_TC(tp, cdb);
158 
159 	return atf_no_error();
160 }
161 
162