8926de0197b7e68d0a9d0b7e28045e62d59731b4
[samba.git] / ctdb / tests / src / protocol_util_test.c
1 /*
2    protocol utilities tests
3
4    Copyright (C) Martin Schwenke  2016
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 "replace.h"
21 #include "system/network.h"
22
23 #include <assert.h>
24
25 #include "protocol/protocol_basic.c"
26 #include "protocol/protocol_types.c"
27 #include "protocol/protocol_util.c"
28
29 /*
30  * Test parsing of IPs, conversion to string
31  */
32
33 static void test_sock_addr_to_string(const char *ip, bool with_port)
34 {
35         ctdb_sock_addr sa;
36         const char *s;
37         int ret;
38
39         ret = ctdb_sock_addr_from_string(ip, &sa, with_port);
40         assert(ret == 0);
41         s = ctdb_sock_addr_to_string(NULL, &sa, with_port);
42         assert(strcmp(ip, s) == 0);
43         talloc_free(discard_const(s));
44 }
45
46 static void test_sock_addr_from_string_bad(const char *ip, bool with_port)
47 {
48         ctdb_sock_addr sa;
49         int ret;
50
51         ret = ctdb_sock_addr_from_string(ip, &sa, with_port);
52         assert(ret != 0);
53 }
54
55 static void test_sock_addr_cmp(const char *ip1, const char *ip2,
56                                bool with_port, int res)
57 {
58         ctdb_sock_addr sa1, sa2;
59         int ret;
60
61         ret = ctdb_sock_addr_from_string(ip1, &sa1, with_port);
62         assert(ret == 0);
63         ret = ctdb_sock_addr_from_string(ip2, &sa2, with_port);
64         assert(ret == 0);
65         ret = ctdb_sock_addr_cmp(&sa1, &sa2);
66         if (ret < 0) {
67                 ret = -1;
68         } else if (ret > 0) {
69                 ret = 1;
70         }
71
72         assert(ret == res);
73 }
74
75 int main(int argc, char *argv[])
76 {
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);
82
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);
88
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);
94
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" ,
107                            false, 0);
108         test_sock_addr_cmp("::ffff:192.0.2.128", "192.0.2.128", false, 0);
109
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);
113         return 0;
114 }