summaryrefslogtreecommitdiff
path: root/tests/t_dumpload.c
blob: cbfab8151d432c5ff476784fafac6afa14b95f83 (plain)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* This file is part of GDBM test suite.
   Copyright (C) 2022-2023 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 <stdarg.h>
#include <string.h>
#include <sys/wait.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include "gdbm.h"

#if __STDC_VERSION__ < 199901L
# if __GNUC__ >= 2
#  define __func__ __FUNCTION__
# else
#  define __func__ "<unknown>"
# endif
#endif

char a_name[] = "a.gdbm";
char b_name[] = "b.gdbm";
char orig_name[] = "orig.gdbm";
char bin_dumpname[] = "a.bin.dump";
char ascii_dumpname[] = "a.ascii.dump";

void
db_perror (char const *fmt, ...)
{
  int en = errno;
  va_list ap;
  va_start (ap, fmt);
  vfprintf (stderr, fmt, ap);
  va_end (ap);
  fprintf (stderr, ": %s", gdbm_strerror (gdbm_errno));
  if (gdbm_check_syserr (gdbm_errno))
    fprintf (stderr, ": %s", strerror (en));
  fputc ('\n', stderr);
}

GDBM_FILE
createdb (char const *name)
{
  int rc;
  char *cmdstub = "num2word 1:2000 | gtload -clear ";
  char *cmd;
  size_t cmdlen;
  GDBM_FILE db;

  cmdlen = strlen (cmdstub) + strlen (name);
  cmd = malloc (cmdlen + 1);
  assert (cmd != NULL);
  strcat (strcpy (cmd, cmdstub), name);

  rc = system (cmd);
  if (rc == -1)
    {
      perror (cmd);
      exit (1);
    }
  else if (WIFEXITED (rc))
    {
      rc = WEXITSTATUS (rc);
      if (rc)
	{
	  fprintf (stderr, "%s: terminated with code %d\n", cmd, rc);
	  exit (1);
	}
    }
  else if (WIFSIGNALED (rc))
    {
      fprintf (stderr, "%s: terminated on signal %d", cmd, WTERMSIG (rc));
      if (WCOREDUMP (rc))
	fprintf (stderr, " (core dumped)");
      fputc ('\n', stderr);
      exit (1);
    }
  else
    {
      fprintf (stderr, "%s: terminated with unrecognized status: %d", cmd, rc);
      exit (1);
    }

  db = gdbm_open (name, 0, GDBM_READER, 0, NULL);
  if (db == NULL)
    {
      db_perror ("can't open %s", name);
      exit (1);
    }

  return db;
}

int
db_cmp (GDBM_FILE a, GDBM_FILE b)
{
  datum key, adata, bdata;
  gdbm_count_t an, bn;

  if (gdbm_count (a, &an))
    {
      fprintf (stderr, "a: gdbm_count: %s\n", gdbm_db_strerror (a));
      return -1;
    }
  if (gdbm_count (b, &bn))
    {
      fprintf (stderr, "b: gdbm_count: %s\n", gdbm_db_strerror (b));
      return -1;
    }

  if (an != bn)
    {
      fprintf (stderr, "key counts differ: a=%lu, b=%lu\n",
	       (unsigned long)an, (unsigned long)bn);
      return -1;
    }

  key = gdbm_firstkey (a);
  while (key.dptr)
    {
      datum nextkey;

      adata = gdbm_fetch (a, key);
      if (adata.dptr == NULL)
	{
	  fprintf (stderr, "a: can't get %*.*s: %s\n",
		   key.dsize, key.dsize, key.dptr,
		   gdbm_db_strerror (a));
	  free (key.dptr);
	  return -1;
	}

      bdata = gdbm_fetch (b, key);
      if (bdata.dptr == NULL)
	{
	  fprintf (stderr, "b: can't get %*.*s: %s\n",
		   key.dsize, key.dsize, key.dptr,
		   gdbm_db_strerror (b));
	  free (key.dptr);
	  free (adata.dptr);
	  return -1;
	}

      if (adata.dsize != bdata.dsize ||
	  memcmp (adata.dptr, bdata.dptr, adata.dsize))
	{
	  fprintf (stderr, "data differ for %*.*s\n",
		   key.dsize, key.dsize, key.dptr);
	  free (key.dptr);
	  free (adata.dptr);
	  free (bdata.dptr);
	  return -1;
	}

      free (adata.dptr);
      free (bdata.dptr);

      nextkey = gdbm_nextkey (a, key);
      free (key.dptr);
      key = nextkey;
    }

  return 0;
}

/*
 * Load binary dump into a non-existing (NULL) database.  This should
 * fail with GDBM_NO_DBNAME.
 */
void
test_bindump_0 (GDBM_FILE dbf)
{
  GDBM_FILE b = NULL;

  if (gdbm_load (&b, bin_dumpname, GDBM_INSERT, 0, NULL))
    {
      if (gdbm_errno != GDBM_NO_DBNAME)
	{
	  db_perror ("%s: loading binary dump to non-existing database failed with unexpected error", __func__);
	  exit (1);
	}
    }
  else
    {
      fprintf (stderr, "%s: loading binary dump to non-existing database succeeded when it should not\n", __func__);
      exit (1);
    }
}

/*
 * Load binary dump into an existing empty database.
 */
void
test_bindump_1 (GDBM_FILE dbf)
{
  GDBM_FILE b;

  /*
   * Create an empty database and retry loading into it.
   */
  b = gdbm_open (b_name, 0, GDBM_NEWDB, 0644, NULL);
  if (b == NULL)
    {
      db_perror ("%s: can't open %s", __func__, b_name);
      exit (1);
    }

  if (gdbm_load (&b, bin_dumpname, GDBM_INSERT, 0, NULL))
    {
      db_perror ("%s: failed to load database from binary dump", __func__);
      exit (1);
    }

  /* Compare two databases. */
  if (db_cmp (dbf, b))
    {
      fprintf (stderr, "%s: databases differ\n", __func__);
      exit (1);
    }

  gdbm_close (b);
}

/*
 * Load binary dump to a non-empty database.
 * When tried with GDBM_INSERT replace parameter, it is expected to
 * fail with GDBM_CANNOT_REPLACE.
 * When run with GDBM_REPLACE, it should succeed.
 */
void
test_bindump_2 (GDBM_FILE dbf)
{
  GDBM_FILE b;
  datum key, dat;

  b = gdbm_open (b_name, 0, GDBM_NEWDB, 0644, NULL);
  if (b == NULL)
    {
      db_perror ("%s: can't open %s", __func__, b_name);
      exit (1);
    }

  key.dptr = "99";
  key.dsize = 2;
  dat.dptr = "99";
  dat.dsize = 2;
  if (gdbm_store (b, key, dat, 0))
    {
      db_perror ("%s: can't store datum", __func__);
      exit (1);
    }
  gdbm_sync (b);
  
  if (gdbm_load (&b, bin_dumpname, GDBM_INSERT, 0, NULL))
    {
      if (gdbm_errno != GDBM_CANNOT_REPLACE)
	{
	  db_perror ("%s: expected GDBM_CANNOT_REPLACE, but got", __func__);
	  exit (1);
	}
    }

  if (gdbm_load (&b, bin_dumpname, GDBM_REPLACE, 0, NULL))
    {
      db_perror ("%s: failed to load from ASCII dump", __func__);
      exit (1);
    }

  if (db_cmp (dbf, b))
    {
      fprintf (stderr, "%s: databases differ\n", __func__);
      exit (1);
    }
  
  gdbm_close (b);
}
  
/*
 * Test dumping to and restoring from binary dump format.
 */
void
test_bindump (GDBM_FILE dbf)
{
  /*
   * Create a binary dump.
   */
  if (gdbm_dump (dbf, bin_dumpname, GDBM_DUMP_FMT_BINARY, GDBM_NEWDB, 0600))
    {
      fprintf (stderr, "%s: failed to dump: %s\n", __func__,
	       gdbm_db_strerror (dbf));
      exit (1);
    }

  test_bindump_0 (dbf);
  test_bindump_1 (dbf);
  test_bindump_2 (dbf);
}

/*
 * Load ASCII dump into a non-existing (NULL) database.
 */
void
test_asciidump_0 (GDBM_FILE dbf)
{
  GDBM_FILE b = NULL;

  /*
   * Loading into a non-existing database should create it.
   */
  if (gdbm_load (&b, ascii_dumpname, GDBM_INSERT,
		 GDBM_META_MASK_MODE|GDBM_META_MASK_OWNER, NULL))
    {
      db_perror ("%s: can't load from ascii dump", __func__);
      exit (1);
    }

  /*
   * The loaded database should have the same name as the original.
   */
  if (access (a_name, F_OK))
    {
      fprintf (stderr, "%s: %s: %s\n", __func__, a_name, strerror (errno));
      exit (1);
    }

  /* Compare the two databases. */
  if (db_cmp (dbf, b))
    {
      fprintf (stderr, "%s: databases differ\n", __func__);
      exit (1);
    }
  
  gdbm_close (b);

  /* Cleanup */
  unlink (a_name);
}

/*
 * Load ASCII dump into an existing empty database.
 */
void
test_asciidump_1 (GDBM_FILE dbf)
{
  GDBM_FILE b;

  /*
   * Now try loading into an existing database.
   */
  b = gdbm_open (b_name, 0, GDBM_NEWDB, 0644, NULL);
  if (b == NULL)
    {
      db_perror ("%s: can't open %s", __func__, b_name);
      exit (1);
    }

  if (gdbm_load (&b, ascii_dumpname, GDBM_INSERT,
		 GDBM_META_MASK_MODE|GDBM_META_MASK_OWNER, NULL))
    {
      db_perror ("%s: can't load from ascii dump", __func__);
      exit (1);
    }

  /*
   * Just in case: check if a_name was not created on disk.
   */
  if (access (a_name, F_OK) == 0)
    {
      fprintf (stderr, "%s: %s exists when it should not\n", __func__, a_name);
      exit (1);
    }

  if (db_cmp (dbf, b))
    {
      fprintf (stderr, "%s: databases differ\n", __func__);
      exit (1);
    }
  
  gdbm_close (b);
}

/*
 * Load ASCII dump to a non-empty database.
 * When tried with GDBM_INSERT replace parameter, it is expected to
 * fail with GDBM_CANNOT_REPLACE.
 * When run with GDBM_REPLACE, it should succeed.
 */
void
test_asciidump_2 (GDBM_FILE dbf)
{
  GDBM_FILE b;
  datum key, dat;

  b = gdbm_open (b_name, 0, GDBM_NEWDB, 0644, NULL);
  if (b == NULL)
    {
      db_perror ("%s: can't open %s", __func__, b_name);
      exit (1);
    }

  key.dptr = "99";
  key.dsize = 2;
  dat.dptr = "99";
  dat.dsize = 2;
  if (gdbm_store (b, key, dat, 0))
    {
      db_perror ("%s: can't store datum", __func__);
      exit (1);
    }
  gdbm_sync (b);

  if (gdbm_load (&b, ascii_dumpname, GDBM_INSERT,
		 GDBM_META_MASK_MODE|GDBM_META_MASK_OWNER, NULL))
    {
      if (gdbm_errno != GDBM_CANNOT_REPLACE)
	{
	  db_perror ("%s: expected GDBM_CANNOT_REPLACE, but got", __func__);
	  exit (1);
	}
    }

  if (gdbm_load (&b, ascii_dumpname, GDBM_REPLACE,
		 GDBM_META_MASK_MODE|GDBM_META_MASK_OWNER, NULL))
    {
      db_perror ("%s: failed to load from ASCII dump", __func__);
      exit (1);
    }

  if (db_cmp (dbf, b))
    {
      fprintf (stderr, "%s: databases differ\n", __func__);
      exit (1);
    }
  
  gdbm_close (b);
}

/*
 * Test dumping to and restoring from ASCII dump format.
 */
void
test_asciidump (GDBM_FILE dbf)
{
  /*
   * Create a dump in ASCII format.
   */
  if (gdbm_dump (dbf, ascii_dumpname, GDBM_DUMP_FMT_ASCII, GDBM_NEWDB, 0600))
    {
      fprintf (stderr, "%s: failed to dump: %s\n", __func__,
	       gdbm_db_strerror (dbf));
      exit (1);
    }

  /*
   * Rename the original database.
   */
  if (unlink (orig_name) && errno != ENOENT)
    {
      fprintf (stderr, "%s: failed to remove %s: %s\n", __func__,
	       orig_name, strerror (errno));
      exit (1);
    }

  if (rename (a_name, orig_name))
    {
      fprintf (stderr, "%s: can't rename %s to %s: %s\n", __func__,
	       a_name, orig_name, strerror (errno));
      exit (1);
    }

  test_asciidump_0 (dbf);
  test_asciidump_1 (dbf);
  test_asciidump_2 (dbf);
}

int
main (int argc, char **argv)
{
  GDBM_FILE db;

  db = createdb (a_name);
  test_bindump (db);
  test_asciidump (db);
  unlink (a_name);
  unlink (b_name);
  unlink (orig_name);
  unlink (bin_dumpname);
  unlink (ascii_dumpname);
  return 0;
}