2006-09-22 08:04:46 +04:00
/*
Unix SMB / CIFS implementation .
test suite for SMB2 write operations
Copyright ( C ) Andrew Tridgell 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
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2006-09-22 08:04:46 +04:00
( 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
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2006-09-22 08:04:46 +04:00
*/
# include "includes.h"
# include "librpc/gen_ndr/security.h"
# include "libcli/smb2/smb2.h"
# include "libcli/smb2/smb2_calls.h"
# include "torture/torture.h"
# include "torture/smb2/proto.h"
2006-09-23 06:32:47 +04:00
# define FNAME "testmaxwrite.dat"
2006-09-22 08:04:46 +04:00
/*
test writing
*/
2007-12-03 02:28:22 +03:00
static NTSTATUS torture_smb2_write ( struct torture_context * tctx ,
2006-09-23 06:32:47 +04:00
struct smb2_tree * tree ,
struct smb2_handle handle )
2006-09-22 08:04:46 +04:00
{
struct smb2_write w ;
struct smb2_read r ;
NTSTATUS status ;
int i , len ;
2006-09-23 06:19:15 +04:00
int max = 80000000 ;
2006-09-22 08:04:46 +04:00
int min = 1 ;
while ( max > min ) {
2007-12-03 02:28:22 +03:00
TALLOC_CTX * tmp_ctx = talloc_new ( tctx ) ;
2006-09-22 08:04:46 +04:00
len = 1 + ( min + max ) / 2 ;
ZERO_STRUCT ( w ) ;
w . in . file . handle = handle ;
w . in . offset = 0 ;
w . in . data = data_blob_talloc ( tmp_ctx , NULL , len ) ;
for ( i = 0 ; i < len ; i + + ) {
w . in . data . data [ i ] = i % 256 ;
}
2016-02-22 17:46:24 +03:00
torture_comment ( tctx , " trying to write %d bytes (min=%d max=%d) \n " ,
2006-09-22 08:04:46 +04:00
len , min , max ) ;
status = smb2_write ( tree , & w ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2016-02-22 17:46:24 +03:00
torture_comment ( tctx , " write failed - %s \n " , nt_errstr ( status ) ) ;
2006-09-22 08:04:46 +04:00
max = len - 1 ;
2006-09-23 06:32:47 +04:00
status = smb2_util_close ( tree , handle ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
/* vista bug */
2016-02-22 17:46:24 +03:00
torture_comment ( tctx , " coping with server disconnect \n " ) ;
2006-09-23 06:32:47 +04:00
talloc_free ( tree ) ;
2016-02-22 17:46:24 +03:00
if ( ! torture_smb2_connection ( tctx , & tree ) ) {
torture_comment ( tctx , " failed to reconnect \n " ) ;
2006-09-23 06:32:47 +04:00
return NT_STATUS_NET_WRITE_FAULT ;
}
2006-09-22 08:04:46 +04:00
}
2016-02-26 19:25:58 +03:00
status = torture_smb2_createfile ( tctx , tree , FNAME , & handle ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
torture_comment ( tctx , " failed to create file handle \n " ) ;
talloc_free ( tmp_ctx ) ;
return status ;
}
2006-09-22 08:04:46 +04:00
continue ;
} else {
min = len ;
}
ZERO_STRUCT ( r ) ;
r . in . file . handle = handle ;
r . in . length = len ;
r . in . offset = 0 ;
2016-02-22 17:46:24 +03:00
torture_comment ( tctx , " reading %d bytes \n " , len ) ;
2006-09-22 08:04:46 +04:00
status = smb2_read ( tree , tmp_ctx , & r ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
2016-02-22 17:46:24 +03:00
torture_comment ( tctx , " read failed - %s \n " , nt_errstr ( status ) ) ;
2006-09-22 08:04:46 +04:00
} else if ( w . in . data . length ! = r . out . data . length | |
memcmp ( w . in . data . data , r . out . data . data , len ) ! = 0 ) {
2016-02-22 17:46:24 +03:00
torture_comment ( tctx , " read data mismatch \n " ) ;
2006-09-22 08:04:46 +04:00
}
talloc_free ( tmp_ctx ) ;
}
2016-02-22 17:46:24 +03:00
torture_comment ( tctx , " converged: len=%d \n " , max ) ;
2006-09-23 06:32:47 +04:00
smb2_util_close ( tree , handle ) ;
smb2_util_unlink ( tree , FNAME ) ;
2006-09-22 08:04:46 +04:00
2006-09-23 06:19:15 +04:00
return NT_STATUS_OK ;
2006-09-22 08:04:46 +04:00
}
/*
basic testing of SMB2 connection calls
*/
2016-02-22 17:46:24 +03:00
bool torture_smb2_maxwrite ( struct torture_context * tctx )
2006-09-22 08:04:46 +04:00
{
struct smb2_tree * tree ;
struct smb2_handle h1 ;
2016-02-22 17:46:24 +03:00
if ( ! torture_smb2_connection ( tctx , & tree ) ) {
2007-10-08 18:22:10 +04:00
return false ;
2006-09-22 08:04:46 +04:00
}
2016-02-22 17:46:24 +03:00
torture_assert_ntstatus_ok ( tctx ,
torture_smb2_createfile ( tctx , tree , FNAME , & h1 ) ,
" failed to create file handle " ) ;
torture_assert_ntstatus_ok ( tctx ,
torture_smb2_write ( tctx , tree , h1 ) ,
" Write failed " ) ;
2006-09-22 08:04:46 +04:00
2007-10-08 18:22:10 +04:00
return true ;
2006-09-22 08:04:46 +04:00
}