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