ctdb-protocol: Add missing push support for new controls
[Samba.git] / ctdb / tests / src / protocol_ctdb_test.c
blob840d465ae30d22a69d4422e818f2027ac098596b
1 /*
2 protocol tests
4 Copyright (C) Amitay Isaacs 2015
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.
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.
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/>.
20 #include <assert.h>
22 #include "protocol/protocol_basic.c"
23 #include "protocol/protocol_types.c"
24 #include "protocol/protocol_header.c"
25 #include "protocol/protocol_call.c"
26 #include "protocol/protocol_control.c"
27 #include "protocol/protocol_message.c"
28 #include "protocol/protocol_keepalive.c"
29 #include "protocol/protocol_tunnel.c"
30 #include "protocol/protocol_packet.c"
32 #include "tests/src/protocol_common.h"
33 #include "tests/src/protocol_common_ctdb.h"
36 * Functions to test marshalling
39 /* for ctdb_req_header */
40 #define PROTOCOL_CTDB1_TEST(TYPE, NAME) \
41 static void TEST_FUNC(NAME)(void) \
42 { \
43 TALLOC_CTX *mem_ctx; \
44 TYPE c1, c2; \
45 uint8_t *pkt; \
46 size_t pkt_len, buflen, np; \
47 int ret; \
49 protocol_test_iterate_tag("%s\n", #NAME); \
50 mem_ctx = talloc_new(NULL); \
51 assert(mem_ctx != NULL); \
52 FILL_FUNC(NAME)(&c1); \
53 buflen = LEN_FUNC(NAME)(&c1); \
54 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
55 assert(ret == 0); \
56 assert(pkt != NULL); \
57 assert(pkt_len >= buflen); \
58 np = 0; \
59 PUSH_FUNC(NAME)(&c1, pkt, &np); \
60 assert(np == buflen); \
61 np = 0; \
62 ret = PULL_FUNC(NAME)(pkt, pkt_len, &c2, &np); \
63 assert(ret == 0); \
64 assert(np == buflen); \
65 VERIFY_FUNC(NAME)(&c1, &c2); \
66 talloc_free(mem_ctx); \
69 /* for ctdb_req_control_data, ctdb_reply_control_data */
70 #define PROTOCOL_CTDB2_TEST(TYPE, NAME) \
71 static void TEST_FUNC(NAME)(uint32_t opcode) \
72 { \
73 TALLOC_CTX *mem_ctx; \
74 TYPE c1, c2; \
75 uint8_t *pkt; \
76 size_t pkt_len, buflen, np; \
77 int ret; \
79 protocol_test_iterate_tag("%s %u\n", #NAME, opcode); \
80 mem_ctx = talloc_new(NULL); \
81 assert(mem_ctx != NULL); \
82 FILL_FUNC(NAME)(mem_ctx, &c1, opcode); \
83 buflen = LEN_FUNC(NAME)(&c1); \
84 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
85 assert(ret == 0); \
86 assert(pkt != NULL); \
87 assert(pkt_len >= buflen); \
88 np = 0; \
89 PUSH_FUNC(NAME)(&c1, pkt, &np); \
90 assert(np == buflen); \
91 np = 0; \
92 ret = PULL_FUNC(NAME)(pkt, pkt_len, opcode, mem_ctx, &c2, &np); \
93 assert(ret == 0); \
94 assert(np == buflen); \
95 VERIFY_FUNC(NAME)(&c1, &c2); \
96 talloc_free(mem_ctx); \
99 /* for ctdb_message_data */
100 #define PROTOCOL_CTDB3_TEST(TYPE, NAME) \
101 static void TEST_FUNC(NAME)(uint64_t srvid) \
103 TALLOC_CTX *mem_ctx; \
104 TYPE c1, c2; \
105 uint8_t *pkt; \
106 size_t pkt_len, buflen, np; \
107 int ret; \
109 protocol_test_iterate_tag("%s %"PRIx64"\n", #NAME, srvid); \
110 mem_ctx = talloc_new(NULL); \
111 assert(mem_ctx != NULL); \
112 FILL_FUNC(NAME)(mem_ctx, &c1, srvid); \
113 buflen = LEN_FUNC(NAME)(&c1, srvid); \
114 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
115 assert(ret == 0); \
116 assert(pkt != NULL); \
117 assert(pkt_len >= buflen); \
118 np = 0; \
119 PUSH_FUNC(NAME)(&c1, srvid, pkt, &np); \
120 assert(np == buflen); \
121 np = 0; \
122 ret = PULL_FUNC(NAME)(pkt, pkt_len, srvid, mem_ctx, &c2, &np); \
123 assert(ret == 0); \
124 assert(np == buflen); \
125 VERIFY_FUNC(NAME)(&c1, &c2, srvid); \
126 talloc_free(mem_ctx); \
129 /* for ctdb_req_call, ctdb_reply_call, etc. */
130 #define PROTOCOL_CTDB4_TEST(TYPE, NAME, OPER) \
131 static void TEST_FUNC(NAME)(void) \
133 TALLOC_CTX *mem_ctx; \
134 struct ctdb_req_header h1, h2; \
135 TYPE c1, c2; \
136 uint8_t *pkt; \
137 size_t pkt_len, buflen, len; \
138 int ret; \
140 protocol_test_iterate_tag("%s\n", #NAME); \
141 mem_ctx = talloc_new(NULL); \
142 assert(mem_ctx != NULL); \
143 fill_ctdb_req_header(&h1); \
144 FILL_FUNC(NAME)(mem_ctx, &c1); \
145 buflen = LEN_FUNC(NAME)(&h1, &c1); \
146 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
147 assert(ret == 0); \
148 assert(pkt != NULL); \
149 assert(pkt_len >= buflen); \
150 len = 0; \
151 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &len); \
152 assert(ret == EMSGSIZE); \
153 assert(len == buflen); \
154 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &pkt_len); \
155 assert(ret == 0); \
156 ret = PULL_FUNC(NAME)(pkt, pkt_len, &h2, mem_ctx, &c2); \
157 assert(ret == 0); \
158 verify_ctdb_req_header(&h1, &h2); \
159 assert(h2.length == pkt_len); \
160 VERIFY_FUNC(NAME)(&c1, &c2); \
161 talloc_free(mem_ctx); \
164 /* for ctdb_req_control */
165 #define PROTOCOL_CTDB5_TEST(TYPE, NAME, OPER) \
166 static void TEST_FUNC(NAME)(uint32_t opcode) \
168 TALLOC_CTX *mem_ctx; \
169 struct ctdb_req_header h1, h2; \
170 TYPE c1, c2; \
171 uint8_t *pkt; \
172 size_t pkt_len, buflen, len; \
173 int ret; \
175 protocol_test_iterate_tag("%s %u\n", #NAME, opcode); \
176 mem_ctx = talloc_new(NULL); \
177 assert(mem_ctx != NULL); \
178 fill_ctdb_req_header(&h1); \
179 FILL_FUNC(NAME)(mem_ctx, &c1, opcode); \
180 buflen = LEN_FUNC(NAME)(&h1, &c1); \
181 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
182 assert(ret == 0); \
183 assert(pkt != NULL); \
184 assert(pkt_len >= buflen); \
185 len = 0; \
186 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &len); \
187 assert(ret == EMSGSIZE); \
188 assert(len == buflen); \
189 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &pkt_len); \
190 assert(ret == 0); \
191 ret = PULL_FUNC(NAME)(pkt, pkt_len, &h2, mem_ctx, &c2); \
192 assert(ret == 0); \
193 verify_ctdb_req_header(&h1, &h2); \
194 assert(h2.length == pkt_len); \
195 VERIFY_FUNC(NAME)(&c1, &c2); \
196 talloc_free(mem_ctx); \
199 /* for ctdb_reply_control */
200 #define PROTOCOL_CTDB6_TEST(TYPE, NAME, OPER) \
201 static void TEST_FUNC(NAME)(uint32_t opcode) \
203 TALLOC_CTX *mem_ctx; \
204 struct ctdb_req_header h1, h2; \
205 TYPE c1, c2; \
206 uint8_t *pkt; \
207 size_t pkt_len, buflen, len; \
208 int ret; \
210 protocol_test_iterate_tag("%s %u\n", #NAME, opcode); \
211 mem_ctx = talloc_new(NULL); \
212 assert(mem_ctx != NULL); \
213 fill_ctdb_req_header(&h1); \
214 FILL_FUNC(NAME)(mem_ctx, &c1, opcode); \
215 buflen = LEN_FUNC(NAME)(&h1, &c1); \
216 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
217 assert(ret == 0); \
218 assert(pkt != NULL); \
219 assert(pkt_len >= buflen); \
220 len = 0; \
221 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &len); \
222 assert(ret == EMSGSIZE); \
223 assert(len == buflen); \
224 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &pkt_len); \
225 assert(ret == 0); \
226 ret = PULL_FUNC(NAME)(pkt, pkt_len, opcode, &h2, mem_ctx, &c2); \
227 assert(ret == 0); \
228 verify_ctdb_req_header(&h1, &h2); \
229 assert(h2.length == pkt_len); \
230 VERIFY_FUNC(NAME)(&c1, &c2); \
231 talloc_free(mem_ctx); \
234 /* for ctdb_req_message */
235 #define PROTOCOL_CTDB7_TEST(TYPE, NAME, OPER) \
236 static void TEST_FUNC(NAME)(uint64_t srvid) \
238 TALLOC_CTX *mem_ctx; \
239 struct ctdb_req_header h1, h2; \
240 TYPE c1, c2; \
241 uint8_t *pkt; \
242 size_t pkt_len, buflen, len; \
243 int ret; \
245 protocol_test_iterate_tag("%s %"PRIx64"\n", #NAME, srvid); \
246 mem_ctx = talloc_new(NULL); \
247 assert(mem_ctx != NULL); \
248 fill_ctdb_req_header(&h1); \
249 FILL_FUNC(NAME)(mem_ctx, &c1, srvid); \
250 buflen = LEN_FUNC(NAME)(&h1, &c1); \
251 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
252 assert(ret == 0); \
253 assert(pkt != NULL); \
254 assert(pkt_len >= buflen); \
255 len = 0; \
256 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &len); \
257 assert(ret == EMSGSIZE); \
258 assert(len == buflen); \
259 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &pkt_len); \
260 assert(ret == 0); \
261 ret = PULL_FUNC(NAME)(pkt, pkt_len, &h2, mem_ctx, &c2); \
262 assert(ret == 0); \
263 verify_ctdb_req_header(&h1, &h2); \
264 assert(h2.length == pkt_len); \
265 VERIFY_FUNC(NAME)(&c1, &c2); \
266 talloc_free(mem_ctx); \
269 PROTOCOL_CTDB1_TEST(struct ctdb_req_header, ctdb_req_header);
271 PROTOCOL_CTDB4_TEST(struct ctdb_req_call, ctdb_req_call, CTDB_REQ_CALL);
272 PROTOCOL_CTDB4_TEST(struct ctdb_reply_call, ctdb_reply_call, CTDB_REPLY_CALL);
273 PROTOCOL_CTDB4_TEST(struct ctdb_reply_error, ctdb_reply_error,
274 CTDB_REPLY_ERROR);
275 PROTOCOL_CTDB4_TEST(struct ctdb_req_dmaster, ctdb_req_dmaster,
276 CTDB_REQ_DMASTER);
277 PROTOCOL_CTDB4_TEST(struct ctdb_reply_dmaster, ctdb_reply_dmaster,
278 CTDB_REPLY_DMASTER);
280 #define NUM_CONTROLS 161
282 PROTOCOL_CTDB2_TEST(struct ctdb_req_control_data, ctdb_req_control_data);
283 PROTOCOL_CTDB2_TEST(struct ctdb_reply_control_data, ctdb_reply_control_data);
285 PROTOCOL_CTDB5_TEST(struct ctdb_req_control, ctdb_req_control,
286 CTDB_REQ_CONTROL);
287 PROTOCOL_CTDB6_TEST(struct ctdb_reply_control, ctdb_reply_control,
288 CTDB_REPLY_CONTROL);
290 PROTOCOL_CTDB3_TEST(union ctdb_message_data, ctdb_message_data);
291 PROTOCOL_CTDB7_TEST(struct ctdb_req_message, ctdb_req_message,
292 CTDB_REQ_MESSAGE);
293 PROTOCOL_CTDB4_TEST(struct ctdb_req_message_data, ctdb_req_message_data,
294 CTDB_REQ_MESSAGE);
296 PROTOCOL_CTDB4_TEST(struct ctdb_req_keepalive, ctdb_req_keepalive,
297 CTDB_REQ_KEEPALIVE);
298 PROTOCOL_CTDB4_TEST(struct ctdb_req_tunnel, ctdb_req_tunnel, CTDB_REQ_TUNNEL);
300 static void protocol_ctdb_test(void)
302 uint32_t opcode;
303 uint64_t test_srvid[] = {
304 CTDB_SRVID_BANNING,
305 CTDB_SRVID_ELECTION,
306 CTDB_SRVID_LEADER,
307 CTDB_SRVID_RECONFIGURE,
308 CTDB_SRVID_RELEASE_IP,
309 CTDB_SRVID_TAKE_IP,
310 CTDB_SRVID_SET_NODE_FLAGS,
311 CTDB_SRVID_RECD_UPDATE_IP,
312 CTDB_SRVID_VACUUM_FETCH,
313 CTDB_SRVID_DETACH_DATABASE,
314 CTDB_SRVID_MEM_DUMP,
315 CTDB_SRVID_GETLOG,
316 CTDB_SRVID_CLEARLOG,
317 CTDB_SRVID_PUSH_NODE_FLAGS,
318 CTDB_SRVID_RELOAD_NODES,
319 CTDB_SRVID_TAKEOVER_RUN,
320 CTDB_SRVID_REBALANCE_NODE,
321 CTDB_SRVID_DISABLE_TAKEOVER_RUNS,
322 CTDB_SRVID_DISABLE_RECOVERIES,
323 CTDB_SRVID_DISABLE_IP_CHECK,
325 size_t i;
327 TEST_FUNC(ctdb_req_header)();
329 TEST_FUNC(ctdb_req_call)();
330 TEST_FUNC(ctdb_reply_call)();
331 TEST_FUNC(ctdb_reply_error)();
332 TEST_FUNC(ctdb_req_dmaster)();
333 TEST_FUNC(ctdb_reply_dmaster)();
335 for (opcode=0; opcode<NUM_CONTROLS; opcode++) {
336 TEST_FUNC(ctdb_req_control_data)(opcode);
338 for (opcode=0; opcode<NUM_CONTROLS; opcode++) {
339 TEST_FUNC(ctdb_reply_control_data)(opcode);
342 for (opcode=0; opcode<NUM_CONTROLS; opcode++) {
343 TEST_FUNC(ctdb_req_control)(opcode);
345 for (opcode=0; opcode<NUM_CONTROLS; opcode++) {
346 TEST_FUNC(ctdb_reply_control)(opcode);
349 for (i=0; i<ARRAY_SIZE(test_srvid); i++) {
350 TEST_FUNC(ctdb_message_data)(test_srvid[i]);
352 for (i=0; i<ARRAY_SIZE(test_srvid); i++) {
353 TEST_FUNC(ctdb_req_message)(test_srvid[i]);
355 TEST_FUNC(ctdb_req_message_data)();
357 TEST_FUNC(ctdb_req_keepalive)();
358 TEST_FUNC(ctdb_req_tunnel)();
361 int main(int argc, const char *argv[])
363 protocol_test_iterate(argc, argv, protocol_ctdb_test);
364 return 0;