VERSION: Bump version number up to 4.1.3...
[Samba.git] / lib / ntdb / test / api-55-transaction.c
blob01a1dcea1b8f33e64801806ca8f8ed7add3c6d6d
1 #include "private.h" // struct ntdb_context
2 #include "ntdb.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"
10 int main(int argc, char *argv[])
12 unsigned int i;
13 struct ntdb_context *ntdb;
14 unsigned char *buffer;
15 int flags[] = { NTDB_DEFAULT, NTDB_NOMMAP,
16 NTDB_CONVERT, NTDB_NOMMAP|NTDB_CONVERT };
17 NTDB_DATA key = ntdb_mkdata("key", 3);
18 NTDB_DATA data;
20 buffer = malloc(1000);
21 for (i = 0; i < 1000; i++)
22 buffer[i] = i;
24 plan_tests(sizeof(flags) / sizeof(flags[0]) * 20 + 1);
26 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
27 ntdb = ntdb_open("run-55-transaction.ntdb",
28 flags[i]|MAYBE_NOSYNC,
29 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
30 ok1(ntdb);
31 if (!ntdb)
32 continue;
34 ok1(ntdb_transaction_start(ntdb) == 0);
35 data.dptr = buffer;
36 data.dsize = 1000;
37 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
38 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
39 ok1(data.dsize == 1000);
40 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
41 free(data.dptr);
43 /* Cancelling a transaction means no store */
44 ntdb_transaction_cancel(ntdb);
45 ok1(ntdb->file->allrecord_lock.count == 0
46 && ntdb->file->num_lockrecs == 0);
47 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
48 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_ERR_NOEXIST);
50 /* Commit the transaction. */
51 ok1(ntdb_transaction_start(ntdb) == 0);
52 data.dptr = buffer;
53 data.dsize = 1000;
54 ok1(ntdb_store(ntdb, key, data, NTDB_INSERT) == 0);
55 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
56 ok1(data.dsize == 1000);
57 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
58 free(data.dptr);
59 ok1(ntdb_transaction_commit(ntdb) == 0);
60 ok1(ntdb->file->allrecord_lock.count == 0
61 && ntdb->file->num_lockrecs == 0);
62 ok1(ntdb_check(ntdb, NULL, NULL) == 0);
63 ok1(ntdb_fetch(ntdb, key, &data) == NTDB_SUCCESS);
64 ok1(data.dsize == 1000);
65 ok1(memcmp(data.dptr, buffer, data.dsize) == 0);
66 free(data.dptr);
68 ntdb_close(ntdb);
71 ok1(tap_log_messages == 0);
72 free(buffer);
73 return exit_status();