2019-09-23 13:44:33 +03:00
# include <config.h>
# include <inttypes.h>
# include "testutils.h"
# include "virfilewrapper.h"
# include "qemu/qemu_vhost_user.h"
# include "configmake.h"
# define VIR_FROM_THIS VIR_FROM_QEMU
/* A very basic test. Parse given JSON vhostuser description into
* an internal structure , format it back and compare with the
* contents of the file ( minus some keys that are not parsed ) .
*/
static int
testParseFormatVU ( const void * opaque )
{
const char * filename = opaque ;
2019-10-15 16:16:31 +03:00
g_autofree char * path = NULL ;
2019-10-15 15:47:50 +03:00
g_autoptr ( qemuVhostUser ) vu = NULL ;
2019-10-15 16:16:31 +03:00
g_autofree char * buf = NULL ;
2019-10-15 15:47:50 +03:00
g_autoptr ( virJSONValue ) json = NULL ;
2019-10-15 16:16:31 +03:00
g_autofree char * expected = NULL ;
g_autofree char * actual = NULL ;
2019-09-23 13:44:33 +03:00
2019-10-22 16:26:14 +03:00
path = g_strdup_printf ( " %s/qemuvhostuserdata/%s " , abs_srcdir , filename ) ;
2019-09-23 13:44:33 +03:00
if ( ! ( vu = qemuVhostUserParse ( path ) ) )
return - 1 ;
if ( virFileReadAll ( path ,
1024 * 1024 , /* 1MiB */
& buf ) < 0 )
return - 1 ;
if ( ! ( json = virJSONValueFromString ( buf ) ) )
return - 1 ;
/* Description and tags are not parsed. */
if ( virJSONValueObjectRemoveKey ( json , " description " , NULL ) < 0 | |
virJSONValueObjectRemoveKey ( json , " tags " , NULL ) < 0 )
return - 1 ;
if ( ! ( expected = virJSONValueToString ( json , true ) ) )
return - 1 ;
if ( ! ( actual = qemuVhostUserFormat ( vu ) ) )
return - 1 ;
return virTestCompareToString ( expected , actual ) ;
}
static int
2019-10-14 15:45:03 +03:00
testVUPrecedence ( const void * opaque G_GNUC_UNUSED )
2019-09-23 13:44:33 +03:00
{
2019-10-15 16:16:31 +03:00
g_autofree char * fakehome = NULL ;
2020-12-01 11:21:32 +03:00
g_auto ( GStrv ) vuList = NULL ;
2019-09-23 13:44:33 +03:00
const char * expected [ ] = {
PREFIX " /share/qemu/vhost-user/30-gpu.json " ,
SYSCONFDIR " /qemu/vhost-user/40-gpu.json " ,
PREFIX " /share/qemu/vhost-user/60-gpu.json " ,
2021-02-05 19:34:13 +03:00
NULL
2019-09-23 13:44:33 +03:00
} ;
2021-02-05 19:34:13 +03:00
const char * * e ;
GStrv f ;
2019-09-23 13:44:33 +03:00
2019-10-20 14:49:46 +03:00
fakehome = g_strdup ( abs_srcdir " /qemuvhostuserdata/home/user/.config " ) ;
2019-09-23 13:44:33 +03:00
2019-12-18 20:16:19 +03:00
g_setenv ( " XDG_CONFIG_HOME " , fakehome , TRUE ) ;
2019-09-23 13:44:33 +03:00
if ( qemuVhostUserFetchConfigs ( & vuList , false ) < 0 )
return - 1 ;
if ( ! vuList ) {
fprintf ( stderr , " Expected a non-NULL result, but got a NULL result \n " ) ;
return - 1 ;
}
2021-02-05 19:34:13 +03:00
for ( e = expected , f = vuList ; * f | | * e ; ) {
if ( STRNEQ_NULLABLE ( * f , * e ) ) {
2019-09-23 13:44:33 +03:00
fprintf ( stderr ,
2021-02-05 19:34:13 +03:00
" Unexpected path. Expected %s got %s \n " ,
NULLSTR ( * e ) , NULLSTR ( * f ) ) ;
2019-09-23 13:44:33 +03:00
return - 1 ;
}
2021-02-05 19:34:13 +03:00
if ( * f )
f + + ;
if ( * e )
e + + ;
2019-09-23 13:44:33 +03:00
}
return 0 ;
}
static int
mymain ( void )
{
int ret = 0 ;
virFileWrapperAddPrefix ( SYSCONFDIR " /qemu/vhost-user " ,
abs_srcdir " /qemuvhostuserdata/etc/qemu/vhost-user " ) ;
virFileWrapperAddPrefix ( PREFIX " /share/qemu/vhost-user " ,
abs_srcdir " /qemuvhostuserdata/usr/share/qemu/vhost-user " ) ;
virFileWrapperAddPrefix ( " /home/user/.config/qemu/vhost-user " ,
abs_srcdir " /qemuvhostuserdata/home/user/.config/qemu/vhost-user " ) ;
# define DO_PARSE_TEST(filename) \
do { \
if ( virTestRun ( " QEMU vhost-user " filename , \
testParseFormatVU , filename ) < 0 ) \
ret = - 1 ; \
} while ( 0 )
DO_PARSE_TEST ( " usr/share/qemu/vhost-user/50-gpu.json " ) ;
if ( virTestRun ( " QEMU vhost-user precedence test " , testVUPrecedence , NULL ) < 0 )
ret = - 1 ;
virFileWrapperClearPrefixes ( ) ;
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
}
VIR_TEST_MAIN ( mymain )