api-check-callback.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "config.h"
  2. #include "../ntdb.h"
  3. #include "../private.h"
  4. #include "tap-interface.h"
  5. #include "logging.h"
  6. #include "helpapi-external-agent.h"
  7. #define NUM_RECORDS 1000
  8. static bool store_records(struct ntdb_context *ntdb)
  9. {
  10. int i;
  11. NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
  12. NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
  13. for (i = 0; i < NUM_RECORDS; i++)
  14. if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
  15. return false;
  16. return true;
  17. }
  18. static enum NTDB_ERROR check(NTDB_DATA key,
  19. NTDB_DATA data,
  20. bool *array)
  21. {
  22. int val;
  23. if (key.dsize != sizeof(val)) {
  24. diag("Wrong key size: %zu\n", key.dsize);
  25. return NTDB_ERR_CORRUPT;
  26. }
  27. if (key.dsize != data.dsize
  28. || memcmp(key.dptr, data.dptr, sizeof(val)) != 0) {
  29. diag("Key and data differ\n");
  30. return NTDB_ERR_CORRUPT;
  31. }
  32. memcpy(&val, key.dptr, sizeof(val));
  33. if (val >= NUM_RECORDS || val < 0) {
  34. diag("check value %i\n", val);
  35. return NTDB_ERR_CORRUPT;
  36. }
  37. if (array[val]) {
  38. diag("Value %i already seen\n", val);
  39. return NTDB_ERR_CORRUPT;
  40. }
  41. array[val] = true;
  42. return NTDB_SUCCESS;
  43. }
  44. int main(int argc, char *argv[])
  45. {
  46. unsigned int i, j;
  47. struct ntdb_context *ntdb;
  48. int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
  49. NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
  50. NTDB_NOMMAP|NTDB_CONVERT };
  51. return 0;
  52. plan_tests(sizeof(flags) / sizeof(flags[0]) * 4 + 1);
  53. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  54. bool array[NUM_RECORDS];
  55. ntdb = ntdb_open("run-check-callback.ntdb",
  56. flags[i]|MAYBE_NOSYNC,
  57. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  58. ok1(ntdb);
  59. if (!ntdb)
  60. continue;
  61. ok1(store_records(ntdb));
  62. for (j = 0; j < NUM_RECORDS; j++)
  63. array[j] = false;
  64. ok1(ntdb_check(ntdb, check, array) == NTDB_SUCCESS);
  65. for (j = 0; j < NUM_RECORDS; j++)
  66. if (!array[j])
  67. break;
  68. ok1(j == NUM_RECORDS);
  69. ntdb_close(ntdb);
  70. }
  71. ok1(tap_log_messages == 0);
  72. return exit_status();
  73. }