2011-07-01 21:58:15 +04:00
# include <config.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include "internal.h"
2012-12-12 22:06:53 +04:00
# include "viralloc.h"
2011-07-01 21:58:15 +04:00
# include "testutils.h"
2012-12-13 21:44:57 +04:00
# include "virutil.h"
2011-07-01 21:58:15 +04:00
static const char * diskNames [ ] = {
" sda " , " sdb " , " sdc " , " sdd " , " sde " , " sdf " , " sdg " , " sdh " , " sdi " , " sdj " , " sdk " , " sdl " , " sdm " , " sdn " , " sdo " , " sdp " , " sdq " , " sdr " , " sds " , " sdt " , " sdu " , " sdv " , " sdw " , " sdx " , " sdy " , " sdz " ,
" sdaa " , " sdab " , " sdac " , " sdad " , " sdae " , " sdaf " , " sdag " , " sdah " , " sdai " , " sdaj " , " sdak " , " sdal " , " sdam " , " sdan " , " sdao " , " sdap " , " sdaq " , " sdar " , " sdas " , " sdat " , " sdau " , " sdav " , " sdaw " , " sdax " , " sday " , " sdaz " ,
" sdba " , " sdbb " , " sdbc " , " sdbd " , " sdbe " , " sdbf " , " sdbg " , " sdbh " , " sdbi " , " sdbj " , " sdbk " , " sdbl " , " sdbm " , " sdbn " , " sdbo " , " sdbp " , " sdbq " , " sdbr " , " sdbs " , " sdbt " , " sdbu " , " sdbv " , " sdbw " , " sdbx " , " sdby " , " sdbz " ,
" sdca " , " sdcb " , " sdcc " , " sdcd " , " sdce " , " sdcf " , " sdcg " , " sdch " , " sdci " , " sdcj " , " sdck " , " sdcl " , " sdcm " , " sdcn " , " sdco " , " sdcp " , " sdcq " , " sdcr " , " sdcs " , " sdct " , " sdcu " , " sdcv " , " sdcw " , " sdcx " , " sdcy " , " sdcz " ,
" sdda " , " sddb " , " sddc " , " sddd " , " sdde " , " sddf " , " sddg " , " sddh " , " sddi " , " sddj " , " sddk " , " sddl " , " sddm " , " sddn " , " sddo " , " sddp " , " sddq " , " sddr " , " sdds " , " sddt " , " sddu " , " sddv " , " sddw " , " sddx " , " sddy " , " sddz " ,
" sdea " , " sdeb " , " sdec " , " sded " , " sdee " , " sdef " , " sdeg " , " sdeh " , " sdei " , " sdej " , " sdek " , " sdel " , " sdem " , " sden " , " sdeo " , " sdep " , " sdeq " , " sder " , " sdes " , " sdet " , " sdeu " , " sdev " , " sdew " , " sdex " , " sdey " , " sdez " ,
" sdfa " , " sdfb " , " sdfc " , " sdfd " , " sdfe " , " sdff " , " sdfg " , " sdfh " , " sdfi " , " sdfj " , " sdfk " , " sdfl " , " sdfm " , " sdfn " , " sdfo " , " sdfp " , " sdfq " , " sdfr " , " sdfs " , " sdft " , " sdfu " , " sdfv " , " sdfw " , " sdfx " , " sdfy " , " sdfz " ,
" sdga " , " sdgb " , " sdgc " , " sdgd " , " sdge " , " sdgf " , " sdgg " , " sdgh " , " sdgi " , " sdgj " , " sdgk " , " sdgl " , " sdgm " , " sdgn " , " sdgo " , " sdgp " , " sdgq " , " sdgr " , " sdgs " , " sdgt " , " sdgu " , " sdgv " , " sdgw " , " sdgx " , " sdgy " , " sdgz " ,
" sdha " , " sdhb " , " sdhc " , " sdhd " , " sdhe " , " sdhf " , " sdhg " , " sdhh " , " sdhi " , " sdhj " , " sdhk " , " sdhl " , " sdhm " , " sdhn " , " sdho " , " sdhp " , " sdhq " , " sdhr " , " sdhs " , " sdht " , " sdhu " , " sdhv " , " sdhw " , " sdhx " , " sdhy " , " sdhz " ,
" sdia " , " sdib " , " sdic " , " sdid " , " sdie " , " sdif " , " sdig " , " sdih " , " sdii " , " sdij " , " sdik " , " sdil " , " sdim " , " sdin " , " sdio " , " sdip " , " sdiq " , " sdir " , " sdis " , " sdit " , " sdiu " , " sdiv " , " sdiw " , " sdix " , " sdiy " , " sdiz "
} ;
2015-11-13 16:14:44 +03:00
struct testDiskName
{
const char * name ;
int idx ;
int partition ;
} ;
static struct testDiskName diskNamesPart [ ] = {
{ " sda0 " , 0 , 0 } ,
{ " sdb10 " , 1 , 10 } ,
{ " sdc2147483647 " , 2 , 2147483647 } ,
} ;
static const char * diskNamesInvalid [ ] = {
2017-02-15 14:14:58 +03:00
" sda00 " , " sda01 " , " sdb-1 " ,
" vd2 "
2015-11-13 16:14:44 +03:00
} ;
2011-07-01 21:58:15 +04:00
static int
testIndexToDiskName ( const void * data ATTRIBUTE_UNUSED )
{
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 ;
2011-07-01 21:58:15 +04:00
char * diskName = NULL ;
for ( i = 0 ; i < ARRAY_CARDINALITY ( diskNames ) ; + + i ) {
VIR_FREE ( diskName ) ;
diskName = virIndexToDiskName ( i , " sd " ) ;
if ( STRNEQ ( diskNames [ i ] , diskName ) ) {
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , diskNames [ i ] , diskName ) ;
2011-07-01 21:58:15 +04:00
VIR_FREE ( diskName ) ;
return - 1 ;
}
}
VIR_FREE ( diskName ) ;
return 0 ;
}
static int
testDiskNameToIndex ( const void * data ATTRIBUTE_UNUSED )
{
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 ;
int idx ;
2011-07-01 21:58:15 +04:00
char * diskName = NULL ;
for ( i = 0 ; i < 100000 ; + + i ) {
VIR_FREE ( diskName ) ;
diskName = virIndexToDiskName ( i , " sd " ) ;
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
idx = virDiskNameToIndex ( diskName ) ;
2011-07-01 21:58:15 +04:00
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
if ( idx < 0 | | idx ! = i ) {
2015-04-23 20:38:00 +03:00
VIR_TEST_DEBUG ( " \n Expect [%zu] \n " , i ) ;
VIR_TEST_DEBUG ( " Actual [%d] \n " , idx ) ;
2011-07-01 21:58:15 +04:00
VIR_FREE ( diskName ) ;
return - 1 ;
}
}
VIR_FREE ( diskName ) ;
return 0 ;
}
2015-11-13 16:14:44 +03:00
static int
testDiskNameParse ( const void * data ATTRIBUTE_UNUSED )
{
size_t i ;
int idx ;
int partition ;
struct testDiskName * disk = NULL ;
for ( i = 0 ; i < ARRAY_CARDINALITY ( diskNamesPart ) ; + + i ) {
disk = & diskNamesPart [ i ] ;
if ( virDiskNameParse ( disk - > name , & idx , & partition ) )
return - 1 ;
if ( disk - > idx ! = idx ) {
VIR_TEST_DEBUG ( " \n Expect [%d] \n " , disk - > idx ) ;
VIR_TEST_DEBUG ( " Actual [%d] \n " , idx ) ;
return - 1 ;
}
if ( disk - > partition ! = partition ) {
VIR_TEST_DEBUG ( " \n Expect [%d] \n " , disk - > partition ) ;
VIR_TEST_DEBUG ( " Actual [%d] \n " , partition ) ;
return - 1 ;
}
}
for ( i = 0 ; i < ARRAY_CARDINALITY ( diskNamesInvalid ) ; + + i ) {
if ( ! virDiskNameParse ( diskNamesInvalid [ i ] , & idx , & partition ) ) {
VIR_TEST_DEBUG ( " Should Fail [%s] \n " , diskNamesInvalid [ i ] ) ;
return - 1 ;
}
}
return 0 ;
}
2011-07-01 21:58:15 +04:00
struct testVersionString
{
const char * string ;
bool allowMissing ;
int result ;
unsigned long version ;
} ;
static struct testVersionString versions [ ] = {
{ " 2.6.38-8-generic " , false , 0 , 1000000 * 2 + 1000 * 6 + 38 } ,
{ " 3.0-1-virtual " , true , 0 , 1000000 * 3 + 1000 * 0 + 0 } ,
{ " 5 " , true , 0 , 1000000 * 5 + 1000 * 0 + 0 } ,
{ " 4.1.0 " , false , 0 , 1000000 * 4 + 1000 * 1 + 0 } ,
{ " 12.345.678 " , false , 0 , 1000000 * 12 + 1000 * 345 + 678 } ,
{ " 1.234.5678 " , false , - 1 , 0 } ,
{ " 1.2345.678 " , false , - 1 , 0 } ,
{ " 12345.6.78 " , false , - 1 , 0 } ,
{ " 123456789 " , true , - 1 , 0 } ,
{ " 3.0-2-virtual " , false , - 1 , 0 } ,
{ " no-number-here " , false , - 1 , 0 } ,
} ;
static int
testParseVersionString ( const void * data ATTRIBUTE_UNUSED )
{
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
int result ;
size_t i ;
2011-07-01 21:58:15 +04:00
unsigned long version ;
for ( i = 0 ; i < ARRAY_CARDINALITY ( versions ) ; + + i ) {
result = virParseVersionString ( versions [ i ] . string , & version ,
versions [ i ] . allowMissing ) ;
if ( result ! = versions [ i ] . result ) {
2015-04-23 20:38:00 +03:00
VIR_TEST_DEBUG ( " \n Version string [%s] \n " , versions [ i ] . string ) ;
VIR_TEST_DEBUG ( " Expect result [%d] \n " , versions [ i ] . result ) ;
VIR_TEST_DEBUG ( " Actual result [%d] \n " , result ) ;
2011-07-01 21:58:15 +04:00
return - 1 ;
}
2014-11-13 17:20:43 +03:00
if ( result < 0 )
2011-07-01 21:58:15 +04:00
continue ;
if ( version ! = versions [ i ] . version ) {
2015-04-23 20:38:00 +03:00
VIR_TEST_DEBUG ( " \n Version string [%s] \n " , versions [ i ] . string ) ;
VIR_TEST_DEBUG ( " Expect version [%lu] \n " , versions [ i ] . version ) ;
VIR_TEST_DEBUG ( " Actual version [%lu] \n " , version ) ;
2011-07-01 21:58:15 +04:00
return - 1 ;
}
}
return 0 ;
}
2014-11-18 19:13:38 +03:00
struct testRoundData {
unsigned int input ;
unsigned int output ;
} ;
static struct testRoundData roundData [ ] = {
{ 0 , 0 } ,
{ 1 , 1 } ,
{ 1000 , 1024 } ,
{ 1024 , 1024 } ,
{ 1025 , 2048 } ,
{ UINT_MAX , 0 } ,
} ;
static int
testRoundValueToPowerOfTwo ( const void * data ATTRIBUTE_UNUSED )
{
unsigned int result ;
size_t i ;
for ( i = 0 ; i < ARRAY_CARDINALITY ( roundData ) ; i + + ) {
result = VIR_ROUND_UP_POWER_OF_TWO ( roundData [ i ] . input ) ;
if ( roundData [ i ] . output ! = result ) {
2015-04-23 20:38:00 +03:00
VIR_TEST_DEBUG ( " \n Input number [%u] \n " , roundData [ i ] . input ) ;
VIR_TEST_DEBUG ( " Expected number [%u] \n " , roundData [ i ] . output ) ;
VIR_TEST_DEBUG ( " Actual number [%u] \n " , result ) ;
2014-11-18 19:13:38 +03:00
return - 1 ;
}
}
return 0 ;
}
2017-11-03 15:09:47 +03:00
# define TEST_OVERFLOW(var, val, expect) \
tmp = val ; \
if ( VIR_ASSIGN_IS_OVERFLOW ( var , tmp ) ! = expect ) { \
fprintf ( stderr , " \n overflow check failed: " \
" var: " # var " val: " # val " \n " ) ; \
return - 1 ; \
2015-05-27 11:54:38 +03:00
}
static int
testOverflowCheckMacro ( const void * data ATTRIBUTE_UNUSED )
{
long long tmp ;
2015-06-08 11:24:08 +03:00
uint8_t luchar ;
int8_t lchar ;
2015-05-27 11:54:38 +03:00
TEST_OVERFLOW ( luchar , 254 , false ) ;
TEST_OVERFLOW ( luchar , 255 , false ) ;
TEST_OVERFLOW ( luchar , 256 , true ) ;
TEST_OVERFLOW ( luchar , 767 , true ) ;
TEST_OVERFLOW ( lchar , 127 , false ) ;
TEST_OVERFLOW ( lchar , - 128 , false ) ;
TEST_OVERFLOW ( lchar , - 129 , true ) ;
TEST_OVERFLOW ( lchar , 128 , true ) ;
return 0 ;
}
2014-11-18 19:13:38 +03:00
2011-07-01 21:58:15 +04:00
static int
mymain ( void )
{
int result = 0 ;
2016-05-26 18:02:00 +03:00
virTestQuiesceLibvirtErrors ( true ) ;
2011-07-01 21:58:15 +04:00
2017-11-03 15:09:47 +03:00
# define DO_TEST(_name) \
do { \
if ( virTestRun ( " Util " # _name , test # # _name , \
NULL ) < 0 ) { \
result = - 1 ; \
} \
2011-07-01 21:58:15 +04:00
} while ( 0 )
DO_TEST ( IndexToDiskName ) ;
DO_TEST ( DiskNameToIndex ) ;
2015-11-13 16:14:44 +03:00
DO_TEST ( DiskNameParse ) ;
2011-07-01 21:58:15 +04:00
DO_TEST ( ParseVersionString ) ;
2014-11-18 19:13:38 +03:00
DO_TEST ( RoundValueToPowerOfTwo ) ;
2015-05-27 11:54:38 +03:00
DO_TEST ( OverflowCheckMacro ) ;
2011-07-01 21:58:15 +04:00
return result = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
}
2017-03-29 17:45:42 +03:00
VIR_TEST_MAIN ( mymain )