lib/ntdb optimize includes in ntdb tests
[sfrench/samba-autobuild/.git] / lib / ntdb / test / api-21-parse_record.c
1 #include "config.h"
2 #include "ntdb.h"
3 #include "private.h"
4 #include "tap-interface.h"
5 #include "logging.h"
6
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
14 static enum NTDB_ERROR parse_err(NTDB_DATA key, NTDB_DATA data, void *unused)
15 {
16         return 100;
17 }
18
19 static bool test_records(struct ntdb_context *ntdb)
20 {
21         int i;
22         NTDB_DATA key = { (unsigned char *)&i, sizeof(i) };
23         NTDB_DATA data = { (unsigned char *)&i, sizeof(i) };
24
25         for (i = 0; i < 1000; i++) {
26                 if (ntdb_store(ntdb, key, data, NTDB_REPLACE) != 0)
27                         return false;
28         }
29
30         for (i = 0; i < 1000; i++) {
31                 if (ntdb_parse_record(ntdb, key, parse, &data) != NTDB_SUCCESS)
32                         return false;
33         }
34
35         if (ntdb_parse_record(ntdb, key, parse, &data) != NTDB_ERR_NOEXIST)
36                 return false;
37
38         /* Test error return from parse function. */
39         i = 0;
40         if (ntdb_parse_record(ntdb, key, parse_err, NULL) != 100)
41                 return false;
42
43         return true;
44 }
45
46 int main(int argc, char *argv[])
47 {
48         unsigned int i;
49         struct ntdb_context *ntdb;
50         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
51                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
52                         NTDB_NOMMAP|NTDB_CONVERT };
53
54         plan_tests(sizeof(flags) / sizeof(flags[0]) * 2 + 1);
55         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
56                 ntdb = ntdb_open("api-21-parse_record.ntdb",
57                                  flags[i]|MAYBE_NOSYNC,
58                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
59                 if (ok1(ntdb))
60                         ok1(test_records(ntdb));
61                 ntdb_close(ntdb);
62         }
63
64         ok1(tap_log_messages == 0);
65         return exit_status();
66 }