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