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_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) \
43 TALLOC_CTX *mem_ctx; \
46 size_t pkt_len, buflen, np; \
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); \
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 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); \
86 assert(pkt != NULL); \
87 assert(pkt_len >= buflen); \
89 PUSH_FUNC(NAME)(&c1, pkt, &np); \
90 assert(np == buflen); \
92 ret = PULL_FUNC(NAME)(pkt, pkt_len, opcode, mem_ctx, &c2, &np); \
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; \
106 size_t pkt_len, buflen, np; \
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); \
116 assert(pkt != NULL); \
117 assert(pkt_len >= buflen); \
119 PUSH_FUNC(NAME)(&c1, srvid, pkt, &np); \
120 assert(np == buflen); \
122 ret = PULL_FUNC(NAME)(pkt, pkt_len, srvid, mem_ctx, &c2, &np); \
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; \
137 size_t pkt_len, buflen, len; \
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); \
148 assert(pkt != NULL); \
149 assert(pkt_len >= buflen); \
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); \
156 ret = PULL_FUNC(NAME)(pkt, pkt_len, &h2, mem_ctx, &c2); \
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; \
172 size_t pkt_len, buflen, len; \
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); \
183 assert(pkt != NULL); \
184 assert(pkt_len >= buflen); \
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); \
191 ret = PULL_FUNC(NAME)(pkt, pkt_len, &h2, mem_ctx, &c2); \
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; \
207 size_t pkt_len, buflen, len; \
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); \
218 assert(pkt != NULL); \
219 assert(pkt_len >= buflen); \
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); \
226 ret = PULL_FUNC(NAME)(pkt, pkt_len, opcode, &h2, mem_ctx, &c2); \
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; \
242 size_t pkt_len, buflen, len; \
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); \
253 assert(pkt != NULL); \
254 assert(pkt_len >= buflen); \
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); \
261 ret = PULL_FUNC(NAME)(pkt, pkt_len, &h2, mem_ctx, &c2); \
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
,
275 PROTOCOL_CTDB4_TEST(struct ctdb_req_dmaster
, ctdb_req_dmaster
,
277 PROTOCOL_CTDB4_TEST(struct ctdb_reply_dmaster
, ctdb_reply_dmaster
,
280 #define NUM_CONTROLS 162
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
,
287 PROTOCOL_CTDB6_TEST(struct ctdb_reply_control
, 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
,
293 PROTOCOL_CTDB4_TEST(struct ctdb_req_message_data
, ctdb_req_message_data
,
296 PROTOCOL_CTDB4_TEST(struct ctdb_req_keepalive
, ctdb_req_keepalive
,
298 PROTOCOL_CTDB4_TEST(struct ctdb_req_tunnel
, ctdb_req_tunnel
, CTDB_REQ_TUNNEL
);
300 static void protocol_ctdb_test(void)
303 uint64_t test_srvid
[] = {
307 CTDB_SRVID_RECONFIGURE
,
308 CTDB_SRVID_RELEASE_IP
,
310 CTDB_SRVID_SET_NODE_FLAGS
,
311 CTDB_SRVID_RECD_UPDATE_IP
,
312 CTDB_SRVID_VACUUM_FETCH
,
313 CTDB_SRVID_DETACH_DATABASE
,
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
,
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
);