api-21-parse_record.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "config.h"
  2. #include "ntdb.h"
  3. #include "private.h"
  4. #include "tap-interface.h"
  5. #include "logging.h"
  6. static enum NTDB_ERROR parse(NTDB_DATA key, NTDB_DATA data, NTDB_DATA *expected)
  7. {
  8. if (!ntdb_deq(data, *expected))
  9. return NTDB_ERR_EINVAL;
  10. return NTDB_SUCCESS;
  11. }
  12. static enum NTDB_ERROR parse_err(NTDB_DATA key, NTDB_DATA data, void *unused)
  13. {
  14. return 100;
  15. }
  16. static bool test_records(struct ntdb_context *ntdb)
  17. {
  18. int i;
  19. NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
  20. NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
  21. for (i = 0; i < 1000; i++) {
  22. if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
  23. return false;
  24. }
  25. for (i = 0; i < 1000; i++) {
  26. if (ntdb_parse_record(ntdb, key, parse, &data) != NTDB_SUCCESS)
  27. return false;
  28. }
  29. if (ntdb_parse_record(ntdb, key, parse, &data) != NTDB_ERR_NOEXIST)
  30. return false;
  31. /* Test error return from parse function. */
  32. i = 0;
  33. if (ntdb_parse_record(ntdb, key, parse_err, NULL) != 100)
  34. return false;
  35. return true;
  36. }
  37. int main(int argc, char *argv[])
  38. {
  39. unsigned int i;
  40. struct ntdb_context *ntdb;
  41. int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
  42. NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
  43. NTDB_NOMMAP|NTDB_CONVERT };
  44. plan_tests(sizeof(flags) / sizeof(flags[0]) * 2 + 1);
  45. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  46. ntdb = ntdb_open("api-21-parse_record.ntdb",
  47. flags[i]|MAYBE_NOSYNC,
  48. O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
  49. if (ok1(ntdb))
  50. ok1(test_records(ntdb));
  51. ntdb_close(ntdb);
  52. }
  53. ok1(tap_log_messages == 0);
  54. return exit_status();
  55. }