s3:lib/memcache: make use of talloc for memcache_elements
[Samba.git] / ctdb / tests / src / ctdb_porting_tests.c
blob063c6daf544dbc52024b8486071997e5dff020f4
1 /*
2 Test porting lib (common/system_*.c)
4 Copyright (C) Mathieu Parent 2013
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/>.
20 #include "includes.h"
21 #include "include/ctdb_private.h"
22 #include "system/filesys.h"
23 #include "popt.h"
24 #include "cmdline.h"
26 static struct {
27 const char *socketname;
28 const char *debuglevel;
29 pid_t helper_pid;
30 int socket;
31 int successcount;
32 int testcount;
33 } globals = {
34 .socketname = "/tmp/test.sock"
40 Socket functions
43 create a unix domain socket and bind it
44 return a file descriptor open on the socket
46 static int socket_server_create(void)
48 struct sockaddr_un addr;
50 globals.socket = socket(AF_UNIX, SOCK_STREAM, 0);
51 if (globals.socket == -1) {
52 DEBUG(DEBUG_CRIT,("Unable to create server socket: %s\n", strerror(errno)));
53 return -1;
56 set_close_on_exec(globals.socket);
57 //set_nonblocking(globals.socket);
59 memset(&addr, 0, sizeof(addr));
60 addr.sun_family = AF_UNIX;
61 strncpy(addr.sun_path, globals.socketname, sizeof(addr.sun_path)-1);
63 if (bind(globals.socket, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
64 DEBUG(DEBUG_CRIT,("Unable to bind on socket '%s': %s\n", globals.socketname, strerror(errno)));
65 goto failed;
68 if (chown(globals.socketname, geteuid(), getegid()) != 0 ||
69 chmod(globals.socketname, 0700) != 0) {
70 DEBUG(DEBUG_CRIT,("Unable to secure socket '%s': %s\n", globals.socketname, strerror(errno)));
71 goto failed;
75 if (listen(globals.socket, 100) != 0) {
76 DEBUG(DEBUG_CRIT,("Unable to listen on socket '%s': %s\n", globals.socketname, strerror(errno)));
77 goto failed;
79 return 0;
81 failed:
82 close(globals.socket);
83 globals.socket = -1;
84 return -1;
87 static int socket_server_wait_peer(void)
89 struct sockaddr_un addr;
90 socklen_t len;
91 int fd;
93 memset(&addr, 0, sizeof(addr));
94 len = sizeof(addr);
95 fd = accept(globals.socket, (struct sockaddr *)&addr, &len);
96 if (fd == -1) {
97 DEBUG(DEBUG_CRIT,("Unable to accept on ctdb socket '%s': %s\n", globals.socketname, strerror(errno)));
98 return -1;
101 //set_nonblocking(fd);
102 set_close_on_exec(fd);
103 return fd;
106 static int socket_server_close(void)
108 if (close(globals.socket) == -1) {
109 DEBUG(DEBUG_CRIT,("Unable to close server socket: %s\n", strerror(errno)));
110 return -1;
112 if (unlink(globals.socketname) == -1) {
113 DEBUG(DEBUG_CRIT,("Unable to remove server socket: %s\n", strerror(errno)));
114 return -1;
116 return 0;
119 static int socket_client_connect(void)
121 struct sockaddr_un addr;
122 int client = 0;
124 client = socket(AF_UNIX, SOCK_STREAM, 0);
125 if (client == -1) {
126 DEBUG(DEBUG_CRIT,("Unable to create client socket: %s\n", strerror(errno)));
127 return -1;
130 memset(&addr, 0, sizeof(addr));
131 addr.sun_family = AF_UNIX;
132 strncpy(addr.sun_path, globals.socketname, sizeof(addr.sun_path)-1);
133 if (connect(client, (struct sockaddr *)&addr, sizeof(addr))==-1) {
134 DEBUG(DEBUG_CRIT,("Unable to connect to '%s': %s\n", globals.socketname, strerror(errno)));
135 close(client);
136 return -1;
139 return client;
142 static int socket_client_write(int client)
144 if (write(client, "\0", 1) == -1) {
145 DEBUG(DEBUG_CRIT,("Unable to write to client socket: %s\n", strerror(errno)));
146 return -1;
148 return 0;
151 static int socket_client_close(int client)
153 if (close(client) == -1) {
154 DEBUG(DEBUG_CRIT,("Unable to close client socket: %s\n", strerror(errno)));
155 return -1;
157 return 0;
161 forked program
163 static int fork_helper(void)
165 pid_t pid;
166 int i, client, max_rounds = 10;
168 pid = fork();
169 if (pid == -1) {
170 DEBUG(DEBUG_CRIT,("Unable to fork: %s\n", strerror(errno)));
171 return -1;
173 if (pid == 0) { // Child
174 client = socket_client_connect();
175 if (client < 0) {
176 exit(1);
178 socket_client_write(client);
179 for (i = 1 ; i <= max_rounds ; i++ ) {
180 DEBUG(DEBUG_DEBUG,("Child process waiting ( %d/%d)\n", i, max_rounds));
181 sleep(1);
183 socket_client_close(client);
184 exit(0);
185 } else {
186 globals.helper_pid = pid;
188 return 0;
192 tests
194 int test_ctdb_sys_check_iface_exists(void)
196 const char *fakename = "fake";
197 bool test;
198 globals.testcount++;
199 test = ctdb_sys_check_iface_exists(fakename);
200 if(test == true) {
201 DEBUG(DEBUG_CRIT,("Test failed: Fake interface detected: %s\n", fakename));
202 return -1;
204 DEBUG(DEBUG_INFO,("Test OK: Fake interface not detected: %s\n", fakename));
205 globals.successcount++;
206 return 0;
209 int test_ctdb_get_peer_pid(void)
211 int ret;
212 int fd;
213 pid_t peer_pid = 0;
214 globals.testcount++;
215 fd = socket_server_wait_peer();
216 if (fd < 0) {
217 return -1;
219 ret = ctdb_get_peer_pid(fd, &peer_pid);
220 if (ret == -1) {
221 DEBUG(DEBUG_CRIT,("Test failed: Unable to get peer process id\n"));
222 close(fd);
223 return -1;
225 if (peer_pid <= 0) {
226 DEBUG(DEBUG_CRIT,("Test failed: Invalid peer process id: %d\n", peer_pid));
227 close(fd);
228 return -1;
230 DEBUG(DEBUG_INFO,("Test OK: Peer process id: %d\n", peer_pid));
231 globals.successcount++;
232 close(fd);
233 return 0;
236 int test_ctdb_get_process_name(void)
238 char *process_name = NULL;
239 globals.testcount++;
240 process_name = ctdb_get_process_name(globals.helper_pid);
241 if ((process_name == NULL) || !strcmp(process_name, "unknown")) {
242 DEBUG(DEBUG_CRIT,("Test failed: Invalid process name of %d: %s\n", globals.helper_pid, process_name));
243 free(process_name);
244 return -1;
246 DEBUG(DEBUG_INFO,("Test OK: Name of PID=%d: %s\n", globals.helper_pid, process_name));
247 globals.successcount++;
248 free(process_name);
249 return 0;
253 main program
255 int main(int argc, const char *argv[])
257 struct poptOption popt_options[] = {
258 POPT_AUTOHELP
259 { "socket", 0, POPT_ARG_STRING, &globals.socketname, 0, "local socket name", "filename" },
260 POPT_TABLEEND
262 int opt;
263 const char **extra_argv;
264 int extra_argc = 0;
265 poptContext pc;
267 LogLevel = DEBUG_INFO;
269 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
271 while ((opt = poptGetNextOpt(pc)) != -1) {
272 switch (opt) {
273 default:
274 fprintf(stderr, "Invalid option %s: %s\n",
275 poptBadOption(pc, 0), poptStrerror(opt));
276 exit(1);
280 /* setup the remaining options for the main program to use */
281 extra_argv = poptGetArgs(pc);
282 if (extra_argv) {
283 extra_argv++;
284 while (extra_argv[extra_argc]) extra_argc++;
287 if (globals.socketname == NULL) {
288 DEBUG(DEBUG_CRIT,("Socket name is undefined\n"));
289 exit(1);
291 if (socket_server_create()) {
292 DEBUG(DEBUG_CRIT,("Socket error: exiting\n"));
293 exit(1);
295 if (fork_helper()) {
296 DEBUG(DEBUG_CRIT,("Forking error: exiting\n"));
297 exit(1);
299 /* FIXME: Test tcp_checksum6, tcp_checksum */
300 /* FIXME: Test ctdb_sys_send_arp, ctdb_sys_send_tcp */
301 /* FIXME: Test ctdb_sys_{open,close}_capture_socket, ctdb_sys_read_tcp_packet */
302 test_ctdb_sys_check_iface_exists();
303 test_ctdb_get_peer_pid();
304 test_ctdb_get_process_name();
305 /* FIXME: Test ctdb_get_lock_info, ctdb_get_blocker_pid*/
307 socket_server_close();
309 DEBUG(DEBUG_INFO,("%d/%d tests successfull\n", globals.successcount, globals.testcount));
310 return 0;