2010-12-06 20:03:35 +03:00
/*
2014-01-01 20:57:42 +04:00
* Copyright ( C ) 2011 , 2014 Red Hat , Inc .
2010-12-06 20:03:35 +03:00
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* This library 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
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
2012-09-21 02:30:55 +04:00
* License along with this library . If not , see
2012-07-21 14:06:23 +04:00
* < http : //www.gnu.org/licenses/>.
2010-12-06 20:03:35 +03:00
*/
# include <config.h>
# include <signal.h>
2020-02-24 00:02:26 +03:00
# include <unistd.h>
2020-09-01 14:27:44 +03:00
# ifdef WITH_IFADDRS_H
2010-12-06 20:03:35 +03:00
# include <ifaddrs.h>
# endif
# include "testutils.h"
2012-12-13 22:21:53 +04:00
# include "virerror.h"
2012-12-12 22:06:53 +04:00
# include "viralloc.h"
2012-12-12 21:59:27 +04:00
# include "virlog.h"
2011-07-19 22:32:58 +04:00
# include "virfile.h"
2010-12-06 20:03:35 +03:00
# include "rpc/virnetsocket.h"
2020-02-11 22:05:53 +03:00
# include "rpc/virnetclient.h"
2010-12-06 20:03:35 +03:00
# define VIR_FROM_THIS VIR_FROM_RPC
2014-02-28 16:16:17 +04:00
VIR_LOG_INIT ( " tests.netsockettest " ) ;
2020-09-01 14:27:44 +03:00
# if WITH_IFADDRS_H
2010-12-06 20:03:35 +03:00
# define BASE_PORT 5672
static int
checkProtocols ( bool * hasIPv4 , bool * hasIPv6 ,
int * freePort )
{
struct sockaddr_in in4 ;
struct sockaddr_in6 in6 ;
Convert 'int i' to 'size_t i' in tests/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 18:09:33 +04:00
size_t i ;
2010-12-06 20:03:35 +03:00
* freePort = 0 ;
2015-05-21 18:27:15 +03:00
if ( virNetSocketCheckProtocols ( hasIPv4 , hasIPv6 ) < 0 )
return - 1 ;
2010-12-06 20:03:35 +03:00
2013-05-21 11:53:48 +04:00
for ( i = 0 ; i < 50 ; i + + ) {
2021-09-04 21:45:33 +03:00
VIR_AUTOCLOSE s4 = - 1 ;
VIR_AUTOCLOSE s6 = - 1 ;
2020-07-14 22:39:50 +03:00
if ( * hasIPv4 ) {
if ( ( s4 = socket ( AF_INET , SOCK_STREAM , 0 ) ) < 0 )
2021-09-04 21:45:33 +03:00
return - 1 ;
2020-07-14 22:39:50 +03:00
}
2010-12-06 20:03:35 +03:00
2012-07-22 21:04:16 +04:00
if ( * hasIPv6 ) {
2020-07-14 22:52:10 +03:00
int only = 1 ;
2012-07-22 21:04:16 +04:00
if ( ( s6 = socket ( AF_INET6 , SOCK_STREAM , 0 ) ) < 0 )
2021-09-04 21:45:33 +03:00
return - 1 ;
2010-12-06 20:03:35 +03:00
2012-07-22 21:04:16 +04:00
if ( setsockopt ( s6 , IPPROTO_IPV6 , IPV6_V6ONLY , & only , sizeof ( only ) ) < 0 )
2021-09-04 21:45:33 +03:00
return - 1 ;
2012-07-22 21:04:16 +04:00
}
2010-12-06 20:03:35 +03:00
memset ( & in4 , 0 , sizeof ( in4 ) ) ;
memset ( & in6 , 0 , sizeof ( in6 ) ) ;
in4 . sin_family = AF_INET ;
in4 . sin_port = htons ( BASE_PORT + i ) ;
in4 . sin_addr . s_addr = htonl ( INADDR_LOOPBACK ) ;
in6 . sin6_family = AF_INET6 ;
in6 . sin6_port = htons ( BASE_PORT + i ) ;
in6 . sin6_addr = in6addr_loopback ;
2020-07-14 22:39:50 +03:00
if ( * hasIPv4 ) {
if ( bind ( s4 , ( struct sockaddr * ) & in4 , sizeof ( in4 ) ) < 0 ) {
if ( errno = = EADDRINUSE ) {
continue ;
}
2021-09-04 21:45:33 +03:00
return - 1 ;
2010-12-06 20:03:35 +03:00
}
}
2012-07-22 21:04:16 +04:00
if ( * hasIPv6 ) {
if ( bind ( s6 , ( struct sockaddr * ) & in6 , sizeof ( in6 ) ) < 0 ) {
if ( errno = = EADDRINUSE ) {
continue ;
}
2021-09-04 21:45:33 +03:00
return - 1 ;
2010-12-06 20:03:35 +03:00
}
}
* freePort = BASE_PORT + i ;
break ;
}
2015-10-27 21:14:01 +03:00
VIR_DEBUG ( " Choose port %d " , * freePort ) ;
2010-12-06 20:03:35 +03:00
2021-09-04 21:45:33 +03:00
return 0 ;
2010-12-06 20:03:35 +03:00
}
2018-05-01 13:55:02 +03:00
struct testClientData {
const char * path ;
const char * cnode ;
const char * portstr ;
} ;
static void
testSocketClient ( void * opaque )
{
struct testClientData * data = opaque ;
char c ;
2021-03-11 10:16:13 +03:00
virNetSocket * csock = NULL ;
2018-05-01 13:55:02 +03:00
if ( data - > path ) {
2021-05-24 17:07:19 +03:00
if ( virNetSocketNewConnectUNIX ( data - > path ,
2018-05-01 13:55:02 +03:00
NULL , & csock ) < 0 )
return ;
} else {
if ( virNetSocketNewConnectTCP ( data - > cnode , data - > portstr ,
AF_UNSPEC ,
& csock ) < 0 )
return ;
}
virNetSocketSetBlocking ( csock , true ) ;
if ( virNetSocketRead ( csock , & c , 1 ) ! = 1 ) {
VIR_DEBUG ( " Cannot read from server " ) ;
goto done ;
}
if ( virNetSocketWrite ( csock , & c , 1 ) ! = 1 ) {
VIR_DEBUG ( " Cannot write to server " ) ;
goto done ;
}
done :
virObjectUnref ( csock ) ;
}
static void
2021-03-11 10:16:13 +03:00
testSocketIncoming ( virNetSocket * sock ,
2019-10-14 15:45:03 +03:00
int events G_GNUC_UNUSED ,
2018-05-01 13:55:02 +03:00
void * opaque )
{
2021-03-11 10:16:13 +03:00
virNetSocket * * retsock = opaque ;
2018-09-17 18:06:30 +03:00
VIR_DEBUG ( " Incoming sock=%p events=%d " , sock , events ) ;
2018-05-01 13:55:02 +03:00
* retsock = sock ;
}
2010-12-06 20:03:35 +03:00
2018-05-01 13:34:51 +03:00
struct testSocketData {
2010-12-06 20:03:35 +03:00
const char * lnode ;
int port ;
const char * cnode ;
} ;
2018-05-01 13:55:02 +03:00
static int
testSocketAccept ( const void * opaque )
2010-12-06 20:03:35 +03:00
{
2021-03-11 10:16:13 +03:00
virNetSocket * * lsock = NULL ; /* Listen socket */
2010-12-06 20:03:35 +03:00
size_t nlsock = 0 , i ;
2021-03-11 10:16:13 +03:00
virNetSocket * ssock = NULL ; /* Server socket */
virNetSocket * rsock = NULL ; /* Incoming client socket */
2018-05-01 13:34:51 +03:00
const struct testSocketData * data = opaque ;
2010-12-06 20:03:35 +03:00
int ret = - 1 ;
char portstr [ 100 ] ;
2018-05-01 13:34:51 +03:00
char * tmpdir = NULL ;
2021-09-04 23:37:31 +03:00
g_autofree char * path = NULL ;
2018-05-01 13:34:51 +03:00
char template [ ] = " /tmp/libvirt_XXXXXX " ;
2018-05-01 13:55:02 +03:00
virThread th ;
struct testClientData cdata = { 0 } ;
bool goodsock = false ;
char a = ' a ' ;
char b = ' \0 ' ;
2010-12-06 20:03:35 +03:00
2018-05-01 13:34:51 +03:00
if ( ! data ) {
2021-03-11 10:16:13 +03:00
virNetSocket * usock ;
2019-11-14 00:35:47 +03:00
tmpdir = g_mkdtemp ( template ) ;
2018-05-01 13:34:51 +03:00
if ( tmpdir = = NULL ) {
VIR_WARN ( " Failed to create temporary directory " ) ;
goto cleanup ;
}
2019-10-22 16:26:14 +03:00
path = g_strdup_printf ( " %s/test.sock " , tmpdir ) ;
2010-12-06 20:03:35 +03:00
2018-05-01 13:34:51 +03:00
if ( virNetSocketNewListenUNIX ( path , 0700 , - 1 , getegid ( ) , & usock ) < 0 )
goto cleanup ;
2021-03-11 10:16:13 +03:00
lsock = g_new0 ( virNetSocket * , 1 ) ;
2018-05-01 13:34:51 +03:00
lsock [ 0 ] = usock ;
nlsock = 1 ;
2018-05-01 13:55:02 +03:00
cdata . path = path ;
2018-05-01 13:34:51 +03:00
} else {
2019-11-13 16:53:42 +03:00
g_snprintf ( portstr , sizeof ( portstr ) , " %d " , data - > port ) ;
2018-05-01 13:34:51 +03:00
if ( virNetSocketNewListenTCP ( data - > lnode , portstr ,
AF_UNSPEC ,
& lsock , & nlsock ) < 0 )
goto cleanup ;
2018-05-01 13:55:02 +03:00
cdata . cnode = data - > cnode ;
cdata . portstr = portstr ;
2018-05-01 13:34:51 +03:00
}
2010-12-06 20:03:35 +03:00
2013-05-21 11:53:48 +04:00
for ( i = 0 ; i < nlsock ; i + + ) {
2011-08-12 13:07:51 +04:00
if ( virNetSocketListen ( lsock [ i ] , 0 ) < 0 )
2010-12-06 20:03:35 +03:00
goto cleanup ;
2018-05-01 13:55:02 +03:00
if ( virNetSocketAddIOCallback ( lsock [ i ] ,
VIR_EVENT_HANDLE_READABLE ,
testSocketIncoming ,
& rsock ,
NULL ) < 0 ) {
2018-05-01 13:34:51 +03:00
goto cleanup ;
2018-05-01 13:55:02 +03:00
}
2018-05-01 13:34:51 +03:00
}
2010-12-06 20:03:35 +03:00
2018-05-01 13:55:02 +03:00
if ( virThreadCreate ( & th , true ,
testSocketClient ,
& cdata ) < 0 )
goto cleanup ;
2018-09-07 15:20:15 +03:00
while ( rsock = = NULL ) {
if ( virEventRunDefaultImpl ( ) < 0 )
break ;
}
2010-12-06 20:03:35 +03:00
2013-05-21 11:53:48 +04:00
for ( i = 0 ; i < nlsock ; i + + ) {
2018-05-01 13:55:02 +03:00
if ( lsock [ i ] = = rsock ) {
goodsock = true ;
break ;
2010-12-06 20:03:35 +03:00
}
}
2018-05-01 13:55:02 +03:00
if ( ! goodsock ) {
virReportError ( VIR_ERR_INTERNAL_ERROR , " %s " ,
" Unexpected server socket seen " ) ;
goto join ;
}
if ( virNetSocketAccept ( rsock , & ssock ) < 0 )
goto join ;
if ( ! ssock ) {
virReportError ( VIR_ERR_INTERNAL_ERROR , " %s " ,
" Client went away unexpectedly " ) ;
goto join ;
}
virNetSocketSetBlocking ( ssock , true ) ;
if ( virNetSocketWrite ( ssock , & a , 1 ) < 0 | |
virNetSocketRead ( ssock , & b , 1 ) < 0 ) {
goto join ;
}
if ( a ! = b ) {
virReportError ( VIR_ERR_INTERNAL_ERROR ,
" Bad data received '%x' != '%x' " , a , b ) ;
goto join ;
}
2022-01-28 20:42:45 +03:00
g_clear_pointer ( & ssock , virObjectUnref ) ;
2018-05-01 13:55:02 +03:00
2010-12-06 20:03:35 +03:00
ret = 0 ;
2018-05-01 13:55:02 +03:00
join :
virThreadJoin ( & th ) ;
2014-03-25 10:53:44 +04:00
cleanup :
2012-07-11 17:35:51 +04:00
virObjectUnref ( ssock ) ;
2018-05-01 13:55:02 +03:00
for ( i = 0 ; i < nlsock ; i + + ) {
virNetSocketRemoveIOCallback ( lsock [ i ] ) ;
virNetSocketClose ( lsock [ i ] ) ;
2012-07-11 17:35:51 +04:00
virObjectUnref ( lsock [ i ] ) ;
2018-05-01 13:55:02 +03:00
}
2010-12-06 20:03:35 +03:00
VIR_FREE ( lsock ) ;
2011-11-03 01:06:44 +04:00
if ( tmpdir )
rmdir ( tmpdir ) ;
2010-12-06 20:03:35 +03:00
return ret ;
}
2018-05-01 13:34:51 +03:00
# endif
2010-12-06 20:03:35 +03:00
2018-05-01 13:34:51 +03:00
# ifndef WIN32
2019-10-14 15:45:03 +03:00
static int testSocketUNIXAddrs ( const void * data G_GNUC_UNUSED )
2010-12-06 20:03:35 +03:00
{
2021-03-11 10:16:13 +03:00
virNetSocket * lsock = NULL ; /* Listen socket */
virNetSocket * ssock = NULL ; /* Server socket */
virNetSocket * csock = NULL ; /* Client socket */
2010-12-06 20:03:35 +03:00
int ret = - 1 ;
2021-09-04 23:37:31 +03:00
g_autofree char * path = NULL ;
2011-11-03 01:06:44 +04:00
char * tmpdir ;
char template [ ] = " /tmp/libvirt_XXXXXX " ;
2019-11-14 00:35:47 +03:00
tmpdir = g_mkdtemp ( template ) ;
2011-11-03 01:06:44 +04:00
if ( tmpdir = = NULL ) {
2011-11-03 14:52:44 +04:00
VIR_WARN ( " Failed to create temporary directory " ) ;
2011-11-03 01:06:44 +04:00
goto cleanup ;
}
2019-10-22 16:26:14 +03:00
path = g_strdup_printf ( " %s/test.sock " , tmpdir ) ;
2010-12-06 20:03:35 +03:00
2013-10-09 15:13:45 +04:00
if ( virNetSocketNewListenUNIX ( path , 0700 , - 1 , getegid ( ) , & lsock ) < 0 )
2010-12-06 20:03:35 +03:00
goto cleanup ;
2016-06-20 17:01:50 +03:00
if ( STRNEQ ( virNetSocketLocalAddrStringSASL ( lsock ) , " 127.0.0.1;0 " ) ) {
2010-12-06 20:03:35 +03:00
VIR_DEBUG ( " Unexpected local address " ) ;
goto cleanup ;
}
2016-06-20 17:01:50 +03:00
if ( virNetSocketRemoteAddrStringSASL ( lsock ) ! = NULL ) {
2010-12-06 20:03:35 +03:00
VIR_DEBUG ( " Unexpected remote address " ) ;
goto cleanup ;
}
2011-08-12 13:07:51 +04:00
if ( virNetSocketListen ( lsock , 0 ) < 0 )
2010-12-06 20:03:35 +03:00
goto cleanup ;
2021-05-24 17:07:19 +03:00
if ( virNetSocketNewConnectUNIX ( path , NULL , & csock ) < 0 )
2010-12-06 20:03:35 +03:00
goto cleanup ;
2016-06-20 17:01:50 +03:00
if ( STRNEQ ( virNetSocketLocalAddrStringSASL ( csock ) , " 127.0.0.1;0 " ) ) {
2010-12-06 20:03:35 +03:00
VIR_DEBUG ( " Unexpected local address " ) ;
goto cleanup ;
}
2016-06-20 17:01:50 +03:00
if ( STRNEQ ( virNetSocketRemoteAddrStringSASL ( csock ) , " 127.0.0.1;0 " ) ) {
2016-06-20 16:52:55 +03:00
VIR_DEBUG ( " Unexpected remote address " ) ;
2010-12-06 20:03:35 +03:00
goto cleanup ;
}
2016-06-20 16:48:47 +03:00
if ( STRNEQ ( virNetSocketRemoteAddrStringURI ( csock ) , " 127.0.0.1:0 " ) ) {
VIR_DEBUG ( " Unexpected remote address " ) ;
goto cleanup ;
}
2010-12-06 20:03:35 +03:00
if ( virNetSocketAccept ( lsock , & ssock ) < 0 ) {
VIR_DEBUG ( " Unexpected client socket missing " ) ;
goto cleanup ;
}
2016-06-20 17:01:50 +03:00
if ( STRNEQ ( virNetSocketLocalAddrStringSASL ( ssock ) , " 127.0.0.1;0 " ) ) {
2010-12-06 20:03:35 +03:00
VIR_DEBUG ( " Unexpected local address " ) ;
goto cleanup ;
}
2016-06-20 17:01:50 +03:00
if ( STRNEQ ( virNetSocketRemoteAddrStringSASL ( ssock ) , " 127.0.0.1;0 " ) ) {
2016-06-20 16:52:55 +03:00
VIR_DEBUG ( " Unexpected remote address " ) ;
2010-12-06 20:03:35 +03:00
goto cleanup ;
}
2016-06-20 16:48:47 +03:00
if ( STRNEQ ( virNetSocketRemoteAddrStringURI ( ssock ) , " 127.0.0.1:0 " ) ) {
VIR_DEBUG ( " Unexpected remote address " ) ;
goto cleanup ;
}
2010-12-06 20:03:35 +03:00
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2012-07-11 17:35:51 +04:00
virObjectUnref ( lsock ) ;
virObjectUnref ( ssock ) ;
virObjectUnref ( csock ) ;
2011-11-03 01:06:44 +04:00
if ( tmpdir )
rmdir ( tmpdir ) ;
2010-12-06 20:03:35 +03:00
return ret ;
}
2019-10-14 15:45:03 +03:00
static int testSocketCommandNormal ( const void * data G_GNUC_UNUSED )
2010-12-06 20:03:35 +03:00
{
2021-03-11 10:16:13 +03:00
virNetSocket * csock = NULL ; /* Client socket */
2010-12-06 20:03:35 +03:00
char buf [ 100 ] ;
size_t i ;
int ret = - 1 ;
2021-10-15 14:39:36 +03:00
g_autoptr ( virCommand ) cmd = virCommandNewArgList ( " /bin/cat " , " /dev/zero " , NULL ) ;
2010-12-06 20:03:35 +03:00
virCommandAddEnvPassCommon ( cmd ) ;
if ( virNetSocketNewConnectCommand ( cmd , & csock ) < 0 )
goto cleanup ;
virNetSocketSetBlocking ( csock , true ) ;
if ( virNetSocketRead ( csock , buf , sizeof ( buf ) ) < 0 )
goto cleanup ;
2013-05-21 11:53:48 +04:00
for ( i = 0 ; i < sizeof ( buf ) ; i + + )
2010-12-06 20:03:35 +03:00
if ( buf [ i ] ! = ' \0 ' )
goto cleanup ;
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2012-07-11 17:35:51 +04:00
virObjectUnref ( csock ) ;
2010-12-06 20:03:35 +03:00
return ret ;
}
2019-10-14 15:45:03 +03:00
static int testSocketCommandFail ( const void * data G_GNUC_UNUSED )
2010-12-06 20:03:35 +03:00
{
2021-03-11 10:16:13 +03:00
virNetSocket * csock = NULL ; /* Client socket */
2010-12-06 20:03:35 +03:00
char buf [ 100 ] ;
int ret = - 1 ;
2021-10-15 14:39:36 +03:00
g_autoptr ( virCommand ) cmd = virCommandNewArgList ( " /bin/cat " , " /dev/does-not-exist " , NULL ) ;
2010-12-06 20:03:35 +03:00
virCommandAddEnvPassCommon ( cmd ) ;
if ( virNetSocketNewConnectCommand ( cmd , & csock ) < 0 )
goto cleanup ;
virNetSocketSetBlocking ( csock , true ) ;
if ( virNetSocketRead ( csock , buf , sizeof ( buf ) ) = = 0 )
goto cleanup ;
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2012-07-11 17:35:51 +04:00
virObjectUnref ( csock ) ;
2010-12-06 20:03:35 +03:00
return ret ;
}
struct testSSHData {
const char * nodename ;
const char * service ;
const char * binary ;
const char * username ;
bool noTTY ;
2011-07-11 23:50:31 +04:00
bool noVerify ;
2022-02-11 20:10:32 +03:00
virNetClientProxy proxy ;
2010-12-06 20:03:35 +03:00
const char * netcat ;
2011-07-19 21:52:21 +04:00
const char * keyfile ;
2010-12-06 20:03:35 +03:00
const char * path ;
const char * expectOut ;
bool failConnect ;
bool dieEarly ;
} ;
static int testSocketSSH ( const void * opaque )
{
const struct testSSHData * data = opaque ;
2021-03-11 10:16:13 +03:00
virNetSocket * csock = NULL ; /* Client socket */
2010-12-06 20:03:35 +03:00
int ret = - 1 ;
char buf [ 1024 ] ;
2022-02-11 20:10:32 +03:00
g_autofree char * command = virNetClientSSHHelperCommand ( data - > proxy ,
2020-07-08 19:03:38 +03:00
data - > netcat ,
data - > path ,
" qemu:///session " ,
true ) ;
2010-12-06 20:03:35 +03:00
if ( virNetSocketNewConnectSSH ( data - > nodename ,
data - > service ,
data - > binary ,
data - > username ,
data - > noTTY ,
2011-07-11 23:50:31 +04:00
data - > noVerify ,
2011-07-19 21:52:21 +04:00
data - > keyfile ,
2020-02-11 22:05:53 +03:00
command ,
2010-12-06 20:03:35 +03:00
& csock ) < 0 )
goto cleanup ;
virNetSocketSetBlocking ( csock , true ) ;
if ( data - > failConnect ) {
if ( virNetSocketRead ( csock , buf , sizeof ( buf ) - 1 ) > = 0 ) {
VIR_DEBUG ( " Expected connect failure, but got some socket data " ) ;
goto cleanup ;
}
} else {
ssize_t rv ;
if ( ( rv = virNetSocketRead ( csock , buf , sizeof ( buf ) - 1 ) ) < 0 ) {
VIR_DEBUG ( " Didn't get any socket data " ) ;
goto cleanup ;
}
buf [ rv ] = ' \0 ' ;
2015-10-20 19:15:12 +03:00
if ( STRNEQ ( buf , data - > expectOut ) ) {
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , data - > expectOut , buf ) ;
2010-12-06 20:03:35 +03:00
goto cleanup ;
}
if ( data - > dieEarly & &
virNetSocketRead ( csock , buf , sizeof ( buf ) - 1 ) > = 0 ) {
VIR_DEBUG ( " Got too much socket data " ) ;
goto cleanup ;
}
}
ret = 0 ;
2014-03-25 10:53:44 +04:00
cleanup :
2012-07-11 17:35:51 +04:00
virObjectUnref ( csock ) ;
2010-12-06 20:03:35 +03:00
return ret ;
}
# endif
static int
mymain ( void )
{
int ret = 0 ;
2020-09-01 14:27:44 +03:00
# ifdef WITH_IFADDRS_H
2010-12-06 20:03:35 +03:00
bool hasIPv4 , hasIPv6 ;
int freePort ;
# endif
2020-01-22 20:59:39 +03:00
# ifndef WIN32
2010-12-06 20:03:35 +03:00
signal ( SIGPIPE , SIG_IGN ) ;
2020-01-22 20:59:39 +03:00
# endif /* WIN32 */
2010-12-06 20:03:35 +03:00
2018-05-01 13:55:02 +03:00
virEventRegisterDefaultImpl ( ) ;
2020-09-01 14:27:44 +03:00
# ifdef WITH_IFADDRS_H
2010-12-06 20:03:35 +03:00
if ( checkProtocols ( & hasIPv4 , & hasIPv6 , & freePort ) < 0 ) {
fprintf ( stderr , " Cannot identify IPv4/6 availability \n " ) ;
2012-03-22 15:33:35 +04:00
return EXIT_FAILURE ;
2010-12-06 20:03:35 +03:00
}
if ( hasIPv4 ) {
2018-05-01 13:34:51 +03:00
struct testSocketData tcpData = { " 127.0.0.1 " , freePort , " 127.0.0.1 " } ;
if ( virTestRun ( " Socket TCP/IPv4 Accept " , testSocketAccept , & tcpData ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
}
if ( hasIPv6 ) {
2018-05-01 13:34:51 +03:00
struct testSocketData tcpData = { " ::1 " , freePort , " ::1 " } ;
if ( virTestRun ( " Socket TCP/IPv6 Accept " , testSocketAccept , & tcpData ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
}
if ( hasIPv6 & & hasIPv4 ) {
2018-05-01 13:34:51 +03:00
struct testSocketData tcpData = { NULL , freePort , " 127.0.0.1 " } ;
if ( virTestRun ( " Socket TCP/IPv4+IPv6 Accept " , testSocketAccept , & tcpData ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
tcpData . cnode = " ::1 " ;
2018-05-01 13:34:51 +03:00
if ( virTestRun ( " Socket TCP/IPv4+IPv6 Accept " , testSocketAccept , & tcpData ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
}
# endif
# ifndef WIN32
2018-05-01 13:34:51 +03:00
if ( virTestRun ( " Socket UNIX Accept " , testSocketAccept , NULL ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " Socket UNIX Addrs " , testSocketUNIXAddrs , NULL ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " Socket External Command /dev/zero " , testSocketCommandNormal , NULL ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " Socket External Command /dev/does-not-exist " , testSocketCommandFail , NULL ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
2020-08-03 18:32:22 +03:00
VIR_WARNINGS_NO_DECLARATION_AFTER_STATEMENT
2010-12-06 20:03:35 +03:00
struct testSSHData sshData1 = {
. nodename = " somehost " ,
. path = " /tmp/socket " ,
2020-02-11 22:05:53 +03:00
. netcat = " nc " ,
2019-07-09 20:02:38 +03:00
. expectOut = " -T -e none -- somehost sh -c ' "
2022-02-09 21:54:34 +03:00
" if 'nc' -q 2>&1 | grep \" requires an argument \" >/dev/null 2>&1; then "
" ARG=-q0; "
" else "
" ARG=; "
" fi; "
" 'nc' $ARG -U /tmp/socket "
" ' \n " ,
2010-12-06 20:03:35 +03:00
} ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " SSH test 1 " , testSocketSSH , & sshData1 ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
struct testSSHData sshData2 = {
. nodename = " somehost " ,
. service = " 9000 " ,
. username = " fred " ,
. netcat = " netcat " ,
. noTTY = true ,
2011-07-11 23:50:31 +04:00
. noVerify = false ,
2010-12-06 20:03:35 +03:00
. path = " /tmp/socket " ,
2019-07-09 20:02:38 +03:00
. expectOut = " -p 9000 -l fred -T -e none -o BatchMode=yes -- somehost sh -c ' "
2022-02-09 21:54:34 +03:00
" if 'netcat' -q 2>&1 | grep \" requires an argument \" >/dev/null 2>&1; then "
" ARG=-q0; "
" else "
" ARG=; "
" fi; "
" 'netcat' $ARG -U /tmp/socket "
" ' \n " ,
2010-12-06 20:03:35 +03:00
} ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " SSH test 2 " , testSocketSSH , & sshData2 ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
struct testSSHData sshData3 = {
2011-07-11 23:50:31 +04:00
. nodename = " somehost " ,
. service = " 9000 " ,
. username = " fred " ,
. netcat = " netcat " ,
. noTTY = false ,
. noVerify = true ,
2010-12-06 20:03:35 +03:00
. path = " /tmp/socket " ,
2019-07-09 20:02:38 +03:00
. expectOut = " -p 9000 -l fred -T -e none -o StrictHostKeyChecking=no -- somehost sh -c ' "
2022-02-09 21:54:34 +03:00
" if 'netcat' -q 2>&1 | grep \" requires an argument \" >/dev/null 2>&1; then "
" ARG=-q0; "
" else "
" ARG=; "
" fi; "
" 'netcat' $ARG -U /tmp/socket "
" ' \n " ,
2010-12-06 20:03:35 +03:00
} ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " SSH test 3 " , testSocketSSH , & sshData3 ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
struct testSSHData sshData4 = {
2011-07-11 23:50:31 +04:00
. nodename = " nosuchhost " ,
. path = " /tmp/socket " ,
. failConnect = true ,
} ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " SSH test 4 " , testSocketSSH , & sshData4 ) < 0 )
2011-07-11 23:50:31 +04:00
ret = - 1 ;
struct testSSHData sshData5 = {
2010-12-06 20:03:35 +03:00
. nodename = " crashyhost " ,
. path = " /tmp/socket " ,
2020-02-11 22:05:53 +03:00
. netcat = " nc " ,
2022-02-09 21:23:42 +03:00
. expectOut = " -T -e none -- crashyhost sh -c ' "
2022-02-09 21:54:34 +03:00
" if 'nc' -q 2>&1 | grep \" requires an argument \" >/dev/null 2>&1; then "
" ARG=-q0; "
" else "
" ARG=; "
" fi; "
" 'nc' $ARG -U /tmp/socket "
" ' \n " ,
2010-12-06 20:03:35 +03:00
. dieEarly = true ,
} ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " SSH test 5 " , testSocketSSH , & sshData5 ) < 0 )
2010-12-06 20:03:35 +03:00
ret = - 1 ;
2011-07-19 21:52:21 +04:00
struct testSSHData sshData6 = {
. nodename = " example.com " ,
. path = " /tmp/socket " ,
2020-02-11 22:05:53 +03:00
. netcat = " nc " ,
2011-07-19 21:52:21 +04:00
. keyfile = " /root/.ssh/example_key " ,
. noVerify = true ,
2019-07-09 20:02:38 +03:00
. expectOut = " -i /root/.ssh/example_key -T -e none -o StrictHostKeyChecking=no -- example.com sh -c ' "
2022-02-09 21:54:34 +03:00
" if 'nc' -q 2>&1 | grep \" requires an argument \" >/dev/null 2>&1; then "
" ARG=-q0; "
" else "
" ARG=; "
" fi; "
" 'nc' $ARG -U /tmp/socket "
" ' \n " ,
2011-07-19 21:52:21 +04:00
} ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " SSH test 6 " , testSocketSSH , & sshData6 ) < 0 )
2011-07-19 21:52:21 +04:00
ret = - 1 ;
2011-10-13 23:49:01 +04:00
struct testSSHData sshData7 = {
. nodename = " somehost " ,
2022-02-10 12:48:09 +03:00
. netcat = " n c " ,
2011-10-13 23:49:01 +04:00
. path = " /tmp/socket " ,
2019-07-09 20:02:38 +03:00
. expectOut = " -T -e none -- somehost sh -c ' "
2022-02-10 12:48:09 +03:00
" if ''' \\ ''n c' \\ '''' -q 2>&1 | grep \" requires an argument \" >/dev/null 2>&1; then "
2022-02-09 21:54:34 +03:00
" ARG=-q0; "
" else "
" ARG=; "
" fi; "
2022-02-10 12:48:09 +03:00
" ''' \\ ''n c' \\ '''' $ARG -U /tmp/socket "
2022-02-09 21:54:34 +03:00
" ' \n " ,
2011-10-13 23:49:01 +04:00
} ;
2016-05-26 18:01:50 +03:00
if ( virTestRun ( " SSH test 7 " , testSocketSSH , & sshData7 ) < 0 )
2011-10-13 23:49:01 +04:00
ret = - 1 ;
2022-02-09 21:32:49 +03:00
struct testSSHData sshData8 = {
. nodename = " somehost " ,
. netcat = " n'c " ,
. path = " /tmp/socket " ,
. expectOut = " -T -e none -- somehost sh -c ' "
" if ''' \\ ''n' \\ '' \\ ' \\ ''' \\ ''c' \\ '''' -q 2>&1 | grep \" requires an argument \" >/dev/null 2>&1; then "
" ARG=-q0; "
" else "
" ARG=; "
" fi; "
" ''' \\ ''n' \\ '' \\ ' \\ ''' \\ ''c' \\ '''' $ARG -U /tmp/socket "
" ' \n " ,
} ;
if ( virTestRun ( " SSH test 8 " , testSocketSSH , & sshData8 ) < 0 )
ret = - 1 ;
struct testSSHData sshData9 = {
. nodename = " somehost " ,
. netcat = " n \" c " ,
. path = " /tmp/socket " ,
. expectOut = " -T -e none -- somehost sh -c ' "
" if ''' \\ ''n \" c' \\ '''' -q 2>&1 | grep \" requires an argument \" >/dev/null 2>&1; then "
" ARG=-q0; "
" else "
" ARG=; "
" fi; "
" ''' \\ ''n \" c' \\ '''' $ARG -U /tmp/socket "
" ' \n " ,
} ;
if ( virTestRun ( " SSH test 9 " , testSocketSSH , & sshData9 ) < 0 )
ret = - 1 ;
struct testSSHData sshData10 = {
. nodename = " somehost " ,
. path = " /tmp/socket " ,
. expectOut = " -T -e none -- somehost sh -c ' "
" which virt-ssh-helper 1>/dev/null 2>&1; "
" if test $? = 0; then "
" virt-ssh-helper -r 'qemu:///session'; "
" else "
" if 'nc' -q 2>&1 | grep \" requires an argument \" >/dev/null 2>&1; then "
" ARG=-q0; "
" else "
" ARG=; "
" fi; "
" 'nc' $ARG -U /tmp/socket; "
" fi "
" ' \n "
} ;
if ( virTestRun ( " SSH test 10 " , testSocketSSH , & sshData10 ) < 0 )
ret = - 1 ;
struct testSSHData sshData11 = {
. nodename = " somehost " ,
. proxy = VIR_NET_CLIENT_PROXY_NATIVE ,
. expectOut = " -T -e none -- somehost sh -c ' "
" virt-ssh-helper -r 'qemu:///session' "
" ' \n "
} ;
if ( virTestRun ( " SSH test 11 " , testSocketSSH , & sshData11 ) < 0 )
ret = - 1 ;
VIR_WARNINGS_RESET
2010-12-06 20:03:35 +03:00
# endif
2014-03-17 13:38:38 +04:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
2010-12-06 20:03:35 +03:00
}
2017-03-29 17:45:42 +03:00
VIR_TEST_MAIN ( mymain )