2013-07-29 19:17:47 +04:00
# include <config.h>
# include <unistd.h>
# include <sys/types.h>
# include <fcntl.h>
# include "internal.h"
# include "testutils.h"
# include "network_conf.h"
# include "testutilsqemu.h"
# include "virstring.h"
# define VIR_FROM_THIS VIR_FROM_NONE
static int
testCompareXMLToXMLFiles ( const char * netxml , const char * updatexml ,
const char * outxml , unsigned int flags ,
unsigned int command , unsigned int section ,
int parentIndex , bool expectFailure )
{
char * updateXmlData = NULL ;
char * actual = NULL ;
int ret = - 1 ;
virNetworkDefPtr def = NULL ;
2016-05-26 18:01:52 +03:00
if ( virTestLoadFile ( updatexml , & updateXmlData ) < 0 )
2013-07-29 19:17:47 +04:00
goto error ;
2019-07-14 19:15:12 +03:00
if ( ! ( def = virNetworkDefParseFile ( netxml , NULL ) ) )
2013-07-29 19:17:47 +04:00
goto fail ;
if ( virNetworkDefUpdateSection ( def , command , section , parentIndex ,
updateXmlData , 0 ) < 0 )
goto fail ;
2019-07-14 19:15:12 +03:00
if ( ! ( actual = virNetworkDefFormat ( def , NULL , flags ) ) )
2013-07-29 19:17:47 +04:00
goto fail ;
if ( ! expectFailure ) {
2016-05-26 18:01:53 +03:00
if ( virTestCompareToFile ( actual , outxml ) < 0 )
2013-07-29 19:17:47 +04:00
goto error ;
}
ret = 0 ;
2014-03-25 10:53:44 +04:00
fail :
2013-07-29 19:17:47 +04:00
if ( expectFailure ) {
if ( ret = = 0 ) {
virReportError ( VIR_ERR_INTERNAL_ERROR , " %s " , " Failed to fail. " ) ;
ret = - 1 ;
} else {
virResetLastError ( ) ;
ret = 0 ;
}
}
2014-03-25 10:53:44 +04:00
error :
2013-07-29 19:17:47 +04:00
VIR_FREE ( updateXmlData ) ;
VIR_FREE ( actual ) ;
virNetworkDefFree ( def ) ;
return ret ;
}
struct testInfo {
const char * name ;
const char * updatexml ;
const char * netxml ;
const char * outxml ;
unsigned int command ;
unsigned int section ;
int parentIndex ;
unsigned int flags ;
bool expectFailure ;
} ;
static int
testCompareXMLToXMLHelper ( const void * data )
{
const struct testInfo * info = data ;
int result = - 1 ;
char * netxml = NULL ;
char * updatexml = NULL ;
char * outxml = NULL ;
2019-10-22 16:26:14 +03:00
netxml = g_strdup_printf ( " %s/networkxml2xmlin/%s.xml " ,
abs_srcdir , info - > netxml ) ;
updatexml = g_strdup_printf ( " %s/networkxml2xmlupdatein/%s.xml " ,
abs_srcdir , info - > updatexml ) ;
outxml = g_strdup_printf ( " %s/networkxml2xmlupdateout/%s.xml " ,
abs_srcdir , info - > outxml ) ;
2013-07-29 19:17:47 +04:00
result = testCompareXMLToXMLFiles ( netxml , updatexml , outxml , info - > flags ,
info - > command , info - > section ,
info - > parentIndex , info - > expectFailure ) ;
VIR_FREE ( netxml ) ;
VIR_FREE ( updatexml ) ;
VIR_FREE ( outxml ) ;
return result ;
}
static int
mymain ( void )
{
int ret = 0 ;
unsigned int section ;
2017-11-03 15:09:47 +03:00
# define DO_TEST_FULL(name, updatexml, netxml, outxml, command, section, \
parentIndex , flags , expectFailure ) \
do { \
const struct testInfo info = { name , updatexml , netxml , outxml , \
command , section , flags , \
parentIndex , expectFailure } ; \
if ( virTestRun ( " Network XML-2-XML " name , \
testCompareXMLToXMLHelper , & info ) < 0 ) \
ret = - 1 ; \
2013-07-29 19:17:47 +04:00
} while ( 0 )
2017-11-03 15:09:47 +03:00
# define DO_TEST(name, updatexml, netxml, outxml, command) \
2013-07-29 19:17:47 +04:00
DO_TEST_FULL ( name , updatexml , netxml , outxml , command , section , - 12435 , \
0 , false )
2017-11-03 15:09:47 +03:00
# define DO_TEST_FAIL(name, updatexml, netxml, command) \
DO_TEST_FULL ( name , updatexml , netxml , " n/a " , command , section , - 12345 , \
2013-07-29 19:17:47 +04:00
0 , true )
2017-11-03 15:09:47 +03:00
# define DO_TEST_INDEX(name, updatexml, netxml, outxml, command, index) \
DO_TEST_FULL ( name , updatexml , netxml , outxml , command , section , index , \
2013-07-29 19:17:47 +04:00
0 , false )
2017-11-03 15:09:47 +03:00
# define DO_TEST_INDEX_FAIL(name, updatexml, netxml, command, index) \
DO_TEST_FULL ( name , updatexml , netxml , " n/a " , command , section , index , \
2013-07-29 19:17:47 +04:00
0 , true )
section = VIR_NETWORK_SECTION_IP_DHCP_HOST ;
DO_TEST_INDEX_FAIL ( " add-host-incomplete " ,
" host-incomplete " ,
" nat-network " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST ,
0 ) ;
DO_TEST_INDEX_FAIL ( " add-host-new-incomplete " ,
" host-new-incomplete " ,
" nat-network " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST ,
0 ) ;
DO_TEST_INDEX_FAIL ( " add-host-existing " ,
" host-existing " ,
" nat-network " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST ,
0 ) ;
DO_TEST_INDEX ( " add-host-new " ,
" host-new " ,
" nat-network " ,
" nat-network-hosts " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST ,
0 ) ;
DO_TEST_INDEX_FAIL ( " modify-host-missing " ,
" host-new " ,
" nat-network " ,
VIR_NETWORK_UPDATE_COMMAND_MODIFY ,
0 ) ;
DO_TEST_INDEX_FAIL ( " modify-host-incomplete " ,
" host-incomplete " ,
" nat-network " ,
VIR_NETWORK_UPDATE_COMMAND_MODIFY ,
0 ) ;
DO_TEST_INDEX ( " modify-host " ,
" host-updated " ,
" nat-network " ,
" nat-network-host-updated " ,
VIR_NETWORK_UPDATE_COMMAND_MODIFY ,
0 ) ;
DO_TEST_INDEX ( " delete-host-incomplete " ,
" host-incomplete " ,
" nat-network " ,
" nat-network-one-host " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ,
0 ) ;
DO_TEST_INDEX ( " delete-host-existing " ,
" host-existing " ,
" nat-network " ,
" nat-network-one-host " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ,
0 ) ;
DO_TEST_INDEX_FAIL ( " delete-host-missing " ,
" host-new " ,
" nat-network " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ,
0 ) ;
section = VIR_NETWORK_SECTION_IP_DHCP_RANGE ;
DO_TEST_INDEX ( " add-dhcp-range " ,
" dhcp-range " ,
" dhcp6host-routed-network " ,
" dhcp6host-routed-network-range " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST ,
0 ) ;
2015-05-23 00:32:02 +03:00
DO_TEST_INDEX_FAIL ( " add-dhcp-range-outside-net " ,
" dhcp-range-10 " ,
" dhcp6host-routed-network " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST ,
0 ) ;
2013-07-29 19:17:47 +04:00
DO_TEST_INDEX ( " append-dhcp-range " ,
" dhcp-range " ,
" dhcp6host-routed-network " ,
" dhcp6host-routed-network-another-range " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST ,
1 ) ;
DO_TEST_INDEX ( " delete-dhcp-range " ,
" dhcp-range-existing " ,
" nat-network " ,
" nat-network-no-range " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ,
0 ) ;
DO_TEST_INDEX_FAIL ( " delete-dhcp-range " ,
" dhcp-range " ,
" nat-network " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ,
0 ) ;
section = VIR_NETWORK_SECTION_FORWARD_INTERFACE ;
DO_TEST ( " insert-forward-interface " ,
" interface-eth47 " ,
" nat-network-dns-srv-record " ,
" nat-network-forward-ifaces " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST ) ;
DO_TEST ( " delete-forward-interface " ,
" interface-eth1 " ,
" nat-network-dns-srv-record " ,
" nat-network-no-forward-ifaces " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
DO_TEST_FAIL ( " delete-forward-interface " ,
" interface-eth47 " ,
" nat-network-dns-srv-record " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
section = VIR_NETWORK_SECTION_PORTGROUP ;
DO_TEST ( " insert-portgroup " ,
" portgroup-alison " ,
" openvswitch-net " ,
" openvswitch-net-more-portgroups " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST ) ;
DO_TEST_FAIL ( " append-duplicate-portgroup " ,
" portgroup-alice-new " ,
" openvswitch-net " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST ) ;
DO_TEST ( " modify-portgroup " ,
" portgroup-alice-new " ,
" openvswitch-net " ,
" openvswitch-net-modified " ,
VIR_NETWORK_UPDATE_COMMAND_MODIFY ) ;
DO_TEST_FAIL ( " modify-missing-portgroup " ,
" portgroup-alison " ,
" openvswitch-net " ,
VIR_NETWORK_UPDATE_COMMAND_MODIFY ) ;
DO_TEST ( " delete-portgroup " ,
" portgroup-alice-new " ,
" openvswitch-net " ,
" openvswitch-net-without-alice " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
DO_TEST_FAIL ( " delete-missing-portgroup " ,
" portgroup-alice-new " ,
" nat-network-dns-srv-record " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
section = VIR_NETWORK_SECTION_DNS_HOST ;
DO_TEST_FAIL ( " insert-incomplete-host " ,
" dns-host-gateway-incomplete " ,
" nat-network-dns-hosts " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST ) ;
DO_TEST ( " insert-host " ,
" dns-host-pudding " ,
" nat-network-dns-hosts " ,
" nat-network-dns-more-hosts " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST ) ;
DO_TEST_FAIL ( " delete-missing-unparsable-dns-host " ,
" unparsable-dns-host " ,
" nat-network " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
DO_TEST ( " delete-dns-host " ,
" dns-host-gateway-incomplete " ,
" nat-network-dns-hosts " ,
" nat-network-no-hosts " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
section = VIR_NETWORK_SECTION_DNS_TXT ;
DO_TEST ( " insert-dns-txt-record " ,
" dns-txt-record-snowman " ,
" nat-network-dns-txt-record " ,
" nat-network-dns-txt-records " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST ) ;
DO_TEST_FAIL ( " append-duplicate-dns-txt-record " ,
" dns-txt-record-example " ,
" nat-network-dns-txt-record " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST ) ;
DO_TEST ( " delete-dns-txt-record " ,
" dns-txt-record-example " ,
" nat-network-dns-txt-record " ,
" nat-network-dns-txt-none " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
DO_TEST_FAIL ( " delete-missing-dns-txt-record " ,
" dns-txt-record-snowman " ,
" nat-network-dns-txt-record " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
section = VIR_NETWORK_SECTION_DNS_SRV ;
DO_TEST ( " insert-first-srv-record-service " ,
" srv-record " ,
" nat-network " ,
" nat-network-dns-srv-record " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST ) ;
DO_TEST ( " append-first-srv-record-service " ,
" srv-record " ,
" nat-network " ,
" nat-network-dns-srv-record " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST ) ;
DO_TEST_FAIL ( " add-existing-dns-srv-record " ,
" srv-record " ,
" nat-network-dns-srv-record " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST ) ;
DO_TEST ( " append-srv-record-service " ,
" srv-record-donkey " ,
" nat-network-dns-srv-record " ,
" nat-network-dns-srv-records " ,
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST ) ;
DO_TEST_FAIL ( " delete-missing-srv-record-service " ,
" srv-record-service " ,
" nat-network " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
DO_TEST_FAIL ( " delete-srv-record-invalid " ,
" srv-record-invalid " ,
" nat-network-dns-srv-record " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
DO_TEST ( " delete-srv-record-donkey " ,
" srv-record-donkey " ,
" nat-network-dns-srv-records " ,
" nat-network-dns-srv-record " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
DO_TEST_FAIL ( " delete-ambiguous-srv-record-service " ,
" srv-record-service " ,
" nat-network-dns-srv-records " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
DO_TEST ( " delete-srv-record-protocol " ,
" srv-record-protocol " ,
" nat-network-dns-srv-record " ,
" nat-network " ,
VIR_NETWORK_UPDATE_COMMAND_DELETE ) ;
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
}
2017-03-29 17:45:42 +03:00
VIR_TEST_MAIN ( mymain )