2016-07-07 17:39:00 +03:00
/*
* virconftest . c : Test the config file API
*
* Copyright ( C ) 2006 - 2016 Red Hat , Inc .
*
* 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
* License along with this library . If not , see
* < http : //www.gnu.org/licenses/>.
*
*/
2008-01-29 21:15:54 +03:00
# include <config.h>
2007-12-06 00:40:15 +03:00
2006-08-30 02:27:07 +04:00
# include <unistd.h>
2012-12-12 20:35:35 +04:00
# include "virconf.h"
2012-12-12 22:06:53 +04:00
# include "viralloc.h"
2016-07-07 17:39:00 +03:00
# include "testutils.h"
# define VIR_FROM_THIS VIR_FROM_NONE
2006-08-30 02:27:07 +04:00
2016-07-07 17:39:00 +03:00
static int testConfRoundTrip ( const void * opaque )
2011-04-25 02:25:10 +04:00
{
2016-07-07 17:39:00 +03:00
const char * name = opaque ;
2019-10-15 15:47:50 +03:00
g_autoptr ( virConf ) conf = NULL ;
2006-08-30 02:27:07 +04:00
int len = 10000 ;
2020-09-14 11:01:52 +03:00
g_autofree char * buffer = NULL ;
g_autofree char * srcfile = NULL ;
g_autofree char * dstfile = NULL ;
2006-08-30 02:27:07 +04:00
2019-10-22 16:26:14 +03:00
srcfile = g_strdup_printf ( " %s/virconfdata/%s.conf " , abs_srcdir , name ) ;
dstfile = g_strdup_printf ( " %s/virconfdata/%s.out " , abs_srcdir , name ) ;
2006-08-30 02:27:07 +04:00
2020-09-14 11:01:52 +03:00
buffer = g_new0 ( char , len ) ;
2016-07-07 17:39:00 +03:00
conf = virConfReadFile ( srcfile , 0 ) ;
2006-08-30 02:27:07 +04:00
if ( conf = = NULL ) {
2016-07-07 17:39:00 +03:00
fprintf ( stderr , " Failed to process %s \n " , srcfile ) ;
2020-09-14 11:01:52 +03:00
return - 1 ;
2006-08-30 02:27:07 +04:00
}
2020-09-14 11:01:48 +03:00
if ( virConfWriteMem ( buffer , & len , conf ) < 0 ) {
2016-07-07 17:39:00 +03:00
fprintf ( stderr , " Failed to serialize %s back \n " , srcfile ) ;
2020-09-14 11:01:52 +03:00
return - 1 ;
2007-10-19 12:29:13 +04:00
}
2011-04-25 02:25:10 +04:00
2016-07-07 17:39:00 +03:00
if ( virTestCompareToFile ( buffer , dstfile ) < 0 )
2020-09-14 11:01:52 +03:00
return - 1 ;
2011-04-25 02:25:10 +04:00
2020-09-14 11:01:52 +03:00
return 0 ;
2006-08-30 02:27:07 +04:00
}
2016-07-07 17:39:00 +03:00
2019-10-14 15:45:03 +03:00
static int testConfMemoryNoNewline ( const void * opaque G_GNUC_UNUSED )
2017-11-08 00:20:44 +03:00
{
const char * srcdata = \
" ullong = '123456789' \n " \
" string = 'foo' \n " \
" uint = 12345 " ;
2019-10-15 15:47:50 +03:00
g_autoptr ( virConf ) conf = virConfReadString ( srcdata , 0 ) ;
2021-03-11 10:16:13 +03:00
virConfValue * val ;
2017-11-08 00:20:44 +03:00
unsigned long long llvalue ;
2021-09-04 23:37:31 +03:00
g_autofree char * str = NULL ;
2017-11-08 00:20:44 +03:00
int uintvalue ;
if ( ! conf )
return - 1 ;
if ( ! ( val = virConfGetValue ( conf , " ullong " ) ) )
2021-09-04 23:41:36 +03:00
return - 1 ;
2017-11-08 00:20:44 +03:00
if ( val - > type ! = VIR_CONF_STRING )
2021-09-04 23:41:36 +03:00
return - 1 ;
2017-11-08 00:20:44 +03:00
if ( virStrToLong_ull ( val - > str , NULL , 10 , & llvalue ) < 0 )
2021-09-04 23:41:36 +03:00
return - 1 ;
2017-11-08 00:20:44 +03:00
if ( llvalue ! = 123456789 ) {
fprintf ( stderr , " Expected '123' got '%llu' \n " , llvalue ) ;
2021-09-04 23:41:36 +03:00
return - 1 ;
2017-11-08 00:20:44 +03:00
}
if ( virConfGetValueType ( conf , " string " ) ! =
VIR_CONF_STRING ) {
fprintf ( stderr , " expected a string for 'string' \n " ) ;
2021-09-04 23:41:36 +03:00
return - 1 ;
2017-11-08 00:20:44 +03:00
}
if ( virConfGetValueString ( conf , " string " , & str ) < 0 )
2021-09-04 23:41:36 +03:00
return - 1 ;
2017-11-08 00:20:44 +03:00
if ( STRNEQ_NULLABLE ( str , " foo " ) ) {
fprintf ( stderr , " Expected 'foo' got '%s' \n " , str ) ;
2021-09-04 23:41:36 +03:00
return - 1 ;
2017-11-08 00:20:44 +03:00
}
if ( virConfGetValueType ( conf , " uint " ) ! = VIR_CONF_ULLONG ) {
fprintf ( stderr , " expected an unsigned long for 'uint' \n " ) ;
2021-09-04 23:41:36 +03:00
return - 1 ;
2017-11-08 00:20:44 +03:00
}
if ( virConfGetValueInt ( conf , " uint " , & uintvalue ) < 0 )
2021-09-04 23:41:36 +03:00
return - 1 ;
2017-11-08 00:20:44 +03:00
if ( uintvalue ! = 12345 ) {
fprintf ( stderr , " Expected 12345 got %ud \n " , uintvalue ) ;
2021-09-04 23:41:36 +03:00
return - 1 ;
2017-11-08 00:20:44 +03:00
}
2021-09-04 23:41:36 +03:00
return 0 ;
2017-11-08 00:20:44 +03:00
}
2019-10-14 15:45:03 +03:00
static int testConfParseInt ( const void * opaque G_GNUC_UNUSED )
2016-07-07 18:52:47 +03:00
{
2017-11-03 15:09:47 +03:00
const char * srcdata = \
" int = -1729 \n " \
" uint = 1729 \n " \
" llong = -6963472309248 \n " \
" ullong = 6963472309248 \n " \
" size_t = 87539319 \n " \
" ssize_t = -87539319 \n " \
2016-07-07 18:52:47 +03:00
" string = \" foo \" \n " ;
2019-10-15 15:47:50 +03:00
g_autoptr ( virConf ) conf = virConfReadString ( srcdata , 0 ) ;
2016-07-07 18:52:47 +03:00
int iv ;
unsigned int ui ;
size_t s ;
ssize_t ss ;
long long l ;
unsigned long long ul ;
if ( ! conf )
return - 1 ;
if ( virConfGetValueType ( conf , " int " ) ! =
2016-07-15 18:36:32 +03:00
VIR_CONF_LLONG ) {
2016-07-07 18:52:47 +03:00
fprintf ( stderr , " expected a long for 'int' \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueInt ( conf , " int " , & iv ) < 0 )
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
if ( iv ! = - 1729 ) {
fprintf ( stderr , " Expected -1729 got %d \n " , iv ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueInt ( conf , " string " , & iv ) ! = - 1 ) {
fprintf ( stderr , " Expected error for 'string' param \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueType ( conf , " uint " ) ! =
2016-07-15 18:36:32 +03:00
VIR_CONF_ULLONG ) {
2016-07-07 18:52:47 +03:00
fprintf ( stderr , " expected a unsigned long for 'uint' \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueUInt ( conf , " uint " , & ui ) < 0 )
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
if ( ui ! = 1729 ) {
fprintf ( stderr , " Expected 1729 got %u \n " , ui ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueUInt ( conf , " string " , & ui ) ! = - 1 ) {
fprintf ( stderr , " Expected error for 'string' param \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueType ( conf , " llong " ) ! =
2016-07-15 18:36:32 +03:00
VIR_CONF_LLONG ) {
2016-07-07 18:52:47 +03:00
fprintf ( stderr , " expected a long for 'llong' \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueLLong ( conf , " llong " , & l ) < 0 )
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
if ( l ! = - 6963472309248 ) {
fprintf ( stderr , " Expected -6963472309248 got %lld \n " , l ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueLLong ( conf , " string " , & l ) ! = - 1 ) {
fprintf ( stderr , " Expected error for 'string' param \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueType ( conf , " ullong " ) ! =
2016-07-15 18:36:32 +03:00
VIR_CONF_ULLONG ) {
2016-07-07 18:52:47 +03:00
fprintf ( stderr , " expected a unsigned long for 'ullong' \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueULLong ( conf , " ullong " , & ul ) < 0 )
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
if ( ul ! = 6963472309248 ) {
fprintf ( stderr , " Expected 6963472309248 got %llu \n " , ul ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueULLong ( conf , " string " , & ul ) ! = - 1 ) {
fprintf ( stderr , " Expected error for 'string' param \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueType ( conf , " size_t " ) ! =
2016-07-15 18:36:32 +03:00
VIR_CONF_ULLONG ) {
2016-07-07 18:52:47 +03:00
fprintf ( stderr , " expected a unsigned long for 'size_T' \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueSizeT ( conf , " size_t " , & s ) < 0 )
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
if ( s ! = 87539319 ) {
fprintf ( stderr , " Expected 87539319 got %zu \n " , s ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueSizeT ( conf , " string " , & s ) ! = - 1 ) {
fprintf ( stderr , " Expected error for 'string' param \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueType ( conf , " ssize_t " ) ! =
2016-07-15 18:36:32 +03:00
VIR_CONF_LLONG ) {
2016-07-07 18:52:47 +03:00
fprintf ( stderr , " expected a unsigned long for 'ssize_t' \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueSSizeT ( conf , " ssize_t " , & ss ) < 0 )
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
if ( ss ! = - 87539319 ) {
fprintf ( stderr , " Expected -87539319 got %zd \n " , ss ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueSSizeT ( conf , " string " , & ss ) ! = - 1 ) {
fprintf ( stderr , " Expected error for 'string' param \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
2019-09-09 18:56:26 +03:00
return 0 ;
2016-07-07 18:52:47 +03:00
}
2019-10-14 15:45:03 +03:00
static int testConfParseBool ( const void * opaque G_GNUC_UNUSED )
2016-07-07 18:52:47 +03:00
{
2017-11-03 15:09:47 +03:00
const char * srcdata = \
" false = 0 \n " \
" true = 1 \n " \
" int = 6963472309248 \n " \
2016-07-07 18:52:47 +03:00
" string = \" foo \" \n " ;
2019-10-15 15:47:50 +03:00
g_autoptr ( virConf ) conf = virConfReadString ( srcdata , 0 ) ;
2016-07-07 18:52:47 +03:00
bool f = true ;
bool t = false ;
if ( ! conf )
return - 1 ;
if ( virConfGetValueType ( conf , " false " ) ! =
2016-07-15 18:36:32 +03:00
VIR_CONF_ULLONG ) {
2016-07-07 18:52:47 +03:00
fprintf ( stderr , " expected a long for 'false' \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueBool ( conf , " false " , & f ) < 0 )
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
if ( f ! = false ) {
fprintf ( stderr , " Expected 0 got %d \n " , f ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueType ( conf , " true " ) ! =
2016-07-15 18:36:32 +03:00
VIR_CONF_ULLONG ) {
2016-07-07 18:52:47 +03:00
fprintf ( stderr , " expected a long for 'true' \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueBool ( conf , " true " , & t ) < 0 )
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
if ( t ! = true ) {
fprintf ( stderr , " Expected 1 got %d \n " , t ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueBool ( conf , " int " , & t ) ! = - 1 ) {
fprintf ( stderr , " Expected error for 'string' param \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueBool ( conf , " string " , & t ) ! = - 1 ) {
fprintf ( stderr , " Expected error for 'string' param \n " ) ;
2019-09-09 18:56:26 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
2019-09-09 18:56:26 +03:00
return 0 ;
2016-07-07 18:52:47 +03:00
}
2019-10-14 15:45:03 +03:00
static int testConfParseString ( const void * opaque G_GNUC_UNUSED )
2016-07-07 18:52:47 +03:00
{
2017-11-03 15:09:47 +03:00
const char * srcdata = \
" int = 6963472309248 \n " \
2016-07-07 18:52:47 +03:00
" string = \" foo \" \n " ;
2019-10-15 15:47:50 +03:00
g_autoptr ( virConf ) conf = virConfReadString ( srcdata , 0 ) ;
2021-09-04 23:37:31 +03:00
g_autofree char * str = NULL ;
2016-07-07 18:52:47 +03:00
if ( ! conf )
return - 1 ;
if ( virConfGetValueType ( conf , " string " ) ! =
VIR_CONF_STRING ) {
fprintf ( stderr , " expected a string for 'string' \n " ) ;
2021-09-04 23:41:36 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueString ( conf , " string " , & str ) < 0 )
2021-09-04 23:41:36 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
if ( STRNEQ_NULLABLE ( str , " foo " ) ) {
fprintf ( stderr , " Expected 'foo' got '%s' \n " , str ) ;
2021-09-04 23:41:36 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
if ( virConfGetValueString ( conf , " int " , & str ) ! = - 1 ) {
fprintf ( stderr , " Expected error for 'int' \n " ) ;
2021-09-04 23:41:36 +03:00
return - 1 ;
2016-07-07 18:52:47 +03:00
}
2021-09-04 23:41:36 +03:00
return 0 ;
2016-07-07 18:52:47 +03:00
}
2019-10-14 15:45:03 +03:00
static int testConfParseStringList ( const void * opaque G_GNUC_UNUSED )
2016-07-07 18:52:47 +03:00
{
2017-11-03 15:09:47 +03:00
const char * srcdata = \
" string_list = [ \" foo \" , \" bar \" ] \n " \
2016-07-07 18:52:47 +03:00
" string = \" foo \" \n " ;
int ret = - 1 ;
2019-10-15 15:47:50 +03:00
g_autoptr ( virConf ) conf = virConfReadString ( srcdata , 0 ) ;
2016-07-07 18:52:47 +03:00
char * * str = NULL ;
if ( ! conf )
return - 1 ;
if ( virConfGetValueType ( conf , " string_list " ) ! =
VIR_CONF_LIST ) {
fprintf ( stderr , " expected a list for 'string_list' \n " ) ;
goto cleanup ;
}
if ( virConfGetValueStringList ( conf , " string_list " , false , & str ) < 0 )
goto cleanup ;
2021-02-05 20:03:26 +03:00
if ( ! str | | g_strv_length ( str ) ! = 2 ) {
2016-07-07 18:52:47 +03:00
fprintf ( stderr , " expected a 2 element list \n " ) ;
goto cleanup ;
}
if ( STRNEQ_NULLABLE ( str [ 0 ] , " foo " ) ) {
fprintf ( stderr , " Expected 'foo' got '%s' \n " , str [ 0 ] ) ;
goto cleanup ;
}
if ( STRNEQ_NULLABLE ( str [ 1 ] , " bar " ) ) {
fprintf ( stderr , " Expected 'bar' got '%s' \n " , str [ 1 ] ) ;
goto cleanup ;
}
if ( virConfGetValueStringList ( conf , " string " , false , & str ) ! = - 1 ) {
fprintf ( stderr , " Expected error for 'string' \n " ) ;
goto cleanup ;
}
if ( virConfGetValueStringList ( conf , " string " , true , & str ) < 0 )
goto cleanup ;
2021-02-05 20:03:26 +03:00
if ( ! str | | g_strv_length ( str ) ! = 1 ) {
2016-07-07 18:52:47 +03:00
fprintf ( stderr , " expected a 1 element list \n " ) ;
goto cleanup ;
}
if ( STRNEQ_NULLABLE ( str [ 0 ] , " foo " ) ) {
fprintf ( stderr , " Expected 'foo' got '%s' \n " , str [ 0 ] ) ;
goto cleanup ;
}
ret = 0 ;
cleanup :
2020-08-02 20:36:03 +03:00
g_strfreev ( str ) ;
2016-07-07 18:52:47 +03:00
return ret ;
}
2016-07-07 17:39:00 +03:00
static int
mymain ( void )
{
int ret = 0 ;
if ( virTestRun ( " fc4 " , testConfRoundTrip , " fc4 " ) < 0 )
ret = - 1 ;
if ( virTestRun ( " libvirtd " , testConfRoundTrip , " libvirtd " ) < 0 )
ret = - 1 ;
if ( virTestRun ( " no-newline " , testConfRoundTrip , " no-newline " ) < 0 )
ret = - 1 ;
2017-11-08 00:20:44 +03:00
if ( virTestRun ( " memory-no-newline " , testConfMemoryNoNewline , NULL ) < 0 )
ret = - 1 ;
2016-07-07 18:52:47 +03:00
if ( virTestRun ( " int " , testConfParseInt , NULL ) < 0 )
ret = - 1 ;
if ( virTestRun ( " bool " , testConfParseBool , NULL ) < 0 )
ret = - 1 ;
if ( virTestRun ( " string " , testConfParseString , NULL ) < 0 )
ret = - 1 ;
if ( virTestRun ( " string-list " , testConfParseStringList , NULL ) < 0 )
ret = - 1 ;
2021-05-16 19:14:53 +03:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
2016-07-07 17:39:00 +03:00
}
2017-03-29 17:45:42 +03:00
VIR_TEST_MAIN ( mymain )