api-95-read-only-during-parse.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Make sure write operations fail during ntdb_parse(). */
  2. #include "config.h"
  3. #include "../ntdb.h"
  4. #include "../private.h"
  5. #include "tap-interface.h"
  6. #include "logging.h"
  7. #include "helpapi-external-agent.h"
  8. static struct ntdb_context *ntdb;
  9. /* We could get either of these. */
  10. static bool xfail(enum NTDB_ERROR ecode)
  11. {
  12. return ecode == NTDB_ERR_RDONLY || ecode == NTDB_ERR_LOCK;
  13. }
  14. static enum NTDB_ERROR parse(NTDB_DATA key, NTDB_DATA data,
  15. NTDB_DATA *expected)
  16. {
  17. NTDB_DATA add = ntdb_mkdata("another", strlen("another"));
  18. if (!ntdb_deq(data, *expected)) {
  19. return NTDB_ERR_EINVAL;
  20. }
  21. /* These should all fail.*/
  22. if (!xfail(ntdb_store(ntdb, add, add, NTDB_INSERT))) {
  23. return NTDB_ERR_EINVAL;
  24. }
  25. tap_log_messages--;
  26. if (!xfail(ntdb_append(ntdb, key, add))) {
  27. return NTDB_ERR_EINVAL;
  28. }
  29. tap_log_messages--;
  30. if (!xfail(ntdb_delete(ntdb, key))) {
  31. return NTDB_ERR_EINVAL;
  32. }
  33. tap_log_messages--;
  34. if (!xfail(ntdb_transaction_start(ntdb))) {
  35. return NTDB_ERR_EINVAL;
  36. }
  37. tap_log_messages--;
  38. if (!xfail(ntdb_chainlock(ntdb, key))) {
  39. return NTDB_ERR_EINVAL;
  40. }
  41. tap_log_messages--;
  42. if (!xfail(ntdb_lockall(ntdb))) {
  43. return NTDB_ERR_EINVAL;
  44. }
  45. tap_log_messages--;
  46. if (!xfail(ntdb_wipe_all(ntdb))) {
  47. return NTDB_ERR_EINVAL;
  48. }
  49. tap_log_messages--;
  50. if (!xfail(ntdb_repack(ntdb))) {
  51. return NTDB_ERR_EINVAL;
  52. }
  53. tap_log_messages--;
  54. /* Access the record one more time. */
  55. if (!ntdb_deq(data, *expected)) {
  56. return NTDB_ERR_EINVAL;
  57. }
  58. return NTDB_SUCCESS;
  59. }
  60. int main(int argc, char *argv[])
  61. {
  62. unsigned int i;
  63. int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP, NTDB_CONVERT };
  64. NTDB_DATA key = ntdb_mkdata("hello", 5), data = ntdb_mkdata("world", 5);
  65. plan_tests(sizeof(flags) / sizeof(flags[0]) * 2 + 1);
  66. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  67. ntdb = ntdb_open("api-95-read-only-during-parse.ntdb",
  68. flags[i]|MAYBE_NOSYNC,
  69. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  70. ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_SUCCESS);
  71. ok1(ntdb_parse_record(ntdb, key, parse, &data) == NTDB_SUCCESS);
  72. ntdb_close(ntdb);
  73. }
  74. ok1(tap_log_messages == 0);
  75. return exit_status();
  76. }