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