2003-08-13 05:53:07 +04:00
/*
Unix SMB / CIFS implementation .
unlink test suite
Copyright ( C ) Andrew Tridgell 2003
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"
2005-02-10 08:09:35 +03:00
# include "system/filesys.h"
2005-08-03 21:10:26 +04:00
# include "librpc/gen_ndr/ndr_security.h"
2003-08-13 05:53:07 +04:00
# define CHECK_STATUS(status, correct) do { \
if ( ! NT_STATUS_EQUAL ( status , correct ) ) { \
2004-10-25 09:26:23 +04:00
printf ( " (%s) Incorrect status %s - should be %s \n " , \
__location__ , nt_errstr ( status ) , nt_errstr ( correct ) ) ; \
2003-08-13 05:53:07 +04:00
ret = False ; \
goto done ; \
} } while ( 0 )
# define BASEDIR "\\testunlink"
/*
test unlink ops
*/
2004-08-04 17:23:35 +04:00
static BOOL test_unlink ( struct smbcli_state * cli , TALLOC_CTX * mem_ctx )
2003-08-13 05:53:07 +04:00
{
struct smb_unlink io ;
NTSTATUS status ;
BOOL ret = True ;
const char * fname = BASEDIR " \\ test.txt " ;
2004-11-12 12:37:59 +03:00
if ( ! torture_setup_dir ( cli , BASEDIR ) ) {
2003-08-13 05:53:07 +04:00
return False ;
}
printf ( " Trying non-existant file \n " ) ;
io . in . pattern = fname ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ;
2004-08-04 17:23:35 +04:00
smbcli_close ( cli - > tree , smbcli_open ( cli - > tree , fname , O_RDWR | O_CREAT , DENY_NONE ) ) ;
2003-08-13 05:53:07 +04:00
io . in . pattern = fname ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
printf ( " Trying a hidden file \n " ) ;
2004-08-04 17:23:35 +04:00
smbcli_close ( cli - > tree , smbcli_open ( cli - > tree , fname , O_RDWR | O_CREAT , DENY_NONE ) ) ;
2003-08-13 05:53:07 +04:00
torture_set_file_attribute ( cli - > tree , fname , FILE_ATTRIBUTE_HIDDEN ) ;
io . in . pattern = fname ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_NO_SUCH_FILE ) ;
io . in . pattern = fname ;
io . in . attrib = FILE_ATTRIBUTE_HIDDEN ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2004-10-27 02:44:28 +04:00
io . in . pattern = fname ;
io . in . attrib = FILE_ATTRIBUTE_HIDDEN ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ;
2003-08-13 05:53:07 +04:00
printf ( " Trying a directory \n " ) ;
io . in . pattern = BASEDIR ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_FILE_IS_A_DIRECTORY ) ;
io . in . pattern = BASEDIR ;
io . in . attrib = FILE_ATTRIBUTE_DIRECTORY ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_FILE_IS_A_DIRECTORY ) ;
printf ( " Trying a bad path \n " ) ;
io . in . pattern = " .. " ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_PATH_SYNTAX_BAD ) ;
2003-08-13 06:04:44 +04:00
io . in . pattern = " \\ .. " ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_PATH_SYNTAX_BAD ) ;
2004-10-26 09:33:23 +04:00
io . in . pattern = BASEDIR " \\ .. \\ .. " ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_PATH_SYNTAX_BAD ) ;
2003-08-13 06:04:44 +04:00
io . in . pattern = BASEDIR " \\ .. " ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_FILE_IS_A_DIRECTORY ) ;
2003-08-13 05:53:07 +04:00
printf ( " Trying wildcards \n " ) ;
2004-08-04 17:23:35 +04:00
smbcli_close ( cli - > tree , smbcli_open ( cli - > tree , fname , O_RDWR | O_CREAT , DENY_NONE ) ) ;
2003-08-13 05:53:07 +04:00
io . in . pattern = BASEDIR " \\ t*.t " ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_NO_SUCH_FILE ) ;
2004-10-26 09:33:23 +04:00
io . in . pattern = BASEDIR " \\ z* " ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_NO_SUCH_FILE ) ;
io . in . pattern = BASEDIR " \\ z* " ;
io . in . attrib = FILE_ATTRIBUTE_DIRECTORY ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_NO_SUCH_FILE ) ;
2003-08-13 05:53:07 +04:00
io . in . pattern = BASEDIR " \\ * " ;
io . in . attrib = FILE_ATTRIBUTE_DIRECTORY ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_NAME_INVALID ) ;
2004-10-26 09:33:23 +04:00
io . in . pattern = BASEDIR " \\ ? " ;
io . in . attrib = FILE_ATTRIBUTE_DIRECTORY ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_NAME_INVALID ) ;
io . in . pattern = BASEDIR " \\ t* " ;
io . in . attrib = FILE_ATTRIBUTE_DIRECTORY ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
smbcli_close ( cli - > tree , smbcli_open ( cli - > tree , fname , O_RDWR | O_CREAT , DENY_NONE ) ) ;
2003-08-13 05:53:07 +04:00
io . in . pattern = BASEDIR " \\ *.dat " ;
io . in . attrib = FILE_ATTRIBUTE_DIRECTORY ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_NO_SUCH_FILE ) ;
io . in . pattern = BASEDIR " \\ *.tx? " ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_NO_SUCH_FILE ) ;
done :
smb_raw_exit ( cli - > session ) ;
2004-08-04 17:23:35 +04:00
smbcli_deltree ( cli - > tree , BASEDIR ) ;
2003-08-13 05:53:07 +04:00
return ret ;
}
2005-07-01 16:47:10 +04:00
/*
test delete on close
*/
static BOOL test_delete_on_close ( struct smbcli_state * cli , TALLOC_CTX * mem_ctx )
{
2005-08-03 21:10:26 +04:00
union smb_open op ;
2005-07-01 16:47:10 +04:00
struct smb_unlink io ;
struct smb_rmdir dio ;
NTSTATUS status ;
BOOL ret = True ;
2005-08-03 20:50:12 +04:00
int fnum , fnum2 ;
2005-07-01 16:47:10 +04:00
const char * fname = BASEDIR " \\ test.txt " ;
const char * dname = BASEDIR " \\ test.dir " ;
2005-08-03 20:50:12 +04:00
const char * inside = BASEDIR " \\ test.dir \\ test.txt " ;
2005-07-01 16:47:10 +04:00
union smb_setfileinfo sfinfo ;
if ( ! torture_setup_dir ( cli , BASEDIR ) ) {
return False ;
}
dio . in . path = dname ;
io . in . pattern = fname ;
io . in . attrib = 0 ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ;
printf ( " Testing with delete_on_close 0 \n " ) ;
fnum = create_complex_file ( cli , mem_ctx , fname ) ;
sfinfo . disposition_info . level = RAW_SFILEINFO_DISPOSITION_INFORMATION ;
sfinfo . disposition_info . file . fnum = fnum ;
sfinfo . disposition_info . in . delete_on_close = 0 ;
status = smb_raw_setfileinfo ( cli - > tree , & sfinfo ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
smbcli_close ( cli - > tree , fnum ) ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
printf ( " Testing with delete_on_close 1 \n " ) ;
fnum = create_complex_file ( cli , mem_ctx , fname ) ;
sfinfo . disposition_info . file . fnum = fnum ;
sfinfo . disposition_info . in . delete_on_close = 1 ;
status = smb_raw_setfileinfo ( cli - > tree , & sfinfo ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
smbcli_close ( cli - > tree , fnum ) ;
status = smb_raw_unlink ( cli - > tree , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ;
printf ( " Testing with directory and delete_on_close 0 \n " ) ;
fnum = create_directory_handle ( cli - > tree , dname ) ;
sfinfo . disposition_info . level = RAW_SFILEINFO_DISPOSITION_INFORMATION ;
sfinfo . disposition_info . file . fnum = fnum ;
sfinfo . disposition_info . in . delete_on_close = 0 ;
status = smb_raw_setfileinfo ( cli - > tree , & sfinfo ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
smbcli_close ( cli - > tree , fnum ) ;
status = smb_raw_rmdir ( cli - > tree , & dio ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
printf ( " Testing with directory delete_on_close 1 \n " ) ;
fnum = create_directory_handle ( cli - > tree , dname ) ;
sfinfo . disposition_info . file . fnum = fnum ;
sfinfo . disposition_info . in . delete_on_close = 1 ;
status = smb_raw_setfileinfo ( cli - > tree , & sfinfo ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
smbcli_close ( cli - > tree , fnum ) ;
status = smb_raw_rmdir ( cli - > tree , & dio ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ;
2005-08-03 20:50:12 +04:00
printf ( " Testing with non-empty directory delete_on_close \n " ) ;
fnum = create_directory_handle ( cli - > tree , dname ) ;
fnum2 = create_complex_file ( cli , mem_ctx , inside ) ;
sfinfo . disposition_info . file . fnum = fnum ;
sfinfo . disposition_info . in . delete_on_close = 1 ;
status = smb_raw_setfileinfo ( cli - > tree , & sfinfo ) ;
CHECK_STATUS ( status , NT_STATUS_DIRECTORY_NOT_EMPTY ) ;
sfinfo . disposition_info . file . fnum = fnum2 ;
status = smb_raw_setfileinfo ( cli - > tree , & sfinfo ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
sfinfo . disposition_info . file . fnum = fnum ;
status = smb_raw_setfileinfo ( cli - > tree , & sfinfo ) ;
CHECK_STATUS ( status , NT_STATUS_DIRECTORY_NOT_EMPTY ) ;
smbcli_close ( cli - > tree , fnum2 ) ;
status = smb_raw_setfileinfo ( cli - > tree , & sfinfo ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
smbcli_close ( cli - > tree , fnum ) ;
status = smb_raw_rmdir ( cli - > tree , & dio ) ;
CHECK_STATUS ( status , NT_STATUS_OBJECT_NAME_NOT_FOUND ) ;
2005-08-03 21:10:26 +04:00
printf ( " Testing open dir with delete_on_close \n " ) ;
fnum = create_directory_handle ( cli - > tree , dname ) ;
smbcli_close ( cli - > tree , fnum ) ;
fnum2 = create_complex_file ( cli , mem_ctx , inside ) ;
smbcli_close ( cli - > tree , fnum2 ) ;
op . generic . level = RAW_OPEN_NTCREATEX ;
op . ntcreatex . in . root_fid = 0 ;
op . ntcreatex . in . flags = 0 ;
op . ntcreatex . in . access_mask = SEC_RIGHTS_FILE_ALL ;
op . ntcreatex . in . create_options = NTCREATEX_OPTIONS_DIRECTORY | NTCREATEX_OPTIONS_DELETE_ON_CLOSE ;
op . ntcreatex . in . file_attr = FILE_ATTRIBUTE_NORMAL ;
op . ntcreatex . in . share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE ;
op . ntcreatex . in . alloc_size = 0 ;
op . ntcreatex . in . open_disposition = NTCREATEX_DISP_OPEN ;
op . ntcreatex . in . impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS ;
op . ntcreatex . in . security_flags = 0 ;
op . ntcreatex . in . fname = dname ;
status = smb_raw_open ( cli - > tree , mem_ctx , & op ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
fnum = op . ntcreatex . out . fnum ;
smbcli_close ( cli - > tree , fnum ) ;
status = smb_raw_rmdir ( cli - > tree , & dio ) ;
CHECK_STATUS ( status , NT_STATUS_DIRECTORY_NOT_EMPTY ) ;
printf ( " Testing double open dir with second delete_on_close \n " ) ;
fnum = create_directory_handle ( cli - > tree , dname ) ;
fnum2 = create_complex_file ( cli , mem_ctx , inside ) ;
smbcli_close ( cli - > tree , fnum2 ) ;
op . generic . level = RAW_OPEN_NTCREATEX ;
op . ntcreatex . in . root_fid = 0 ;
op . ntcreatex . in . flags = 0 ;
op . ntcreatex . in . access_mask = SEC_RIGHTS_FILE_ALL ;
op . ntcreatex . in . create_options = NTCREATEX_OPTIONS_DIRECTORY | NTCREATEX_OPTIONS_DELETE_ON_CLOSE ;
op . ntcreatex . in . file_attr = FILE_ATTRIBUTE_NORMAL ;
op . ntcreatex . in . share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE ;
op . ntcreatex . in . alloc_size = 0 ;
op . ntcreatex . in . open_disposition = NTCREATEX_DISP_OPEN ;
op . ntcreatex . in . impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS ;
op . ntcreatex . in . security_flags = 0 ;
op . ntcreatex . in . fname = dname ;
status = smb_raw_open ( cli - > tree , mem_ctx , & op ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
fnum2 = op . ntcreatex . out . fnum ;
smbcli_close ( cli - > tree , fnum2 ) ;
smbcli_close ( cli - > tree , fnum ) ;
status = smb_raw_rmdir ( cli - > tree , & dio ) ;
CHECK_STATUS ( status , NT_STATUS_DIRECTORY_NOT_EMPTY ) ;
printf ( " Testing pre-existing open dir with second delete_on_close \n " ) ;
fnum = create_directory_handle ( cli - > tree , dname ) ;
smbcli_close ( cli - > tree , fnum ) ;
fnum = create_complex_file ( cli , mem_ctx , inside ) ;
smbcli_close ( cli - > tree , fnum ) ;
/* we have a dir with a file in it, no handles open */
op . generic . level = RAW_OPEN_NTCREATEX ;
op . ntcreatex . in . root_fid = 0 ;
op . ntcreatex . in . flags = 0 ;
op . ntcreatex . in . access_mask = SEC_RIGHTS_FILE_ALL ;
op . ntcreatex . in . create_options = NTCREATEX_OPTIONS_DIRECTORY | NTCREATEX_OPTIONS_DELETE_ON_CLOSE ;
op . ntcreatex . in . file_attr = FILE_ATTRIBUTE_NORMAL ;
op . ntcreatex . in . share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE | NTCREATEX_SHARE_ACCESS_DELETE ;
op . ntcreatex . in . alloc_size = 0 ;
op . ntcreatex . in . open_disposition = NTCREATEX_DISP_OPEN ;
op . ntcreatex . in . impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS ;
op . ntcreatex . in . security_flags = 0 ;
op . ntcreatex . in . fname = dname ;
status = smb_raw_open ( cli - > tree , mem_ctx , & op ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
fnum = op . ntcreatex . out . fnum ;
/* open without delete on close */
op . ntcreatex . in . create_options = NTCREATEX_OPTIONS_DIRECTORY ;
status = smb_raw_open ( cli - > tree , mem_ctx , & op ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
fnum2 = op . ntcreatex . out . fnum ;
/* close 2nd file handle */
smbcli_close ( cli - > tree , fnum2 ) ;
status = smb_raw_rmdir ( cli - > tree , & dio ) ;
CHECK_STATUS ( status , NT_STATUS_DIRECTORY_NOT_EMPTY ) ;
smbcli_close ( cli - > tree , fnum ) ;
status = smb_raw_rmdir ( cli - > tree , & dio ) ;
CHECK_STATUS ( status , NT_STATUS_DIRECTORY_NOT_EMPTY ) ;
2005-07-01 16:47:10 +04:00
done :
smb_raw_exit ( cli - > session ) ;
smbcli_deltree ( cli - > tree , BASEDIR ) ;
return ret ;
}
2003-08-13 05:53:07 +04:00
/*
basic testing of unlink calls
*/
2004-10-28 17:40:50 +04:00
BOOL torture_raw_unlink ( void )
2003-08-13 05:53:07 +04:00
{
2004-08-04 17:23:35 +04:00
struct smbcli_state * cli ;
2003-08-13 05:53:07 +04:00
BOOL ret = True ;
TALLOC_CTX * mem_ctx ;
if ( ! torture_open_connection ( & cli ) ) {
return False ;
}
mem_ctx = talloc_init ( " torture_raw_unlink " ) ;
2005-07-01 16:47:10 +04:00
ret & = test_unlink ( cli , mem_ctx ) ;
ret & = test_delete_on_close ( cli , mem_ctx ) ;
2003-08-13 05:53:07 +04:00
torture_close_connection ( cli ) ;
2005-01-27 10:08:20 +03:00
talloc_free ( mem_ctx ) ;
2003-08-13 05:53:07 +04:00
return ret ;
}