1 /*
2    protocol types tests
3 
4    Copyright (C) Amitay Isaacs  2015-2017
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 <assert.h>
21 
22 #include "protocol/protocol_basic.c"
23 
24 #include "tests/src/protocol_common_basic.h"
25 
26 PROTOCOL_TYPE1_TEST(uint8_t, ctdb_uint8);
27 PROTOCOL_TYPE1_TEST(uint16_t, ctdb_uint16);
28 PROTOCOL_TYPE1_TEST(int32_t, ctdb_int32);
29 PROTOCOL_TYPE1_TEST(uint32_t, ctdb_uint32);
30 PROTOCOL_TYPE1_TEST(uint64_t, ctdb_uint64);
31 PROTOCOL_TYPE1_TEST(double, ctdb_double);
32 PROTOCOL_TYPE1_TEST(bool, ctdb_bool);
33 
34 static void test_ctdb_chararray(void)
35 {
36 	size_t len = rand_int(1000) + 1;
37 	char p1[len], p2[len];
38 	size_t buflen, np = 0;
39 	size_t i;
40 	int ret;
41 
42 	for (i=0; i<len-1; i++) {
43 		p1[i] = 'A' + rand_int(26);
44 	}
45 	p1[len-1] = '\0';
46 	buflen = ctdb_chararray_len(p1, len);
47 	assert(buflen < sizeof(BUFFER));
48 	ctdb_chararray_push(p1, len, BUFFER, &np);
49 	assert(np == buflen);
50 	np = 0;
51 	ret = ctdb_chararray_pull(BUFFER, buflen, p2, len, &np);
52 	assert(ret == 0);
53 	assert(np == buflen);
54 	assert(strncmp(p1, p2, len) == 0);
55 }
socket_server_create(void)56 
57 PROTOCOL_TYPE2_TEST(const char *, ctdb_string);
58 PROTOCOL_TYPE2_TEST(const char *, ctdb_stringn);
59 
60 PROTOCOL_TYPE1_TEST(pid_t, ctdb_pid);
61 PROTOCOL_TYPE1_TEST(struct timeval, ctdb_timeval);
62 
63 static void test_ctdb_padding(void)
64 {
65 	int padding;
66 	size_t buflen, np = 0;
67 	int ret;
68 
69 	padding = rand_int(8);
70 
71 	buflen = ctdb_padding_len(padding);
72 	assert(buflen < sizeof(BUFFER));
73 	ctdb_padding_push(padding, BUFFER, &np);
74 	assert(np == buflen);
75 	np = 0;
76 	ret = ctdb_padding_pull(BUFFER, buflen, padding, &np);
77 	assert(ret == 0);
78 	assert(np == buflen);
79 }
80 
81 int main(int argc, char *argv[])
82 {
83 	if (argc == 2) {
84 		int seed = atoi(argv[1]);
85 		srandom(seed);
socket_server_wait_peer(void)86 	}
87 
88 	TEST_FUNC(ctdb_uint8)();
89 	TEST_FUNC(ctdb_uint16)();
90 	TEST_FUNC(ctdb_int32)();
91 	TEST_FUNC(ctdb_uint32)();
92 	TEST_FUNC(ctdb_uint64)();
93 	TEST_FUNC(ctdb_double)();
94 	TEST_FUNC(ctdb_bool)();
95 
96 	test_ctdb_chararray();
97 
98 	TEST_FUNC(ctdb_string)();
99 	TEST_FUNC(ctdb_stringn)();
100 
101 	TEST_FUNC(ctdb_pid)();
socket_server_close(void)102 	TEST_FUNC(ctdb_timeval)();
103 
104 	test_ctdb_padding();
105 
106 	return 0;
107 }
108