run-wronghash-fail.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #define _XOPEN_SOURCE 500
  2. #include <ccan/tdb/tdb.h>
  3. #include <ccan/tdb/io.c>
  4. #include <ccan/tdb/tdb.c>
  5. #include <ccan/tdb/lock.c>
  6. #include <ccan/tdb/freelist.c>
  7. #include <ccan/tdb/traverse.c>
  8. #include <ccan/tdb/transaction.c>
  9. #include <ccan/tdb/error.c>
  10. #include <ccan/tdb/open.c>
  11. #include <ccan/tdb/check.c>
  12. #include <ccan/tdb/hash.c>
  13. #include <ccan/tap/tap.h>
  14. #include <stdlib.h>
  15. #include <err.h>
  16. static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level, const char *fmt, ...)
  17. {
  18. unsigned int *count = tdb_get_logging_private(tdb);
  19. if (strstr(fmt, "hash"))
  20. (*count)++;
  21. }
  22. int main(int argc, char *argv[])
  23. {
  24. struct tdb_context *tdb;
  25. unsigned int log_count;
  26. struct tdb_logging_context log_ctx = { log_fn, &log_count };
  27. plan_tests(18);
  28. /* Create with default hash. */
  29. log_count = 0;
  30. tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0,
  31. O_CREAT|O_RDWR|O_TRUNC, 0600, &log_ctx, NULL);
  32. ok1(tdb);
  33. ok1(log_count == 0);
  34. tdb_close(tdb);
  35. /* Fail to open with different hash. */
  36. tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
  37. &log_ctx, tdb_jenkins_hash);
  38. ok1(!tdb);
  39. ok1(log_count == 1);
  40. /* Create with different hash. */
  41. log_count = 0;
  42. tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0,
  43. O_CREAT|O_RDWR|O_TRUNC,
  44. 0600, &log_ctx, tdb_jenkins_hash);
  45. ok1(tdb);
  46. ok1(log_count == 0);
  47. tdb_close(tdb);
  48. /* Endian should be no problem. */
  49. log_count = 0;
  50. tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDWR, 0,
  51. &log_ctx, NULL);
  52. ok1(!tdb);
  53. ok1(log_count == 1);
  54. log_count = 0;
  55. tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDWR, 0,
  56. &log_ctx, NULL);
  57. ok1(!tdb);
  58. ok1(log_count == 1);
  59. log_count = 0;
  60. /* Fail to open with default hash. */
  61. tdb = tdb_open_ex("run-wronghash-fail.tdb", 0, 0, O_RDWR, 0,
  62. &log_ctx, NULL);
  63. ok1(!tdb);
  64. ok1(log_count == 1);
  65. log_count = 0;
  66. tdb = tdb_open_ex("test/jenkins-le-hash.tdb", 0, 0, O_RDONLY,
  67. 0, &log_ctx, tdb_jenkins_hash);
  68. ok1(tdb);
  69. ok1(log_count == 0);
  70. ok1(tdb_check(tdb, NULL, NULL) == 0);
  71. tdb_close(tdb);
  72. log_count = 0;
  73. tdb = tdb_open_ex("test/jenkins-be-hash.tdb", 0, 0, O_RDONLY,
  74. 0, &log_ctx, tdb_jenkins_hash);
  75. ok1(tdb);
  76. ok1(log_count == 0);
  77. ok1(tdb_check(tdb, NULL, NULL) == 0);
  78. tdb_close(tdb);
  79. return exit_status();
  80. }