api-check-callback.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <ccan/tdb2/tdb2.h>
  2. #include <ccan/tap/tap.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include "logging.h"
  7. #define NUM_RECORDS 1000
  8. static bool store_records(struct tdb_context *tdb)
  9. {
  10. int i;
  11. struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
  12. struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
  13. for (i = 0; i < NUM_RECORDS; i++)
  14. if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
  15. return false;
  16. return true;
  17. }
  18. static enum TDB_ERROR check(struct tdb_data key,
  19. struct tdb_data data,
  20. bool *array)
  21. {
  22. int val;
  23. if (key.dsize != sizeof(val)) {
  24. diag("Wrong key size: %u\n", key.dsize);
  25. return TDB_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 TDB_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 TDB_ERR_CORRUPT;
  36. }
  37. if (array[val]) {
  38. diag("Value %i already seen\n", val);
  39. return TDB_ERR_CORRUPT;
  40. }
  41. array[val] = true;
  42. return TDB_SUCCESS;
  43. }
  44. int main(int argc, char *argv[])
  45. {
  46. unsigned int i, j;
  47. struct tdb_context *tdb;
  48. int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
  49. TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
  50. TDB_NOMMAP|TDB_CONVERT,
  51. TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
  52. TDB_NOMMAP|TDB_VERSION1,
  53. TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
  54. TDB_CONVERT|TDB_VERSION1,
  55. TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
  56. plan_tests(sizeof(flags) / sizeof(flags[0]) * 4 + 1);
  57. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  58. bool array[NUM_RECORDS];
  59. tdb = tdb_open("run-check-callback.tdb", flags[i],
  60. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  61. ok1(tdb);
  62. if (!tdb)
  63. continue;
  64. ok1(store_records(tdb));
  65. for (j = 0; j < NUM_RECORDS; j++)
  66. array[j] = false;
  67. ok1(tdb_check(tdb, check, array) == TDB_SUCCESS);
  68. for (j = 0; j < NUM_RECORDS; j++)
  69. if (!array[j])
  70. break;
  71. ok1(j == NUM_RECORDS);
  72. tdb_close(tdb);
  73. }
  74. ok1(tap_log_messages == 0);
  75. return exit_status();
  76. }