ctdb-protocol: Fix marshalling for ctdb_vnn_map
[nivanova/samba-autobuild/.git] / ctdb / tests / src / protocol_types_test.c
1 /*
2    protocol types tests
3
4    Copyright (C) Amitay Isaacs  2015
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/filesys.h"
22
23 #include <assert.h>
24
25 #include "protocol/protocol_basic.c"
26 #include "protocol/protocol_types.c"
27
28 #include "tests/src/protocol_common.h"
29
30 PROTOCOL_TYPE2_TEST(TDB_DATA, ctdb_tdb_data);
31 PROTOCOL_TYPE2_TEST(TDB_DATA, ctdb_tdb_datan);
32 PROTOCOL_TYPE1_TEST(struct ctdb_latency_counter, ctdb_latency_counter);
33
34 static void test_ctdb_ltdb_header(void)
35 {
36         TALLOC_CTX *mem_ctx = talloc_new(NULL);
37         struct ctdb_ltdb_header p1, p2;
38         size_t buflen;
39         int ret;
40
41         fill_ctdb_ltdb_header(mem_ctx, &p1);
42         buflen = ctdb_ltdb_header_len(&p1);
43         ctdb_ltdb_header_push(&p1, BUFFER);
44         ret = ctdb_ltdb_header_pull(BUFFER, buflen, &p2);
45         assert(ret == 0);
46         verify_ctdb_ltdb_header(&p1, &p2);
47         talloc_free(mem_ctx);
48 }
49
50 static void test_ctdb_g_lock(void)
51 {
52         TALLOC_CTX *mem_ctx = talloc_new(NULL);
53         struct ctdb_g_lock p1, p2;
54         size_t buflen;
55         int ret;
56
57         fill_ctdb_g_lock(mem_ctx, &p1);
58         buflen = ctdb_g_lock_len(&p1);
59         ctdb_g_lock_push(&p1, BUFFER);
60         ret = ctdb_g_lock_pull(BUFFER, buflen, &p2);
61         assert(ret == 0);
62         verify_ctdb_g_lock(&p1, &p2);
63         talloc_free(mem_ctx);
64 }
65
66 PROTOCOL_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics);
67 PROTOCOL_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map);
68 DEFINE_TEST(struct ctdb_dbid_map, ctdb_dbid_map);
69 DEFINE_TEST(struct ctdb_pulldb, ctdb_pulldb);
70 DEFINE_TEST(struct ctdb_pulldb_ext, ctdb_pulldb_ext);
71 DEFINE_TEST(struct ctdb_rec_data, ctdb_rec_data);
72 DEFINE_TEST(struct ctdb_rec_buffer, ctdb_rec_buffer);
73 DEFINE_TEST(struct ctdb_traverse_start, ctdb_traverse_start);
74 DEFINE_TEST(struct ctdb_traverse_all, ctdb_traverse_all);
75 DEFINE_TEST(struct ctdb_traverse_start_ext, ctdb_traverse_start_ext);
76 DEFINE_TEST(struct ctdb_traverse_all_ext, ctdb_traverse_all_ext);
77 DEFINE_TEST(ctdb_sock_addr, ctdb_sock_addr);
78 DEFINE_TEST(struct ctdb_connection, ctdb_connection);
79 DEFINE_TEST(struct ctdb_tunable, ctdb_tunable);
80 DEFINE_TEST(struct ctdb_node_flag_change, ctdb_node_flag_change);
81 DEFINE_TEST(struct ctdb_var_list, ctdb_var_list);
82 DEFINE_TEST(struct ctdb_tunable_list, ctdb_tunable_list);
83 DEFINE_TEST(struct ctdb_tickle_list, ctdb_tickle_list);
84 DEFINE_TEST(struct ctdb_addr_info, ctdb_addr_info);
85 DEFINE_TEST(struct ctdb_transdb, ctdb_transdb);
86 DEFINE_TEST(struct ctdb_uptime, ctdb_uptime);
87 DEFINE_TEST(struct ctdb_public_ip, ctdb_public_ip);
88 DEFINE_TEST(struct ctdb_public_ip_list, ctdb_public_ip_list);
89 DEFINE_TEST(struct ctdb_node_and_flags, ctdb_node_and_flags);
90 DEFINE_TEST(struct ctdb_node_map, ctdb_node_map);
91 DEFINE_TEST(struct ctdb_script, ctdb_script);
92 DEFINE_TEST(struct ctdb_script_list, ctdb_script_list);
93 DEFINE_TEST(struct ctdb_ban_state, ctdb_ban_state);
94 DEFINE_TEST(struct ctdb_notify_data, ctdb_notify_data);
95 DEFINE_TEST(struct ctdb_iface, ctdb_iface);
96 DEFINE_TEST(struct ctdb_iface_list, ctdb_iface_list);
97 DEFINE_TEST(struct ctdb_public_ip_info, ctdb_public_ip_info);
98 DEFINE_TEST(struct ctdb_statistics_list, ctdb_statistics_list);
99 DEFINE_TEST(struct ctdb_key_data, ctdb_key_data);
100 DEFINE_TEST(struct ctdb_db_statistics, ctdb_db_statistics);
101 DEFINE_TEST(struct ctdb_election_message, ctdb_election_message);
102 DEFINE_TEST(struct ctdb_srvid_message, ctdb_srvid_message);
103 DEFINE_TEST(struct ctdb_disable_message, ctdb_disable_message);
104 DEFINE_TEST(struct ctdb_g_lock_list, ctdb_g_lock_list);
105
106 static void test_ctdb_rec_buffer_read_write(void)
107 {
108         TALLOC_CTX *mem_ctx = talloc_new(NULL);
109         struct ctdb_rec_buffer *p1, **p2;
110         const char *filename = "ctdb_rec_buffer_test.dat";
111         int count = 100;
112         int fd, i, ret;
113         off_t offset;
114
115         p1 = talloc_array(mem_ctx, struct ctdb_rec_buffer, count);
116         assert(p1 != NULL);
117         for (i=0; i<count; i++) {
118                 fill_ctdb_rec_buffer(mem_ctx, &p1[i]);
119         }
120
121         fd = open(filename, O_RDWR|O_CREAT, 0600);
122         assert(fd != -1);
123         unlink(filename);
124
125         for (i=0; i<count; i++) {
126                 ret = ctdb_rec_buffer_write(&p1[i], fd);
127                 assert(ret == 0);
128         }
129
130         offset = lseek(fd, 0, SEEK_CUR);
131         assert(offset != -1);
132         offset = lseek(fd, -offset, SEEK_CUR);
133         assert(offset == 0);
134
135         p2 = talloc_array(mem_ctx, struct ctdb_rec_buffer *, count);
136         assert(p2 != NULL);
137
138         for (i=0; i<count; i++) {
139                 ret = ctdb_rec_buffer_read(fd, mem_ctx, &p2[i]);
140                 assert(ret == 0);
141         }
142
143         close(fd);
144
145         for (i=0; i<count; i++) {
146                 verify_ctdb_rec_buffer(&p1[i], p2[i]);
147         }
148
149         talloc_free(mem_ctx);
150 }
151
152 int main(int argc, char *argv[])
153 {
154         if (argc == 2) {
155                 int seed = atoi(argv[1]);
156                 srandom(seed);
157         }
158
159         TEST_FUNC(ctdb_tdb_data)();
160         TEST_FUNC(ctdb_tdb_datan)();
161         TEST_FUNC(ctdb_latency_counter)();
162         test_ctdb_ltdb_header();
163         test_ctdb_g_lock();
164
165         TEST_FUNC(ctdb_statistics)();
166         TEST_FUNC(ctdb_vnn_map)();
167         TEST_FUNC(ctdb_dbid_map)();
168         TEST_FUNC(ctdb_pulldb)();
169         TEST_FUNC(ctdb_pulldb_ext)();
170         TEST_FUNC(ctdb_rec_data)();
171         TEST_FUNC(ctdb_rec_buffer)();
172         TEST_FUNC(ctdb_traverse_start)();
173         TEST_FUNC(ctdb_traverse_all)();
174         TEST_FUNC(ctdb_traverse_start_ext)();
175         TEST_FUNC(ctdb_traverse_all_ext)();
176         TEST_FUNC(ctdb_sock_addr)();
177         TEST_FUNC(ctdb_connection)();
178         TEST_FUNC(ctdb_tunable)();
179         TEST_FUNC(ctdb_node_flag_change)();
180         TEST_FUNC(ctdb_var_list)();
181         TEST_FUNC(ctdb_tunable_list)();
182         TEST_FUNC(ctdb_tickle_list)();
183         TEST_FUNC(ctdb_addr_info)();
184         TEST_FUNC(ctdb_transdb)();
185         TEST_FUNC(ctdb_uptime)();
186         TEST_FUNC(ctdb_public_ip)();
187         TEST_FUNC(ctdb_public_ip_list)();
188         TEST_FUNC(ctdb_node_and_flags)();
189         TEST_FUNC(ctdb_node_map)();
190         TEST_FUNC(ctdb_script)();
191         TEST_FUNC(ctdb_script_list)();
192         TEST_FUNC(ctdb_ban_state)();
193         TEST_FUNC(ctdb_notify_data)();
194         TEST_FUNC(ctdb_iface)();
195         TEST_FUNC(ctdb_iface_list)();
196         TEST_FUNC(ctdb_public_ip_info)();
197         TEST_FUNC(ctdb_statistics_list)();
198         TEST_FUNC(ctdb_key_data)();
199         TEST_FUNC(ctdb_db_statistics)();
200         TEST_FUNC(ctdb_election_message)();
201         TEST_FUNC(ctdb_srvid_message)();
202         TEST_FUNC(ctdb_disable_message)();
203         TEST_FUNC(ctdb_g_lock_list)();
204
205         test_ctdb_rec_buffer_read_write();
206
207         return 0;
208 }