ntdb: test arbitrary operations during ntdb_parse_record().
[ddiss/samba.git] / lib / ntdb / test / api-94-expand-during-parse.c
1 /* We use direct access to hand to the parse function: what if db expands? */
2 #include "config.h"
3 #include "ntdb.h"
4 #include "tap-interface.h"
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include "logging.h"
9 #include "../private.h" /* To establish size, esp. for NTDB_INTERNAL dbs */
10
11 static struct ntdb_context *ntdb;
12
13 static off_t ntdb_size(void)
14 {
15         return ntdb->file->map_size;
16 }
17
18 struct parse_info {
19         unsigned int depth;
20         NTDB_DATA expected;
21 };
22
23 static enum NTDB_ERROR parse(NTDB_DATA key, NTDB_DATA data,
24                              struct parse_info *pinfo)
25 {
26         off_t flen;
27         unsigned int i;
28
29         if (!ntdb_deq(data, pinfo->expected))
30                 return NTDB_ERR_EINVAL;
31
32         flen = ntdb_size();
33
34         for (i = 0; ntdb_size() == flen; i++) {
35                 NTDB_DATA add = ntdb_mkdata(&i, sizeof(i));
36
37                 /* This is technically illegal parse(), which is why we
38                  * grabbed allrecord lock.*/
39                 ntdb_store(ntdb, add, add, NTDB_INSERT);
40         }
41
42         /* Access the record again. */
43         if (!ntdb_deq(data, pinfo->expected))
44                 return NTDB_ERR_EINVAL;
45
46         /* Recurse!  Woot! */
47         if (pinfo->depth != 0) {
48                 enum NTDB_ERROR ecode;
49
50                 pinfo->depth--;
51                 ecode = ntdb_parse_record(ntdb, key, parse, pinfo);
52                 if (ecode) {
53                         return ecode;
54                 }
55         }
56
57         /* Access the record one more time. */
58         if (!ntdb_deq(data, pinfo->expected))
59                 return NTDB_ERR_EINVAL;
60
61         return NTDB_SUCCESS;
62 }
63
64 int main(int argc, char *argv[])
65 {
66         unsigned int i;
67         int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
68                         NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
69                         NTDB_NOMMAP|NTDB_CONVERT };
70         struct parse_info pinfo;
71         NTDB_DATA key = ntdb_mkdata("hello", 5), data = ntdb_mkdata("world", 5);
72
73         plan_tests(sizeof(flags) / sizeof(flags[0]) * 3 + 1);
74         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
75                 ntdb = ntdb_open("api-94-expand-during-parse.ntdb",
76                                  flags[i]|MAYBE_NOSYNC,
77                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
78                 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_SUCCESS);
79                 ok1(ntdb_lockall(ntdb) == NTDB_SUCCESS);
80                 pinfo.expected = data;
81                 pinfo.depth = 3;
82                 ok1(ntdb_parse_record(ntdb, key, parse, &pinfo) == NTDB_SUCCESS);
83                 ntdb_unlockall(ntdb);
84                 ntdb_close(ntdb);
85         }
86
87         ok1(tap_log_messages == 0);
88         return exit_status();
89 }