summaryrefslogtreecommitdiff
path: root/ctdb/tests/src/protocol_util_test.c
blob: eb7eb0ff88f17a8b01cdeeecbd374aaad359ed22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
   protocol utilities tests

   Copyright (C) Martin Schwenke  2016

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/network.h"

#include <assert.h>

#include "protocol/protocol_basic.c"
#include "protocol/protocol_types.c"
#include "protocol/protocol_util.c"

/*
 * Test parsing of IPs, conversion to string
 */

static void test_sock_addr_to_string(const char *ip, bool with_port)
{
	ctdb_sock_addr sa;
	const char *s;
	int ret;

	ret = ctdb_sock_addr_from_string(ip, &sa, with_port);
	assert(ret == 0);
	s = ctdb_sock_addr_to_string(NULL, &sa, with_port);
	assert(strcmp(ip, s) == 0);
	talloc_free(discard_const(s));
}

static void test_sock_addr_from_string_bad(const char *ip, bool with_port)
{
	ctdb_sock_addr sa;
	int ret;

	ret = ctdb_sock_addr_from_string(ip, &sa, with_port);
	assert(ret == EINVAL);
}

static void test_sock_addr_cmp(const char *ip1, const char *ip2,
			       bool with_port, int res)
{
	ctdb_sock_addr sa1, sa2;
	int ret;

	ret = ctdb_sock_addr_from_string(ip1, &sa1, with_port);
	assert(ret == 0);
	ret = ctdb_sock_addr_from_string(ip2, &sa2, with_port);
	assert(ret == 0);
	ret = ctdb_sock_addr_cmp(&sa1, &sa2);
	if (ret < 0) {
		ret = -1;
	} else if (ret > 0) {
		ret = 1;
	}

	assert(ret == res);
}

/*
 * Test parsing of connection, conversion to string
 */

static void test_connection_to_string(const char *conn_str)
{
	TALLOC_CTX *tmp_ctx;
	struct ctdb_connection conn;
	const char *s, *r;
	int ret;

	tmp_ctx = talloc_new(NULL);
	assert(tmp_ctx != NULL);

	/*
	 * Test non-reversed parse and render
	 */

	ret = ctdb_connection_from_string(conn_str, false, &conn);
	assert(ret == 0);

	s = ctdb_connection_to_string(tmp_ctx, &conn, false);
	assert(s != NULL);
	ret = strcmp(conn_str, s);
	assert(ret == 0);

	talloc_free(discard_const(s));

	/*
	 * Reversed render
	 */
	r = ctdb_connection_to_string(tmp_ctx, &conn, true);
	assert(r != NULL);
	ret = strcmp(conn_str, r);
	assert(ret != 0);

	/*
	 * Reversed parse with forward render
	 */
	ret = ctdb_connection_from_string(conn_str, true, &conn);
	assert(ret == 0);

	s = ctdb_connection_to_string(tmp_ctx, &conn, false);
	assert(s != NULL);
	ret = strcmp(r, s);
	assert(ret == 0);

	talloc_free(discard_const(s));

	/*
	 * Reversed parse and render
	 */
	ret = ctdb_connection_from_string(conn_str, true, &conn);
	assert(ret == 0);

	s = ctdb_connection_to_string(tmp_ctx, &conn, true);
	assert(s != NULL);
	ret = strcmp(conn_str, s);
	assert(ret == 0);

	talloc_free(tmp_ctx);
}

static void test_connection_from_string_bad(const char *conn_str)
{
	struct ctdb_connection conn;
	int ret;

	ret = ctdb_connection_from_string(conn_str, false, &conn);
	assert(ret == EINVAL);
}

/*
 * Test connection list utilities
 */

static void test_connection_list_read(const char *s1, const char *s2)
{
	TALLOC_CTX *tmp_ctx;
	int pipefd[2];
	pid_t pid;
	struct ctdb_connection_list *conn_list;
	const char *t;
	int ret;

	tmp_ctx = talloc_new(NULL);
	assert(tmp_ctx != NULL);

	ret = pipe(pipefd);
	assert(ret == 0);

	pid = fork();
	assert(pid != -1);

	if (pid == 0) {
		close(pipefd[0]);

		ret = dup2(pipefd[1], STDOUT_FILENO);
		assert(ret != -1);

		close(pipefd[1]);

		printf("%s", s1);
		fflush(stdout);

		exit(0);
	}

	close(pipefd[1]);

	ret = dup2(pipefd[0], STDIN_FILENO);
	assert(ret != -1);

	close(pipefd[0]);

	ret = ctdb_connection_list_read(tmp_ctx, false, &conn_list);
	assert(ret == 0);

	ret = ctdb_connection_list_sort(conn_list);
	assert(ret == 0);

	t = ctdb_connection_list_to_string(tmp_ctx, conn_list, false);
	assert(t != NULL);
	ret = strcmp(t, s2);
	assert(ret == 0);

	talloc_free(tmp_ctx);
}

static void test_connection_list_read_bad(const char *s1)
{
	TALLOC_CTX *tmp_ctx;
	int pipefd[2];
	pid_t pid;
	struct ctdb_connection_list *conn_list;
	int ret;

	tmp_ctx = talloc_new(NULL);
	assert(tmp_ctx != NULL);

	ret = pipe(pipefd);
	assert(ret == 0);

	pid = fork();
	assert(pid != -1);

	if (pid == 0) {
		close(pipefd[0]);

		ret = dup2(pipefd[1], STDOUT_FILENO);
		assert(ret != -1);

		close(pipefd[1]);

		printf("%s", s1);
		fflush(stdout);

		exit(0);
	}

	close(pipefd[1]);

	ret = dup2(pipefd[0], STDIN_FILENO);
	assert(ret != -1);

	close(pipefd[0]);

	ret = ctdb_connection_list_read(tmp_ctx, false, &conn_list);
	assert(ret == EINVAL);

	talloc_free(tmp_ctx);
}

/*
 * Use macros for these to make them easy to concatenate
 */

#define CONN4 \
"\
127.0.0.1:12345 127.0.0.2:54321\n\
127.0.0.2:12345 127.0.0.1:54322\n\
127.0.0.1:12346 127.0.0.2:54323\n\
127.0.0.2:12345 127.0.0.1:54324\n\
127.0.0.1:12345 127.0.0.2:54325\n\
"

#define CONN4_SORT \
"\
127.0.0.1:12345 127.0.0.2:54321\n\
127.0.0.1:12345 127.0.0.2:54325\n\
127.0.0.1:12346 127.0.0.2:54323\n\
127.0.0.2:12345 127.0.0.1:54322\n\
127.0.0.2:12345 127.0.0.1:54324\n\
"

#define CONN6 \
"\
fe80::6af7:28ff:fefa:d136:12345 fe80::6af7:28ff:fefa:d137:54321\n\
fe80::6af7:28ff:fefa:d138:12345 fe80::6af7:28ff:fefa:d137:54322\n\
fe80::6af7:28ff:fefa:d136:12346 fe80::6af7:28ff:fefa:d137:54323\n\
fe80::6af7:28ff:fefa:d132:12345 fe80::6af7:28ff:fefa:d137:54324\n\
fe80::6af7:28ff:fefa:d136:12345 fe80::6af7:28ff:fefa:d137:54325\n\
"

#define CONN6_SORT \
"\
fe80::6af7:28ff:fefa:d132:12345 fe80::6af7:28ff:fefa:d137:54324\n\
fe80::6af7:28ff:fefa:d136:12345 fe80::6af7:28ff:fefa:d137:54321\n\
fe80::6af7:28ff:fefa:d136:12345 fe80::6af7:28ff:fefa:d137:54325\n\
fe80::6af7:28ff:fefa:d136:12346 fe80::6af7:28ff:fefa:d137:54323\n\
fe80::6af7:28ff:fefa:d138:12345 fe80::6af7:28ff:fefa:d137:54322\n\
"

int main(int argc, char *argv[])
{
	test_sock_addr_to_string("0.0.0.0", false);
	test_sock_addr_to_string("127.0.0.1", false);
	test_sock_addr_to_string("::1", false);
	test_sock_addr_to_string("192.168.2.1", false);
	test_sock_addr_to_string("fe80::6af7:28ff:fefa:d136", false);

	test_sock_addr_to_string("0.0.0.0:0", true);
	test_sock_addr_to_string("127.0.0.1:123", true);
	test_sock_addr_to_string("::1:234", true);
	test_sock_addr_to_string("192.168.2.1:123", true);
	test_sock_addr_to_string("fe80::6af7:28ff:fefa:d136:234", true);

	test_sock_addr_from_string_bad("0.0.0", false);
	test_sock_addr_from_string_bad("0.0.0:0", true);
	test_sock_addr_from_string_bad("fe80::6af7:28ff:fefa:d136", true);
	test_sock_addr_from_string_bad("junk", false);
	test_sock_addr_from_string_bad("0.0.0.0:0 trailing junk", true);

	test_sock_addr_cmp("127.0.0.1", "127.0.0.1" , false, 0);
	test_sock_addr_cmp("127.0.0.1", "127.0.0.2" , false, -1);
	test_sock_addr_cmp("127.0.0.2", "127.0.0.1" , false, 1);
	test_sock_addr_cmp("127.0.1.2", "127.0.2.1" , false, -1);
	test_sock_addr_cmp("127.0.2.1", "127.0.1.2" , false, 1);
	test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136", "127.0.1.2" , false, 1);
	test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
			   "fe80::6af7:28ff:fefa:d136" , false, 0);
	test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
			   "fe80::6af7:28ff:fefa:d137" , false, -1);
	test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136",
			   "fe80:0000:0000:0000:6af7:28ff:fefa:d136" ,
			   false, 0);
	test_sock_addr_cmp("::ffff:192.0.2.128", "192.0.2.128", false, 0);

	test_sock_addr_cmp("127.0.0.1:123", "127.0.0.1:124" , true, -1);
	test_sock_addr_cmp("fe80::6af7:28ff:fefa:d136:123",
			   "fe80::6af7:28ff:fefa:d136:122" , true, 1);

	test_connection_to_string("127.0.0.1:12345 127.0.0.2:54321");
	test_connection_to_string("fe80::6af7:28ff:fefa:d137:12345 "
				  "fe80::6af7:28ff:fefa:d138:54321");

	test_connection_from_string_bad("127.0.0.1:12345 127.0.0.2:");
	test_connection_from_string_bad("127.0.0.1:12345");
	test_connection_from_string_bad("127.0.0.1:12345 "
					"fe80::6af7:28ff:fefa:d136:122");
	test_connection_from_string_bad("Junk!");
	test_connection_from_string_bad("More junk");

	test_connection_list_read(CONN4, CONN4_SORT);
	test_connection_list_read(CONN6, CONN6_SORT);
	test_connection_list_read(CONN4 CONN6, CONN4_SORT CONN6_SORT);
	test_connection_list_read(CONN4 "# Comment\n\n# Comment\n" CONN6,
				  CONN4_SORT CONN6_SORT);

	test_connection_list_read_bad(CONN4 "# Comment\n\nJunk!!!\n" CONN6);
	test_connection_list_read_bad(CONN4
				      "# Comment\n\n127.0.0.1: 127.0.0.1:124\n"
				      CONN6);

	return 0;
}