LCOV - code coverage report
Current view: top level - lib/tdb/test - run-3G-file.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 44 55 80.0 %
Date: 2021-09-23 10:06:22 Functions: 3 3 100.0 %

          Line data    Source code
       1             : #include "../common/tdb_private.h"
       2             : #include "../common/io.c"
       3             : #include "../common/tdb.c"
       4             : #include "../common/lock.c"
       5             : #include "../common/freelist.c"
       6             : #include "../common/traverse.c"
       7             : #include "../common/transaction.c"
       8             : #include "../common/error.c"
       9             : #include "../common/open.c"
      10             : #include "../common/check.c"
      11             : #include "../common/hash.c"
      12             : #include "../common/mutex.c"
      13             : #include "tap-interface.h"
      14             : #include <stdlib.h>
      15             : #include "logging.h"
      16             : 
      17           2 : static int tdb_expand_file_sparse(struct tdb_context *tdb,
      18             :                                   tdb_off_t size,
      19             :                                   tdb_off_t addition)
      20             : {
      21           2 :         if (tdb->read_only || tdb->traverse_read) {
      22           0 :                 tdb->ecode = TDB_ERR_RDONLY;
      23           0 :                 return -1;
      24             :         }
      25             : 
      26           2 :         if (tdb_ftruncate(tdb, size+addition) == -1) {
      27           0 :                 char b = 0;
      28           0 :                 ssize_t written = tdb_pwrite(tdb,  &b, 1, (size+addition) - 1);
      29           0 :                 if (written == 0) {
      30             :                         /* try once more, potentially revealing errno */
      31           0 :                         written = tdb_pwrite(tdb,  &b, 1, (size+addition) - 1);
      32             :                 }
      33           0 :                 if (written == 0) {
      34             :                         /* again - give up, guessing errno */
      35           0 :                         errno = ENOSPC;
      36             :                 }
      37           0 :                 if (written != 1) {
      38           0 :                         TDB_LOG((tdb, TDB_DEBUG_FATAL, "expand_file to %d failed (%s)\n",
      39             :                                  size+addition, strerror(errno)));
      40           0 :                         return -1;
      41             :                 }
      42             :         }
      43             : 
      44           2 :         return 0;
      45             : }
      46             : 
      47             : static const struct tdb_methods large_io_methods = {
      48             :         tdb_read,
      49             :         tdb_write,
      50             :         tdb_next_hash_chain,
      51             :         tdb_notrans_oob,
      52             :         tdb_expand_file_sparse
      53             : };
      54             : 
      55           2 : static int test_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
      56             :                          void *_data)
      57             : {
      58           2 :         TDB_DATA *expect = _data;
      59           2 :         ok1(key.dsize == strlen("hi"));
      60           2 :         ok1(memcmp(key.dptr, "hi", strlen("hi")) == 0);
      61           2 :         ok1(data.dsize == expect->dsize);
      62           2 :         ok1(memcmp(data.dptr, expect->dptr, data.dsize) == 0);
      63           2 :         return 0;
      64             : }
      65             : 
      66           1 : int main(int argc, char *argv[])
      67             : {
      68             :         struct tdb_context *tdb;
      69             :         TDB_DATA key, orig_data, data;
      70             :         uint32_t hashval;
      71             :         tdb_off_t rec_ptr;
      72             :         struct tdb_record rec;
      73             :         int ret;
      74             : 
      75             :         plan_tests(24);
      76           1 :         tdb = tdb_open_ex("run-36-file.tdb", 1024, TDB_CLEAR_IF_FIRST,
      77             :                           O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
      78             : 
      79           1 :         ok1(tdb);
      80           1 :         tdb->methods = &large_io_methods;
      81             : 
      82           1 :         key.dsize = strlen("hi");
      83           1 :         key.dptr = discard_const_p(uint8_t, "hi");
      84           1 :         orig_data.dsize = strlen("world");
      85           1 :         orig_data.dptr = discard_const_p(uint8_t, "world");
      86             : 
      87             :         /* Enlarge the file (internally multiplies by 2). */
      88           1 :         ret = tdb_expand(tdb, 1500000000);
      89             : #ifdef HAVE_INCOHERENT_MMAP
      90             :         /* This can fail due to mmap failure on 32 bit systems. */
      91             :         if (ret == -1) {
      92             :                 /* These should now fail. */
      93             :                 ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == -1);
      94             :                 data = tdb_fetch(tdb, key);
      95             :                 ok1(data.dptr == NULL);
      96             :                 ok1(tdb_traverse(tdb, test_traverse, &orig_data) == -1);
      97             :                 ok1(tdb_delete(tdb, key) == -1);
      98             :                 ok1(tdb_traverse(tdb, test_traverse, NULL) == -1);
      99             :                 /* Skip the rest... */
     100             :                 for (ret = 0; ret < 24 - 6; ret++)
     101             :                         ok1(1);
     102             :                 tdb_close(tdb);
     103             :                 return exit_status();
     104             :         }
     105             : #endif
     106           1 :         ok1(ret == 0);
     107             : 
     108             :         /* Put an entry in, and check it. */
     109           1 :         ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
     110             : 
     111           1 :         data = tdb_fetch(tdb, key);
     112           1 :         ok1(data.dsize == strlen("world"));
     113           1 :         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
     114           1 :         free(data.dptr);
     115             : 
     116             :         /* That currently fills at the end, make sure that's true. */
     117           1 :         hashval = tdb->hash_fn(&key);
     118           1 :         rec_ptr = tdb_find_lock_hash(tdb, key, hashval, F_RDLCK, &rec);
     119           1 :         ok1(rec_ptr);
     120           1 :         ok1(rec_ptr > 2U*1024*1024*1024);
     121           1 :         tdb_unlock(tdb, BUCKET(rec.full_hash), F_RDLCK);
     122             : 
     123             :         /* Traverse must work. */
     124           1 :         ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
     125             : 
     126             :         /* Delete should work. */
     127           1 :         ok1(tdb_delete(tdb, key) == 0);
     128             : 
     129           1 :         ok1(tdb_traverse(tdb, test_traverse, NULL) == 0);
     130             : 
     131             :         /* Transactions should work. */
     132           1 :         ok1(tdb_transaction_start(tdb) == 0);
     133           1 :         ok1(tdb_store(tdb, key, orig_data, TDB_INSERT) == 0);
     134             : 
     135           1 :         data = tdb_fetch(tdb, key);
     136           1 :         ok1(data.dsize == strlen("world"));
     137           1 :         ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
     138           1 :         free(data.dptr);
     139           1 :         ok1(tdb_transaction_commit(tdb) == 0);
     140             : 
     141           1 :         ok1(tdb_traverse(tdb, test_traverse, &orig_data) == 1);
     142           1 :         tdb_close(tdb);
     143             : 
     144           1 :         return exit_status();
     145             : }

Generated by: LCOV version 1.13