1313535Sngie/*	$NetBSD: t_cdb.c,v 1.2 2017/01/10 22:24:29 christos 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>
35313535Sngie__RCSID("$NetBSD: t_cdb.c,v 1.2 2017/01/10 22:24:29 christos Exp $");
36272343Sngie
37272343Sngie#include <atf-c.h>
38313535Sngie
39313535Sngie#include <sys/stat.h>
40313535Sngie
41272343Sngie#include <assert.h>
42272343Sngie#include <cdbr.h>
43272343Sngie#include <cdbw.h>
44272343Sngie#include <fcntl.h>
45272343Sngie#include <stdlib.h>
46272343Sngie#include <string.h>
47272343Sngie#include <unistd.h>
48272343Sngie
49272343Sngie#define	MAXKEYS	16384
50272343Sngie
51272343Sngiestatic const char database_name[] = "test.cdb";
52272343Sngie
53272343Sngieuint32_t keys[MAXKEYS];
54272343Sngie
55272343Sngiestatic int
56272343Sngiecmp_keys(const void *a_, const void *b_)
57272343Sngie{
58272343Sngie	uint32_t a = *(const uint32_t *)a_;
59272343Sngie	uint32_t b = *(const uint32_t *)b_;
60272343Sngie
61272343Sngie	return a > b ? 1 : (a < b ? 1 : 0);
62272343Sngie}
63272343Sngie
64272343Sngiestatic void
65272343Sngieinit_keys(size_t len)
66272343Sngie{
67272343Sngie	uint32_t sorted_keys[MAXKEYS];
68272343Sngie	size_t i;
69272343Sngie
70272343Sngie	assert(len <= MAXKEYS);
71272343Sngie
72272343Sngie	if (len == 0)
73272343Sngie		return;
74272343Sngie
75272343Sngie	do {
76272343Sngie		for (i = 0; i < len; ++i)
77272343Sngie			sorted_keys[i] = keys[i] = arc4random();
78272343Sngie
79272343Sngie		qsort(sorted_keys, len, sizeof(*sorted_keys), cmp_keys);
80272343Sngie		for (i = 1; i < len; ++i) {
81272343Sngie			if (sorted_keys[i - 1] == sorted_keys[i])
82272343Sngie				break;
83272343Sngie		}
84272343Sngie	} while (i != len);
85272343Sngie}
86272343Sngie
87272343Sngiestatic void
88272343Sngiewrite_database(size_t len)
89272343Sngie{
90272343Sngie	struct cdbw *db;
91272343Sngie	int fd;
92272343Sngie	size_t i;
93272343Sngie	uint32_t buf[2];
94272343Sngie
95272343Sngie	ATF_REQUIRE((db = cdbw_open()) != NULL);
96272343Sngie	ATF_REQUIRE((fd = creat(database_name, S_IRUSR|S_IWUSR)) != -1);
97272343Sngie	for (i = 0; i < len; ++i) {
98272343Sngie		buf[0] = i;
99272343Sngie		buf[1] = keys[i];
100272343Sngie		ATF_REQUIRE(cdbw_put(db, &keys[i], sizeof(keys[i]),
101272343Sngie		    buf, sizeof(buf)) == 0);
102272343Sngie	}
103272343Sngie	ATF_REQUIRE(cdbw_output(db, fd, "test database", arc4random) == 0);
104272343Sngie	cdbw_close(db);
105272343Sngie	ATF_REQUIRE(close(fd) == 0);
106272343Sngie}
107272343Sngie
108272343Sngiestatic void
109272343Sngiecheck_database(size_t len)
110272343Sngie{
111272343Sngie	struct cdbr *db;
112272343Sngie	size_t i, data_len;
113272343Sngie	const void *data;
114272343Sngie	uint32_t buf[2];
115272343Sngie
116272343Sngie	ATF_REQUIRE((db = cdbr_open(database_name, CDBR_DEFAULT)) != NULL);
117272343Sngie	ATF_REQUIRE_EQ(cdbr_entries(db), len);
118272343Sngie	for (i = 0; i < len; ++i) {
119272343Sngie		ATF_REQUIRE(cdbr_find(db, &keys[i], sizeof(keys[i]),
120272343Sngie		    &data, &data_len) != -1);
121272343Sngie		ATF_REQUIRE_EQ(data_len, sizeof(buf));
122272343Sngie		memcpy(buf, data, sizeof(buf));
123272343Sngie		ATF_REQUIRE_EQ(buf[0], i);
124272343Sngie		ATF_REQUIRE_EQ(buf[1], keys[i]);
125272343Sngie	}
126272343Sngie	cdbr_close(db);
127272343Sngie}
128272343Sngie
129272343SngieATF_TC_WITH_CLEANUP(cdb);
130272343Sngie
131272343SngieATF_TC_HEAD(cdb, tc)
132272343Sngie{
133272343Sngie
134272343Sngie	atf_tc_set_md_var(tc, "descr", "Test cdb(5) reading and writing");
135272343Sngie}
136272343Sngie
137272343SngieATF_TC_BODY(cdb, tc)
138272343Sngie{
139272343Sngie	size_t i, sizes[] = { 0, 16, 64, 1024, 2048 };
140272343Sngie	for (i = 0; i < __arraycount(sizes); ++i) {
141272343Sngie		init_keys(sizes[i]);
142272343Sngie		write_database(sizes[i]);
143272343Sngie		check_database(sizes[i]);
144272343Sngie		unlink(database_name);
145272343Sngie	}
146272343Sngie}
147272343Sngie
148272343SngieATF_TC_CLEANUP(cdb, tc)
149272343Sngie{
150272343Sngie
151272343Sngie	unlink(database_name);
152272343Sngie}
153272343Sngie
154272343SngieATF_TP_ADD_TCS(tp)
155272343Sngie{
156272343Sngie
157272343Sngie	ATF_TP_ADD_TC(tp, cdb);
158272343Sngie
159272343Sngie	return atf_no_error();
160272343Sngie}
161272343Sngie
162