1 /* $NetBSD: t_cdb.c,v 1.2 2017/01/10 22:24:29 christos Exp $ */
3 * Copyright (c) 2012 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Joerg Sonnenberger.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: t_cdb.c,v 1.2 2017/01/10 22:24:29 christos Exp $");
51 static const char database_name[] = "test.cdb";
53 uint32_t keys[MAXKEYS];
56 cmp_keys(const void *a_, const void *b_)
58 uint32_t a = *(const uint32_t *)a_;
59 uint32_t b = *(const uint32_t *)b_;
61 return a > b ? 1 : (a < b ? 1 : 0);
67 uint32_t sorted_keys[MAXKEYS];
70 assert(len <= MAXKEYS);
76 for (i = 0; i < len; ++i)
77 sorted_keys[i] = keys[i] = arc4random();
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])
88 write_database(size_t len)
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) {
100 ATF_REQUIRE(cdbw_put(db, &keys[i], sizeof(keys[i]),
101 buf, sizeof(buf)) == 0);
103 ATF_REQUIRE(cdbw_output(db, fd, "test database", arc4random) == 0);
105 ATF_REQUIRE(close(fd) == 0);
109 check_database(size_t len)
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]);
129 ATF_TC_WITH_CLEANUP(cdb);
134 atf_tc_set_md_var(tc, "descr", "Test cdb(5) reading and writing");
139 size_t i, sizes[] = { 0, 16, 64, 1024, 2048 };
140 for (i = 0; i < __arraycount(sizes); ++i) {
142 write_database(sizes[i]);
143 check_database(sizes[i]);
144 unlink(database_name);
148 ATF_TC_CLEANUP(cdb, tc)
151 unlink(database_name);
157 ATF_TP_ADD_TC(tp, cdb);
159 return atf_no_error();