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);
75 int main(int argc, char *argv[])
77 test_sock_addr_to_string("0.0.0.0", false);
78 test_sock_addr_to_string("127.0.0.1", false);
79 test_sock_addr_to_string("::1", false);
80 test_sock_addr_to_string("192.168.2.1", false);
81 test_sock_addr_to_string("fe80::6af7:28ff:fefa:d136", false);
83 test_sock_addr_to_string("0.0.0.0:0", true);
84 test_sock_addr_to_string("127.0.0.1:123", true);
85 test_sock_addr_to_string("::1:234", true);
86 test_sock_addr_to_string("192.168.2.1:123", true);
87 test_sock_addr_to_string("fe80::6af7:28ff:fefa:d136:234", true);
89 test_sock_addr_from_string_bad("0.0.0", false);
90 test_sock_addr_from_string_bad("0.0.0:0", true);
91 test_sock_addr_from_string_bad("fe80::6af7:28ff:fefa:d136", true);
92 test_sock_addr_from_string_bad("junk", false);
93 test_sock_addr_from_string_bad("0.0.0.0:0 trailing junk", true);
95 test_sock_addr_cmp("127.0.0.1", "127.0.0.1" , false, 0);
96 test_sock_addr_cmp("127.0.0.1", "127.0.0.2" , false, -1);
97 test_sock_addr_cmp("127.0.0.2", "127.0.0.1" , false, 1);
98 test_sock_addr_cmp("127.0.1.2", "127.0.2.1" , false, -1);
99 test_sock_addr_cmp("127.0.2.1", "127.0.1.2" , false, 1);
100 test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136", "127.0.1.2" , false, 1);
101 test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
102 "fe80::6af7:28ff:fefa:d136" , false, 0);
103 test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
104 "fe80::6af7:28ff:fefa:d137" , false, -1);
105 test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
106 "fe80:0000:0000:0000:6af7:28ff:fefa:d136" ,
108 test_sock_addr_cmp("::ffff:192.0.2.128", "192.0.2.128", false, 0);
110 test_sock_addr_cmp("127.0.0.1:123", "127.0.0.1:124" , true, -1);
111 test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136:123",
112 "fe80::6af7:28ff:fefa:d136:122" , true, 1);