2012-03-20 15:55:11 +04:00
/*
2014-03-17 13:38:38 +04:00
* Copyright ( C ) 2012 , 2014 Red Hat , Inc .
2012-03-20 15:55:11 +04:00
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
2012-09-21 02:30:55 +04:00
* License along with this library . If not , see
2012-07-21 14:06:23 +04:00
* < http : //www.gnu.org/licenses/>.
2012-03-20 15:55:11 +04:00
*/
# include <config.h>
# include <signal.h>
# include "testutils.h"
2012-12-12 22:06:53 +04:00
# include "viralloc.h"
2012-12-12 21:59:27 +04:00
# include "virlog.h"
2012-03-20 15:55:11 +04:00
# include "viruri.h"
# define VIR_FROM_THIS VIR_FROM_RPC
2014-02-28 16:16:17 +04:00
VIR_LOG_INIT ( " tests.uritest " ) ;
2012-03-20 15:55:11 +04:00
struct URIParseData {
const char * uri ;
2012-03-26 14:23:45 +04:00
const char * uri_out ;
2012-03-20 15:55:11 +04:00
const char * scheme ;
const char * server ;
int port ;
const char * path ;
const char * query ;
const char * fragment ;
2012-04-05 19:52:42 +04:00
const char * user ;
2021-03-11 10:16:13 +03:00
virURIParam * params ;
2012-03-20 15:55:11 +04:00
} ;
static int testURIParse ( const void * args )
{
2022-06-22 08:14:04 +03:00
g_autoptr ( virURI ) uri = NULL ;
2012-03-20 15:55:11 +04:00
const struct URIParseData * data = args ;
2021-09-04 23:37:31 +03:00
g_autofree char * uristr = NULL ;
2012-03-20 17:59:32 +04:00
size_t i ;
2019-05-30 14:56:51 +03:00
bool fail = false ;
2012-03-20 15:55:11 +04:00
Centralize error reporting for URI parsing/formatting problems
Move error reporting out of the callers, into virURIParse
and virURIFormat, to get consistency.
* include/libvirt/virterror.h, src/util/virterror.c: Add VIR_FROM_URI
* src/util/viruri.c, src/util/viruri.h: Add error reporting
* src/esx/esx_driver.c, src/libvirt.c, src/libxl/libxl_driver.c,
src/lxc/lxc_driver.c, src/openvz/openvz_driver.c,
src/qemu/qemu_driver.c, src/qemu/qemu_migration.c,
src/remote/remote_driver.c, src/uml/uml_driver.c,
src/vbox/vbox_tmpl.c, src/vmx/vmx.c, src/xen/xen_driver.c,
src/xen/xend_internal.c, tests/viruritest.c: Remove error
reporting
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2012-03-20 16:16:54 +04:00
if ( ! ( uri = virURIParse ( data - > uri ) ) )
2022-06-22 08:21:54 +03:00
return - 1 ;
2012-03-20 15:55:11 +04:00
2015-10-20 19:15:12 +03:00
if ( STRNEQ ( uri - > scheme , data - > scheme ) ) {
2019-05-30 12:30:32 +03:00
VIR_TEST_DEBUG ( " Expected scheme '%s', actual '%s' " ,
data - > scheme , uri - > scheme ) ;
2019-05-30 14:56:51 +03:00
fail = true ;
2012-03-20 15:55:11 +04:00
}
2015-10-20 19:15:12 +03:00
if ( STRNEQ ( uri - > server , data - > server ) ) {
2019-05-30 12:30:32 +03:00
VIR_TEST_DEBUG ( " Expected server '%s', actual '%s' " ,
data - > server , uri - > server ) ;
2019-05-30 14:56:51 +03:00
fail = true ;
2012-03-20 15:55:11 +04:00
}
if ( uri - > port ! = data - > port ) {
2019-05-30 12:30:32 +03:00
VIR_TEST_DEBUG ( " Expected port '%d', actual '%d' " ,
data - > port , uri - > port ) ;
2019-05-30 14:56:51 +03:00
fail = true ;
2012-03-20 15:55:11 +04:00
}
2015-10-20 19:15:12 +03:00
if ( STRNEQ_NULLABLE ( uri - > path , data - > path ) ) {
2019-05-30 12:30:32 +03:00
VIR_TEST_DEBUG ( " Expected path '%s', actual '%s' " ,
data - > path , uri - > path ) ;
2019-05-30 14:56:51 +03:00
fail = true ;
2012-03-20 15:55:11 +04:00
}
2015-10-20 19:15:12 +03:00
if ( STRNEQ_NULLABLE ( uri - > query , data - > query ) ) {
2019-05-30 12:30:32 +03:00
VIR_TEST_DEBUG ( " Expected query '%s', actual '%s' " ,
data - > query , uri - > query ) ;
2019-05-30 14:56:51 +03:00
fail = true ;
2012-03-20 15:55:11 +04:00
}
2015-10-20 19:15:12 +03:00
if ( STRNEQ_NULLABLE ( uri - > fragment , data - > fragment ) ) {
2019-05-30 12:30:32 +03:00
VIR_TEST_DEBUG ( " Expected fragment '%s', actual '%s' " ,
data - > fragment , uri - > fragment ) ;
2019-05-30 14:56:51 +03:00
fail = true ;
2012-03-20 15:55:11 +04:00
}
2013-05-21 11:53:48 +04:00
for ( i = 0 ; data - > params & & data - > params [ i ] . name & & i < uri - > paramsCount ; i + + ) {
2015-10-20 19:15:12 +03:00
if ( STRNEQ_NULLABLE ( data - > params [ i ] . name , uri - > params [ i ] . name ) ) {
2019-05-30 12:30:32 +03:00
VIR_TEST_DEBUG ( " Expected param name %zu '%s', actual '%s' " ,
i , data - > params [ i ] . name , uri - > params [ i ] . name ) ;
2019-05-30 14:56:51 +03:00
fail = true ;
2012-03-20 17:59:32 +04:00
}
2015-10-20 19:15:12 +03:00
if ( STRNEQ_NULLABLE ( data - > params [ i ] . value , uri - > params [ i ] . value ) ) {
2019-05-30 12:30:32 +03:00
VIR_TEST_DEBUG ( " Expected param value %zu '%s', actual '%s' " ,
i , data - > params [ i ] . value , uri - > params [ i ] . value ) ;
2019-05-30 14:56:51 +03:00
fail = true ;
2012-03-20 17:59:32 +04:00
}
}
if ( data - > params & & data - > params [ i ] . name ) {
2019-05-30 12:30:32 +03:00
VIR_TEST_DEBUG ( " Missing parameter %zu %s=%s " ,
i , data - > params [ i ] . name , data - > params [ i ] . value ) ;
2019-05-30 14:56:51 +03:00
fail = true ;
2012-03-20 17:59:32 +04:00
}
if ( i ! = uri - > paramsCount ) {
2019-05-30 14:56:51 +03:00
for ( ; i < uri - > paramsCount ; i + + ) {
VIR_TEST_DEBUG ( " Unexpected parameter %zu %s=%s " ,
i , uri - > params [ i ] . name , uri - > params [ i ] . value ) ;
}
fail = true ;
2012-03-20 17:59:32 +04:00
}
2012-03-26 14:23:45 +04:00
VIR_FREE ( uri - > query ) ;
uri - > query = virURIFormatParams ( uri ) ;
2023-02-03 17:38:03 +03:00
uristr = virURIFormat ( uri ) ;
2012-03-26 14:23:45 +04:00
2015-10-20 19:15:12 +03:00
if ( STRNEQ ( uristr , data - > uri_out ) ) {
2019-05-30 12:30:32 +03:00
VIR_TEST_DEBUG ( " URI did not roundtrip, expect '%s', actual '%s' " ,
data - > uri_out , uristr ) ;
2019-05-30 14:56:51 +03:00
fail = true ;
2012-03-26 14:23:45 +04:00
}
2019-05-30 14:56:51 +03:00
if ( fail )
2022-06-22 08:21:54 +03:00
return - 1 ;
2019-05-30 14:56:51 +03:00
2022-06-22 08:21:54 +03:00
return 0 ;
2012-03-20 15:55:11 +04:00
}
static int
mymain ( void )
{
int ret = 0 ;
2020-01-22 20:59:39 +03:00
# ifndef WIN32
2012-03-20 15:55:11 +04:00
signal ( SIGPIPE , SIG_IGN ) ;
2020-01-22 20:59:39 +03:00
# endif /* WIN32 */
2012-03-20 15:55:11 +04:00
2017-11-03 15:09:47 +03:00
# define TEST_FULL(uri, uri_out, scheme, server, port, path, query, \
fragment , user , params ) \
do { \
const struct URIParseData data = { \
uri , ( uri_out ) ? ( uri_out ) : ( uri ) , scheme , server , port , \
path , query , fragment , user , params \
} ; \
if ( virTestRun ( " Test URI " # uri , testURIParse , & data ) < 0 ) \
ret = - 1 ; \
2012-03-20 15:55:11 +04:00
} while ( 0 )
2012-04-05 19:52:42 +04:00
# define TEST_PARSE(uri, scheme, server, port, path, query, fragment, user, params) \
TEST_FULL ( uri , NULL , scheme , server , port , path , query , fragment , user , params )
2017-11-03 15:09:47 +03:00
# define TEST_PARAMS(query_in, query_out, params) \
TEST_FULL ( " test://example.com/? " query_in , \
* query_out ? " test://example.com/? " query_out : NULL , \
2012-04-05 19:52:42 +04:00
" test " , " example.com " , 0 , " / " , query_in , NULL , NULL , params )
2012-03-20 15:55:11 +04:00
2020-08-03 18:32:22 +03:00
VIR_WARNINGS_NO_DECLARATION_AFTER_STATEMENT
2012-03-20 17:59:32 +04:00
virURIParam params [ ] = {
2012-04-06 06:04:23 +04:00
{ ( char * ) " name " , ( char * ) " value " , false } ,
{ NULL , NULL , false } ,
2012-03-20 17:59:32 +04:00
} ;
2012-04-05 19:52:42 +04:00
TEST_PARSE ( " test://example.com " , " test " , " example.com " , 0 , NULL , NULL , NULL , NULL , NULL ) ;
TEST_PARSE ( " test://foo@example.com " , " test " , " example.com " , 0 , NULL , NULL , NULL , " foo " , NULL ) ;
2019-08-15 17:16:59 +03:00
TEST_PARSE ( " test://foo:pass@example.com " , " test " , " example.com " , 0 , NULL , NULL , NULL , " foo:pass " , NULL ) ;
2012-04-05 19:52:42 +04:00
TEST_PARSE ( " test://example.com:123 " , " test " , " example.com " , 123 , NULL , NULL , NULL , NULL , NULL ) ;
TEST_PARSE ( " test://example.com:123/system?name=value#foo " , " test " , " example.com " , 123 , " /system " , " name=value " , " foo " , NULL , params ) ;
TEST_PARSE ( " test://127.0.0.1:123/system " , " test " , " 127.0.0.1 " , 123 , " /system " , NULL , NULL , NULL , NULL ) ;
TEST_PARSE ( " test://[::1]:123/system " , " test " , " ::1 " , 123 , " /system " , NULL , NULL , NULL , NULL ) ;
TEST_PARSE ( " test://[2001:41c8:1:4fd4::2]:123/system " , " test " , " 2001:41c8:1:4fd4::2 " , 123 , " /system " , NULL , NULL , NULL , NULL ) ;
2014-10-29 13:01:48 +03:00
TEST_PARSE ( " gluster+rdma://example.com:1234/gv0/vol.img " , " gluster+rdma " , " example.com " , 1234 , " /gv0/vol.img " , NULL , NULL , NULL , NULL ) ;
2012-03-20 15:55:11 +04:00
2019-05-30 14:57:08 +03:00
virURIParam spiceparams [ ] = {
{ ( char * ) " tlsSubject " , ( char * ) " C=XX,L=Testtown,O=Test Company,CN=tester.test " , false } ,
{ NULL , NULL , false } ,
} ;
TEST_FULL ( " spice://[3ffe::104]:5900/?tlsSubject=C=XX,L=Testtown,O=Test%20Company,CN=tester.test " ,
2019-10-24 14:26:16 +03:00
" spice://[3ffe::104]:5900/?tlsSubject=C%3DXX%2CL%3DTesttown%2CO%3DTest%20Company%2CCN%3Dtester.test " ,
2019-05-30 14:57:08 +03:00
" spice " , " 3ffe::104 " , 5900 , " / " , " tlsSubject=C=XX,L=Testtown,O=Test%20Company,CN=tester.test " , NULL , NULL , spiceparams ) ;
2012-03-26 14:23:45 +04:00
virURIParam params1 [ ] = {
2012-04-06 06:04:23 +04:00
{ ( char * ) " foo " , ( char * ) " one " , false } ,
{ ( char * ) " bar " , ( char * ) " two " , false } ,
{ NULL , NULL , false } ,
2012-03-26 14:23:45 +04:00
} ;
virURIParam params2 [ ] = {
2012-04-06 06:04:23 +04:00
{ ( char * ) " foo " , ( char * ) " one " , false } ,
{ ( char * ) " foo " , ( char * ) " two " , false } ,
{ NULL , NULL , false } ,
2012-03-26 14:23:45 +04:00
} ;
virURIParam params3 [ ] = {
2012-04-06 06:04:23 +04:00
{ ( char * ) " foo " , ( char * ) " &one " , false } ,
{ ( char * ) " bar " , ( char * ) " &two " , false } ,
{ NULL , NULL , false } ,
2012-03-26 14:23:45 +04:00
} ;
virURIParam params4 [ ] = {
2012-04-06 06:04:23 +04:00
{ ( char * ) " foo " , ( char * ) " " , false } ,
{ NULL , NULL , false } ,
2012-03-26 14:23:45 +04:00
} ;
virURIParam params5 [ ] = {
2012-04-06 06:04:23 +04:00
{ ( char * ) " foo " , ( char * ) " one two " , false } ,
{ NULL , NULL , false } ,
2012-03-26 14:23:45 +04:00
} ;
virURIParam params6 [ ] = {
2012-04-06 06:04:23 +04:00
{ ( char * ) " foo " , ( char * ) " one " , false } ,
{ NULL , NULL , false } ,
2012-03-26 14:23:45 +04:00
} ;
2020-08-03 18:32:22 +03:00
VIR_WARNINGS_RESET
2012-03-26 14:23:45 +04:00
TEST_PARAMS ( " foo=one&bar=two " , " " , params1 ) ;
TEST_PARAMS ( " foo=one&foo=two " , " " , params2 ) ;
TEST_PARAMS ( " foo=one&&foo=two " , " foo=one&foo=two " , params2 ) ;
TEST_PARAMS ( " foo=one;foo=two " , " foo=one&foo=two " , params2 ) ;
TEST_PARAMS ( " foo=%26one&bar=%26two " , " " , params3 ) ;
TEST_PARAMS ( " foo " , " foo= " , params4 ) ;
TEST_PARAMS ( " foo= " , " " , params4 ) ;
TEST_PARAMS ( " foo=& " , " foo= " , params4 ) ;
TEST_PARAMS ( " foo=&& " , " foo= " , params4 ) ;
TEST_PARAMS ( " foo=one%20two " , " " , params5 ) ;
TEST_PARAMS ( " =bogus&foo=one " , " foo=one " , params6 ) ;
2014-03-17 13:38:38 +04:00
return ret = = 0 ? EXIT_SUCCESS : EXIT_FAILURE ;
2012-03-20 15:55:11 +04:00
}
2017-03-29 17:45:42 +03:00
VIR_TEST_MAIN ( mymain )