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
35 static char copyright[] =
36 "@(#) Copyright (c) 1992, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)dbtest.c 8.17 (Berkeley) 9/1/94";
44 static const char rcsid[] =
45 "$FreeBSD: src/lib/libc/db/test/dbtest.c,v 1.3.8.1 2000/08/21 22:44:47 jhb Exp $";
48 #include <sys/param.h>
62 enum S { COMMAND, COMPARE, GET, PUT, REMOVE, SEQ, SEQFLAG, KEY, DATA };
64 void compare __P((DBT *, DBT *));
65 DBTYPE dbtype __P((char *));
66 void dump __P((DB *, int));
67 void err __P((const char *, ...));
68 void get __P((DB *, DBT *));
69 void getdata __P((DB *, DBT *, DBT *));
70 void put __P((DB *, DBT *, DBT *));
71 void rem __P((DB *, DBT *));
72 char *sflags __P((int));
73 void synk __P((DB *));
74 void *rfile __P((char *, size_t *));
75 void seq __P((DB *, DBT *));
76 u_int setflags __P((char *));
77 void *setinfo __P((DBTYPE, char *));
78 void usage __P((void));
79 void *xmalloc __P((char *, size_t));
81 DBTYPE type; /* Database type. */
82 void *infop; /* Iflags. */
83 u_long lineno; /* Current line in test script. */
84 u_int flags; /* Current DB flags. */
85 int ofd = STDOUT_FILENO; /* Standard output fd. */
87 DB *XXdbp; /* Global for gdb. */
88 int XXlineno; /* Fast breakpoint for gdb. */
97 enum S command, state;
99 DBT data, key, keydata;
101 int ch, oflags, sflag;
102 char *fname, *infoarg, *p, *t, buf[8 * 1024];
106 oflags = O_CREAT | O_RDWR;
108 while ((ch = getopt(argc, argv, "f:i:lo:s")) != EOF)
120 if ((ofd = open(optarg,
121 O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
122 err("%s: %s", optarg, strerror(errno));
138 type = dbtype(*argv++);
140 /* Open the descriptor file. */
141 if (strcmp(*argv, "-") && freopen(*argv, "r", stdin) == NULL)
142 err("%s: %s", *argv, strerror(errno));
144 /* Set up the db structure as necessary. */
148 for (p = strtok(infoarg, ",\t "); p != NULL;
149 p = strtok(0, ",\t "))
151 infop = setinfo(type, p);
154 * Open the DB. Delete any preexisting copy, you almost never
155 * want it around, and it often screws up tests.
158 p = getenv("TMPDIR");
161 (void)snprintf(buf, sizeof(buf), "%s/__dbtest", p);
167 if ((dbp = dbopen(fname,
168 oflags, S_IRUSR | S_IWUSR, type, infop)) == NULL)
169 err("dbopen: %s", strerror(errno));
174 (p = fgets(buf, sizeof(buf), stdin)) != NULL; ++lineno) {
175 /* Delete the newline, displaying the key/data is easier. */
176 if (ofd == STDOUT_FILENO && (t = strchr(p, '\n')) != NULL)
178 if ((len = strlen(buf)) == 0 || isspace(*p) || *p == '#')
181 /* Convenient gdb break point. */
182 if (XXlineno == lineno)
185 case 'c': /* compare */
186 if (state != COMMAND)
187 err("line %lu: not expecting command", lineno);
192 if (state != COMMAND)
193 err("line %lu: not expecting command", lineno);
194 /* Don't display the newline, if CR at EOL. */
195 if (p[len - 2] == '\r')
197 if (write(ofd, p + 1, len - 1) != len - 1 ||
198 write(ofd, "\n", 1) != 1)
199 err("write: %s", strerror(errno));
202 if (state != COMMAND)
203 err("line %lu: not expecting command", lineno);
208 if (state != COMMAND)
209 err("line %lu: not expecting command", lineno);
213 case 'r': /* remove */
214 if (state != COMMAND)
215 err("line %lu: not expecting command", lineno);
216 if (flags == R_CURSOR) {
225 if (state != COMMAND)
226 err("line %lu: not expecting command", lineno);
231 if (state != COMMAND)
232 err("line %lu: not expecting command", lineno);
233 if (flags == R_CURSOR) {
240 flags = setflags(p + 1);
242 case 'D': /* data file */
244 err("line %lu: not expecting data", lineno);
245 data.data = rfile(p + 1, &data.size);
249 err("line %lu: not expecting data", lineno);
250 data.data = xmalloc(p + 1, len - 1);
252 ldata: switch (command) {
254 compare(&keydata, &data);
257 put(dbp, &key, &data);
260 err("line %lu: command doesn't take data",
263 if (type != DB_RECNO)
268 case 'K': /* key file */
270 err("line %lu: not expecting a key", lineno);
271 if (type == DB_RECNO)
272 err("line %lu: 'K' not available for recno",
274 key.data = rfile(p + 1, &key.size);
278 err("line %lu: not expecting a key", lineno);
279 if (type == DB_RECNO) {
280 static recno_t recno;
283 key.size = sizeof(recno);
285 key.data = xmalloc(p + 1, len - 1);
288 lkey: switch (command) {
290 getdata(dbp, &key, &keydata);
295 if (type != DB_RECNO)
304 if ((type != DB_RECNO) && (flags != R_CURSOR))
310 if ((type != DB_RECNO) && (flags != R_CURSOR))
315 err("line %lu: command doesn't take a key",
320 dump(dbp, p[1] == 'r');
323 err("line %lu: %s: unknown command character",
329 * -l must be used (DB_LOCK must be set) for this to be
330 * used, otherwise a page will be locked and it will fail.
332 if (type == DB_BTREE && oflags & DB_LOCK)
336 err("db->close: %s", strerror(errno));
341 #define NOOVERWRITE "put failed, would overwrite key\n"
348 register u_char *p1, *p2;
350 if (db1->size != db2->size)
351 printf("compare failed: key->data len %lu != data len %lu\n",
352 db1->size, db2->size);
354 len = MIN(db1->size, db2->size);
355 for (p1 = db1->data, p2 = db2->data; len--;)
356 if (*p1++ != *p2++) {
357 printf("compare failed at offset %d\n",
358 p1 - (u_char *)db1->data);
370 switch (dbp->get(dbp, kp, &data, flags)) {
372 (void)write(ofd, data.data, data.size);
373 if (ofd == STDOUT_FILENO)
374 (void)write(ofd, "\n", 1);
377 err("line %lu: get: %s", lineno, strerror(errno));
380 #define NOSUCHKEY "get failed, no such key\n"
381 if (ofd != STDOUT_FILENO)
382 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
384 (void)fprintf(stderr, "%d: %.*s: %s",
385 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
396 switch (dbp->get(dbp, kp, dp, flags)) {
400 err("line %lu: getdata: %s", lineno, strerror(errno));
403 err("line %lu: getdata failed, no such key", lineno);
413 switch (dbp->put(dbp, kp, dp, flags)) {
417 err("line %lu: put: %s", lineno, strerror(errno));
420 (void)write(ofd, NOOVERWRITE, sizeof(NOOVERWRITE) - 1);
430 switch (dbp->del(dbp, kp, flags)) {
434 err("line %lu: rem: %s", lineno, strerror(errno));
437 #define NOSUCHKEY "rem failed, no such key\n"
438 if (ofd != STDOUT_FILENO)
439 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
440 else if (flags != R_CURSOR)
441 (void)fprintf(stderr, "%d: %.*s: %s",
442 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
444 (void)fprintf(stderr,
445 "%d: rem of cursor failed\n", lineno);
455 switch (dbp->sync(dbp, flags)) {
459 err("line %lu: synk: %s", lineno, strerror(errno));
471 switch (dbp->seq(dbp, kp, &data, flags)) {
473 (void)write(ofd, data.data, data.size);
474 if (ofd == STDOUT_FILENO)
475 (void)write(ofd, "\n", 1);
478 err("line %lu: seq: %s", lineno, strerror(errno));
481 #define NOSUCHKEY "seq failed, no such key\n"
482 if (ofd != STDOUT_FILENO)
483 (void)write(ofd, NOSUCHKEY, sizeof(NOSUCHKEY) - 1);
484 else if (flags == R_CURSOR)
485 (void)fprintf(stderr, "%d: %.*s: %s",
486 lineno, MIN(kp->size, 20), kp->data, NOSUCHKEY);
488 (void)fprintf(stderr,
489 "%d: seq (%s) failed\n", lineno, sflags(flags));
510 for (;; flags = nflags)
511 switch (dbp->seq(dbp, &key, &data, flags)) {
513 (void)write(ofd, data.data, data.size);
514 if (ofd == STDOUT_FILENO)
515 (void)write(ofd, "\n", 1);
520 err("line %lu: (dump) seq: %s",
521 lineno, strerror(errno));
533 for (; isspace(*s); ++s);
534 if (*s == '\n' || *s == '\0')
536 if ((p = index(s, '\n')) != NULL)
538 if (!strcmp(s, "R_CURSOR")) return (R_CURSOR);
539 if (!strcmp(s, "R_FIRST")) return (R_FIRST);
540 if (!strcmp(s, "R_IAFTER")) return (R_IAFTER);
541 if (!strcmp(s, "R_IBEFORE")) return (R_IBEFORE);
542 if (!strcmp(s, "R_LAST")) return (R_LAST);
543 if (!strcmp(s, "R_NEXT")) return (R_NEXT);
544 if (!strcmp(s, "R_NOOVERWRITE")) return (R_NOOVERWRITE);
545 if (!strcmp(s, "R_PREV")) return (R_PREV);
546 if (!strcmp(s, "R_SETCURSOR")) return (R_SETCURSOR);
548 err("line %lu: %s: unknown flag", lineno, s);
557 case R_CURSOR: return ("R_CURSOR");
558 case R_FIRST: return ("R_FIRST");
559 case R_IAFTER: return ("R_IAFTER");
560 case R_IBEFORE: return ("R_IBEFORE");
561 case R_LAST: return ("R_LAST");
562 case R_NEXT: return ("R_NEXT");
563 case R_NOOVERWRITE: return ("R_NOOVERWRITE");
564 case R_PREV: return ("R_PREV");
565 case R_SETCURSOR: return ("R_SETCURSOR");
575 if (!strcmp(s, "btree"))
577 if (!strcmp(s, "hash"))
579 if (!strcmp(s, "recno"))
581 err("%s: unknown type (use btree, hash or recno)", s);
595 if ((eq = index(s, '=')) == NULL)
596 err("%s: illegal structure set statement", s);
599 err("%s: structure set statement must be a number", s);
603 if (!strcmp("flags", s)) {
607 if (!strcmp("cachesize", s)) {
608 ib.cachesize = atoi(eq);
611 if (!strcmp("maxkeypage", s)) {
612 ib.maxkeypage = atoi(eq);
615 if (!strcmp("minkeypage", s)) {
616 ib.minkeypage = atoi(eq);
619 if (!strcmp("lorder", s)) {
620 ib.lorder = atoi(eq);
623 if (!strcmp("psize", s)) {
629 if (!strcmp("bsize", s)) {
633 if (!strcmp("ffactor", s)) {
634 ih.ffactor = atoi(eq);
637 if (!strcmp("nelem", s)) {
641 if (!strcmp("cachesize", s)) {
642 ih.cachesize = atoi(eq);
645 if (!strcmp("lorder", s)) {
646 ih.lorder = atoi(eq);
651 if (!strcmp("flags", s)) {
655 if (!strcmp("cachesize", s)) {
656 rh.cachesize = atoi(eq);
659 if (!strcmp("lorder", s)) {
660 rh.lorder = atoi(eq);
663 if (!strcmp("reclen", s)) {
664 rh.reclen = atoi(eq);
667 if (!strcmp("bval", s)) {
671 if (!strcmp("psize", s)) {
677 err("%s: unknown structure value", s);
691 for (; isspace(*name); ++name);
692 if ((np = index(name, '\n')) != NULL)
694 if ((fd = open(name, O_RDONLY, 0)) < 0 ||
696 err("%s: %s\n", name, strerror(errno));
698 if (sb.st_size > (off_t)SIZE_T_MAX)
699 err("%s: %s\n", name, strerror(E2BIG));
701 if ((p = (void *)malloc((u_int)sb.st_size)) == NULL)
702 err("%s", strerror(errno));
703 (void)read(fd, p, (int)sb.st_size);
716 if ((p = (void *)malloc(len)) == NULL)
717 err("%s", strerror(errno));
718 memmove(p, text, len);
725 (void)fprintf(stderr,
726 "usage: dbtest [-l] [-f file] [-i info] [-o file] type script\n");
738 err(const char *fmt, ...)
751 (void)fprintf(stderr, "dbtest: ");
752 (void)vfprintf(stderr, fmt, ap);
754 (void)fprintf(stderr, "\n");