tdb2: change API to return the error value.
[ccan] / ccan / tdb2 / test / run-11-simple-fetch.c
1 #include <ccan/tdb2/tdb.c>
2 #include <ccan/tdb2/free.c>
3 #include <ccan/tdb2/lock.c>
4 #include <ccan/tdb2/io.c>
5 #include <ccan/tdb2/hash.c>
6 #include <ccan/tdb2/transaction.c>
7 #include <ccan/tdb2/check.c>
8 #include <ccan/tap/tap.h>
9 #include "logging.h"
10
11 int main(int argc, char *argv[])
12 {
13         unsigned int i;
14         struct tdb_context *tdb;
15         int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
16                         TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
17                         TDB_NOMMAP|TDB_CONVERT };
18         struct tdb_data key = { (unsigned char *)"key", 3 };
19         struct tdb_data data = { (unsigned char *)"data", 4 };
20
21         plan_tests(sizeof(flags) / sizeof(flags[0]) * 8 + 1);
22         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
23                 tdb = tdb_open("run-11-simple-fetch.tdb", flags[i],
24                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
25                 ok1(tdb);
26                 if (tdb) {
27                         struct tdb_data d;
28
29                         /* fetch should fail. */
30                         ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_NOEXIST);
31                         ok1(tdb_check(tdb, NULL, NULL) == 0);
32                         /* Insert should succeed. */
33                         ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
34                         ok1(tdb_check(tdb, NULL, NULL) == 0);
35                         /* Fetch should now work. */
36                         ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS);
37                         ok1(data_equal(d, data));
38                         free(d.dptr);
39                         ok1(tdb_check(tdb, NULL, NULL) == 0);
40                         tdb_close(tdb);
41                 }
42         }
43         ok1(tap_log_messages == 0);
44         return exit_status();
45 }