2003-08-13 05:53:07 +04:00
/*
Unix SMB / CIFS implementation .
basic raw test suite for change notify
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"
2006-01-03 16:41:17 +03:00
# include "torture/torture.h"
2005-12-28 18:38:36 +03:00
# include "libcli/raw/libcliraw.h"
2006-01-03 18:40:05 +03:00
# include "libcli/libcli.h"
2006-03-09 06:52:10 +03:00
# include "system/filesys.h"
2003-08-13 05:53:07 +04:00
# define BASEDIR "\\test_notify"
# define CHECK_STATUS(status, correct) do { \
if ( ! NT_STATUS_EQUAL ( status , correct ) ) { \
printf ( " (%d) Incorrect status %s - should be %s \n " , \
__LINE__ , nt_errstr ( status ) , nt_errstr ( correct ) ) ; \
ret = False ; \
goto done ; \
} } while ( 0 )
# define CHECK_VAL(v, correct) do { \
if ( ( v ) ! = ( correct ) ) { \
2006-03-09 06:52:10 +03:00
printf ( " (%d) wrong value for %s 0x%x should be 0x%x \n " , \
2003-08-13 05:53:07 +04:00
__LINE__ , # v , ( int ) v , ( int ) correct ) ; \
ret = False ; \
goto done ; \
} } while ( 0 )
# define CHECK_WSTR(field, value, flags) do { \
2004-08-12 01:11:37 +04:00
if ( ! field . s | | strcmp ( field . s , value ) | | wire_bad_flags ( & field , flags , cli ) ) { \
2003-08-13 05:53:07 +04:00
printf ( " (%d) %s [%s] != %s \n " , __LINE__ , # field , field . s , value ) ; \
ret = False ; \
goto done ; \
} } while ( 0 )
/*
basic testing of change notify
*/
2004-10-28 17:40:50 +04:00
BOOL torture_raw_notify ( 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 ;
NTSTATUS status ;
2006-03-10 23:49:20 +03:00
union smb_notify notify ;
2003-08-13 05:53:07 +04:00
union smb_open io ;
2006-03-09 06:52:10 +03:00
int i , count , fnum , fnum2 ;
struct smbcli_request * req , * req2 ;
extern int torture_numops ;
2003-08-13 05:53:07 +04:00
if ( ! torture_open_connection ( & cli ) ) {
return False ;
}
mem_ctx = talloc_init ( " torture_raw_notify " ) ;
2004-11-12 12:37:59 +03:00
if ( ! torture_setup_dir ( cli , BASEDIR ) ) {
return False ;
2003-08-13 05:53:07 +04:00
}
/*
get a handle on the directory
*/
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_ALL ;
2003-08-13 05:53:07 +04:00
io . ntcreatex . in . create_options = NTCREATEX_OPTIONS_DIRECTORY ;
io . ntcreatex . in . file_attr = FILE_ATTRIBUTE_NORMAL ;
io . ntcreatex . in . share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE ;
io . ntcreatex . in . alloc_size = 0 ;
2005-07-04 09:54:09 +04:00
io . ntcreatex . in . open_disposition = NTCREATEX_DISP_OPEN ;
2003-08-13 05:53:07 +04:00
io . ntcreatex . in . impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS ;
io . ntcreatex . in . security_flags = 0 ;
io . ntcreatex . in . fname = BASEDIR ;
status = smb_raw_open ( cli - > tree , mem_ctx , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-10 23:49:20 +03:00
fnum = io . ntcreatex . file . fnum ;
2003-08-13 05:53:07 +04:00
2006-03-09 06:52:10 +03:00
status = smb_raw_open ( cli - > tree , mem_ctx , & io ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-10 23:49:20 +03:00
fnum2 = io . ntcreatex . file . fnum ;
2006-03-09 06:52:10 +03:00
2003-08-13 05:53:07 +04:00
/* ask for a change notify */
2006-03-10 23:49:20 +03:00
notify . notify . in . buffer_size = 1000 ;
notify . notify . in . completion_filter = 0x3 ;
notify . notify . file . fnum = fnum ;
notify . notify . in . recursive = True ;
2003-08-13 05:53:07 +04:00
printf ( " testing notify mkdir \n " ) ;
req = smb_raw_changenotify_send ( cli - > tree , & notify ) ;
2004-08-04 17:23:35 +04:00
smbcli_mkdir ( cli - > tree , BASEDIR " \\ subdir-name " ) ;
2003-08-13 05:53:07 +04:00
status = smb_raw_changenotify_recv ( req , mem_ctx , & notify ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-10 23:49:20 +03:00
CHECK_VAL ( notify . notify . out . num_changes , 1 ) ;
CHECK_VAL ( notify . notify . out . changes [ 0 ] . action , NOTIFY_ACTION_ADDED ) ;
CHECK_WSTR ( notify . notify . out . changes [ 0 ] . name , " subdir-name " , STR_UNICODE ) ;
2003-08-13 05:53:07 +04:00
printf ( " testing notify rmdir \n " ) ;
req = smb_raw_changenotify_send ( cli - > tree , & notify ) ;
2004-08-04 17:23:35 +04:00
smbcli_rmdir ( cli - > tree , BASEDIR " \\ subdir-name " ) ;
2003-08-13 05:53:07 +04:00
status = smb_raw_changenotify_recv ( req , mem_ctx , & notify ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-10 23:49:20 +03:00
CHECK_VAL ( notify . notify . out . num_changes , 1 ) ;
CHECK_VAL ( notify . notify . out . changes [ 0 ] . action , NOTIFY_ACTION_REMOVED ) ;
CHECK_WSTR ( notify . notify . out . changes [ 0 ] . name , " subdir-name " , STR_UNICODE ) ;
2003-08-13 05:53:07 +04:00
printf ( " testing notify cancel \n " ) ;
req = smb_raw_changenotify_send ( cli - > tree , & notify ) ;
smb_raw_ntcancel ( req ) ;
2004-08-04 17:23:35 +04:00
smbcli_mkdir ( cli - > tree , BASEDIR " \\ subdir-name " ) ;
2003-08-13 05:53:07 +04:00
status = smb_raw_changenotify_recv ( req , mem_ctx , & notify ) ;
CHECK_STATUS ( status , NT_STATUS_CANCELLED ) ;
2006-03-09 06:52:10 +03:00
count = torture_numops ;
printf ( " testing buffered notify on create of %d files \n " , count ) ;
for ( i = 0 ; i < count ; i + + ) {
char * fname = talloc_asprintf ( cli , BASEDIR " \\ test%d.txt " , i ) ;
int fnum3 = smbcli_open ( cli - > tree , fname , O_CREAT | O_RDWR , DENY_NONE ) ;
if ( fnum3 = = - 1 ) {
printf ( " Failed to create %s - %s \n " ,
fname , smbcli_errstr ( cli - > tree ) ) ;
ret = False ;
goto done ;
}
talloc_free ( fname ) ;
smbcli_close ( cli - > tree , fnum3 ) ;
}
/* setup a new notify on a different directory handle. This
new notify won ' t see the events above . */
2006-03-10 23:49:20 +03:00
notify . notify . file . fnum = fnum2 ;
2006-03-09 06:52:10 +03:00
req2 = smb_raw_changenotify_send ( cli - > tree , & notify ) ;
/* whereas this notify will see the above buffered events as
well */
2006-03-10 23:49:20 +03:00
notify . notify . file . fnum = fnum ;
2006-03-09 06:52:10 +03:00
req = smb_raw_changenotify_send ( cli - > tree , & notify ) ;
status = smbcli_unlink ( cli - > tree , BASEDIR " \\ test0.txt " ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
/* receive the reply from the 2nd notify */
status = smb_raw_changenotify_recv ( req , mem_ctx , & notify ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-10 23:49:20 +03:00
CHECK_VAL ( notify . notify . out . num_changes , count + 1 ) ;
for ( i = 0 ; i < notify . notify . out . num_changes ; i + + ) {
CHECK_VAL ( notify . notify . out . changes [ i ] . action , NOTIFY_ACTION_ADDED ) ;
2006-03-09 06:52:10 +03:00
}
2006-03-10 23:49:20 +03:00
CHECK_WSTR ( notify . notify . out . changes [ 0 ] . name , " subdir-name " , STR_UNICODE ) ;
2006-03-09 06:52:10 +03:00
/* and now from the 1st notify */
status = smb_raw_changenotify_recv ( req2 , mem_ctx , & notify ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-10 23:49:20 +03:00
CHECK_VAL ( notify . notify . out . num_changes , 1 ) ;
2006-03-09 06:52:10 +03:00
printf ( " testing notify on wildcard unlink for %d files \n " , count ) ;
req = smb_raw_changenotify_send ( cli - > tree , & notify ) ;
status = smbcli_unlink ( cli - > tree , BASEDIR " \\ test*.txt " ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
status = smb_raw_changenotify_recv ( req , mem_ctx , & notify ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-10 23:49:20 +03:00
CHECK_VAL ( notify . notify . out . num_changes , 1 ) ;
CHECK_VAL ( notify . notify . out . changes [ 0 ] . action , NOTIFY_ACTION_REMOVED ) ;
CHECK_WSTR ( notify . notify . out . changes [ 0 ] . name , " test0.txt " , STR_UNICODE ) ;
2006-03-09 06:52:10 +03:00
/* and we now see the rest of the unlink calls */
req = smb_raw_changenotify_send ( cli - > tree , & notify ) ;
status = smb_raw_changenotify_recv ( req , mem_ctx , & notify ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-10 23:49:20 +03:00
CHECK_VAL ( notify . notify . out . num_changes , count - 1 ) ;
for ( i = 0 ; i < notify . notify . out . num_changes ; i + + ) {
CHECK_VAL ( notify . notify . out . changes [ i ] . action , NOTIFY_ACTION_REMOVED ) ;
2006-03-09 06:52:10 +03:00
}
2006-03-10 23:49:20 +03:00
notify . notify . file . fnum = fnum2 ;
2006-03-09 06:52:10 +03:00
req = smb_raw_changenotify_send ( cli - > tree , & notify ) ;
status = smb_raw_changenotify_recv ( req , mem_ctx , & notify ) ;
CHECK_STATUS ( status , NT_STATUS_OK ) ;
2006-03-10 23:49:20 +03:00
CHECK_VAL ( notify . notify . out . num_changes , count - 1 ) ;
for ( i = 0 ; i < notify . notify . out . num_changes ; i + + ) {
CHECK_VAL ( notify . notify . out . changes [ i ] . action , NOTIFY_ACTION_REMOVED ) ;
2006-03-09 06:52:10 +03:00
}
2003-08-13 05:53:07 +04:00
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
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 ;
}