1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/* This file is part of GDBM test suite.
Copyright (C) 2011-2024 Free Software Foundation, Inc.
GDBM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GDBM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GDBM. If not, see <http://www.gnu.org/licenses/>.
*/
#include "autoconf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "gdbmdefs.h"
#include "progname.h"
const char *progname;
const char *dbname;
int flags = 0; /* gdbm_open flags */
int mode = GDBM_WRCREAT; /* gdbm_open mode */
int block_size = 0; /* block size for the db. 0 means default */
size_t mapped_size_max = 32768; /* size of the memory mapped region */
size_t cache_size = 32; /* cache size */
static size_t
get_max_mmap_size (const char *arg)
{
char *p;
size_t size;
errno = 0;
size = strtoul (arg, &p, 10);
if (errno)
{
fprintf (stderr, "%s: ", progname);
perror ("maxmap");
exit (1);
}
if (*p)
{
fprintf (stderr, "%s: bad maxmap\n", progname);
exit (1);
}
return size;
}
/* Test results */
#define RES_PASS 0
#define RES_FAIL 1
#define RES_XFAIL 2
#define RES_SKIP 3
const char *resstr[] = { "PASS", "FAIL", "XFAIL", "SKIP" };
static int _res_max = sizeof(resstr) / sizeof(resstr[0]);
/* A single setopt testcase */
struct optest
{
char *group; /* Group this testcase belongs to */
char *name; /* Testcase name */
/* gdbm_setopt arguments: */
int code; /* option code */
void *valptr; /* points to the value */
int valsize; /* size of the value */
/* end of arguments */
int xfail; /* if !0, expected value of gdbm_errno */
int (*test) (void *valptr); /* Test function (can be NULL) */
void (*init) (void *valptr, int valsize); /* Initialization function
(can be NULL) */
};
/* Storage for the test value */
char *string;
size_t size;
int intval;
int retbool;
/* Individual test and initialization functions */
int
test_getflags (void *valptr)
{
int expected = mode | flags;
#ifndef HAVE_MMAP
expected |= GDBM_NOMMAP;
#endif
return (*(int*) valptr == expected) ? RES_PASS : RES_FAIL;
}
int
test_dbname (void *valptr)
{
char *s = *(char**)valptr;
int rc = strcmp (string, dbname) == 0 ? RES_PASS : RES_FAIL;
if (rc != RES_PASS)
printf ("[got %s instead of %s] ", s, dbname);
free (s);
return rc;
}
void
init_cachesize (void *valptr, int valsize)
{
*(size_t*) valptr = cache_size;
}
int
test_getcachesize (void *valptr)
{
return *(size_t*) valptr == cache_size ? RES_PASS : RES_FAIL;
}
void
init_true (void *valptr, int valsize)
{
*(int*) valptr = 1;
}
void
init_false (void *valptr, int valsize)
{
*(int*) valptr = 0;
}
void
init_negate_bool (void *valptr, int valsize)
{
*(int*) valptr = !retbool;
}
int
test_true (void *valptr)
{
return *(int*) valptr == 1 ? RES_PASS : RES_FAIL;
}
int
test_false (void *valptr)
{
return *(int*) valptr == 0 ? RES_PASS : RES_FAIL;
}
int
test_negate_bool (void *valptr)
{
return *(int*) valptr == !retbool ? RES_PASS : RES_FAIL;
}
int
test_bool (void *valptr)
{
return *(int*) valptr == retbool ? RES_PASS : RES_FAIL;
}
int
test_initial_maxmapsize(void *valptr)
{
return *(size_t*) valptr == SIZE_T_MAX ? RES_PASS : RES_FAIL;
}
void
init_maxmapsize (void *valptr, int valsize)
{
*(size_t*)valptr = mapped_size_max;
}
int
test_maxmapsize (void *valptr)
{
size_t page_size = sysconf (_SC_PAGESIZE);
size_t expected_size = ((mapped_size_max + page_size - 1) / page_size) *
page_size;
return (*(size_t*) valptr == expected_size) ? RES_PASS : RES_FAIL;
}
int
test_mmap_group (void *valptr)
{
#ifdef HAVE_MMAP
return RES_PASS;
#else
return RES_SKIP;
#endif
}
/* Create a group of testcases for testing a boolean option.
Arguments:
grp - group name
set - GDBM_SETxxx option
get - GDBM_GETxxx option
*/
#define TEST_BOOL_OPTION(grp, set,get) \
{ #grp, }, \
{ #grp, "initial " #get, get, &retbool, sizeof (retbool), \
0, NULL, NULL }, \
{ #grp, #set, set, &intval, sizeof (intval), \
0, NULL, init_negate_bool }, \
{ #grp, #get, get, &intval, sizeof (intval), \
0, test_negate_bool, NULL }, \
{ #grp, #set " true", set, &intval, sizeof (intval), \
0, NULL, init_true }, \
{ #grp, #get, get, &intval, sizeof (intval), \
0, test_true, NULL }, \
{ #grp, #set " false", set, &intval, sizeof (intval), \
0, NULL, init_false }, \
{ #grp, #get, get, &intval, sizeof (intval), \
0, test_false, NULL }
/* Table of testcases: */
struct optest optest_tab[] = {
{ "GETFLAGS", "GDBM_GETFLAGS", GDBM_GETFLAGS, &intval, sizeof (intval),
0, test_getflags },
{ "CACHESIZE" },
{ "CACHESIZE", "initial GDBM_SETCACHESIZE", GDBM_SETCACHESIZE,
&size, sizeof (size), 0,
NULL, init_cachesize },
{ "CACHESIZE", "GDBM_GETCACHESIZE", GDBM_GETCACHESIZE,
&size, sizeof (size), 0,
test_getcachesize },
{ "CACHESIZE", "second GDBM_SETCACHESIZE", GDBM_SETCACHESIZE,
&size, sizeof (size),
GDBM_OPT_ALREADY_SET, NULL, init_cachesize },
TEST_BOOL_OPTION (SYNCMODE, GDBM_SETSYNCMODE, GDBM_GETSYNCMODE),
TEST_BOOL_OPTION (CENTFREE, GDBM_SETCENTFREE, GDBM_GETCENTFREE),
TEST_BOOL_OPTION (COALESCEBLKS, GDBM_SETCOALESCEBLKS, GDBM_GETCOALESCEBLKS),
/* MMAP group */
{ "MMAP", NULL, 0, NULL, 0, 0, test_mmap_group },
{ "MMAP", "initial GDBM_GETMMAP", GDBM_GETMMAP,
&intval, sizeof (intval), 0,
test_true },
{ "MMAP", "GDBM_SETMMAP false", GDBM_SETMMAP,
&intval, sizeof (intval), 0,
NULL, init_false },
{ "MMAP", "GDBM_GETMMAP", GDBM_GETMMAP,
&intval, sizeof (intval), 0,
test_false },
{ "MMAP", "initial GDBM_GETMAXMAPSIZE", GDBM_GETMAXMAPSIZE,
&size, sizeof (size), 0,
test_initial_maxmapsize, NULL },
{ "MMAP", "GDBM_SETMAXMAPSIZE", GDBM_SETMAXMAPSIZE,
&size, sizeof (size), 0,
NULL, init_maxmapsize },
{ "MMAP", "GDBM_GETMAXMAPSIZE", GDBM_GETMAXMAPSIZE,
&size, sizeof (size), 0,
test_maxmapsize, NULL },
{ "GETDBNAME", "GDBM_GETDBNAME", GDBM_GETDBNAME,
&string, sizeof (string), 0,
test_dbname, NULL },
{ NULL }
};
/* Use ARGV to determine whether to run the given GROUP of
testcases.
ARGV is a NULL-terminated array of allowed group names. A "!"
prefix can be used to denote negation. */
int
groupok (char **argv, const char *group)
{
int retval = 1;
if (*argv)
{
char *arg;
while ((arg = *argv++))
{
if (*arg == '!')
{
if (strcasecmp (arg + 1, group) == 0)
return 0;
retval = 1;
}
else
{
if (strcasecmp (arg, group) == 0)
return 1;
retval = 0;
}
}
}
return retval;
}
int
main (int argc, char **argv)
{
GDBM_FILE dbf;
struct optest *op;
progname = canonical_progname (argv[0]);
while (--argc)
{
char *arg = *++argv;
if (strcmp (arg, "-h") == 0)
{
printf ("usage: %s [-blocksize=N] [-nolock] [-sync] [-maxmap=N] DBFILE [GROUP [GROUP...]\n",
progname);
exit (0);
}
else if (strcmp (arg, "-nolock") == 0)
flags |= GDBM_NOLOCK;
else if (strcmp (arg, "-sync") == 0)
flags |= GDBM_SYNC;
else if (strncmp (arg, "-blocksize=", 11) == 0)
block_size = atoi (arg + 11);
else if (strncmp (arg, "-maxmap=", 8) == 0)
mapped_size_max = get_max_mmap_size (arg + 8);
else if (strcmp (arg, "--") == 0)
{
--argc;
++argv;
break;
}
else if (arg[0] == '-')
{
fprintf (stderr, "%s: unknown option %s\n", progname, arg);
exit (1);
}
else
break;
}
if (argc == 0)
{
fprintf (stderr, "%s: wrong arguments\n", progname);
exit (1);
}
dbname = *argv;
++argv;
--argc;
dbf = gdbm_open (dbname, block_size, mode|flags, 00664, NULL);
if (!dbf)
{
fprintf (stderr, "gdbm_open failed: %s\n", gdbm_strerror (gdbm_errno));
exit (1);
}
for (op = optest_tab; op->group; op++)
{
int rc;
if (!groupok (argv, op->group))
continue;
if (!op->name)
{
/* Group header */
const char *grp = op->group;
printf ("* %s:", grp);
if (op->test && (rc = op->test (NULL)) != RES_PASS)
{
printf (" %s", resstr[rc]);
for (op++; op->name && strcmp (op->group, grp) == 0; op++)
;
op--;
}
putchar ('\n');
continue;
}
printf ("%s: ", op->name);
if (op->init)
op->init (op->valptr, op->valsize);
rc = gdbm_setopt (dbf, op->code, op->valptr, op->valsize);
if (rc)
{
if (gdbm_errno == op->xfail)
puts (resstr[RES_XFAIL]);
else
printf ("%s: %s\n", resstr[RES_FAIL],
gdbm_strerror (gdbm_errno));
}
else if (!op->test)
puts (resstr[RES_PASS]);
else
{
rc = op->test (op->valptr);
assert (rc >= 0 && rc < _res_max);
puts (resstr[rc]);
}
}
if (gdbm_close (dbf))
{
fprintf (stderr, "gdbm_close: %s; %s\n", gdbm_strerror (gdbm_errno),
strerror (errno));
exit (3);
}
exit (0);
}
|