lib/tdb2: adapt unit tests to SAMBA environment.
[kai/samba-autobuild/.git] / lib / tdb2 / test / api-55-transaction.c
1 #include "private.h" // struct tdb_context
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 <stdlib.h>
8 #include "logging.h"
9
10 int main(int argc, char *argv[])
11 {
12         unsigned int i;
13         struct tdb_context *tdb;
14         unsigned char *buffer;
15         int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
16                         TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
17                         TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
18                         TDB_CONVERT|TDB_VERSION1,
19                         TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
20         struct tdb_data key = tdb_mkdata("key", 3);
21         struct tdb_data data;
22
23         buffer = malloc(1000);
24         for (i = 0; i < 1000; i++)
25                 buffer[i] = i;
26
27         plan_tests(sizeof(flags) / sizeof(flags[0]) * 20 + 1);
28
29         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
30                 tdb = tdb_open("run-55-transaction.tdb", flags[i],
31                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
32                 ok1(tdb);
33                 if (!tdb)
34                         continue;
35
36                 ok1(tdb_transaction_start(tdb) == 0);
37                 data.dptr = buffer;
38                 data.dsize = 1000;
39                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
40                 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
41                 ok1(data.dsize == 1000);
42                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
43                 free(data.dptr);
44
45                 /* Cancelling a transaction means no store */
46                 tdb_transaction_cancel(tdb);
47                 ok1(tdb->file->allrecord_lock.count == 0
48                     && tdb->file->num_lockrecs == 0);
49                 ok1(tdb_check(tdb, NULL, NULL) == 0);
50                 ok1(tdb_fetch(tdb, key, &data) == TDB_ERR_NOEXIST);
51
52                 /* Commit the transaction. */
53                 ok1(tdb_transaction_start(tdb) == 0);
54                 data.dptr = buffer;
55                 data.dsize = 1000;
56                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
57                 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
58                 ok1(data.dsize == 1000);
59                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
60                 free(data.dptr);
61                 ok1(tdb_transaction_commit(tdb) == 0);
62                 ok1(tdb->file->allrecord_lock.count == 0
63                     && tdb->file->num_lockrecs == 0);
64                 ok1(tdb_check(tdb, NULL, NULL) == 0);
65                 ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
66                 ok1(data.dsize == 1000);
67                 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
68                 free(data.dptr);
69
70                 tdb_close(tdb);
71         }
72
73         ok1(tap_log_messages == 0);
74         free(buffer);
75         return exit_status();
76 }