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/>.
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_packet.c"
31 #include "tests/src/protocol_common.h"
32 #include "tests/src/protocol_common_ctdb.h"
35 * Functions to test marshalling
38 /* for ctdb_req_header */
39 #define PROTOCOL_CTDB1_TEST(TYPE, NAME) \
40 static void TEST_FUNC(NAME)(void) \
42 TALLOC_CTX *mem_ctx; \
45 size_t pkt_len, buflen, np; \
48 printf("%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); \
56 assert(pkt != NULL); \
57 assert(pkt_len >= buflen); \
59 PUSH_FUNC(NAME)(&c1, pkt, &np); \
60 assert(np == buflen); \
62 ret = PULL_FUNC(NAME)(pkt, pkt_len, &c2, &np); \
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) \
73 TALLOC_CTX *mem_ctx; \
76 size_t pkt_len, buflen, np; \
79 printf("%s %u\n", #NAME, opcode); \
81 mem_ctx = talloc_new(NULL); \
82 assert(mem_ctx != NULL); \
83 FILL_FUNC(NAME)(mem_ctx, &c1, opcode); \
84 buflen = LEN_FUNC(NAME)(&c1); \
85 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
87 assert(pkt != NULL); \
88 assert(pkt_len >= buflen); \
90 PUSH_FUNC(NAME)(&c1, pkt, &np); \
91 assert(np == buflen); \
93 ret = PULL_FUNC(NAME)(pkt, pkt_len, opcode, mem_ctx, &c2, &np); \
95 assert(np == buflen); \
96 VERIFY_FUNC(NAME)(&c1, &c2); \
97 talloc_free(mem_ctx); \
100 /* for ctdb_message_data */
101 #define PROTOCOL_CTDB3_TEST(TYPE, NAME) \
102 static void TEST_FUNC(NAME)(uint64_t srvid) \
104 TALLOC_CTX *mem_ctx; \
107 size_t pkt_len, buflen, np; \
110 printf("%s %"PRIx64"\n", #NAME, srvid); \
112 mem_ctx = talloc_new(NULL); \
113 assert(mem_ctx != NULL); \
114 FILL_FUNC(NAME)(mem_ctx, &c1, srvid); \
115 buflen = LEN_FUNC(NAME)(&c1, srvid); \
116 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
118 assert(pkt != NULL); \
119 assert(pkt_len >= buflen); \
121 PUSH_FUNC(NAME)(&c1, srvid, pkt, &np); \
122 assert(np == buflen); \
124 ret = PULL_FUNC(NAME)(pkt, pkt_len, srvid, mem_ctx, &c2, &np); \
126 assert(np == buflen); \
127 VERIFY_FUNC(NAME)(&c1, &c2, srvid); \
128 talloc_free(mem_ctx); \
131 /* for ctdb_req_call, ctdb_reply_call, etc. */
132 #define PROTOCOL_CTDB4_TEST(TYPE, NAME, OPER) \
133 static void TEST_FUNC(NAME)(void) \
135 TALLOC_CTX *mem_ctx; \
136 struct ctdb_req_header h1, h2; \
139 size_t pkt_len, buflen, len; \
142 printf("%s\n", #NAME); \
144 mem_ctx = talloc_new(NULL); \
145 assert(mem_ctx != NULL); \
146 fill_ctdb_req_header(&h1); \
147 FILL_FUNC(NAME)(mem_ctx, &c1); \
148 buflen = LEN_FUNC(NAME)(&h1, &c1); \
149 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
151 assert(pkt != NULL); \
152 assert(pkt_len >= buflen); \
154 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &len); \
155 assert(ret == EMSGSIZE); \
156 assert(len == buflen); \
157 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &pkt_len); \
159 ret = PULL_FUNC(NAME)(pkt, pkt_len, &h2, mem_ctx, &c2); \
161 verify_ctdb_req_header(&h1, &h2); \
162 assert(h2.length == pkt_len); \
163 VERIFY_FUNC(NAME)(&c1, &c2); \
164 talloc_free(mem_ctx); \
167 /* for ctdb_req_control */
168 #define PROTOCOL_CTDB5_TEST(TYPE, NAME, OPER) \
169 static void TEST_FUNC(NAME)(uint32_t opcode) \
171 TALLOC_CTX *mem_ctx; \
172 struct ctdb_req_header h1, h2; \
175 size_t pkt_len, buflen, len; \
178 printf("%s %u\n", #NAME, opcode); \
180 mem_ctx = talloc_new(NULL); \
181 assert(mem_ctx != NULL); \
182 fill_ctdb_req_header(&h1); \
183 FILL_FUNC(NAME)(mem_ctx, &c1, opcode); \
184 buflen = LEN_FUNC(NAME)(&h1, &c1); \
185 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
187 assert(pkt != NULL); \
188 assert(pkt_len >= buflen); \
190 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &len); \
191 assert(ret == EMSGSIZE); \
192 assert(len == buflen); \
193 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &pkt_len); \
195 ret = PULL_FUNC(NAME)(pkt, pkt_len, &h2, mem_ctx, &c2); \
197 verify_ctdb_req_header(&h1, &h2); \
198 assert(h2.length == pkt_len); \
199 VERIFY_FUNC(NAME)(&c1, &c2); \
200 talloc_free(mem_ctx); \
203 /* for ctdb_reply_control */
204 #define PROTOCOL_CTDB6_TEST(TYPE, NAME, OPER) \
205 static void TEST_FUNC(NAME)(uint32_t opcode) \
207 TALLOC_CTX *mem_ctx; \
208 struct ctdb_req_header h1, h2; \
211 size_t pkt_len, buflen, len; \
214 printf("%s %u\n", #NAME, opcode); \
216 mem_ctx = talloc_new(NULL); \
217 assert(mem_ctx != NULL); \
218 fill_ctdb_req_header(&h1); \
219 FILL_FUNC(NAME)(mem_ctx, &c1, opcode); \
220 buflen = LEN_FUNC(NAME)(&h1, &c1); \
221 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
223 assert(pkt != NULL); \
224 assert(pkt_len >= buflen); \
226 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &len); \
227 assert(ret == EMSGSIZE); \
228 assert(len == buflen); \
229 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &pkt_len); \
231 ret = PULL_FUNC(NAME)(pkt, pkt_len, opcode, &h2, mem_ctx, &c2); \
233 verify_ctdb_req_header(&h1, &h2); \
234 assert(h2.length == pkt_len); \
235 VERIFY_FUNC(NAME)(&c1, &c2); \
236 talloc_free(mem_ctx); \
239 /* for ctdb_req_message */
240 #define PROTOCOL_CTDB7_TEST(TYPE, NAME, OPER) \
241 static void TEST_FUNC(NAME)(uint64_t srvid) \
243 TALLOC_CTX *mem_ctx; \
244 struct ctdb_req_header h1, h2; \
247 size_t pkt_len, buflen, len; \
250 printf("%s %"PRIx64"\n", #NAME, srvid); \
252 mem_ctx = talloc_new(NULL); \
253 assert(mem_ctx != NULL); \
254 fill_ctdb_req_header(&h1); \
255 FILL_FUNC(NAME)(mem_ctx, &c1, srvid); \
256 buflen = LEN_FUNC(NAME)(&h1, &c1); \
257 ret = ctdb_allocate_pkt(mem_ctx, buflen, &pkt, &pkt_len); \
259 assert(pkt != NULL); \
260 assert(pkt_len >= buflen); \
262 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &len); \
263 assert(ret == EMSGSIZE); \
264 assert(len == buflen); \
265 ret = PUSH_FUNC(NAME)(&h1, &c1, pkt, &pkt_len); \
267 ret = PULL_FUNC(NAME)(pkt, pkt_len, &h2, mem_ctx, &c2); \
269 verify_ctdb_req_header(&h1, &h2); \
270 assert(h2.length == pkt_len); \
271 VERIFY_FUNC(NAME)(&c1, &c2); \
272 talloc_free(mem_ctx); \
275 PROTOCOL_CTDB1_TEST(struct ctdb_req_header, ctdb_req_header);
277 PROTOCOL_CTDB4_TEST(struct ctdb_req_call, ctdb_req_call, CTDB_REQ_CALL);
278 PROTOCOL_CTDB4_TEST(struct ctdb_reply_call, ctdb_reply_call, CTDB_REPLY_CALL);
279 PROTOCOL_CTDB4_TEST(struct ctdb_reply_error, ctdb_reply_error,
281 PROTOCOL_CTDB4_TEST(struct ctdb_req_dmaster, ctdb_req_dmaster,
283 PROTOCOL_CTDB4_TEST(struct ctdb_reply_dmaster, ctdb_reply_dmaster,
286 #define NUM_CONTROLS 152
288 PROTOCOL_CTDB2_TEST(struct ctdb_req_control_data, ctdb_req_control_data);
289 PROTOCOL_CTDB2_TEST(struct ctdb_reply_control_data, ctdb_reply_control_data);
291 PROTOCOL_CTDB5_TEST(struct ctdb_req_control, ctdb_req_control,
293 PROTOCOL_CTDB6_TEST(struct ctdb_reply_control, ctdb_reply_control,
296 PROTOCOL_CTDB3_TEST(union ctdb_message_data, ctdb_message_data);
297 PROTOCOL_CTDB7_TEST(struct ctdb_req_message, ctdb_req_message,
299 PROTOCOL_CTDB4_TEST(struct ctdb_req_message_data, ctdb_req_message_data,
302 PROTOCOL_CTDB4_TEST(struct ctdb_req_keepalive, ctdb_req_keepalive,
305 int main(int argc, char *argv[])
308 uint64_t test_srvid[] = {
311 CTDB_SRVID_RECONFIGURE,
312 CTDB_SRVID_RELEASE_IP,
314 CTDB_SRVID_SET_NODE_FLAGS,
315 CTDB_SRVID_RECD_UPDATE_IP,
316 CTDB_SRVID_VACUUM_FETCH,
317 CTDB_SRVID_DETACH_DATABASE,
321 CTDB_SRVID_PUSH_NODE_FLAGS,
322 CTDB_SRVID_RELOAD_NODES,
323 CTDB_SRVID_TAKEOVER_RUN,
324 CTDB_SRVID_REBALANCE_NODE,
325 CTDB_SRVID_DISABLE_TAKEOVER_RUNS,
326 CTDB_SRVID_DISABLE_RECOVERIES,
327 CTDB_SRVID_DISABLE_IP_CHECK,
332 int seed = atoi(argv[1]);
336 TEST_FUNC(ctdb_req_header)();
338 TEST_FUNC(ctdb_req_call)();
339 TEST_FUNC(ctdb_reply_call)();
340 TEST_FUNC(ctdb_reply_error)();
341 TEST_FUNC(ctdb_req_dmaster)();
342 TEST_FUNC(ctdb_reply_dmaster)();
344 for (opcode=0; opcode<NUM_CONTROLS; opcode++) {
345 TEST_FUNC(ctdb_req_control_data)(opcode);
347 for (opcode=0; opcode<NUM_CONTROLS; opcode++) {
348 TEST_FUNC(ctdb_reply_control_data)(opcode);
351 for (opcode=0; opcode<NUM_CONTROLS; opcode++) {
352 TEST_FUNC(ctdb_req_control)(opcode);
354 for (opcode=0; opcode<NUM_CONTROLS; opcode++) {
355 TEST_FUNC(ctdb_reply_control)(opcode);
358 for (i=0; i<ARRAY_SIZE(test_srvid); i++) {
359 TEST_FUNC(ctdb_message_data)(test_srvid[i]);
361 for (i=0; i<ARRAY_SIZE(test_srvid); i++) {
362 TEST_FUNC(ctdb_req_message)(test_srvid[i]);
364 TEST_FUNC(ctdb_req_message_data)();
366 TEST_FUNC(ctdb_req_keepalive)();