2 protocol utilities tests
4 Copyright (C) Martin Schwenke 2016
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/network.h"
25 #include "protocol/protocol_basic.c"
26 #include "protocol/protocol_types.c"
27 #include "protocol/protocol_util.c"
30 * Test parsing of IPs, conversion to string
33 static void test_sock_addr_to_string(const char *ip, bool with_port)
39 ret = ctdb_sock_addr_from_string(ip, &sa, with_port);
41 s = ctdb_sock_addr_to_string(NULL, &sa, with_port);
42 assert(strcmp(ip, s) == 0);
43 talloc_free(discard_const(s));
46 static void test_sock_addr_from_string_bad(const char *ip, bool with_port)
51 ret = ctdb_sock_addr_from_string(ip, &sa, with_port);
55 static void test_sock_addr_cmp(const char *ip1, const char *ip2,
56 bool with_port, int res)
58 ctdb_sock_addr sa1, sa2;
61 ret = ctdb_sock_addr_from_string(ip1, &sa1, with_port);
63 ret = ctdb_sock_addr_from_string(ip2, &sa2, with_port);
65 ret = ctdb_sock_addr_cmp(&sa1, &sa2);
76 * Test parsing of connection, conversion to string
79 static void test_connection_to_string(const char *conn_str)
82 struct ctdb_connection conn;
86 tmp_ctx = talloc_new(NULL);
87 assert(tmp_ctx != NULL);
90 * Test non-reversed parse and render
93 ret = ctdb_connection_from_string(conn_str, false, &conn);
96 s = ctdb_connection_to_string(tmp_ctx, &conn, false);
98 ret = strcmp(conn_str, s);
101 talloc_free(discard_const(s));
106 r = ctdb_connection_to_string(tmp_ctx, &conn, true);
108 ret = strcmp(conn_str, r);
112 * Reversed parse with forward render
114 ret = ctdb_connection_from_string(conn_str, true, &conn);
117 s = ctdb_connection_to_string(tmp_ctx, &conn, false);
122 talloc_free(discard_const(s));
125 * Reversed parse and render
127 ret = ctdb_connection_from_string(conn_str, true, &conn);
130 s = ctdb_connection_to_string(tmp_ctx, &conn, true);
132 ret = strcmp(conn_str, s);
135 talloc_free(tmp_ctx);
138 static void test_connection_from_string_bad(const char *conn_str)
140 struct ctdb_connection conn;
143 ret = ctdb_connection_from_string(conn_str, false, &conn);
148 * Test connection list utilities
151 static void test_connection_list_read(const char *s1, const char *s2)
156 struct ctdb_connection_list *conn_list;
160 tmp_ctx = talloc_new(NULL);
161 assert(tmp_ctx != NULL);
172 ret = dup2(pipefd[1], STDOUT_FILENO);
185 ret = dup2(pipefd[0], STDIN_FILENO);
190 ret = ctdb_connection_list_read(tmp_ctx, false, &conn_list);
193 ret = ctdb_connection_list_sort(conn_list);
196 t = ctdb_connection_list_to_string(tmp_ctx, conn_list, false);
201 talloc_free(tmp_ctx);
204 static void test_connection_list_read_bad(const char *s1)
209 struct ctdb_connection_list *conn_list;
212 tmp_ctx = talloc_new(NULL);
213 assert(tmp_ctx != NULL);
224 ret = dup2(pipefd[1], STDOUT_FILENO);
237 ret = dup2(pipefd[0], STDIN_FILENO);
242 ret = ctdb_connection_list_read(tmp_ctx, false, &conn_list);
245 talloc_free(tmp_ctx);
249 * Use macros for these to make them easy to concatenate
254 127.0.0.1:12345 127.0.0.2:54321\n\
255 127.0.0.2:12345 127.0.0.1:54322\n\
256 127.0.0.1:12346 127.0.0.2:54323\n\
257 127.0.0.2:12345 127.0.0.1:54324\n\
258 127.0.0.1:12345 127.0.0.2:54325\n\
263 127.0.0.1:12345 127.0.0.2:54321\n\
264 127.0.0.1:12345 127.0.0.2:54325\n\
265 127.0.0.1:12346 127.0.0.2:54323\n\
266 127.0.0.2:12345 127.0.0.1:54322\n\
267 127.0.0.2:12345 127.0.0.1:54324\n\
272 fe80::6af7:28ff:fefa:d136:12345 fe80::6af7:28ff:fefa:d137:54321\n\
273 fe80::6af7:28ff:fefa:d138:12345 fe80::6af7:28ff:fefa:d137:54322\n\
274 fe80::6af7:28ff:fefa:d136:12346 fe80::6af7:28ff:fefa:d137:54323\n\
275 fe80::6af7:28ff:fefa:d132:12345 fe80::6af7:28ff:fefa:d137:54324\n\
276 fe80::6af7:28ff:fefa:d136:12345 fe80::6af7:28ff:fefa:d137:54325\n\
281 fe80::6af7:28ff:fefa:d132:12345 fe80::6af7:28ff:fefa:d137:54324\n\
282 fe80::6af7:28ff:fefa:d136:12345 fe80::6af7:28ff:fefa:d137:54321\n\
283 fe80::6af7:28ff:fefa:d136:12345 fe80::6af7:28ff:fefa:d137:54325\n\
284 fe80::6af7:28ff:fefa:d136:12346 fe80::6af7:28ff:fefa:d137:54323\n\
285 fe80::6af7:28ff:fefa:d138:12345 fe80::6af7:28ff:fefa:d137:54322\n\
288 int main(int argc, char *argv[])
290 test_sock_addr_to_string("0.0.0.0", false);
291 test_sock_addr_to_string("127.0.0.1", false);
292 test_sock_addr_to_string("::1", false);
293 test_sock_addr_to_string("192.168.2.1", false);
294 test_sock_addr_to_string("fe80::6af7:28ff:fefa:d136", false);
296 test_sock_addr_to_string("0.0.0.0:0", true);
297 test_sock_addr_to_string("127.0.0.1:123", true);
298 test_sock_addr_to_string("::1:234", true);
299 test_sock_addr_to_string("192.168.2.1:123", true);
300 test_sock_addr_to_string("fe80::6af7:28ff:fefa:d136:234", true);
302 test_sock_addr_from_string_bad("0.0.0", false);
303 test_sock_addr_from_string_bad("0.0.0:0", true);
304 test_sock_addr_from_string_bad("fe80::6af7:28ff:fefa:d136", true);
305 test_sock_addr_from_string_bad("junk", false);
306 test_sock_addr_from_string_bad("0.0.0.0:0 trailing junk", true);
308 test_sock_addr_cmp("127.0.0.1", "127.0.0.1" , false, 0);
309 test_sock_addr_cmp("127.0.0.1", "127.0.0.2" , false, -1);
310 test_sock_addr_cmp("127.0.0.2", "127.0.0.1" , false, 1);
311 test_sock_addr_cmp("127.0.1.2", "127.0.2.1" , false, -1);
312 test_sock_addr_cmp("127.0.2.1", "127.0.1.2" , false, 1);
313 test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136", "127.0.1.2" , false, 1);
314 test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
315 "fe80::6af7:28ff:fefa:d136" , false, 0);
316 test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
317 "fe80::6af7:28ff:fefa:d137" , false, -1);
318 test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
319 "fe80:0000:0000:0000:6af7:28ff:fefa:d136" ,
321 test_sock_addr_cmp("::ffff:192.0.2.128", "192.0.2.128", false, 0);
323 test_sock_addr_cmp("127.0.0.1:123", "127.0.0.1:124" , true, -1);
324 test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136:123",
325 "fe80::6af7:28ff:fefa:d136:122" , true, 1);
327 test_connection_to_string("127.0.0.1:12345 127.0.0.2:54321");
328 test_connection_to_string("fe80::6af7:28ff:fefa:d137:12345 "
329 "fe80::6af7:28ff:fefa:d138:54321");
331 test_connection_from_string_bad("127.0.0.1:12345 127.0.0.2:");
332 test_connection_from_string_bad("127.0.0.1:12345");
333 test_connection_from_string_bad("127.0.0.1:12345 "
334 "fe80::6af7:28ff:fefa:d136:122");
335 test_connection_from_string_bad("Junk!");
336 test_connection_from_string_bad("More junk");
338 test_connection_list_read(CONN4, CONN4_SORT);
339 test_connection_list_read(CONN6, CONN6_SORT);
340 test_connection_list_read(CONN4 CONN6, CONN4_SORT CONN6_SORT);
341 test_connection_list_read(CONN4 "# Comment\n\n# Comment\n" CONN6,
342 CONN4_SORT CONN6_SORT);
344 test_connection_list_read_bad(CONN4 "# Comment\n\nJunk!!!\n" CONN6);
345 test_connection_list_read_bad(CONN4
346 "# Comment\n\n127.0.0.1: 127.0.0.1:124\n"