2008-04-08 13:02:17 +04:00
/*
* Unix SMB / CIFS implementation .
* libsmbconf - Samba configuration library : testsuite
* Copyright ( C ) Michael Adam 2008
*
* 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 3 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 , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
2010-08-05 12:49:53 +04:00
# include "popt_common.h"
2010-05-18 04:27:34 +04:00
# include "lib/smbconf/smbconf.h"
# include "lib/smbconf/smbconf_init.h"
# include "lib/smbconf/smbconf_reg.h"
# include "lib/smbconf/smbconf_txt.h"
2008-04-08 13:02:17 +04:00
2008-04-08 19:49:10 +04:00
static void print_strings ( const char * prefix ,
2014-02-26 23:16:26 +04:00
uint32_t num_strings ,
const char * const * strings )
2008-04-08 19:49:10 +04:00
{
uint32_t count ;
if ( prefix = = NULL ) {
prefix = " " ;
}
for ( count = 0 ; count < num_strings ; count + + ) {
printf ( " %s%s \n " , prefix , strings [ count ] ) ;
}
}
2008-04-08 14:30:21 +04:00
static bool test_get_includes ( struct smbconf_ctx * ctx )
{
2011-04-11 16:52:52 +04:00
sbcErr err ;
2008-04-08 14:30:21 +04:00
bool ret = false ;
uint32_t num_includes = 0 ;
char * * includes = NULL ;
TALLOC_CTX * mem_ctx = talloc_stackframe ( ) ;
2009-01-30 16:36:48 +03:00
printf ( " TEST: get_includes \n " ) ;
2011-04-11 16:52:52 +04:00
err = smbconf_get_global_includes ( ctx , mem_ctx ,
& num_includes , & includes ) ;
if ( ! SBC_ERROR_IS_OK ( err ) ) {
printf ( " FAIL: get_includes - %s \n " , sbcErrorString ( err ) ) ;
2008-04-08 14:30:21 +04:00
goto done ;
}
printf ( " got %u includes%s \n " , num_includes ,
( num_includes > 0 ) ? " : " : " . " ) ;
2014-02-26 23:16:26 +04:00
print_strings ( " " , num_includes , ( const char * const * ) includes ) ;
2008-04-08 14:30:21 +04:00
2009-01-30 16:36:48 +03:00
printf ( " OK: get_includes \n " ) ;
2008-04-08 14:30:21 +04:00
ret = true ;
done :
2009-01-21 19:11:46 +03:00
talloc_free ( mem_ctx ) ;
2008-04-08 14:30:21 +04:00
return ret ;
}
2008-04-08 13:02:17 +04:00
2008-04-08 19:54:29 +04:00
static bool test_set_get_includes ( struct smbconf_ctx * ctx )
{
2011-04-11 16:52:52 +04:00
sbcErr err ;
2008-04-08 19:54:29 +04:00
uint32_t count ;
bool ret = false ;
const char * set_includes [ ] = {
" /path/to/include1 " ,
" /path/to/include2 "
} ;
uint32_t set_num_includes = 2 ;
char * * get_includes = NULL ;
uint32_t get_num_includes = 0 ;
TALLOC_CTX * mem_ctx = talloc_stackframe ( ) ;
2009-01-30 16:36:48 +03:00
printf ( " TEST: set_get_includes \n " ) ;
2008-04-08 19:54:29 +04:00
2011-04-11 17:14:52 +04:00
err = smbconf_set_global_includes ( ctx , set_num_includes , set_includes ) ;
if ( ! SBC_ERROR_IS_OK ( err ) ) {
2009-01-30 16:36:48 +03:00
printf ( " FAIL: get_set_includes (setting includes) - %s \n " ,
2011-04-11 17:14:52 +04:00
sbcErrorString ( err ) ) ;
2008-04-08 19:54:29 +04:00
goto done ;
}
2011-04-11 16:52:52 +04:00
err = smbconf_get_global_includes ( ctx , mem_ctx , & get_num_includes ,
& get_includes ) ;
if ( ! SBC_ERROR_IS_OK ( err ) ) {
2009-01-30 16:36:48 +03:00
printf ( " FAIL: get_set_includes (getting includes) - %s \n " ,
2011-04-11 16:52:52 +04:00
sbcErrorString ( err ) ) ;
2008-04-08 19:54:29 +04:00
goto done ;
}
if ( get_num_includes ! = set_num_includes ) {
2009-01-30 16:36:48 +03:00
printf ( " FAIL: get_set_includes - set %d includes, got %d \n " ,
2008-04-08 19:54:29 +04:00
set_num_includes , get_num_includes ) ;
goto done ;
}
for ( count = 0 ; count < get_num_includes ; count + + ) {
if ( ! strequal ( set_includes [ count ] , get_includes [ count ] ) ) {
printf ( " expected: \n " ) ;
2014-02-26 23:16:26 +04:00
print_strings ( " * " , set_num_includes ,
( const char * const * ) set_includes ) ;
2008-04-08 19:54:29 +04:00
printf ( " got: \n " ) ;
print_strings ( " * " , get_num_includes ,
2014-02-26 23:16:26 +04:00
( const char * const * ) get_includes ) ;
2009-01-30 16:36:48 +03:00
printf ( " FAIL: get_set_includes - data mismatch: \n " ) ;
2008-04-08 19:54:29 +04:00
goto done ;
}
}
2009-01-30 16:36:48 +03:00
printf ( " OK: set_includes \n " ) ;
2008-04-08 19:54:29 +04:00
ret = true ;
done :
2009-01-21 19:11:46 +03:00
talloc_free ( mem_ctx ) ;
2008-04-08 19:54:29 +04:00
return ret ;
}
2008-04-10 00:23:12 +04:00
static bool test_delete_includes ( struct smbconf_ctx * ctx )
{
2011-04-11 16:52:52 +04:00
sbcErr err ;
2008-04-10 00:23:12 +04:00
bool ret = false ;
const char * set_includes [ ] = {
" /path/to/include " ,
} ;
uint32_t set_num_includes = 1 ;
char * * get_includes = NULL ;
uint32_t get_num_includes = 0 ;
TALLOC_CTX * mem_ctx = talloc_stackframe ( ) ;
2009-01-30 16:36:48 +03:00
printf ( " TEST: delete_includes \n " ) ;
2008-04-10 00:23:12 +04:00
2011-04-11 17:14:52 +04:00
err = smbconf_set_global_includes ( ctx , set_num_includes , set_includes ) ;
if ( ! SBC_ERROR_IS_OK ( err ) ) {
2009-01-30 16:36:48 +03:00
printf ( " FAIL: delete_includes (setting includes) - %s \n " ,
2011-04-11 17:14:52 +04:00
sbcErrorString ( err ) ) ;
2008-04-10 00:23:12 +04:00
goto done ;
}
2011-04-11 18:01:22 +04:00
err = smbconf_delete_global_includes ( ctx ) ;
if ( ! SBC_ERROR_IS_OK ( err ) ) {
2009-01-30 16:36:48 +03:00
printf ( " FAIL: delete_includes (deleting includes) - %s \n " ,
2011-04-11 18:01:22 +04:00
sbcErrorString ( err ) ) ;
2008-04-10 00:23:12 +04:00
goto done ;
}
2011-04-11 16:52:52 +04:00
err = smbconf_get_global_includes ( ctx , mem_ctx , & get_num_includes ,
& get_includes ) ;
if ( ! SBC_ERROR_IS_OK ( err ) ) {
2009-01-30 16:36:48 +03:00
printf ( " FAIL: delete_includes (getting includes) - %s \n " ,
2011-04-11 16:52:52 +04:00
sbcErrorString ( err ) ) ;
2008-04-10 00:23:12 +04:00
goto done ;
}
if ( get_num_includes ! = 0 ) {
2009-01-30 16:36:48 +03:00
printf ( " FAIL: delete_includes (not empty after delete) \n " ) ;
2008-04-10 00:23:12 +04:00
goto done ;
}
2011-04-11 18:01:22 +04:00
err = smbconf_delete_global_includes ( ctx ) ;
if ( ! SBC_ERROR_IS_OK ( err ) ) {
2009-01-30 16:36:48 +03:00
printf ( " FAIL: delete_includes (delete empty includes) - "
2011-04-11 18:01:22 +04:00
" %s \n " , sbcErrorString ( err ) ) ;
2008-04-10 00:23:12 +04:00
goto done ;
}
2009-01-30 16:36:48 +03:00
printf ( " OK: delete_includes \n " ) ;
2008-04-10 00:23:12 +04:00
ret = true ;
done :
2012-07-17 23:37:31 +04:00
talloc_free ( mem_ctx ) ;
2008-04-10 00:23:12 +04:00
return ret ;
}
2008-10-24 02:00:20 +04:00
static bool create_conf_file ( const char * filename )
{
FILE * f ;
2009-01-30 16:36:48 +03:00
printf ( " TEST: creating file \n " ) ;
2012-03-28 05:51:17 +04:00
f = fopen ( filename , " w " ) ;
2008-10-24 02:00:20 +04:00
if ( ! f ) {
printf ( " failure: failed to open %s for writing: %s \n " ,
filename , strerror ( errno ) ) ;
return false ;
}
fprintf ( f , " [global] \n " ) ;
fprintf ( f , " \t server string = smbconf testsuite \n " ) ;
fprintf ( f , " \t workgroup = SAMBA \n " ) ;
fprintf ( f , " \t security = user \n " ) ;
fclose ( f ) ;
2009-01-30 16:36:48 +03:00
printf ( " OK: create file \n " ) ;
2008-10-24 02:00:20 +04:00
return true ;
}
2008-04-08 13:02:17 +04:00
static bool torture_smbconf_txt ( void )
{
2011-04-07 19:19:03 +04:00
sbcErr err ;
2008-04-08 13:02:17 +04:00
bool ret = true ;
2008-10-24 02:00:20 +04:00
const char * filename = " /tmp/smb.conf.smbconf_testsuite " ;
2008-04-08 13:02:17 +04:00
struct smbconf_ctx * conf_ctx = NULL ;
TALLOC_CTX * mem_ctx = talloc_stackframe ( ) ;
printf ( " test: text backend \n " ) ;
2008-10-24 02:00:20 +04:00
if ( ! create_conf_file ( filename ) ) {
ret = false ;
goto done ;
}
2009-01-30 16:36:48 +03:00
printf ( " TEST: init \n " ) ;
2011-04-07 19:19:03 +04:00
err = smbconf_init_txt ( mem_ctx , & conf_ctx , filename ) ;
if ( ! SBC_ERROR_IS_OK ( err ) ) {
printf ( " FAIL: text backend failed: %s \n " , sbcErrorString ( err ) ) ;
2008-04-08 13:02:17 +04:00
ret = false ;
goto done ;
}
2009-01-30 16:36:48 +03:00
printf ( " OK: init \n " ) ;
2008-04-08 13:02:17 +04:00
2008-04-08 14:30:21 +04:00
ret & = test_get_includes ( conf_ctx ) ;
2008-04-08 13:02:17 +04:00
smbconf_shutdown ( conf_ctx ) ;
2009-01-30 16:36:48 +03:00
printf ( " TEST: unlink file \n " ) ;
2008-10-24 02:00:20 +04:00
if ( unlink ( filename ) ! = 0 ) {
2009-01-30 16:36:48 +03:00
printf ( " OK: unlink failed: %s \n " , strerror ( errno ) ) ;
2008-10-24 02:00:20 +04:00
ret = false ;
goto done ;
}
2009-01-30 16:36:48 +03:00
printf ( " OK: unlink file \n " ) ;
2008-04-08 13:02:17 +04:00
done :
2009-01-30 16:36:48 +03:00
printf ( " %s: text backend \n " , ret ? " success " : " failure " ) ;
2009-01-21 19:11:46 +03:00
talloc_free ( mem_ctx ) ;
2008-04-08 13:02:17 +04:00
return ret ;
}
static bool torture_smbconf_reg ( void )
{
2011-04-07 19:19:03 +04:00
sbcErr err ;
2008-04-08 13:02:17 +04:00
bool ret = true ;
struct smbconf_ctx * conf_ctx = NULL ;
TALLOC_CTX * mem_ctx = talloc_stackframe ( ) ;
printf ( " test: registry backend \n " ) ;
2009-01-30 16:36:48 +03:00
printf ( " TEST: init \n " ) ;
2011-04-07 19:19:03 +04:00
err = smbconf_init_reg ( mem_ctx , & conf_ctx , NULL ) ;
if ( ! SBC_ERROR_IS_OK ( err ) ) {
printf ( " FAIL: init failed: %s \n " , sbcErrorString ( err ) ) ;
2008-04-08 13:02:17 +04:00
ret = false ;
goto done ;
}
2009-01-30 16:36:48 +03:00
printf ( " OK: init \n " ) ;
2008-04-08 16:31:16 +04:00
ret & = test_get_includes ( conf_ctx ) ;
2008-04-08 19:54:29 +04:00
ret & = test_set_get_includes ( conf_ctx ) ;
2008-04-10 00:23:12 +04:00
ret & = test_delete_includes ( conf_ctx ) ;
2008-04-08 13:02:17 +04:00
smbconf_shutdown ( conf_ctx ) ;
done :
2009-01-30 16:36:48 +03:00
printf ( " %s: registry backend \n " , ret ? " success " : " failure " ) ;
2009-01-21 19:11:46 +03:00
talloc_free ( mem_ctx ) ;
2008-04-08 13:02:17 +04:00
return ret ;
}
static bool torture_smbconf ( void )
{
bool ret = true ;
ret & = torture_smbconf_txt ( ) ;
printf ( " \n " ) ;
ret & = torture_smbconf_reg ( ) ;
return ret ;
}
int main ( int argc , const char * * argv )
{
2008-04-08 14:07:35 +04:00
bool ret ;
poptContext pc ;
2008-04-08 19:51:57 +04:00
TALLOC_CTX * mem_ctx = talloc_stackframe ( ) ;
2008-04-08 14:07:35 +04:00
struct poptOption long_options [ ] = {
2008-04-08 19:52:46 +04:00
POPT_COMMON_SAMBA
2008-04-08 14:07:35 +04:00
{ 0 , 0 , 0 , 0 }
} ;
2015-03-21 22:00:06 +03:00
smb_init_locale ( ) ;
2010-10-29 07:19:32 +04:00
setup_logging ( argv [ 0 ] , DEBUG_STDERR ) ;
2008-04-08 14:07:35 +04:00
/* parse options */
pc = poptGetContext ( " smbconftort " , argc , ( const char * * ) argv ,
long_options , 0 ) ;
while ( poptGetNextOpt ( pc ) ! = - 1 ) { }
poptFreeContext ( pc ) ;
2011-07-27 18:29:04 +04:00
ret = lp_load_global ( get_dyn_CONFIGFILE ( ) ) ;
2008-04-08 14:07:35 +04:00
if ( ! ret ) {
printf ( " failure: error loading the configuration \n " ) ;
goto done ;
}
ret = torture_smbconf ( ) ;
done :
2009-01-21 19:11:46 +03:00
talloc_free ( mem_ctx ) ;
2008-04-08 13:02:17 +04:00
return ret ? 0 : - 1 ;
}