2014-05-19 16:47:31 +04:00
# include <config.h>
# include "testutils.h"
# ifdef WITH_VBOX
# include "vbox / vbox_snapshot_conf.h"
# define VIR_FROM_THIS VIR_FROM_NONE
static const char * testSnapshotXMLVariableLineRegexStr =
" lastStateChange=[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z " ;
2019-11-13 18:06:45 +03:00
GRegex * testSnapshotXMLVariableLineRegex = NULL ;
2014-05-19 16:47:31 +04:00
static char *
testFilterXML ( char * xml )
{
virBuffer buf = VIR_BUFFER_INITIALIZER ;
char * * xmlLines = NULL ;
char * * xmlLine ;
char * ret = NULL ;
if ( ! ( xmlLines = virStringSplit ( xml , " \n " , 0 ) ) ) {
VIR_FREE ( xml ) ;
goto cleanup ;
}
VIR_FREE ( xml ) ;
for ( xmlLine = xmlLines ; * xmlLine ; xmlLine + + ) {
2019-11-13 18:06:45 +03:00
if ( g_regex_match ( testSnapshotXMLVariableLineRegex , * xmlLine , 0 , NULL ) )
2014-05-19 16:47:31 +04:00
continue ;
virBufferStrcat ( & buf , * xmlLine , " \n " , NULL ) ;
}
ret = virBufferContentAndReset ( & buf ) ;
cleanup :
virBufferFreeAndReset ( & buf ) ;
2016-11-25 11:18:35 +03:00
virStringListFree ( xmlLines ) ;
2014-05-19 16:47:31 +04:00
return ret ;
}
static int
testCompareXMLtoXMLFiles ( const char * xml )
{
char * xmlData = NULL ;
char * actual = NULL ;
char * pathResult = NULL ;
int ret = - 1 ;
virVBoxSnapshotConfMachinePtr machine = NULL ;
2014-07-01 17:23:36 +04:00
2019-10-20 14:49:46 +03:00
pathResult = g_strdup ( abs_builddir " /vboxsnapshotxmldata/testResult.vbox " ) ;
2014-05-19 16:47:31 +04:00
2014-07-01 17:23:36 +04:00
if ( virFileMakePath ( abs_builddir " /vboxsnapshotxmldata " ) < 0 )
goto cleanup ;
2016-05-26 18:01:52 +03:00
if ( virTestLoadFile ( xml , & xmlData ) < 0 )
2014-07-01 17:23:36 +04:00
goto cleanup ;
2014-05-19 16:47:31 +04:00
if ( ! ( machine = virVBoxSnapshotConfLoadVboxFile ( xml , ( char * ) " " ) ) )
2014-07-01 17:23:36 +04:00
goto cleanup ;
2014-05-19 16:47:31 +04:00
if ( virVBoxSnapshotConfSaveVboxFile ( machine , pathResult ) < 0 )
2014-07-01 17:23:36 +04:00
goto cleanup ;
2014-05-19 16:47:31 +04:00
2016-05-26 18:01:52 +03:00
if ( virTestLoadFile ( pathResult , & actual ) < 0 )
2014-07-01 17:23:36 +04:00
goto cleanup ;
2014-05-19 16:47:31 +04:00
if ( ! ( actual = testFilterXML ( actual ) ) )
2014-07-01 17:23:36 +04:00
goto cleanup ;
2014-05-19 16:47:31 +04:00
if ( ! ( xmlData = testFilterXML ( xmlData ) ) )
2014-07-01 17:23:36 +04:00
goto cleanup ;
2014-05-19 16:47:31 +04:00
if ( STRNEQ ( actual , xmlData ) ) {
2016-05-26 18:01:51 +03:00
virTestDifference ( stderr , xmlData , actual ) ;
2014-07-01 17:23:36 +04:00
goto cleanup ;
2014-05-19 16:47:31 +04:00
}
ret = 0 ;
2014-07-01 17:23:36 +04:00
cleanup :
unlink ( pathResult ) ;
rmdir ( abs_builddir " /vboxsnapshotxmldata " ) ;
2014-05-19 16:47:31 +04:00
VIR_FREE ( xmlData ) ;
VIR_FREE ( actual ) ;
virVBoxSnapshotConfMachineFree ( machine ) ;
VIR_FREE ( pathResult ) ;
return ret ;
}
static int
testCompareXMLToXMLHelper ( const void * data )
{
int result = - 1 ;
char * xml = NULL ;
2019-10-22 16:26:14 +03:00
xml = g_strdup_printf ( " %s/vboxsnapshotxmldata/%s.vbox " , abs_srcdir ,
( const char * ) data ) ;
2014-05-19 16:47:31 +04:00
result = testCompareXMLtoXMLFiles ( xml ) ;
VIR_FREE ( xml ) ;
return result ;
}
static int
mymain ( void )
{
int ret = 0 ;
2019-11-13 18:06:45 +03:00
g_autoptr ( GError ) err = NULL ;
testSnapshotXMLVariableLineRegex = g_regex_new ( testSnapshotXMLVariableLineRegexStr ,
0 , 0 , & err ) ;
2014-05-19 16:47:31 +04:00
2019-11-13 18:06:45 +03:00
if ( ! testSnapshotXMLVariableLineRegex ) {
2014-05-19 16:47:31 +04:00
ret = - 1 ;
virReportError ( VIR_ERR_INTERNAL_ERROR , " %s " ,
" failed to compile test regex " ) ;
goto cleanup ;
}
2017-11-03 15:09:47 +03:00
# define DO_TEST(name) \
if ( virTestRun ( " VBox Snapshot XML-2-XML " name , \
testCompareXMLToXMLHelper , ( name ) ) < 0 ) \
2014-05-19 16:47:31 +04:00
ret = - 1
DO_TEST ( " 2disks-nosnap " ) ;
DO_TEST ( " 2disks-1snap " ) ;
DO_TEST ( " 2disks-2snap " ) ;
DO_TEST ( " 2disks-3snap " ) ;
DO_TEST ( " 2disks-3snap-brother " ) ;
cleanup :
2019-11-13 18:06:45 +03:00
g_regex_unref ( testSnapshotXMLVariableLineRegex ) ;
2014-05-19 16:47:31 +04:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
}
2017-03-29 17:45:42 +03:00
VIR_TEST_MAIN ( mymain )
2014-05-19 16:47:31 +04:00
# else
int main ( void )
{
return EXIT_AM_SKIP ;
}
# endif /*WITH_VBOX*/