1272343Sngie/*	$NetBSD: t_cdb.c,v 1.1 2012/09/27 00:38:57 joerg Exp $	*/
2272343Sngie/*-
3272343Sngie * Copyright (c) 2012 The NetBSD Foundation, Inc.
4272343Sngie * All rights reserved.
5272343Sngie *
6272343Sngie * This code is derived from software contributed to The NetBSD Foundation
7272343Sngie * by Joerg Sonnenberger.
8272343Sngie *
9272343Sngie * Redistribution and use in source and binary forms, with or without
10272343Sngie * modification, are permitted provided that the following conditions
11272343Sngie * are met:
12272343Sngie *
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in
17272343Sngie *    the documentation and/or other materials provided with the
18272343Sngie *    distribution.
19272343Sngie *
20272343Sngie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22272343Sngie * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23272343Sngie * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24272343Sngie * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25272343Sngie * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26272343Sngie * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27272343Sngie * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28272343Sngie * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29272343Sngie * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30272343Sngie * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31272343Sngie * SUCH DAMAGE.
32272343Sngie */
33272343Sngie
34272343Sngie#include <sys/cdefs.h>
35272343Sngie__RCSID("$NetBSD: t_cdb.c,v 1.1 2012/09/27 00:38:57 joerg Exp $");
36272343Sngie
37272343Sngie#include <atf-c.h>
38272343Sngie#include <assert.h>
39272343Sngie#include <cdbr.h>
40272343Sngie#include <cdbw.h>
41272343Sngie#include <fcntl.h>
42272343Sngie#include <stdlib.h>
43272343Sngie#include <string.h>
44272343Sngie#include <unistd.h>
45272343Sngie
46272343Sngie#define	MAXKEYS	16384
47272343Sngie
48272343Sngiestatic const char database_name[] = "test.cdb";
49272343Sngie
50272343Sngieuint32_t keys[MAXKEYS];
51272343Sngie
52272343Sngiestatic int
53272343Sngiecmp_keys(const void *a_, const void *b_)
54272343Sngie{
55272343Sngie	uint32_t a = *(const uint32_t *)a_;
56272343Sngie	uint32_t b = *(const uint32_t *)b_;
57272343Sngie
58272343Sngie	return a > b ? 1 : (a < b ? 1 : 0);
59272343Sngie}
60272343Sngie
61272343Sngiestatic void
62272343Sngieinit_keys(size_t len)
63272343Sngie{
64272343Sngie	uint32_t sorted_keys[MAXKEYS];
65272343Sngie	size_t i;
66272343Sngie
67272343Sngie	assert(len <= MAXKEYS);
68272343Sngie
69272343Sngie	if (len == 0)
70272343Sngie		return;
71272343Sngie
72272343Sngie	do {
73272343Sngie		for (i = 0; i < len; ++i)
74272343Sngie			sorted_keys[i] = keys[i] = arc4random();
75272343Sngie
76272343Sngie		qsort(sorted_keys, len, sizeof(*sorted_keys), cmp_keys);
77272343Sngie		for (i = 1; i < len; ++i) {
78272343Sngie			if (sorted_keys[i - 1] == sorted_keys[i])
79272343Sngie				break;
80272343Sngie		}
81272343Sngie	} while (i != len);
82272343Sngie}
83272343Sngie
84272343Sngiestatic void
85272343Sngiewrite_database(size_t len)
86272343Sngie{
87272343Sngie	struct cdbw *db;
88272343Sngie	int fd;
89272343Sngie	size_t i;
90272343Sngie	uint32_t buf[2];
91272343Sngie
92272343Sngie	ATF_REQUIRE((db = cdbw_open()) != NULL);
93272343Sngie	ATF_REQUIRE((fd = creat(database_name, S_IRUSR|S_IWUSR)) != -1);
94272343Sngie	for (i = 0; i < len; ++i) {
95272343Sngie		buf[0] = i;
96272343Sngie		buf[1] = keys[i];
97272343Sngie		ATF_REQUIRE(cdbw_put(db, &keys[i], sizeof(keys[i]),
98272343Sngie		    buf, sizeof(buf)) == 0);
99272343Sngie	}
100272343Sngie	ATF_REQUIRE(cdbw_output(db, fd, "test database", arc4random) == 0);
101272343Sngie	cdbw_close(db);
102272343Sngie	ATF_REQUIRE(close(fd) == 0);
103272343Sngie}
104272343Sngie
105272343Sngiestatic void
106272343Sngiecheck_database(size_t len)
107272343Sngie{
108272343Sngie	struct cdbr *db;
109272343Sngie	size_t i, data_len;
110272343Sngie	const void *data;
111272343Sngie	uint32_t buf[2];
112272343Sngie
113272343Sngie	ATF_REQUIRE((db = cdbr_open(database_name, CDBR_DEFAULT)) != NULL);
114272343Sngie	ATF_REQUIRE_EQ(cdbr_entries(db), len);
115272343Sngie	for (i = 0; i < len; ++i) {
116272343Sngie		ATF_REQUIRE(cdbr_find(db, &keys[i], sizeof(keys[i]),
117272343Sngie		    &data, &data_len) != -1);
118272343Sngie		ATF_REQUIRE_EQ(data_len, sizeof(buf));
119272343Sngie		memcpy(buf, data, sizeof(buf));
120272343Sngie		ATF_REQUIRE_EQ(buf[0], i);
121272343Sngie		ATF_REQUIRE_EQ(buf[1], keys[i]);
122272343Sngie	}
123272343Sngie	cdbr_close(db);
124272343Sngie}
125272343Sngie
126272343SngieATF_TC_WITH_CLEANUP(cdb);
127272343Sngie
128272343SngieATF_TC_HEAD(cdb, tc)
129272343Sngie{
130272343Sngie
131272343Sngie	atf_tc_set_md_var(tc, "descr", "Test cdb(5) reading and writing");
132272343Sngie}
133272343Sngie
134272343SngieATF_TC_BODY(cdb, tc)
135272343Sngie{
136272343Sngie	size_t i, sizes[] = { 0, 16, 64, 1024, 2048 };
137272343Sngie	for (i = 0; i < __arraycount(sizes); ++i) {
138272343Sngie		init_keys(sizes[i]);
139272343Sngie		write_database(sizes[i]);
140272343Sngie		check_database(sizes[i]);
141272343Sngie		unlink(database_name);
142272343Sngie	}
143272343Sngie}
144272343Sngie
145272343SngieATF_TC_CLEANUP(cdb, tc)
146272343Sngie{
147272343Sngie
148272343Sngie	unlink(database_name);
149272343Sngie}
150272343Sngie
151272343SngieATF_TP_ADD_TCS(tp)
152272343Sngie{
153272343Sngie
154272343Sngie	ATF_TP_ADD_TC(tp, cdb);
155272343Sngie
156272343Sngie	return atf_no_error();
157272343Sngie}
158272343Sngie
159