run-25-hashoverload.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "ntdb-source.h"
  2. #include "tap-interface.h"
  3. #include "logging.h"
  4. #include "helprun-external-agent.h"
  5. #define OVERLOAD 100
  6. static uint32_t badhash(const void *key, size_t len, uint32_t seed, void *priv)
  7. {
  8. return 0;
  9. }
  10. static int trav(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA dbuf, void *p)
  11. {
  12. if (p)
  13. return ntdb_delete(ntdb, key);
  14. return 0;
  15. }
  16. int main(int argc, char *argv[])
  17. {
  18. unsigned int i, j;
  19. struct ntdb_context *ntdb;
  20. NTDB_DATA key = { (unsigned char *)&j, sizeof(j) };
  21. NTDB_DATA dbuf = { (unsigned char *)&j, sizeof(j) };
  22. union ntdb_attribute hattr = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
  23. .fn = badhash } };
  24. int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
  25. NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
  26. NTDB_NOMMAP|NTDB_CONVERT,
  27. };
  28. hattr.base.next = &tap_log_attr;
  29. plan_tests(sizeof(flags) / sizeof(flags[0]) * (7 * OVERLOAD + 11) + 1);
  30. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  31. NTDB_DATA d = { NULL, 0 }; /* Bogus GCC warning */
  32. ntdb = ntdb_open("run-25-hashoverload.ntdb",
  33. flags[i]|MAYBE_NOSYNC,
  34. O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
  35. ok1(ntdb);
  36. if (!ntdb)
  37. continue;
  38. /* Overload a bucket. */
  39. for (j = 0; j < OVERLOAD; j++) {
  40. ok1(ntdb_store(ntdb, key, dbuf, NTDB_INSERT) == 0);
  41. }
  42. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  43. /* Check we can find them all. */
  44. for (j = 0; j < OVERLOAD; j++) {
  45. ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
  46. ok1(d.dsize == sizeof(j));
  47. ok1(d.dptr != NULL);
  48. ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
  49. free(d.dptr);
  50. }
  51. /* Traverse through them. */
  52. ok1(ntdb_traverse(ntdb, trav, NULL) == OVERLOAD);
  53. /* Delete the first 99. */
  54. for (j = 0; j < OVERLOAD-1; j++)
  55. ok1(ntdb_delete(ntdb, key) == 0);
  56. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  57. ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
  58. ok1(d.dsize == sizeof(j));
  59. ok1(d.dptr != NULL);
  60. ok1(d.dptr && memcmp(d.dptr, &j, d.dsize) == 0);
  61. free(d.dptr);
  62. /* Traverse through them. */
  63. ok1(ntdb_traverse(ntdb, trav, NULL) == 1);
  64. /* Re-add */
  65. for (j = 0; j < OVERLOAD-1; j++) {
  66. ok1(ntdb_store(ntdb, key, dbuf, NTDB_INSERT) == 0);
  67. }
  68. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  69. /* Now try deleting as we go. */
  70. ok1(ntdb_traverse(ntdb, trav, trav) == OVERLOAD);
  71. ok1(ntdb_check(ntdb, NULL, NULL) == 0);
  72. ok1(ntdb_traverse(ntdb, trav, NULL) == 0);
  73. ntdb_close(ntdb);
  74. }
  75. ok1(tap_log_messages == 0);
  76. return exit_status();
  77. }