s3:libads: Fix creating machine account using LDAP
[bbaumbach/samba-autobuild/.git] / source3 / torture / test_dbwrap_ctdb.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Test dbwrap_ctdb API
4  * Copyright (C) Volker Lendecke 2012
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "torture/proto.h"
22 #include "system/filesys.h"
23 #include "lib/dbwrap/dbwrap.h"
24 #include "lib/dbwrap/dbwrap_ctdb.h"
25 #include "messages.h"
26 #include "lib/messages_ctdb.h"
27
28 bool run_local_dbwrap_ctdb(int dummy)
29 {
30         struct db_context *db = NULL;
31         int res;
32         bool ret = false;
33         NTSTATUS status;
34         uint32_t val;
35         struct messaging_context *msg_ctx;
36
37         msg_ctx = global_messaging_context();
38
39         db = db_open_ctdb(talloc_tos(), msg_ctx, "torture.tdb",
40                           0, TDB_DEFAULT,
41                           O_RDWR, 0755, DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
42         if (db == NULL) {
43                 perror("db_open_ctdb failed");
44                 goto fail;
45         }
46
47         res = dbwrap_transaction_start(db);
48         if (res != 0) {
49                 fprintf(stderr, "dbwrap_transaction_start failed");
50                 goto fail;
51         }
52         res = dbwrap_transaction_cancel(db);
53         if (res != 0) {
54                 fprintf(stderr, "dbwrap_transaction_cancel failed");
55                 goto fail;
56         }
57
58         res = dbwrap_transaction_start(db);
59         if (res != 0) {
60                 fprintf(stderr, "dbwrap_transaction_start failed");
61                 goto fail;
62         }
63
64         status = dbwrap_store_uint32_bystring(db, "foo", 1);
65         if (!NT_STATUS_IS_OK(status)) {
66                 fprintf(stderr, "store_uint32 failed: %s\n",
67                         nt_errstr(status));
68                 goto fail;
69         }
70         status = dbwrap_fetch_uint32_bystring(db, "foo", &val);
71         if (!NT_STATUS_IS_OK(status)) {
72                 fprintf(stderr, "fetch_uint32 failed: %s\n",
73                         nt_errstr(status));
74                 goto fail;
75         }
76         if (val != 1) {
77                 fprintf(stderr, "fetch_uint32 gave %u, expected 1",
78                         (unsigned)val);
79                 goto fail;
80         }
81
82         status = dbwrap_store_uint32_bystring(db, "bar", 5);
83         if (!NT_STATUS_IS_OK(status)) {
84                 fprintf(stderr, "store_uint32 failed: %s\n",
85                         nt_errstr(status));
86                 goto fail;
87         }
88         status = dbwrap_fetch_uint32_bystring(db, "bar", &val);
89         if (!NT_STATUS_IS_OK(status)) {
90                 fprintf(stderr, "fetch_uint32 failed: %s\n",
91                         nt_errstr(status));
92                 goto fail;
93         }
94         if (val != 5) {
95                 fprintf(stderr, "fetch_uint32 gave %u, expected 5",
96                         (unsigned)val);
97                 goto fail;
98         }
99
100         status = dbwrap_store_uint32_bystring(db, "foo", 2);
101         if (!NT_STATUS_IS_OK(status)) {
102                 fprintf(stderr, "store_uint32 failed: %s\n",
103                         nt_errstr(status));
104                 goto fail;
105         }
106         status = dbwrap_fetch_uint32_bystring(db, "foo", &val);
107         if (!NT_STATUS_IS_OK(status)) {
108                 fprintf(stderr, "fetch_uint32 failed: %s\n",
109                         nt_errstr(status));
110                 goto fail;
111         }
112         if (val != 2) {
113                 fprintf(stderr, "fetch_uint32 gave %u, expected 2",
114                         (unsigned)val);
115                 goto fail;
116         }
117
118         res = dbwrap_transaction_commit(db);
119         if (res != 0) {
120                 fprintf(stderr, "dbwrap_transaction_commit failed");
121                 goto fail;
122         }
123
124         /*
125          * check that the values have reached the disk
126          */
127         status = dbwrap_fetch_uint32_bystring(db, "foo", &val);
128         if (!NT_STATUS_IS_OK(status)) {
129                 fprintf(stderr, "fetch_uint32 failed: %s\n",
130                         nt_errstr(status));
131                 goto fail;
132         }
133         if (val != 2) {
134                 fprintf(stderr, "fetch_uint32 gave %u, expected 1",
135                         (unsigned)val);
136                 goto fail;
137         }
138
139         status = dbwrap_fetch_uint32_bystring(db, "bar", &val);
140         if (!NT_STATUS_IS_OK(status)) {
141                 fprintf(stderr, "fetch_uint32 failed: %s\n",
142                         nt_errstr(status));
143                 goto fail;
144         }
145         if (val != 5) {
146                 fprintf(stderr, "fetch_uint32 gave %u, expected 1",
147                         (unsigned)val);
148                 goto fail;
149         }
150
151         ret = true;
152 fail:
153         TALLOC_FREE(db);
154         return ret;
155 }