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