run-11-simple-fetch.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <ccan/failtest/failtest_override.h>
  2. #include <ccan/tdb2/tdb.c>
  3. #include <ccan/tdb2/open.c>
  4. #include <ccan/tdb2/free.c>
  5. #include <ccan/tdb2/lock.c>
  6. #include <ccan/tdb2/io.c>
  7. #include <ccan/tdb2/hash.c>
  8. #include <ccan/tdb2/transaction.c>
  9. #include <ccan/tdb2/check.c>
  10. #include <ccan/tap/tap.h>
  11. #include <ccan/failtest/failtest.h>
  12. #include "logging.h"
  13. #include "failtest_helper.h"
  14. static bool failtest_suppress = false;
  15. /* Don't need to test everything here, just want fetch testing. */
  16. static enum failtest_result
  17. suppress_failure(struct failtest_call *history, unsigned num)
  18. {
  19. if (failtest_suppress)
  20. return FAIL_DONT_FAIL;
  21. return block_repeat_failures(history, num);
  22. }
  23. int main(int argc, char *argv[])
  24. {
  25. unsigned int i;
  26. struct tdb_context *tdb;
  27. int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
  28. TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
  29. TDB_NOMMAP|TDB_CONVERT };
  30. struct tdb_data key = { (unsigned char *)"key", 3 };
  31. struct tdb_data data = { (unsigned char *)"data", 4 };
  32. failtest_init(argc, argv);
  33. failtest_hook = suppress_failure;
  34. failtest_exit_check = exit_check_log;
  35. failtest_suppress = true;
  36. plan_tests(sizeof(flags) / sizeof(flags[0]) * 8 + 1);
  37. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  38. tdb = tdb_open("run-11-simple-fetch.tdb", flags[i],
  39. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  40. ok1(tdb);
  41. if (tdb) {
  42. struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
  43. /* fetch should fail. */
  44. failtest_suppress = false;
  45. if (!ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_NOEXIST))
  46. goto fail;
  47. failtest_suppress = true;
  48. ok1(tdb_check(tdb, NULL, NULL) == 0);
  49. /* Insert should succeed. */
  50. ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
  51. ok1(tdb_check(tdb, NULL, NULL) == 0);
  52. /* Fetch should now work. */
  53. failtest_suppress = false;
  54. if (!ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS))
  55. goto fail;
  56. failtest_suppress = true;
  57. ok1(tdb_deq(d, data));
  58. free(d.dptr);
  59. ok1(tdb_check(tdb, NULL, NULL) == 0);
  60. tdb_close(tdb);
  61. }
  62. }
  63. ok1(tap_log_messages == 0);
  64. return exit_status();
  65. fail:
  66. failtest_suppress = true;
  67. tdb_close(tdb);
  68. failtest_exit(exit_status());
  69. }