]> git.ozlabs.org Git - ccan/blob - ccan/ntdb/test/api-95-read-only-during-parse.c
ntdb: next-generation trivial key-value database
[ccan] / ccan / ntdb / test / api-95-read-only-during-parse.c
1 /* Make sure write operations fail during ntdb_parse(). */
2 #include "config.h"
3 #include "ntdb.h"
4 #include "private.h"
5 #include "tap-interface.h"
6 #include "logging.h"
7
8 static struct ntdb_context *ntdb;
9
10 /* We could get either of these. */
11 static bool xfail(enum NTDB_ERROR ecode)
12 {
13         return ecode == NTDB_ERR_RDONLY || ecode == NTDB_ERR_LOCK;
14 }
15
16 static enum NTDB_ERROR parse(NTDB_DATA key, NTDB_DATA data,
17                              NTDB_DATA *expected)
18 {
19         NTDB_DATA add = ntdb_mkdata("another", strlen("another"));
20
21         if (!ntdb_deq(data, *expected)) {
22                 return NTDB_ERR_EINVAL;
23         }
24
25         /* These should all fail.*/
26         if (!xfail(ntdb_store(ntdb, add, add, NTDB_INSERT))) {
27                 return NTDB_ERR_EINVAL;
28         }
29         tap_log_messages--;
30
31         if (!xfail(ntdb_append(ntdb, key, add))) {
32                 return NTDB_ERR_EINVAL;
33         }
34         tap_log_messages--;
35
36         if (!xfail(ntdb_delete(ntdb, key))) {
37                 return NTDB_ERR_EINVAL;
38         }
39         tap_log_messages--;
40
41         if (!xfail(ntdb_transaction_start(ntdb))) {
42                 return NTDB_ERR_EINVAL;
43         }
44         tap_log_messages--;
45
46         if (!xfail(ntdb_chainlock(ntdb, key))) {
47                 return NTDB_ERR_EINVAL;
48         }
49         tap_log_messages--;
50
51         if (!xfail(ntdb_lockall(ntdb))) {
52                 return NTDB_ERR_EINVAL;
53         }
54         tap_log_messages--;
55
56         if (!xfail(ntdb_wipe_all(ntdb))) {
57                 return NTDB_ERR_EINVAL;
58         }
59         tap_log_messages--;
60
61         if (!xfail(ntdb_repack(ntdb))) {
62                 return NTDB_ERR_EINVAL;
63         }
64         tap_log_messages--;
65
66         /* Access the record one more time. */
67         if (!ntdb_deq(data, *expected)) {
68                 return NTDB_ERR_EINVAL;
69         }
70
71         return NTDB_SUCCESS;
72 }
73
74 int main(int argc, char *argv[])
75 {
76         unsigned int i;
77         int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP, NTDB_CONVERT };
78         NTDB_DATA key = ntdb_mkdata("hello", 5), data = ntdb_mkdata("world", 5);
79
80         plan_tests(sizeof(flags) / sizeof(flags[0]) * 2 + 1);
81         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
82                 ntdb = ntdb_open("api-95-read-only-during-parse.ntdb",
83                                  flags[i]|MAYBE_NOSYNC,
84                                  O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
85                 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == NTDB_SUCCESS);
86                 ok1(ntdb_parse_record(ntdb, key, parse, &data) == NTDB_SUCCESS);
87                 ntdb_close(ntdb);
88         }
89
90         ok1(tap_log_messages == 0);
91         return exit_status();
92 }