2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)dbtest.c 8.17 (Berkeley) 9/1/94
35 * $FreeBSD: src/lib/libc/db/test/dbtest.c,v 1.3.8.1 2000/08/21 22:44:47 jhb Exp $
36 * $DragonFly: src/lib/libc/db/test/dbtest.c,v 1.5 2004/10/25 19:38:01 drhodus Exp $
39 #include <sys/param.h>
53 enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
55 void compare (DBT *, DBT *);
56 DBTYPE dbtype (char *);
57 void dump (DB *, int);
58 void err (const char *, ...);
59 void get (DB *, DBT *);
60 void getdata (DB *, DBT *, DBT *);
61 void put (DB *, DBT *, DBT *);
62 void rem (DB *, DBT *);
65 void *rfile (char *, size_t *);
66 void seq (DB *, DBT *);
67 u_int setflags (char *);
68 void *setinfo (DBTYPE, char *);
70 void *xmalloc (char *, size_t);
72 DBTYPE type; /* Database type. */
73 void *infop; /* Iflags. */
74 u_long lineno; /* Current line in test script. */
75 u_int flags; /* Current DB flags. */
76 int ofd = STDOUT_FILENO; /* Standard output fd. */
78 DB *XXdbp; /* Global for gdb. */
79 int XXlineno; /* Fast breakpoint for gdb. */
88 enum S command, state;
90 DBT data, key, keydata;
92 int ch, oflags, sflag;
93 char *fname, *infoarg, *p, *t, buf[8 * 1024];
97 oflags = O_CREAT | O_RDWR;
99 while ((ch = getopt(argc, argv, "f:i:lo:s")) != EOF)
111 if ((ofd = open(optarg,
112 O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
113 err("%s: %s", optarg, strerror(errno));
129 type = dbtype(*argv++);
131 /* Open the descriptor file. */
132 if (strcmp(*argv, "-") && freopen(*argv, "r", stdin) == NULL)
133 err("%s: %s", *argv, strerror(errno));
135 /* Set up the db structure as necessary. */
139 for (p = strtok(infoarg, ",\t "); p != NULL;
140 p = strtok(0, ",\t "))
142 infop = setinfo(type, p);
145 * Open the DB. Delete any preexisting copy, you almost never
146 * want it around, and it often screws up tests.
149 p = getenv("TMPDIR");
152 (void)snprintf(buf, sizeof(buf), "%s/__dbtest", p);
158 if ((dbp = dbopen(fname,
159 oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL)
160 err("dbopen: %s", strerror(errno));
165 (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
166 /* Delete the newline, displaying the key/data is easier. */
167 if (ofd == STDOUT_FILENO && (t = strchr(p, '\n')) != NULL)
169 if ((len = strlen(buf)) == 0 || isspace(*p) || *p == '#')
172 /* Convenient gdb break point. */
173 if (XXlineno == lineno)
176 case 'c': /* compare */
177 if (state != COMMAND)
178 err("line %lu: not expecting command", lineno);
183 if (state != COMMAND)
184 err("line %lu: not expecting command", lineno);
185 /* Don't display the newline, if CR at EOL. */
186 if (p[len - 2] == '\r')
188 if (write(ofd, p + 1, len - 1) != len - 1 ||
189 write(ofd, "\n", 1) != 1)
190 err("write: %s", strerror(errno));
193 if (state != COMMAND)
194 err("line %lu: not expecting command", lineno);
199 if (state != COMMAND)
200 err("line %lu: not expecting command", lineno);
204 case 'r': /* remove */
205 if (state != COMMAND)
206 err("line %lu: not expecting command", lineno);
207 if (flags == R_CURSOR) {
216 if (state != COMMAND)
217 err("line %lu: not expecting command", lineno);
222 if (state != COMMAND)
223 err("line %lu: not expecting command", lineno);
224 if (flags == R_CURSOR) {
231 flags = setflags(p + 1);
233 case 'D': /* data file */
235 err("line %lu: not expecting data", lineno);
236 data.data = rfile(p + 1, &data.size);
240 err("line %lu: not expecting data", lineno);
241 data.data = xmalloc(p + 1, len - 1);
243 ldata: switch (command) {
245 compare(&keydata, &data);
248 put(dbp, &key, &data);
251 err("line %lu: command doesn't take data",
254 if (type != DB_RECNO)
259 case 'K': /* key file */
261 err("line %lu: not expecting a key", lineno);
262 if (type == DB_RECNO)
263 err("line %lu: 'K' not available for recno",
265 key.data = rfile(p + 1, &key.size);
269 err("line %lu: not expecting a key", lineno);
270 if (type == DB_RECNO) {
271 static recno_t recno;
274 key.size = sizeof(recno);
276 key.data = xmalloc(p + 1, len - 1);
279 lkey: switch (command) {
281 getdata(dbp, &key, &keydata);
286 if (type != DB_RECNO)
295 if ((type != DB_RECNO) && (flags != R_CURSOR))
301 if ((type != DB_RECNO) && (flags != R_CURSOR))
306 err("line %lu: command doesn't take a key",
311 dump(dbp, p[1] == 'r');
314 err("line %lu: %s: unknown command character",
320 * -l must be used (DB_LOCK must be set) for this to be
321 * used, otherwise a page will be locked and it will fail.
323 if (type == DB_BTREE && oflags & DB_LOCK)
327 err("db->close: %s", strerror(errno));
332 #define NOOVERWRITE "put failed, would overwrite key\n"
341 if (db1->size != db2->size)
342 printf("compare failed: key->data len %lu != data len %lu\n",
343 db1->size, db2->size);
345 len = MIN(db1->size, db2->size);
346 for (p1 = db1->data, p2 = db2->data; len--;)
347 if (*p1++ != *p2++) {
348 printf("compare failed at offset %d\n",
349 p1 - (u_char *)db1->data);
361 switch (dbp->get(dbp, kp, &data, flags)) {
363 (void)write(ofd, data.data, data.size);
364 if (ofd == STDOUT_FILENO)
365 (void)write(ofd, "\n", 1);
368 err("line %lu: get: %s", lineno, strerror(errno));
371 #define NOSUCHKEY "get failed, no such key\n"
372 if (ofd != STDOUT_FILENO)
373 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
375 (void)fprintf(stderr, "%d: %.*s: %s",
376 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
387 switch (dbp->get(dbp, kp, dp, flags)) {
391 err("line %lu: getdata: %s", lineno, strerror(errno));
394 err("line %lu: getdata failed, no such key", lineno);
404 switch (dbp->put(dbp, kp, dp, flags)) {
408 err("line %lu: put: %s", lineno, strerror(errno));
411 (void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
421 switch (dbp->del(dbp, kp, flags)) {
425 err("line %lu: rem: %s", lineno, strerror(errno));
428 #define NOSUCHKEY "rem failed, no such key\n"
429 if (ofd != STDOUT_FILENO)
430 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
431 else if (flags != R_CURSOR)
432 (void)fprintf(stderr, "%d: %.*s: %s",
433 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
435 (void)fprintf(stderr,
436 "%d: rem of cursor failed\n", lineno);
446 switch (dbp->sync(dbp, flags)) {
450 err("line %lu: synk: %s", lineno, strerror(errno));
462 switch (dbp->seq(dbp, kp, &data, flags)) {
464 (void)write(ofd, data.data, data.size);
465 if (ofd == STDOUT_FILENO)
466 (void)write(ofd, "\n", 1);
469 err("line %lu: seq: %s", lineno, strerror(errno));
472 #define NOSUCHKEY "seq failed, no such key\n"
473 if (ofd != STDOUT_FILENO)
474 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
475 else if (flags == R_CURSOR)
476 (void)fprintf(stderr, "%d: %.*s: %s",
477 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
479 (void)fprintf(stderr,
480 "%d: seq (%s) failed\n", lineno, sflags(flags));
501 for (;; flags = nflags)
502 switch (dbp->seq(dbp, &key, &data, flags)) {
504 (void)write(ofd, data.data, data.size);
505 if (ofd == STDOUT_FILENO)
506 (void)write(ofd, "\n", 1);
511 err("line %lu: (dump) seq: %s",
512 lineno, strerror(errno));
524 for (; isspace(*s); ++s);
525 if (*s == '\n' || *s == '\0')
527 if ((p = index(s, '\n')) != NULL)
529 if (!strcmp(s, "R_CURSOR")) return (R_CURSOR);
530 if (!strcmp(s, "R_FIRST")) return (R_FIRST);
531 if (!strcmp(s, "R_IAFTER")) return (R_IAFTER);
532 if (!strcmp(s, "R_IBEFORE")) return (R_IBEFORE);
533 if (!strcmp(s, "R_LAST")) return (R_LAST);
534 if (!strcmp(s, "R_NEXT")) return (R_NEXT);
535 if (!strcmp(s, "R_NOOVERWRITE")) return (R_NOOVERWRITE);
536 if (!strcmp(s, "R_PREV")) return (R_PREV);
537 if (!strcmp(s, "R_SETCURSOR")) return (R_SETCURSOR);
539 err("line %lu: %s: unknown flag", lineno, s);
548 case R_CURSOR: return ("R_CURSOR");
549 case R_FIRST: return ("R_FIRST");
550 case R_IAFTER: return ("R_IAFTER");
551 case R_IBEFORE: return ("R_IBEFORE");
552 case R_LAST: return ("R_LAST");
553 case R_NEXT: return ("R_NEXT");
554 case R_NOOVERWRITE: return ("R_NOOVERWRITE");
555 case R_PREV: return ("R_PREV");
556 case R_SETCURSOR: return ("R_SETCURSOR");
566 if (!strcmp(s, "btree"))
568 if (!strcmp(s, "hash"))
570 if (!strcmp(s, "recno"))
572 err("%s: unknown type (use btree, hash or recno)", s);
586 if ((eq = index(s, '=')) == NULL)
587 err("%s: illegal structure set statement", s);
590 err("%s: structure set statement must be a number", s);
594 if (!strcmp("flags", s)) {
598 if (!strcmp("cachesize", s)) {
599 ib.cachesize = atoi(eq);
602 if (!strcmp("maxkeypage", s)) {
603 ib.maxkeypage = atoi(eq);
606 if (!strcmp("minkeypage", s)) {
607 ib.minkeypage = atoi(eq);
610 if (!strcmp("lorder", s)) {
611 ib.lorder = atoi(eq);
614 if (!strcmp("psize", s)) {
620 if (!strcmp("bsize", s)) {
624 if (!strcmp("ffactor", s)) {
625 ih.ffactor = atoi(eq);
628 if (!strcmp("nelem", s)) {
632 if (!strcmp("cachesize", s)) {
633 ih.cachesize = atoi(eq);
636 if (!strcmp("lorder", s)) {
637 ih.lorder = atoi(eq);
642 if (!strcmp("flags", s)) {
646 if (!strcmp("cachesize", s)) {
647 rh.cachesize = atoi(eq);
650 if (!strcmp("lorder", s)) {
651 rh.lorder = atoi(eq);
654 if (!strcmp("reclen", s)) {
655 rh.reclen = atoi(eq);
658 if (!strcmp("bval", s)) {
662 if (!strcmp("psize", s)) {
668 err("%s: unknown structure value", s);
682 for (; isspace(*name); ++name);
683 if ((np = index(name, '\n')) != NULL)
685 if ((fd = open(name, O_RDONLY, 0)) < 0 ||
687 err("%s: %s\n", name, strerror(errno));
689 if (sb.st_size > (off_t)SIZE_T_MAX)
690 err("%s: %s\n", name, strerror(E2BIG));
692 if ((p = (void *)malloc((u_int)sb.st_size)) == NULL)
693 err("%s", strerror(errno));
694 (void)read(fd, p, (int)sb.st_size);
707 if ((p = (void *)malloc(len)) == NULL)
708 err("%s", strerror(errno));
709 memmove(p, text, len);
716 (void)fprintf(stderr,
717 "usage: dbtest [-l] [-f file] [-i info] [-o file] type script\n");
724 err(const char *fmt, ...)
728 (void)fprintf(stderr, "dbtest: ");
729 (void)vfprintf(stderr, fmt, ap);
731 (void)fprintf(stderr, "\n");