api-93-repack.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <ccan/tdb2/tdb2.h>
  2. #include <ccan/tap/tap.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include "logging.h"
  7. #define NUM_TESTS 1000
  8. static bool store_all(struct tdb_context *tdb)
  9. {
  10. unsigned int i;
  11. struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
  12. struct tdb_data dbuf = { (unsigned char *)&i, sizeof(i) };
  13. for (i = 0; i < NUM_TESTS; i++) {
  14. if (tdb_store(tdb, key, dbuf, TDB_INSERT) != TDB_SUCCESS)
  15. return false;
  16. }
  17. return true;
  18. }
  19. static int mark_entry(struct tdb_context *tdb,
  20. TDB_DATA key, TDB_DATA data, bool found[])
  21. {
  22. unsigned int num;
  23. if (key.dsize != sizeof(num))
  24. return -1;
  25. memcpy(&num, key.dptr, key.dsize);
  26. if (num >= NUM_TESTS)
  27. return -1;
  28. if (found[num])
  29. return -1;
  30. found[num] = true;
  31. return 0;
  32. }
  33. static bool is_all_set(bool found[], unsigned int num)
  34. {
  35. unsigned int i;
  36. for (i = 0; i < num; i++)
  37. if (!found[i])
  38. return false;
  39. return true;
  40. }
  41. int main(int argc, char *argv[])
  42. {
  43. unsigned int i;
  44. bool found[NUM_TESTS];
  45. struct tdb_context *tdb;
  46. int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
  47. TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
  48. TDB_VERSION1, TDB_VERSION1|TDB_NOMMAP,
  49. TDB_VERSION1|TDB_CONVERT,
  50. TDB_VERSION1|TDB_NOMMAP|TDB_CONVERT
  51. };
  52. plan_tests(sizeof(flags) / sizeof(flags[0]) * 6 + 1);
  53. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  54. tdb = tdb_open("run-93-repack.tdb", flags[i],
  55. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  56. ok1(tdb);
  57. if (!tdb)
  58. break;
  59. ok1(store_all(tdb));
  60. ok1(tdb_repack(tdb) == TDB_SUCCESS);
  61. memset(found, 0, sizeof(found));
  62. ok1(tdb_check(tdb, NULL, NULL) == TDB_SUCCESS);
  63. ok1(tdb_traverse(tdb, mark_entry, found) == NUM_TESTS);
  64. ok1(is_all_set(found, NUM_TESTS));
  65. tdb_close(tdb);
  66. }
  67. ok1(tap_log_messages == 0);
  68. return exit_status();
  69. }