2014-05-19 14:47:31 +02:00
# include <config.h>
2020-02-16 22:59:28 +01:00
# include <unistd.h>
2014-05-19 14:47:31 +02:00
# 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 16:06:45 +01:00
GRegex * testSnapshotXMLVariableLineRegex = NULL ;
2014-05-19 14:47:31 +02:00
static char *
testFilterXML ( char * xml )
{
2020-07-02 19:35:41 -04:00
g_auto ( virBuffer ) buf = VIR_BUFFER_INITIALIZER ;
2014-05-19 14:47:31 +02:00
char * * xmlLines = NULL ;
char * * xmlLine ;
char * ret = NULL ;
2021-02-05 18:35:07 +01:00
if ( ! ( xmlLines = g_strsplit ( xml , " \n " , 0 ) ) ) {
2014-05-19 14:47:31 +02:00
VIR_FREE ( xml ) ;
goto cleanup ;
}
VIR_FREE ( xml ) ;
for ( xmlLine = xmlLines ; * xmlLine ; xmlLine + + ) {
2019-11-13 16:06:45 +01:00
if ( g_regex_match ( testSnapshotXMLVariableLineRegex , * xmlLine , 0 , NULL ) )
2014-05-19 14:47:31 +02:00
continue ;
virBufferStrcat ( & buf , * xmlLine , " \n " , NULL ) ;
}
ret = virBufferContentAndReset ( & buf ) ;
cleanup :
2020-08-02 19:36:03 +02:00
g_strfreev ( xmlLines ) ;
2014-05-19 14:47:31 +02: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 15:23:36 +02:00
2019-10-20 13:49:46 +02:00
pathResult = g_strdup ( abs_builddir " /vboxsnapshotxmldata/testResult.vbox " ) ;
2014-05-19 14:47:31 +02:00
2021-02-26 09:37:10 +01:00
if ( g_mkdir_with_parents ( abs_builddir " /vboxsnapshotxmldata " , 0777 ) < 0 )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2016-05-26 17:01:52 +02:00
if ( virTestLoadFile ( xml , & xmlData ) < 0 )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
if ( ! ( machine = virVBoxSnapshotConfLoadVboxFile ( xml , ( char * ) " " ) ) )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
if ( virVBoxSnapshotConfSaveVboxFile ( machine , pathResult ) < 0 )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
2016-05-26 17:01:52 +02:00
if ( virTestLoadFile ( pathResult , & actual ) < 0 )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
if ( ! ( actual = testFilterXML ( actual ) ) )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
if ( ! ( xmlData = testFilterXML ( xmlData ) ) )
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
if ( STRNEQ ( actual , xmlData ) ) {
2016-05-26 17:01:51 +02:00
virTestDifference ( stderr , xmlData , actual ) ;
2014-07-01 15:23:36 +02:00
goto cleanup ;
2014-05-19 14:47:31 +02:00
}
ret = 0 ;
2014-07-01 15:23:36 +02:00
cleanup :
unlink ( pathResult ) ;
rmdir ( abs_builddir " /vboxsnapshotxmldata " ) ;
2014-05-19 14:47:31 +02: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 15:26:14 +02:00
xml = g_strdup_printf ( " %s/vboxsnapshotxmldata/%s.vbox " , abs_srcdir ,
( const char * ) data ) ;
2014-05-19 14:47:31 +02:00
result = testCompareXMLtoXMLFiles ( xml ) ;
VIR_FREE ( xml ) ;
return result ;
}
static int
mymain ( void )
{
int ret = 0 ;
2019-11-13 16:06:45 +01:00
g_autoptr ( GError ) err = NULL ;
testSnapshotXMLVariableLineRegex = g_regex_new ( testSnapshotXMLVariableLineRegexStr ,
0 , 0 , & err ) ;
2014-05-19 14:47:31 +02:00
2019-11-13 16:06:45 +01:00
if ( ! testSnapshotXMLVariableLineRegex ) {
2014-05-19 14:47:31 +02:00
ret = - 1 ;
virReportError ( VIR_ERR_INTERNAL_ERROR , " %s " ,
" failed to compile test regex " ) ;
goto cleanup ;
}
2017-11-03 13:09:47 +01:00
# define DO_TEST(name) \
if ( virTestRun ( " VBox Snapshot XML-2-XML " name , \
testCompareXMLToXMLHelper , ( name ) ) < 0 ) \
2014-05-19 14:47:31 +02: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 :
2020-09-08 14:57:14 +02:00
if ( testSnapshotXMLVariableLineRegex )
g_regex_unref ( testSnapshotXMLVariableLineRegex ) ;
2014-05-19 14:47:31 +02:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
}
2017-03-29 16:45:42 +02:00
VIR_TEST_MAIN ( mymain )
2014-05-19 14:47:31 +02:00
# else
int main ( void )
{
return EXIT_AM_SKIP ;
}
2020-01-24 21:30:04 +01:00
# endif /* WITH_VBOX */