api-21-parse_record.c 1.6 KB

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