2004-11-15 09:55:27 +03:00
/*
Unix SMB / CIFS implementation .
test alternate data streams
Copyright ( C ) Andrew Tridgell 2004
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"
2006-01-03 16:41:17 +03:00
# include "torture/torture.h"
2004-11-15 09:55:27 +03:00
# include "libcli/raw/libcliraw.h"
2005-02-10 08:09:35 +03:00
# include "system/filesys.h"
2006-01-03 18:40:05 +03:00
# include "libcli/libcli.h"
2006-03-17 20:59:58 +03:00
# include "torture/util.h"
2004-11-15 09:55:27 +03:00
# define BASEDIR "\\teststreams"
# define CHECK_STATUS(status, correct) do { \
if ( ! NT_STATUS_EQUAL ( status , correct ) ) { \
printf ( " (%s) Incorrect status %s - should be %s \n " , \
__location__ , nt_errstr ( status ) , nt_errstr ( correct ) ) ; \
ret = False ; \
goto done ; \
} } while ( 0 )
# define CHECK_VALUE(v, correct) do { \
if ( ( v ) ! = ( correct ) ) { \
printf ( " (%s) Incorrect value %s=%d - should be %d \n " , \
2005-07-17 13:20:52 +04:00
__location__ , # v , ( int ) v , ( int ) correct ) ; \
2004-11-15 09:55:27 +03:00
ret = False ; \
} } while ( 0 )
/*
check that a stream has the right contents
*/
static BOOL check_stream ( struct smbcli_state * cli , TALLOC_CTX * mem_ctx ,
const char * fname , const char * sname ,
const char * value )
{
int fnum ;
const char * full_name ;
2004-12-04 16:56:25 +03:00
uint8_t * buf ;
2004-11-15 09:55:27 +03:00
ssize_t ret ;
full_name = talloc_asprintf ( mem_ctx , " %s:%s " , fname , sname ) ;
fnum = smbcli_open ( cli - > tree , full_name , O_RDONLY , DENY_NONE ) ;
if ( value = = NULL ) {
if ( fnum ! = - 1 ) {
printf ( " should have failed stream open of %s \n " , full_name ) ;
return False ;
}
return True ;
}
if ( fnum = = - 1 ) {
printf ( " Failed to open stream '%s' - %s \n " ,
full_name , smbcli_errstr ( cli - > tree ) ) ;
return False ;
}
2005-01-06 06:06:58 +03:00
buf = talloc_size ( mem_ctx , strlen ( value ) + 11 ) ;
2004-11-15 09:55:27 +03:00
ret = smbcli_read ( cli - > tree , fnum , buf , 0 , strlen ( value ) + 11 ) ;
if ( ret ! = strlen ( value ) ) {
2005-09-16 11:19:37 +04:00
printf ( " Failed to read %lu bytes from stream '%s' - got %d \n " ,
( long ) strlen ( value ) , full_name , ( int ) ret ) ;
2004-11-15 09:55:27 +03:00
return False ;
}
if ( memcmp ( buf , value , strlen ( value ) ) ! = 0 ) {
printf ( " Bad data in stream \n " ) ;
return False ;
}
smbcli_close ( cli - > tree , fnum ) ;
return True ;
}
/*
test basic io on streams
*/
static BOOL test_stream_io ( struct smbcli_state * cli , TALLOC_CTX * mem_ctx )
{
NTSTATUS status ;
union smb_open io ;
const char * fname = BASEDIR " \\ stream.txt " ;
const char * sname1 , * sname2 ;
BOOL ret = True ;
2004-12-02 07:51:56 +03:00
int fnum = - 1 ;
2004-11-15 09:55:27 +03:00
ssize_t retsize ;
sname1 = talloc_asprintf ( mem_ctx , " %s:%s " , fname , " Stream One " ) ;
2004-11-17 03:39:20 +03:00
sname2 = talloc_asprintf ( mem_ctx , " %s:%s:$DaTa " , fname , " Second Stream " ) ;
2004-11-15 09:55:27 +03:00
printf ( " opening non-existant directory stream \n " ) ;
io . generic . level = RAW_OPEN_NTCREATEX ;
io . ntcreatex . in . root_fid = 0 ;
io . ntcreatex . in . flags = 0 ;
2004-11-30 07:33:27 +03:00
io . ntcreatex . in . access_mask = SEC_FILE_WRITE_DATA ;
2004-11-15 09:55:27 +03:00
io . ntcreatex . in . create_options = NTCREATEX_OPTIONS_DIRECTORY ;
io . ntcreatex . in . file_attr = FILE_ATTRIBUTE_NORMAL ;
2004-11-17 08:58:04 +03:00
io . ntcreatex . in . share_access = 0 ;
2004-11-15 09:55:27 +03:00
io . ntcreatex . in . alloc_size = 0 ;
io . ntcreatex . in . open_disposition = NTCREATEX_DISP_CREATE ;
io . ntcreatex . in . impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS ;
io . ntcreatex . in . security_flags = 0 ;
io . ntcreatex . in . fname = sname1 ;
status = smb_raw_open ( cli - > tree , mem_ctx , & io ) ;
CHECK_STATUS ( status , NT_STATUS_NOT_A_DIRECTORY ) ;
printf ( " creating a stream on a non-existant file \n " ) ;
io . ntcreatex . in . create_options = 0 ;
io . ntcreatex . in . fname = sname1 ;
status = smb_raw_open ( cli - > tree , mem_ctx , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-13 01:48:25 +03:00
fnum = io . ntcreatex . out . file . fnum ;
2004-11-15 09:55:27 +03:00
ret & = check_stream ( cli , mem_ctx , fname , " Stream One " , NULL ) ;
2004-11-17 08:58:04 +03:00
printf ( " check that open of base file is allowed \n " ) ;
io . ntcreatex . in . open_disposition = NTCREATEX_DISP_OPEN ;
io . ntcreatex . in . fname = fname ;
status = smb_raw_open ( cli - > tree , mem_ctx , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-13 01:48:25 +03:00
smbcli_close ( cli - > tree , io . ntcreatex . out . file . fnum ) ;
2004-11-17 08:58:04 +03:00
2004-11-15 09:55:27 +03:00
printf ( " writing to stream \n " ) ;
retsize = smbcli_write ( cli - > tree , fnum , 0 , " test data " , 0 , 9 ) ;
CHECK_VALUE ( retsize , 9 ) ;
smbcli_close ( cli - > tree , fnum ) ;
ret & = check_stream ( cli , mem_ctx , fname , " Stream One " , " test data " ) ;
io . ntcreatex . in . open_disposition = NTCREATEX_DISP_OPEN ;
2004-11-17 08:58:04 +03:00
io . ntcreatex . in . fname = sname1 ;
2004-11-15 09:55:27 +03:00
status = smb_raw_open ( cli - > tree , mem_ctx , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-13 01:48:25 +03:00
fnum = io . ntcreatex . out . file . fnum ;
2004-11-15 09:55:27 +03:00
printf ( " modifying stream \n " ) ;
retsize = smbcli_write ( cli - > tree , fnum , 0 , " MORE DATA " , 5 , 10 ) ;
CHECK_VALUE ( retsize , 10 ) ;
smbcli_close ( cli - > tree , fnum ) ;
2004-11-17 03:39:20 +03:00
ret & = check_stream ( cli , mem_ctx , fname , " Stream One:$FOO " , NULL ) ;
printf ( " creating a stream2 on a existing file \n " ) ;
io . ntcreatex . in . fname = sname2 ;
io . ntcreatex . in . open_disposition = NTCREATEX_DISP_OPEN_IF ;
status = smb_raw_open ( cli - > tree , mem_ctx , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-13 01:48:25 +03:00
fnum = io . ntcreatex . out . file . fnum ;
2004-11-17 03:39:20 +03:00
printf ( " modifying stream \n " ) ;
retsize = smbcli_write ( cli - > tree , fnum , 0 , " SECOND STREAM " , 0 , 13 ) ;
CHECK_VALUE ( retsize , 13 ) ;
smbcli_close ( cli - > tree , fnum ) ;
2004-11-15 09:55:27 +03:00
ret & = check_stream ( cli , mem_ctx , fname , " Stream One " , " test MORE DATA " ) ;
2004-11-17 03:39:20 +03:00
ret & = check_stream ( cli , mem_ctx , fname , " Stream One:$DATA " , " test MORE DATA " ) ;
ret & = check_stream ( cli , mem_ctx , fname , " Stream One: " , NULL ) ;
ret & = check_stream ( cli , mem_ctx , fname , " Second Stream " , " SECOND STREAM " ) ;
ret & = check_stream ( cli , mem_ctx , fname , " Second Stream:$DATA " , " SECOND STREAM " ) ;
ret & = check_stream ( cli , mem_ctx , fname , " Second Stream: " , NULL ) ;
ret & = check_stream ( cli , mem_ctx , fname , " Second Stream:$FOO " , NULL ) ;
2004-11-15 09:55:27 +03:00
printf ( " deleting stream \n " ) ;
status = smbcli_unlink ( cli - > tree , sname1 ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2004-11-17 09:44:50 +03:00
printf ( " delete a stream via delete-on-close \n " ) ;
io . ntcreatex . in . fname = sname2 ;
io . ntcreatex . in . create_options = NTCREATEX_OPTIONS_DELETE_ON_CLOSE ;
io . ntcreatex . in . share_access = NTCREATEX_SHARE_ACCESS_DELETE ;
2004-12-02 07:37:36 +03:00
io . ntcreatex . in . access_mask = SEC_RIGHTS_FILE_ALL ;
2004-11-17 09:44:50 +03:00
io . ntcreatex . in . open_disposition = NTCREATEX_DISP_OPEN ;
status = smb_raw_open ( cli - > tree , mem_ctx , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-13 01:48:25 +03:00
fnum = io . ntcreatex . out . file . fnum ;
2004-11-17 09:44:50 +03:00
smbcli_close ( cli - > tree , fnum ) ;
status = smbcli_unlink ( cli - > tree , sname2 ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ;
2004-11-15 09:55:27 +03:00
printf ( " deleting file \n " ) ;
status = smbcli_unlink ( cli - > tree , fname ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
done :
smbcli_close ( cli - > tree , fnum ) ;
2005-08-04 08:20:43 +04:00
return ret ;
2004-11-15 09:55:27 +03:00
}
/*
basic testing of streams calls
*/
2006-03-25 19:01:28 +03:00
BOOL torture_raw_streams ( struct torture_context * torture )
2004-11-15 09:55:27 +03:00
{
struct smbcli_state * cli ;
BOOL ret = True ;
TALLOC_CTX * mem_ctx ;
2006-07-10 12:00:06 +04:00
if ( ! torture_open_connection ( & cli , 0 ) ) {
2004-11-15 09:55:27 +03:00
return False ;
}
mem_ctx = talloc_init ( " torture_raw_streams " ) ;
if ( ! torture_setup_dir ( cli , BASEDIR ) ) {
return False ;
}
ret & = test_stream_io ( cli , mem_ctx ) ;
smb_raw_exit ( cli - > session ) ;
smbcli_deltree ( cli - > tree , BASEDIR ) ;
torture_close_connection ( cli ) ;
2005-01-27 10:08:20 +03:00
talloc_free ( mem_ctx ) ;
2004-11-15 09:55:27 +03:00
return ret ;
}