run-05-readonly-open.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <ccan/failtest/failtest_override.h>
  2. #include "ntdb-source.h"
  3. #include "tap-interface.h"
  4. #include <ccan/failtest/failtest.h>
  5. #include "logging.h"
  6. #include "failtest_helper.h"
  7. #include "helprun-external-agent.h"
  8. int main(int argc, char *argv[])
  9. {
  10. unsigned int i;
  11. struct ntdb_context *ntdb;
  12. int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
  13. NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
  14. NTDB_DATA key = ntdb_mkdata("key", 3);
  15. NTDB_DATA data = ntdb_mkdata("data", 4), d;
  16. union ntdb_attribute seed_attr;
  17. unsigned int msgs = 0;
  18. failtest_init(argc, argv);
  19. failtest_hook = block_repeat_failures;
  20. failtest_exit_check = exit_check_log;
  21. seed_attr.base.attr = NTDB_ATTRIBUTE_SEED;
  22. seed_attr.base.next = &tap_log_attr;
  23. seed_attr.seed.seed = 0;
  24. failtest_suppress = true;
  25. plan_tests(sizeof(flags) / sizeof(flags[0]) * 11);
  26. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  27. ntdb = ntdb_open("run-05-readonly-open.ntdb",
  28. flags[i]|MAYBE_NOSYNC,
  29. O_RDWR|O_CREAT|O_TRUNC, 0600,
  30. &seed_attr);
  31. ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
  32. ntdb_close(ntdb);
  33. failtest_suppress = false;
  34. ntdb = ntdb_open("run-05-readonly-open.ntdb",
  35. flags[i]|MAYBE_NOSYNC,
  36. O_RDONLY, 0600, &tap_log_attr);
  37. if (!ok1(ntdb))
  38. break;
  39. ok1(tap_log_messages == msgs);
  40. /* Fetch should succeed, stores should fail. */
  41. if (!ok1(ntdb_fetch(ntdb, key, &d) == 0))
  42. goto fail;
  43. ok1(ntdb_deq(d, data));
  44. free(d.dptr);
  45. if (!ok1(ntdb_store(ntdb, key, data, NTDB_MODIFY)
  46. == NTDB_ERR_RDONLY))
  47. goto fail;
  48. ok1(tap_log_messages == ++msgs);
  49. if (!ok1(ntdb_store(ntdb, key, data, NTDB_INSERT)
  50. == NTDB_ERR_RDONLY))
  51. goto fail;
  52. ok1(tap_log_messages == ++msgs);
  53. failtest_suppress = true;
  54. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  55. ntdb_close(ntdb);
  56. ok1(tap_log_messages == msgs);
  57. /* SIGH: failtest bug, it doesn't save the ntdb file because
  58. * we have it read-only. If we go around again, it gets
  59. * changed underneath us and things get screwy. */
  60. if (failtest_has_failed())
  61. break;
  62. }
  63. failtest_exit(exit_status());
  64. fail:
  65. failtest_suppress = true;
  66. ntdb_close(ntdb);
  67. failtest_exit(exit_status());
  68. /*
  69. * We will never reach this but the compiler complains if we do not
  70. * return in this function.
  71. */
  72. return EFAULT;
  73. }