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_event.c"
26 #include "tests/src/protocol_common.h"
27 #include "tests/src/protocol_common_event.h"
29 #define REQID 0x34567890
33 * Functions to test eventd protocol marshalling
36 /* for ctdb_event_request_data, ctdb_event_reply_data */
37 #define PROTOCOL_EVENT1_TEST(TYPE, NAME) \
38 static void TEST_FUNC(NAME)(uint32_t command) \
40 TALLOC_CTX *mem_ctx; \
46 printf("%s %u\n", #NAME, command); \
48 mem_ctx = talloc_new(NULL); \
49 assert(mem_ctx != NULL); \
50 FILL_FUNC(NAME)(mem_ctx, &c1, command); \
51 buflen = LEN_FUNC(NAME)(&c1); \
52 buf = talloc_size(mem_ctx, buflen); \
53 assert(buf != NULL); \
55 PUSH_FUNC(NAME)(&c1, buf, &np); \
56 assert(np == buflen); \
58 ret = PULL_FUNC(NAME)(buf, buflen, mem_ctx, &c2, &np); \
60 assert(np == buflen); \
61 VERIFY_FUNC(NAME)(&c1, &c2); \
62 talloc_free(mem_ctx); \
65 #define PROTOCOL_EVENT2_TEST(TYPE, NAME) \
66 static void TEST_FUNC(NAME)(uint32_t command) \
68 TALLOC_CTX *mem_ctx; \
74 printf("%s %u\n", #NAME, command); \
76 mem_ctx = talloc_new(NULL); \
77 assert(mem_ctx != NULL); \
78 FILL_FUNC(NAME)(mem_ctx, &c1, command); \
79 buflen = LEN_FUNC(NAME)(&c1); \
80 buf = talloc_size(mem_ctx, buflen); \
81 assert(buf != NULL); \
83 ret = PUSH_FUNC(NAME)(&c1, buf, &len); \
84 assert(ret == EMSGSIZE); \
85 assert(len == buflen); \
86 ret = PUSH_FUNC(NAME)(&c1, buf, &buflen); \
88 ret = PULL_FUNC(NAME)(buf, buflen, mem_ctx, &c2); \
90 assert(c2.header.length == buflen); \
91 VERIFY_FUNC(NAME)(&c1, &c2); \
92 talloc_free(mem_ctx); \
95 static void test_ctdb_event_header(void)
99 struct ctdb_event_header h, h2;
102 printf("ctdb_event_header\n");
105 mem_ctx = talloc_new(NULL);
106 assert(mem_ctx != NULL);
108 ctdb_event_header_fill(&h, REQID);
110 buflen = ctdb_event_header_len(&h);
111 ctdb_event_header_push(&h, BUFFER);
112 ret = ctdb_event_header_pull(BUFFER, buflen, mem_ctx, &h2);
115 verify_ctdb_event_header(&h, &h2);
117 talloc_free(mem_ctx);
120 #define NUM_COMMANDS 5
122 static void test_ctdb_event_request_data(void)
126 struct ctdb_event_request_data rd, rd2;
130 printf("ctdb_event_request_data\n");
133 for (command=1; command<=NUM_COMMANDS; command++) {
134 mem_ctx = talloc_new(NULL);
135 assert(mem_ctx != NULL);
137 printf("%u.. ", command);
139 fill_ctdb_event_request_data(mem_ctx, &rd, command);
140 buflen = ctdb_event_request_data_len(&rd);
141 ctdb_event_request_data_push(&rd, BUFFER);
142 ret = ctdb_event_request_data_pull(BUFFER, buflen, mem_ctx, &rd2);
144 verify_ctdb_event_request_data(&rd, &rd2);
146 talloc_free(mem_ctx);
153 static void test_ctdb_event_reply_data(void)
157 struct ctdb_event_reply_data rd, rd2;
161 printf("ctdb_event_reply_data\n");
164 for (command=1; command<=NUM_COMMANDS; command++) {
165 mem_ctx = talloc_new(NULL);
166 assert(mem_ctx != NULL);
168 printf("%u.. ", command);
170 fill_ctdb_event_reply_data(mem_ctx, &rd, command);
171 buflen = ctdb_event_reply_data_len(&rd);
172 ctdb_event_reply_data_push(&rd, BUFFER);
173 ret = ctdb_event_reply_data_pull(BUFFER, buflen, mem_ctx, &rd2);
175 verify_ctdb_event_reply_data(&rd, &rd2);
177 talloc_free(mem_ctx);
184 static void test_ctdb_event_request(void)
190 struct ctdb_event_request r, r2;
193 printf("ctdb_event_request\n");
196 for (command=1; command<=NUM_COMMANDS; command++) {
197 mem_ctx = talloc_new(NULL);
198 assert(mem_ctx != NULL);
200 printf("%u.. ", command);
202 fill_ctdb_event_request(mem_ctx, &r, command);
203 buflen = ctdb_event_request_len(&r);
204 buf = talloc_size(mem_ctx, buflen);
207 ret = ctdb_event_request_push(&r, buf, &len);
208 assert(ret == EMSGSIZE);
209 assert(len == buflen);
210 ret = ctdb_event_request_push(&r, buf, &buflen);
212 ret = ctdb_event_request_pull(buf, buflen, mem_ctx, &r2);
214 assert(r2.header.length == buflen);
215 verify_ctdb_event_request(&r, &r2);
217 talloc_free(mem_ctx);
224 static void test_ctdb_event_reply(void)
230 struct ctdb_event_reply r, r2;
233 printf("ctdb_event_reply\n");
236 for (command=1; command<=NUM_COMMANDS; command++) {
237 mem_ctx = talloc_new(NULL);
238 assert(mem_ctx != NULL);
240 printf("%u.. ", command);
242 fill_ctdb_event_reply(mem_ctx, &r, command);
243 buflen = ctdb_event_reply_len(&r);
244 buf = talloc_size(mem_ctx, buflen);
247 ret = ctdb_event_reply_push(&r, buf, &len);
248 assert(ret == EMSGSIZE);
249 assert(len == buflen);
250 ret = ctdb_event_reply_push(&r, buf, &buflen);
252 ret = ctdb_event_reply_pull(buf, buflen, mem_ctx, &r2);
254 assert(r2.header.length == buflen);
255 verify_ctdb_event_reply(&r, &r2);
257 talloc_free(mem_ctx);
264 PROTOCOL_TYPE3_TEST(struct ctdb_event_request_run, ctdb_event_request_run);
265 PROTOCOL_TYPE3_TEST(struct ctdb_event_request_status,
266 ctdb_event_request_status);
267 PROTOCOL_TYPE3_TEST(struct ctdb_event_request_script_enable,
268 ctdb_event_request_script_enable);
269 DEFINE_TEST(struct ctdb_event_request_script_disable,
270 ctdb_event_request_script_disable);
271 DEFINE_TEST(struct ctdb_event_reply_status, ctdb_event_reply_status);
272 DEFINE_TEST(struct ctdb_event_reply_script_list, ctdb_event_reply_script_list);
274 int main(int argc, char *argv[])
277 int seed = atoi(argv[1]);
281 TEST_FUNC(ctdb_event_request_run)();
282 TEST_FUNC(ctdb_event_request_status)();
283 TEST_FUNC(ctdb_event_request_script_enable)();
284 TEST_FUNC(ctdb_event_request_script_disable)();
285 TEST_FUNC(ctdb_event_reply_status)();
286 TEST_FUNC(ctdb_event_reply_script_list)();
288 test_ctdb_event_header();
290 test_ctdb_event_request_data();
291 test_ctdb_event_reply_data();
292 test_ctdb_event_request();
293 test_ctdb_event_reply();