run-tdb1-corrupt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "tdb2-source.h"
  2. #include <ccan/tap/tap.h>
  3. #include <stdlib.h>
  4. #include <err.h>
  5. #include "logging.h"
  6. static int check(TDB_DATA key, TDB_DATA data, void *private)
  7. {
  8. unsigned int *sizes = private;
  9. if (key.dsize > strlen("hello"))
  10. return -1;
  11. if (memcmp(key.dptr, "hello", key.dsize) != 0)
  12. return -1;
  13. if (data.dsize != strlen("world"))
  14. return -1;
  15. if (memcmp(data.dptr, "world", data.dsize) != 0)
  16. return -1;
  17. sizes[0] += key.dsize;
  18. sizes[1] += data.dsize;
  19. return 0;
  20. }
  21. static void tdb1_flip_bit(struct tdb_context *tdb, unsigned int bit)
  22. {
  23. unsigned int off = bit / CHAR_BIT;
  24. unsigned char mask = (1 << (bit % CHAR_BIT));
  25. if (tdb->file->map_ptr)
  26. ((unsigned char *)tdb->file->map_ptr)[off] ^= mask;
  27. else {
  28. unsigned char c;
  29. if (pread(tdb->file->fd, &c, 1, off) != 1)
  30. err(1, "pread");
  31. c ^= mask;
  32. if (pwrite(tdb->file->fd, &c, 1, off) != 1)
  33. err(1, "pwrite");
  34. }
  35. }
  36. static void check_test(struct tdb_context *tdb)
  37. {
  38. TDB_DATA key, data;
  39. unsigned int i, verifiable, corrupt, sizes[2], dsize, ksize;
  40. ok1(tdb1_check(tdb, NULL, NULL) == 0);
  41. key.dptr = (void *)"hello";
  42. data.dsize = strlen("world");
  43. data.dptr = (void *)"world";
  44. /* Key and data size respectively. */
  45. dsize = ksize = 0;
  46. /* 5 keys in hash size 2 means we'll have multichains. */
  47. for (key.dsize = 1; key.dsize <= 5; key.dsize++) {
  48. ksize += key.dsize;
  49. dsize += data.dsize;
  50. if (tdb_store(tdb, key, data, TDB_INSERT) != TDB_SUCCESS)
  51. abort();
  52. }
  53. /* This is how many bytes we expect to be verifiable. */
  54. /* From the file header. */
  55. verifiable = strlen(TDB_MAGIC_FOOD) + 1
  56. + 2 * sizeof(uint32_t) + 2 * sizeof(tdb1_off_t)
  57. + 2 * sizeof(uint32_t);
  58. /* From the free list chain and hash chains. */
  59. verifiable += 3 * sizeof(tdb1_off_t);
  60. /* From the record headers & tailer */
  61. verifiable += 5 * (sizeof(struct tdb1_record) + sizeof(uint32_t));
  62. /* The free block: we ignore datalen, keylen, full_hash. */
  63. verifiable += sizeof(struct tdb1_record) - 3*sizeof(uint32_t) +
  64. sizeof(uint32_t);
  65. /* Our check function verifies the key and data. */
  66. verifiable += ksize + dsize;
  67. /* Flip one bit at a time, make sure it detects verifiable bytes. */
  68. for (i = 0, corrupt = 0; i < tdb->file->map_size * CHAR_BIT; i++) {
  69. tdb1_flip_bit(tdb, i);
  70. memset(sizes, 0, sizeof(sizes));
  71. if (tdb1_check(tdb, check, sizes) != 0)
  72. corrupt++;
  73. else if (sizes[0] != ksize || sizes[1] != dsize)
  74. corrupt++;
  75. tdb1_flip_bit(tdb, i);
  76. }
  77. ok(corrupt == verifiable * CHAR_BIT, "corrupt %u should be %u",
  78. corrupt, verifiable * CHAR_BIT);
  79. }
  80. int main(int argc, char *argv[])
  81. {
  82. struct tdb_context *tdb;
  83. union tdb_attribute hsize;
  84. hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
  85. hsize.base.next = &tap_log_attr;
  86. hsize.tdb1_hashsize.hsize = 2;
  87. plan_tests(4);
  88. /* This should use mmap. */
  89. tdb = tdb_open("run-corrupt.tdb1", TDB_VERSION1,
  90. O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
  91. if (!tdb)
  92. abort();
  93. check_test(tdb);
  94. tdb_close(tdb);
  95. /* This should not. */
  96. tdb = tdb_open("run-corrupt.tdb1", TDB_VERSION1|TDB_NOMMAP,
  97. O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
  98. if (!tdb)
  99. abort();
  100. check_test(tdb);
  101. tdb_close(tdb);
  102. return exit_status();
  103. }