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/>.
21 #include "include/ctdb_private.h"
22 #include "system/filesys.h"
27 const char *socketname
;
28 const char *debuglevel
;
34 .socketname
= "/tmp/test.sock"
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
)));
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
)));
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
)));
75 if (listen(globals
.socket
, 100) != 0) {
76 DEBUG(DEBUG_CRIT
,("Unable to listen on socket '%s': %s\n", globals
.socketname
, strerror(errno
)));
82 close(globals
.socket
);
87 static int socket_server_wait_peer(void)
89 struct sockaddr_un addr
;
93 memset(&addr
, 0, sizeof(addr
));
95 fd
= accept(globals
.socket
, (struct sockaddr
*)&addr
, &len
);
97 DEBUG(DEBUG_CRIT
,("Unable to accept on ctdb socket '%s': %s\n", globals
.socketname
, strerror(errno
)));
101 //set_nonblocking(fd);
102 set_close_on_exec(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
)));
112 if (unlink(globals
.socketname
) == -1) {
113 DEBUG(DEBUG_CRIT
,("Unable to remove server socket: %s\n", strerror(errno
)));
119 static int socket_client_connect(void)
121 struct sockaddr_un addr
;
124 client
= socket(AF_UNIX
, SOCK_STREAM
, 0);
126 DEBUG(DEBUG_CRIT
,("Unable to create client socket: %s\n", strerror(errno
)));
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
)));
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
)));
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
)));
163 static int fork_helper(void)
166 int i
, client
, max_rounds
= 10;
170 DEBUG(DEBUG_CRIT
,("Unable to fork: %s\n", strerror(errno
)));
173 if (pid
== 0) { // Child
174 client
= socket_client_connect();
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
));
183 socket_client_close(client
);
186 globals
.helper_pid
= pid
;
194 int test_ctdb_sys_check_iface_exists(void)
196 const char *fakename
= "fake";
199 test
= ctdb_sys_check_iface_exists(fakename
);
201 DEBUG(DEBUG_CRIT
,("Test failed: Fake interface detected: %s\n", fakename
));
204 DEBUG(DEBUG_INFO
,("Test OK: Fake interface not detected: %s\n", fakename
));
205 globals
.successcount
++;
209 int test_ctdb_get_peer_pid(void)
215 fd
= socket_server_wait_peer();
219 ret
= ctdb_get_peer_pid(fd
, &peer_pid
);
221 DEBUG(DEBUG_CRIT
,("Test failed: Unable to get peer process id\n"));
226 DEBUG(DEBUG_CRIT
,("Test failed: Invalid peer process id: %d\n", peer_pid
));
230 DEBUG(DEBUG_INFO
,("Test OK: Peer process id: %d\n", peer_pid
));
231 globals
.successcount
++;
236 int test_ctdb_get_process_name(void)
238 char *process_name
= NULL
;
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
));
246 DEBUG(DEBUG_INFO
,("Test OK: Name of PID=%d: %s\n", globals
.helper_pid
, process_name
));
247 globals
.successcount
++;
255 int main(int argc
, const char *argv
[])
257 struct poptOption popt_options
[] = {
259 { "socket", 0, POPT_ARG_STRING
, &globals
.socketname
, 0, "local socket name", "filename" },
263 const char **extra_argv
;
267 LogLevel
= DEBUG_INFO
;
269 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
271 while ((opt
= poptGetNextOpt(pc
)) != -1) {
274 fprintf(stderr
, "Invalid option %s: %s\n",
275 poptBadOption(pc
, 0), poptStrerror(opt
));
280 /* setup the remaining options for the main program to use */
281 extra_argv
= poptGetArgs(pc
);
284 while (extra_argv
[extra_argc
]) extra_argc
++;
287 if (globals
.socketname
== NULL
) {
288 DEBUG(DEBUG_CRIT
,("Socket name is undefined\n"));
291 if (socket_server_create()) {
292 DEBUG(DEBUG_CRIT
,("Socket error: exiting\n"));
296 DEBUG(DEBUG_CRIT
,("Forking error: exiting\n"));
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
));