4 Copyright (C) Christof Schmitt 2019
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/>.
21 #include "system/filesys.h"
25 #include "common/ctdb_io.c"
27 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...)
35 static void test_setup(ctdb_queue_cb_fn_t cb,
37 struct ctdb_context **pctdb,
38 struct ctdb_queue **pqueue)
41 struct ctdb_context *ctdb;
42 struct ctdb_queue *queue;
47 ctdb = talloc_zero(NULL, struct ctdb_context);
50 ctdb->ev = tevent_context_init(NULL);
52 queue = ctdb_queue_setup(ctdb, ctdb, pipefd[0], 0, cb,
54 assert(queue != NULL);
63 static const size_t test1_req_len = 8;
64 static const char *test1_req = "abcdefgh";
66 static void test1_callback(uint8_t *data, size_t length, void *private_data)
70 len = *(uint32_t *)data;
71 assert(len == sizeof(uint32_t) + test1_req_len);
73 assert(length == sizeof(uint32_t) + test1_req_len);
74 assert(memcmp(data + sizeof(len), test1_req, test1_req_len) == 0);
77 static void test1(void)
79 struct ctdb_context *ctdb;
83 test_setup(test1_callback, &fd, &ctdb, NULL);
85 pkt_size = sizeof(uint32_t) + test1_req_len;
86 ret = write(fd, &pkt_size, sizeof(pkt_size));
87 assert(ret == sizeof(pkt_size));
89 ret = write(fd, test1_req, test1_req_len);
90 assert(ret == test1_req_len);
92 tevent_loop_once(ctdb->ev);
97 static const size_t test2_req_len[] = { 900, 24, 600 };
99 static int test2_cb_num = 0;
101 static void test2_callback(uint8_t *data, size_t length, void *private_data)
105 len = *(uint32_t *)data;
106 assert(len == sizeof(uint32_t) + test2_req_len[test2_cb_num]);
107 assert(length == sizeof(uint32_t) + test2_req_len[test2_cb_num]);
112 static void test2(void)
114 struct ctdb_context *ctdb;
117 char req[1024] = { 0 };
119 for (i = 0; i < sizeof(req); i++) {
120 req[i] = i % CHAR_MAX;
123 test_setup(test2_callback, &fd, &ctdb, NULL);
129 pkt_size = sizeof(uint32_t) + test2_req_len[0];
130 ret = write(fd, &pkt_size, sizeof(pkt_size));
131 assert(ret == sizeof(pkt_size));
133 ret = write(fd, req, test2_req_len[0]);
134 assert(ret == test2_req_len[0]);
139 pkt_size = sizeof(uint32_t) + test2_req_len[1];
140 ret = write(fd, &pkt_size, sizeof(pkt_size));
141 assert(ret == sizeof(pkt_size));
144 * Omit the last byte to avoid buffer processing.
146 ret = write(fd, req, test2_req_len[1] - 1);
147 assert(ret == test2_req_len[1] - 1);
149 tevent_loop_once(ctdb->ev);
152 * Write the missing byte now.
154 ret = write(fd, &req[test2_req_len[1] - 1], 1);
160 pkt_size = sizeof(uint32_t) + test2_req_len[2];
161 ret = write(fd, &pkt_size, sizeof(pkt_size));
162 assert(ret == sizeof(pkt_size));
164 ret = write(fd, req, test2_req_len[2]);
165 assert(ret == test2_req_len[2]);
167 tevent_loop_once(ctdb->ev);
168 tevent_loop_once(ctdb->ev);
170 assert(test2_cb_num == 2);
175 int main(int argc, const char **argv)
180 fprintf(stderr, "%s <testnum>\n", argv[0]);
196 fprintf(stderr, "Unknown test number %s\n", argv[1]);