2006-03-25 17:49:00 +03:00
/*
Unix SMB / CIFS implementation .
SMB torture UI functions
Copyright ( C ) Jelmer Vernooij 2006
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program 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 General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
# include "torture/ui.h"
2006-08-30 15:29:34 +04:00
# include "lib/util/dlinklist.h"
2006-03-25 17:49:00 +03:00
2006-06-24 01:05:45 +04:00
void torture_comment ( struct torture_context * context , const char * comment , . . . )
2006-06-13 03:03:02 +04:00
{
2006-06-17 02:06:09 +04:00
va_list ap ;
char * tmp ;
2006-06-17 03:10:15 +04:00
if ( ! context - > ui_ops - > comment )
return ;
2006-06-17 02:06:09 +04:00
va_start ( ap , comment ) ;
tmp = talloc_vasprintf ( context , comment , ap ) ;
context - > ui_ops - > comment ( context , tmp ) ;
talloc_free ( tmp ) ;
}
2006-06-13 03:03:02 +04:00
2006-06-24 01:05:45 +04:00
void torture_fail ( struct torture_context * context , const char * fmt , . . . )
2006-06-17 02:06:09 +04:00
{
va_list ap ;
2006-06-17 03:10:15 +04:00
2006-06-17 02:06:09 +04:00
va_start ( ap , fmt ) ;
2006-06-17 04:17:50 +04:00
context - > last_reason = talloc_vasprintf ( context , fmt , ap ) ;
2006-10-16 01:42:45 +04:00
/* make sure the reason for the failure is displayed */
context - > ui_ops - > comment ( context , context - > last_reason ) ;
2006-06-17 02:06:09 +04:00
va_end ( ap ) ;
2006-06-17 04:17:50 +04:00
context - > last_result = TORTURE_FAIL ;
2006-06-17 02:06:09 +04:00
}
2006-06-24 01:05:45 +04:00
void torture_skip ( struct torture_context * context , const char * fmt , . . . )
2006-03-25 17:49:00 +03:00
{
2006-06-17 02:06:09 +04:00
va_list ap ;
2006-06-17 03:10:15 +04:00
context - > skipped + + ;
2006-06-17 02:06:09 +04:00
va_start ( ap , fmt ) ;
2006-06-17 04:17:50 +04:00
context - > last_result = TORTURE_SKIP ;
context - > last_reason = talloc_vasprintf ( context , fmt , ap ) ;
2006-06-17 02:06:09 +04:00
va_end ( ap ) ;
}
2006-03-25 17:49:00 +03:00
2006-06-17 02:06:09 +04:00
struct torture_suite * torture_suite_create ( TALLOC_CTX * ctx , const char * name )
{
struct torture_suite * suite = talloc ( ctx , struct torture_suite ) ;
2006-03-25 17:49:00 +03:00
2006-06-17 02:06:09 +04:00
suite - > name = talloc_strdup ( suite , name ) ;
suite - > testcases = NULL ;
2006-06-13 03:03:02 +04:00
2006-06-17 02:06:09 +04:00
return suite ;
}
void torture_tcase_set_fixture ( struct torture_tcase * tcase ,
BOOL ( * setup ) ( struct torture_context * , void * * ) ,
BOOL ( * teardown ) ( struct torture_context * , void * ) )
{
tcase - > setup = setup ;
tcase - > teardown = teardown ;
2006-03-25 17:49:00 +03:00
}
2006-06-17 02:06:09 +04:00
struct torture_test * torture_tcase_add_test ( struct torture_tcase * tcase ,
const char * name ,
BOOL ( * run ) ( struct torture_context * ,
const void * tcase_data ,
const void * test_data ) ,
const void * data )
2006-03-25 17:49:00 +03:00
{
2006-06-17 02:06:09 +04:00
struct torture_test * test = talloc ( tcase , struct torture_test ) ;
2006-03-25 17:49:00 +03:00
test - > name = talloc_strdup ( test , name ) ;
2006-06-17 02:06:09 +04:00
test - > description = NULL ;
test - > run = run ;
2006-06-17 02:48:50 +04:00
test - > dangerous = False ;
2006-06-17 02:06:09 +04:00
test - > data = data ;
2006-03-25 17:49:00 +03:00
2006-09-14 15:47:13 +04:00
DLIST_ADD_END ( tcase - > tests , test , struct torture_test * ) ;
2006-06-13 03:03:02 +04:00
return test ;
2006-03-25 17:49:00 +03:00
}
2006-06-17 02:06:09 +04:00
struct torture_tcase * torture_suite_add_tcase ( struct torture_suite * suite ,
const char * name )
2006-03-25 17:49:00 +03:00
{
2006-06-17 02:06:09 +04:00
struct torture_tcase * tcase = talloc ( suite , struct torture_tcase ) ;
tcase - > name = talloc_strdup ( tcase , name ) ;
tcase - > description = NULL ;
tcase - > setup = NULL ;
tcase - > teardown = NULL ;
tcase - > fixture_persistent = True ;
tcase - > tests = NULL ;
DLIST_ADD ( suite - > testcases , tcase ) ;
return tcase ;
}
BOOL torture_run_suite ( struct torture_context * context ,
struct torture_suite * suite )
{
BOOL ret = True ;
struct torture_tcase * tcase ;
2006-06-17 03:10:15 +04:00
if ( context - > ui_ops - > suite_start )
context - > ui_ops - > suite_start ( context , suite ) ;
2006-06-17 02:06:09 +04:00
for ( tcase = suite - > testcases ; tcase ; tcase = tcase - > next ) {
ret & = torture_run_tcase ( context , tcase ) ;
}
2006-06-17 03:10:15 +04:00
if ( context - > ui_ops - > suite_finish )
context - > ui_ops - > suite_finish ( context , suite ) ;
2006-03-25 17:49:00 +03:00
2006-06-17 02:06:09 +04:00
return ret ;
2006-03-25 17:49:00 +03:00
}
2006-06-17 02:48:50 +04:00
static BOOL internal_torture_run_test ( struct torture_context * context ,
struct torture_tcase * tcase ,
struct torture_test * test ,
2006-06-17 05:20:02 +04:00
BOOL already_setup ,
2006-06-17 03:10:15 +04:00
const void * tcase_data )
2006-06-17 02:48:50 +04:00
{
BOOL ret ;
void * data = NULL ;
if ( test - > dangerous & & ! lp_parm_bool ( - 1 , " torture " , " dangerous " , False ) ) {
torture_skip ( context , " disabled %s - enable dangerous tests to use " ,
test - > name ) ;
return True ;
}
2006-06-17 05:20:02 +04:00
if ( ! already_setup & & tcase - > setup & & ! tcase - > setup ( context , & data ) )
2006-06-17 02:48:50 +04:00
return False ;
context - > active_tcase = tcase ;
context - > active_test = test ;
2006-06-17 04:17:50 +04:00
2006-06-17 03:10:15 +04:00
if ( context - > ui_ops - > test_start )
context - > ui_ops - > test_start ( context , tcase , test ) ;
2006-06-17 02:48:50 +04:00
2006-06-17 04:17:50 +04:00
context - > last_reason = NULL ;
context - > last_result = TORTURE_OK ;
2006-06-17 05:20:02 +04:00
ret = test - > run ( context , ! already_setup ? data : tcase_data , test - > data ) ;
2006-06-21 21:47:19 +04:00
if ( ! ret ) {
context - > last_reason = talloc_strdup ( context , " ... " ) ;
context - > last_result = TORTURE_FAIL ;
}
2006-06-17 04:17:50 +04:00
if ( context - > ui_ops - > test_result )
context - > ui_ops - > test_result ( context , context - > last_result ,
context - > last_reason ) ;
switch ( context - > last_result ) {
case TORTURE_SKIP : context - > success + + ; break ;
case TORTURE_FAIL : context - > failed + + ; break ;
case TORTURE_TODO : context - > todo + + ; break ;
case TORTURE_OK : context - > success + + ; break ;
}
talloc_free ( context - > last_reason ) ;
2006-06-17 02:48:50 +04:00
context - > active_test = NULL ;
context - > active_tcase = NULL ;
2006-06-17 05:20:02 +04:00
if ( ! already_setup & & tcase - > teardown & & ! tcase - > teardown ( context , data ) )
2006-06-17 02:48:50 +04:00
return False ;
return ret ;
}
2006-06-17 02:06:09 +04:00
BOOL torture_run_tcase ( struct torture_context * context ,
struct torture_tcase * tcase )
{
BOOL ret = True ;
void * data = NULL ;
struct torture_test * test ;
context - > active_tcase = tcase ;
if ( context - > ui_ops - > tcase_start )
context - > ui_ops - > tcase_start ( context , tcase ) ;
if ( tcase - > fixture_persistent & & tcase - > setup
2006-06-17 03:10:15 +04:00
& & ! tcase - > setup ( context , & data ) ) {
ret = False ;
goto done ;
}
2006-06-17 02:06:09 +04:00
for ( test = tcase - > tests ; test ; test = test - > next ) {
2006-06-17 02:48:50 +04:00
ret & = internal_torture_run_test ( context , tcase , test ,
2006-06-17 05:20:02 +04:00
tcase - > fixture_persistent ,
( tcase - > setup ? data : tcase - > data ) ) ;
2006-06-17 02:06:09 +04:00
}
if ( tcase - > fixture_persistent & & tcase - > teardown & &
! tcase - > teardown ( context , data ) )
2006-06-17 03:10:15 +04:00
ret = False ;
2006-06-17 02:06:09 +04:00
2006-06-17 03:10:15 +04:00
done :
2006-06-17 02:06:09 +04:00
context - > active_tcase = NULL ;
2006-06-17 03:10:15 +04:00
if ( context - > ui_ops - > tcase_finish )
context - > ui_ops - > tcase_finish ( context , tcase ) ;
2006-06-17 02:06:09 +04:00
return ret ;
}
BOOL torture_run_test ( struct torture_context * context ,
struct torture_tcase * tcase ,
struct torture_test * test )
2006-03-25 17:49:00 +03:00
{
2006-06-17 05:20:02 +04:00
return internal_torture_run_test ( context , tcase , test , False , NULL ) ;
2006-03-25 17:49:00 +03:00
}
2006-06-17 02:06:09 +04:00
const char * torture_setting ( struct torture_context * test , const char * name ,
const char * default_value )
2006-03-25 17:49:00 +03:00
{
2006-06-17 02:06:09 +04:00
const char * ret = lp_parm_string ( - 1 , " torture " , name ) ;
2006-05-22 22:59:29 +04:00
2006-06-17 02:06:09 +04:00
if ( ret = = NULL )
return default_value ;
return ret ;
2006-03-25 17:49:00 +03:00
}
2006-06-17 02:06:09 +04:00
static BOOL simple_tcase_helper ( struct torture_context * test ,
const void * tcase_data ,
const void * test_data )
2006-06-12 23:49:53 +04:00
{
2006-06-17 02:06:09 +04:00
BOOL ( * run ) ( struct torture_context * , const void * ) = test_data ;
return run ( test , tcase_data ) ;
2006-06-12 23:49:53 +04:00
}
2006-06-17 02:06:09 +04:00
struct torture_tcase * torture_suite_add_simple_tcase (
struct torture_suite * suite ,
const char * name ,
BOOL ( * run ) ( struct torture_context * test , const void * ) ,
const void * data )
2006-03-25 17:49:00 +03:00
{
2006-06-17 02:06:09 +04:00
struct torture_tcase * tcase ;
tcase = torture_suite_add_tcase ( suite , name ) ;
tcase - > data = data ;
2006-06-21 21:47:19 +04:00
torture_tcase_add_test ( tcase , name , simple_tcase_helper , run ) ;
2006-06-17 02:06:09 +04:00
return tcase ;
}
BOOL torture_teardown_free ( struct torture_context * torture , void * data )
{
return talloc_free ( data ) ;
2006-03-25 17:49:00 +03:00
}