tdb2: Fix typo in TDB1_porting.txt
[sharpe/samba-autobuild/.git] / lib / tdb2 / test / api-21-parse_record.c
1 #include "config.h"
2 #include "tdb2.h"
3 #include "tap-interface.h"
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include "logging.h"
8
9 static enum TDB_ERROR parse(TDB_DATA key, TDB_DATA data, TDB_DATA *expected)
10 {
11         if (!tdb_deq(data, *expected))
12                 return TDB_ERR_EINVAL;
13         return TDB_SUCCESS;
14 }
15
16 static enum TDB_ERROR parse_err(TDB_DATA key, TDB_DATA data, void *unused)
17 {
18         return 100;
19 }
20
21 static bool test_records(struct tdb_context *tdb)
22 {
23         int i;
24         struct tdb_data key = { (unsigned char *)&i, sizeof(i) };
25         struct tdb_data data = { (unsigned char *)&i, sizeof(i) };
26
27         for (i = 0; i < 1000; i++) {
28                 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
29                         return false;
30         }
31
32         for (i = 0; i < 1000; i++) {
33                 if (tdb_parse_record(tdb, key, parse, &data) != TDB_SUCCESS)
34                         return false;
35         }
36
37         if (tdb_parse_record(tdb, key, parse, &data) != TDB_ERR_NOEXIST)
38                 return false;
39
40         /* Test error return from parse function. */
41         i = 0;
42         if (tdb_parse_record(tdb, key, parse_err, NULL) != 100)
43                 return false;
44
45         return true;
46 }
47
48 int main(int argc, char *argv[])
49 {
50         unsigned int i;
51         struct tdb_context *tdb;
52         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
53                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
54                         TDB_NOMMAP|TDB_CONVERT };
55
56         plan_tests(sizeof(flags) / sizeof(flags[0]) * 2 + 1);
57         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
58                 tdb = tdb_open("api-21-parse_record.tdb", flags[i],
59                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
60                 if (ok1(tdb))
61                         ok1(test_records(tdb));
62                 tdb_close(tdb);
63         }
64
65         ok1(tap_log_messages == 0);
66         return exit_status();
67 }