ctdb-protocol: Fix marshalling for ctdb_event_request_script_enable
[vlendec/samba-autobuild/.git] / ctdb / tests / src / protocol_event_test.c
1 /*
2    protocol types tests
3
4    Copyright (C) Amitay Isaacs  2015
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 <assert.h>
21
22 #include "protocol/protocol_basic.c"
23 #include "protocol/protocol_types.c"
24 #include "protocol/protocol_event.c"
25
26 #include "tests/src/protocol_common.h"
27 #include "tests/src/protocol_common_event.h"
28
29 #define REQID           0x34567890
30
31
32 /*
33  * Functions to test eventd protocol marshalling
34  */
35
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) \
39 { \
40         TALLOC_CTX *mem_ctx; \
41         TYPE c1, c2; \
42         uint8_t *buf; \
43         size_t buflen, np; \
44         int ret; \
45 \
46         printf("%s %u\n", #NAME, command); \
47         fflush(stdout); \
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); \
54         np = 0; \
55         PUSH_FUNC(NAME)(&c1, buf, &np); \
56         assert(np == buflen); \
57         np = 0; \
58         ret = PULL_FUNC(NAME)(buf, buflen, mem_ctx, &c2, &np); \
59         assert(ret == 0); \
60         assert(np == buflen); \
61         VERIFY_FUNC(NAME)(&c1, &c2); \
62         talloc_free(mem_ctx); \
63 }
64
65 #define PROTOCOL_EVENT2_TEST(TYPE, NAME) \
66 static void TEST_FUNC(NAME)(uint32_t command) \
67 { \
68         TALLOC_CTX *mem_ctx; \
69         TYPE c1, c2; \
70         uint8_t *buf; \
71         size_t buflen, len; \
72         int ret; \
73 \
74         printf("%s %u\n", #NAME, command); \
75         fflush(stdout); \
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); \
82         len = 0; \
83         ret = PUSH_FUNC(NAME)(&c1, buf, &len); \
84         assert(ret == EMSGSIZE); \
85         assert(len == buflen); \
86         ret = PUSH_FUNC(NAME)(&c1, buf, &buflen); \
87         assert(ret == 0); \
88         ret = PULL_FUNC(NAME)(buf, buflen, mem_ctx, &c2); \
89         assert(ret == 0); \
90         assert(c2.header.length == buflen); \
91         VERIFY_FUNC(NAME)(&c1, &c2); \
92         talloc_free(mem_ctx); \
93 }
94
95 static void test_ctdb_event_header(void)
96 {
97         TALLOC_CTX *mem_ctx;
98         size_t buflen;
99         struct ctdb_event_header h, h2;
100         int ret;
101
102         printf("ctdb_event_header\n");
103         fflush(stdout);
104
105         mem_ctx = talloc_new(NULL);
106         assert(mem_ctx != NULL);
107
108         ctdb_event_header_fill(&h, REQID);
109
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);
113         assert(ret == 0);
114
115         verify_ctdb_event_header(&h, &h2);
116
117         talloc_free(mem_ctx);
118 }
119
120 #define NUM_COMMANDS    5
121
122 static void test_ctdb_event_request_data(void)
123 {
124         TALLOC_CTX *mem_ctx;
125         size_t buflen;
126         struct ctdb_event_request_data rd, rd2;
127         uint32_t command;
128         int ret;
129
130         printf("ctdb_event_request_data\n");
131         fflush(stdout);
132
133         for (command=1; command<=NUM_COMMANDS; command++) {
134                 mem_ctx = talloc_new(NULL);
135                 assert(mem_ctx != NULL);
136
137                 printf("%u.. ", command);
138                 fflush(stdout);
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);
143                 assert(ret == 0);
144                 verify_ctdb_event_request_data(&rd, &rd2);
145
146                 talloc_free(mem_ctx);
147         }
148
149         printf("\n");
150         fflush(stdout);
151 }
152
153 static void test_ctdb_event_reply_data(void)
154 {
155         TALLOC_CTX *mem_ctx;
156         size_t buflen;
157         struct ctdb_event_reply_data rd, rd2;
158         uint32_t command;
159         int ret;
160
161         printf("ctdb_event_reply_data\n");
162         fflush(stdout);
163
164         for (command=1; command<=NUM_COMMANDS; command++) {
165                 mem_ctx = talloc_new(NULL);
166                 assert(mem_ctx != NULL);
167
168                 printf("%u.. ", command);
169                 fflush(stdout);
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);
174                 assert(ret == 0);
175                 verify_ctdb_event_reply_data(&rd, &rd2);
176
177                 talloc_free(mem_ctx);
178         }
179
180         printf("\n");
181         fflush(stdout);
182 }
183
184 static void test_ctdb_event_request(void)
185 {
186         TALLOC_CTX *mem_ctx;
187         uint8_t *buf;
188         size_t len, buflen;
189         int ret;
190         struct ctdb_event_request r, r2;
191         uint32_t command;
192
193         printf("ctdb_event_request\n");
194         fflush(stdout);
195
196         for (command=1; command<=NUM_COMMANDS; command++) {
197                 mem_ctx = talloc_new(NULL);
198                 assert(mem_ctx != NULL);
199
200                 printf("%u.. ", command);
201                 fflush(stdout);
202                 fill_ctdb_event_request(mem_ctx, &r, command);
203                 buflen = ctdb_event_request_len(&r);
204                 buf = talloc_size(mem_ctx, buflen);
205                 assert(buf != NULL);
206                 len = 0;
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);
211                 assert(ret == 0);
212                 ret = ctdb_event_request_pull(buf, buflen, mem_ctx, &r2);
213                 assert(ret == 0);
214                 assert(r2.header.length == buflen);
215                 verify_ctdb_event_request(&r, &r2);
216
217                 talloc_free(mem_ctx);
218         }
219
220         printf("\n");
221         fflush(stdout);
222 }
223
224 static void test_ctdb_event_reply(void)
225 {
226         TALLOC_CTX *mem_ctx;
227         uint8_t *buf;
228         size_t len, buflen;
229         int ret;
230         struct ctdb_event_reply r, r2;
231         uint32_t command;
232
233         printf("ctdb_event_reply\n");
234         fflush(stdout);
235
236         for (command=1; command<=NUM_COMMANDS; command++) {
237                 mem_ctx = talloc_new(NULL);
238                 assert(mem_ctx != NULL);
239
240                 printf("%u.. ", command);
241                 fflush(stdout);
242                 fill_ctdb_event_reply(mem_ctx, &r, command);
243                 buflen = ctdb_event_reply_len(&r);
244                 buf = talloc_size(mem_ctx, buflen);
245                 assert(buf != NULL);
246                 len = 0;
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);
251                 assert(ret == 0);
252                 ret = ctdb_event_reply_pull(buf, buflen, mem_ctx, &r2);
253                 assert(ret == 0);
254                 assert(r2.header.length == buflen);
255                 verify_ctdb_event_reply(&r, &r2);
256
257                 talloc_free(mem_ctx);
258         }
259
260         printf("\n");
261         fflush(stdout);
262 }
263
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);
273
274 int main(int argc, char *argv[])
275 {
276         if (argc == 2) {
277                 int seed = atoi(argv[1]);
278                 srandom(seed);
279         }
280
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)();
287
288         test_ctdb_event_header();
289
290         test_ctdb_event_request_data();
291         test_ctdb_event_reply_data();
292         test_ctdb_event_request();
293         test_ctdb_event_reply();
294
295         return 0;
296 }