2014-05-19 16:47:31 +04:00
# include <config.h>
2020-02-17 00:59:28 +03:00
# include <unistd.h>
2014-05-19 16:47:31 +04: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 18:06:45 +03:00
GRegex * testSnapshotXMLVariableLineRegex = NULL ;
2014-05-19 16:47:31 +04:00
static char *
testFilterXML ( char * xml )
{
2020-07-03 02:35:41 +03:00
g_auto ( virBuffer ) buf = VIR_BUFFER_INITIALIZER ;
2021-11-01 12:34:10 +03:00
g_auto ( GStrv ) xmlLines = NULL ;
2014-05-19 16:47:31 +04:00
char * * xmlLine ;
2021-02-05 20:35:07 +03:00
if ( ! ( xmlLines = g_strsplit ( xml , " \n " , 0 ) ) ) {
2014-05-19 16:47:31 +04:00
VIR_FREE ( xml ) ;
2021-12-10 18:21:59 +03:00
return NULL ;
2014-05-19 16:47:31 +04:00
}
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 ) ;
}
2021-12-10 18:21:59 +03:00
return virBufferContentAndReset ( & buf ) ;
2014-05-19 16:47:31 +04:00
}
static int
testCompareXMLtoXMLFiles ( const char * xml )
{
2021-09-04 23:37:44 +03:00
g_autofree char * xmlData = NULL ;
g_autofree char * actual = NULL ;
g_autofree char * pathResult = NULL ;
2014-05-19 16:47:31 +04:00
int ret = - 1 ;
2021-03-11 10:16:13 +03:00
virVBoxSnapshotConfMachine * 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
2021-02-26 11:37:10 +03:00
if ( g_mkdir_with_parents ( abs_builddir " /vboxsnapshotxmldata " , 0777 ) < 0 )
2014-07-01 17:23:36 +04:00
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
virVBoxSnapshotConfMachineFree ( machine ) ;
return ret ;
}
static int
testCompareXMLToXMLHelper ( const void * data )
{
int result = - 1 ;
2021-09-04 23:37:44 +03:00
g_autofree char * xml = NULL ;
2014-05-19 16:47:31 +04:00
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 ) ;
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 :
2020-09-08 15:57:14 +03:00
if ( testSnapshotXMLVariableLineRegex )
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 ;
}
2020-01-24 23:30:04 +03:00
# endif /* WITH_VBOX */