2003-11-03 10:26:30 +03:00
/*
Unix SMB / CIFS implementation .
test suite for echo rpc operations
Copyright ( C ) Andrew Tridgell 2003
2005-01-11 19:53:02 +03:00
Copyright ( C ) Stefan ( metze ) Metzmacher 2005
2005-02-19 02:43:40 +03:00
Copyright ( C ) Jelmer Vernooij 2005
2003-11-03 10:26:30 +03:00
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-02-03 14:56:03 +03:00
# include "lib/events/events.h"
2004-11-01 13:30:34 +03:00
# include "librpc/gen_ndr/ndr_echo.h"
2003-11-03 10:26:30 +03:00
2003-11-03 11:37:48 +03:00
/*
test the AddOne interface
*/
2005-08-01 04:58:33 +04:00
# define TEST_ADDONE(value) do { \
n = i = value ; \
r . in . in_data = n ; \
r . out . out_data = & n ; \
status = dcerpc_echo_AddOne ( p , mem_ctx , & r ) ; \
if ( ! NT_STATUS_IS_OK ( status ) ) { \
printf ( " AddOne(%d) failed - %s \n " , i , nt_errstr ( status ) ) ; \
return False ; \
} \
if ( n ! = i + 1 ) { \
printf ( " %d + 1 != %u (should be %u) \n " , i , n , i + 1 ) ; \
ret = False ; \
} else { \
printf ( " %d + 1 = %u \n " , i , n ) ; \
} \
} while ( 0 )
2003-11-03 11:37:48 +03:00
static BOOL test_addone ( struct dcerpc_pipe * p , TALLOC_CTX * mem_ctx )
{
2005-08-01 04:58:33 +04:00
BOOL ret = True ;
uint32_t i ;
2003-11-03 11:37:48 +03:00
NTSTATUS status ;
2005-08-01 04:58:33 +04:00
uint32_t n ;
struct echo_AddOne r ;
2003-11-03 11:37:48 +03:00
printf ( " \n Testing AddOne \n " ) ;
for ( i = 0 ; i < 10 ; i + + ) {
2005-08-01 04:58:33 +04:00
TEST_ADDONE ( i ) ;
2003-11-03 11:37:48 +03:00
}
2005-08-01 04:58:33 +04:00
TEST_ADDONE ( 0x7FFFFFFE ) ;
TEST_ADDONE ( 0xFFFFFFFE ) ;
TEST_ADDONE ( 0xFFFFFFFF ) ;
TEST_ADDONE ( random ( ) & 0xFFFFFFFF ) ;
return ret ;
2003-11-03 11:37:48 +03:00
}
2003-11-03 12:18:38 +03:00
/*
test the EchoData interface
*/
static BOOL test_echodata ( struct dcerpc_pipe * p , TALLOC_CTX * mem_ctx )
{
int i ;
NTSTATUS status ;
2004-11-25 21:01:40 +03:00
uint8_t * data_in , * data_out ;
2003-11-04 12:10:31 +03:00
int len = 1 + ( random ( ) % 5000 ) ;
2003-11-09 10:24:06 +03:00
struct echo_EchoData r ;
2003-11-03 12:18:38 +03:00
printf ( " \n Testing EchoData \n " ) ;
2005-01-06 06:06:58 +03:00
data_in = talloc_size ( mem_ctx , len ) ;
data_out = talloc_size ( mem_ctx , len ) ;
2003-11-03 12:18:38 +03:00
for ( i = 0 ; i < len ; i + + ) {
2003-11-09 10:24:06 +03:00
data_in [ i ] = i ;
2003-11-03 12:18:38 +03:00
}
2003-11-09 10:24:06 +03:00
r . in . len = len ;
2003-11-13 12:26:53 +03:00
r . in . in_data = data_in ;
2003-11-03 12:18:38 +03:00
2003-11-09 10:24:06 +03:00
status = dcerpc_echo_EchoData ( p , mem_ctx , & r ) ;
2003-11-03 12:18:38 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2003-11-04 05:28:08 +03:00
printf ( " EchoData(%d) failed - %s \n " , len , nt_errstr ( status ) ) ;
2003-11-03 12:18:38 +03:00
return False ;
}
2003-11-13 12:26:53 +03:00
data_out = r . out . out_data ;
for ( i = 0 ; i < len ; i + + ) {
if ( data_in [ i ] ! = data_out [ i ] ) {
printf ( " Bad data returned for len %d at offset %d \n " ,
len , i ) ;
printf ( " in: \n " ) ;
2003-11-13 13:29:58 +03:00
dump_data ( 0 , data_in + i , MIN ( len - i , 16 ) ) ;
2003-11-13 12:26:53 +03:00
printf ( " out: \n " ) ;
2003-11-13 13:29:58 +03:00
dump_data ( 0 , data_out + i , MIN ( len - 1 , 16 ) ) ;
2003-11-13 12:26:53 +03:00
return False ;
}
2003-11-03 12:18:38 +03:00
}
return True ;
}
2003-11-03 13:01:20 +03:00
/*
test the SourceData interface
*/
static BOOL test_sourcedata ( struct dcerpc_pipe * p , TALLOC_CTX * mem_ctx )
{
int i ;
NTSTATUS status ;
2006-01-10 06:18:33 +03:00
int len ;
2003-11-09 10:24:06 +03:00
struct echo_SourceData r ;
2003-11-03 13:01:20 +03:00
2006-01-10 06:18:33 +03:00
if ( lp_parm_bool ( - 1 , " torture " , " quick " , False ) & &
( p - > conn - > flags & DCERPC_DEBUG_VALIDATE_BOTH ) ) {
len = 100 + ( random ( ) % 5000 ) ;
} else {
len = 200000 + ( random ( ) % 5000 ) ;
}
2003-11-03 13:01:20 +03:00
printf ( " \n Testing SourceData \n " ) ;
2003-11-09 10:24:06 +03:00
r . in . len = len ;
status = dcerpc_echo_SourceData ( p , mem_ctx , & r ) ;
2003-11-03 13:01:20 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2003-11-04 05:28:08 +03:00
printf ( " SourceData(%d) failed - %s \n " , len , nt_errstr ( status ) ) ;
2003-11-03 13:01:20 +03:00
return False ;
}
for ( i = 0 ; i < len ; i + + ) {
2005-06-02 17:21:11 +04:00
uint8_t * v = ( uint8_t * ) r . out . data ;
2003-11-04 05:28:08 +03:00
if ( v [ i ] ! = ( i & 0xFF ) ) {
2005-06-02 17:21:11 +04:00
printf ( " bad data 0x%x at %d \n " , ( uint8_t ) r . out . data [ i ] , i ) ;
2003-11-03 13:01:20 +03:00
return False ;
}
}
return True ;
}
/*
test the SinkData interface
*/
static BOOL test_sinkdata ( struct dcerpc_pipe * p , TALLOC_CTX * mem_ctx )
{
int i ;
NTSTATUS status ;
2004-11-25 21:01:40 +03:00
uint8_t * data_in ;
2006-01-10 06:18:33 +03:00
int len ;
2003-11-09 10:24:06 +03:00
struct echo_SinkData r ;
2003-11-03 13:01:20 +03:00
2006-01-10 06:18:33 +03:00
if ( lp_parm_bool ( - 1 , " torture " , " quick " , False ) & &
( p - > conn - > flags & DCERPC_DEBUG_VALIDATE_BOTH ) ) {
len = 100 + ( random ( ) % 5000 ) ;
} else {
len = 200000 + ( random ( ) % 5000 ) ;
}
2003-11-03 13:01:20 +03:00
printf ( " \n Testing SinkData \n " ) ;
2005-01-06 06:06:58 +03:00
data_in = talloc_size ( mem_ctx , len ) ;
2003-11-03 13:01:20 +03:00
for ( i = 0 ; i < len ; i + + ) {
data_in [ i ] = i + 1 ;
}
2003-11-09 10:24:06 +03:00
r . in . len = len ;
r . in . data = data_in ;
status = dcerpc_echo_SinkData ( p , mem_ctx , & r ) ;
2003-11-03 13:01:20 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
2003-11-04 05:28:08 +03:00
printf ( " SinkData(%d) failed - %s \n " , len , nt_errstr ( status ) ) ;
2003-11-03 13:01:20 +03:00
return False ;
}
2003-11-04 12:10:31 +03:00
printf ( " sunk %d bytes \n " , len ) ;
2003-11-03 13:01:20 +03:00
return True ;
}
2003-11-13 12:26:53 +03:00
/*
test the testcall interface
*/
static BOOL test_testcall ( struct dcerpc_pipe * p , TALLOC_CTX * mem_ctx )
{
NTSTATUS status ;
2003-12-14 16:22:12 +03:00
struct echo_TestCall r ;
2003-11-17 12:34:19 +03:00
2003-11-19 15:03:11 +03:00
r . in . s1 = " input string " ;
2003-11-13 12:26:53 +03:00
printf ( " \n Testing TestCall \n " ) ;
2003-12-14 16:22:12 +03:00
status = dcerpc_echo_TestCall ( p , mem_ctx , & r ) ;
2003-11-13 12:26:53 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " TestCall failed - %s \n " , nt_errstr ( status ) ) ;
return False ;
}
return True ;
}
2003-11-19 15:03:11 +03:00
/*
test the testcall interface
*/
static BOOL test_testcall2 ( struct dcerpc_pipe * p , TALLOC_CTX * mem_ctx )
{
NTSTATUS status ;
2003-12-14 16:22:12 +03:00
struct echo_TestCall2 r ;
2003-11-19 15:03:11 +03:00
int i ;
BOOL ret = True ;
for ( i = 1 ; i < = 7 ; i + + ) {
r . in . level = i ;
2005-06-02 17:21:11 +04:00
r . out . info = talloc ( mem_ctx , union echo_Info ) ;
2003-11-19 15:03:11 +03:00
printf ( " \n Testing TestCall2 level %d \n " , i ) ;
2003-12-14 16:22:12 +03:00
status = dcerpc_echo_TestCall2 ( p , mem_ctx , & r ) ;
2003-11-19 15:03:11 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " TestCall2 failed - %s \n " , nt_errstr ( status ) ) ;
ret = False ;
}
}
return ret ;
}
2004-08-30 11:36:16 +04:00
/*
test the TestSleep interface
*/
static BOOL test_sleep ( struct dcerpc_pipe * p , TALLOC_CTX * mem_ctx )
{
int i ;
NTSTATUS status ;
2005-01-11 19:53:02 +03:00
# define ASYNC_COUNT 3
2004-08-30 11:36:16 +04:00
struct rpc_request * req [ ASYNC_COUNT ] ;
struct echo_TestSleep r [ ASYNC_COUNT ] ;
2005-01-11 19:53:02 +03:00
BOOL done [ ASYNC_COUNT ] ;
struct timeval snd [ ASYNC_COUNT ] ;
struct timeval rcv [ ASYNC_COUNT ] ;
struct timeval diff [ ASYNC_COUNT ] ;
2004-08-30 11:36:16 +04:00
struct event_context * ctx ;
int total_done = 0 ;
BOOL ret = True ;
2005-11-21 07:18:43 +03:00
if ( lp_parm_bool ( - 1 , " torture " , " quick " , False ) ) {
printf ( " TestSleep disabled - use \" torture:quick=no \" to enable \n " ) ;
2005-06-08 21:55:58 +04:00
return True ;
}
2005-11-21 07:18:43 +03:00
printf ( " Testing TestSleep - use \" torture:quick=no \" to disable \n " ) ;
2004-08-30 11:36:16 +04:00
for ( i = 0 ; i < ASYNC_COUNT ; i + + ) {
2005-01-11 19:53:02 +03:00
done [ i ] = False ;
snd [ i ] = timeval_current ( ) ;
rcv [ i ] = timeval_zero ( ) ;
2004-08-30 11:36:16 +04:00
r [ i ] . in . seconds = ASYNC_COUNT - i ;
req [ i ] = dcerpc_echo_TestSleep_send ( p , mem_ctx , & r [ i ] ) ;
if ( ! req [ i ] ) {
printf ( " Failed to send async sleep request \n " ) ;
return False ;
}
}
ctx = dcerpc_event_context ( p ) ;
while ( total_done < ASYNC_COUNT ) {
2004-09-20 14:40:11 +04:00
if ( event_loop_once ( ctx ) ! = 0 ) {
return False ;
}
2004-08-30 11:36:16 +04:00
for ( i = 0 ; i < ASYNC_COUNT ; i + + ) {
2005-01-11 19:53:02 +03:00
if ( done [ i ] = = False & & req [ i ] - > state = = RPC_REQUEST_DONE ) {
2004-08-30 11:36:16 +04:00
total_done + + ;
2005-01-11 19:53:02 +03:00
done [ i ] = True ;
rcv [ i ] = timeval_current ( ) ;
2005-08-01 04:34:39 +04:00
diff [ i ] = timeval_until ( & snd [ i ] , & rcv [ i ] ) ;
2005-01-11 19:53:02 +03:00
status = dcerpc_ndr_request_recv ( req [ i ] ) ;
2004-08-30 11:36:16 +04:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " TestSleep(%d) failed - %s \n " ,
i , nt_errstr ( status ) ) ;
ret = False ;
2005-01-11 19:53:02 +03:00
} else if ( r [ i ] . out . result ! = r [ i ] . in . seconds ) {
2005-04-30 12:14:33 +04:00
printf ( " Failed - Asked to sleep for %u seconds (server replied with %u seconds and the reply takes only %u seconds) \n " ,
2005-01-11 19:53:02 +03:00
r [ i ] . out . result , r [ i ] . in . seconds , ( uint_t ) diff [ i ] . tv_sec ) ;
ret = False ;
2004-08-30 11:36:16 +04:00
} else {
2005-02-14 12:44:58 +03:00
if ( r [ i ] . out . result > diff [ i ] . tv_sec ) {
2005-03-23 02:00:12 +03:00
printf ( " Failed - Slept for %u seconds (but reply takes only %u.%06u seconds) \n " ,
2005-02-14 12:44:58 +03:00
r [ i ] . out . result , ( uint_t ) diff [ i ] . tv_sec , ( uint_t ) diff [ i ] . tv_usec ) ;
2005-01-11 19:53:02 +03:00
} else if ( r [ i ] . out . result + 1 = = diff [ i ] . tv_sec ) {
2005-03-23 02:00:12 +03:00
printf ( " Slept for %u seconds (but reply takes %u.%06u seconds - busy server?) \n " ,
2005-02-14 12:44:58 +03:00
r [ i ] . out . result , ( uint_t ) diff [ i ] . tv_sec , ( uint_t ) diff [ i ] . tv_usec ) ;
2005-01-11 19:53:02 +03:00
} else if ( r [ i ] . out . result = = diff [ i ] . tv_sec ) {
2005-03-23 02:00:12 +03:00
printf ( " Slept for %u seconds (reply takes %u.%06u seconds - ok) \n " ,
2005-02-14 12:44:58 +03:00
r [ i ] . out . result , ( uint_t ) diff [ i ] . tv_sec , ( uint_t ) diff [ i ] . tv_usec ) ;
2005-01-11 19:53:02 +03:00
} else {
2005-03-23 02:00:12 +03:00
printf ( " (Failed) - Not async - Slept for %u seconds (but reply takes %u.%06u seconds) \n " ,
2005-02-14 12:44:58 +03:00
r [ i ] . out . result , ( uint_t ) diff [ i ] . tv_sec , ( uint_t ) diff [ i ] . tv_usec ) ;
2005-01-11 19:53:02 +03:00
/* TODO: let the test fail here, when we support async rpc on ncacn_np
ret = False ; */
}
2004-08-30 11:36:16 +04:00
}
}
}
}
return ret ;
}
2005-01-05 02:27:12 +03:00
/*
test enum handling
*/
static BOOL test_enum ( struct dcerpc_pipe * p , TALLOC_CTX * mem_ctx )
{
NTSTATUS status ;
struct echo_TestEnum r ;
BOOL ret = True ;
enum echo_Enum1 v = ECHO_ENUM1 ;
struct echo_Enum2 e2 ;
union echo_Enum3 e3 ;
r . in . foo1 = & v ;
r . in . foo2 = & e2 ;
r . in . foo3 = & e3 ;
r . out . foo1 = & v ;
r . out . foo2 = & e2 ;
r . out . foo3 = & e3 ;
e2 . e1 = 76 ;
e2 . e2 = ECHO_ENUM1_32 ;
e3 . e1 = ECHO_ENUM2 ;
printf ( " \n Testing TestEnum \n " ) ;
status = dcerpc_echo_TestEnum ( p , mem_ctx , & r ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " TestEnum failed - %s \n " , nt_errstr ( status ) ) ;
ret = False ;
}
return ret ;
}
2005-02-19 02:30:26 +03:00
/*
test surrounding conformant array handling
*/
static BOOL test_surrounding ( struct dcerpc_pipe * p , TALLOC_CTX * mem_ctx )
{
NTSTATUS status ;
struct echo_TestSurrounding r ;
BOOL ret = True ;
ZERO_STRUCT ( r ) ;
r . in . data = talloc ( mem_ctx , struct echo_Surrounding ) ;
r . in . data - > x = 20 ;
r . in . data - > surrounding = talloc_zero_array ( mem_ctx , uint16_t , r . in . data - > x ) ;
r . out . data = talloc ( mem_ctx , struct echo_Surrounding ) ;
printf ( " \n Testing TestSurrounding \n " ) ;
status = dcerpc_echo_TestSurrounding ( p , mem_ctx , & r ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " TestSurrounding failed - %s \n " , nt_errstr ( status ) ) ;
2005-11-10 07:28:55 +03:00
return False ;
2005-02-19 02:30:26 +03:00
}
if ( r . out . data - > x ! = 2 * r . in . data - > x ) {
printf ( " TestSurrounding did not make the array twice as large \n " ) ;
ret = False ;
}
return ret ;
}
2005-02-20 05:57:38 +03:00
/*
test multiple levels of pointers
*/
static BOOL test_doublepointer ( struct dcerpc_pipe * p , TALLOC_CTX * mem_ctx )
{
NTSTATUS status ;
struct echo_TestDoublePointer r ;
BOOL ret = True ;
uint16_t value = 12 ;
uint16_t * pvalue = & value ;
uint16_t * * ppvalue = & pvalue ;
ZERO_STRUCT ( r ) ;
r . in . data = & ppvalue ;
printf ( " \n Testing TestDoublePointer \n " ) ;
status = dcerpc_echo_TestDoublePointer ( p , mem_ctx , & r ) ;
if ( ! NT_STATUS_IS_OK ( status ) ) {
printf ( " TestDoublePointer failed - %s \n " , nt_errstr ( status ) ) ;
ret = False ;
}
if ( value ! = r . out . result ) {
2005-03-23 02:00:12 +03:00
printf ( " TestDoublePointer did not return original value (%d != %d) \n " , value , r . out . result ) ;
2005-02-20 05:57:38 +03:00
ret = False ;
}
return ret ;
}
2005-06-17 03:28:24 +04:00
/*
test request timeouts
*/
static BOOL test_timeout ( struct dcerpc_pipe * p , TALLOC_CTX * mem_ctx )
{
NTSTATUS status ;
struct rpc_request * req ;
struct echo_TestSleep r ;
int timeout_saved = p - > request_timeout ;
2005-11-21 07:18:43 +03:00
if ( lp_parm_bool ( - 1 , " torture " , " quick " , False ) ) {
printf ( " timeout testing disabled - use \" torture:quick=no \" to enable \n " ) ;
2005-06-17 03:28:24 +04:00
return True ;
}
printf ( " testing request timeouts \n " ) ;
r . in . seconds = 2 ;
p - > request_timeout = 1 ;
req = dcerpc_echo_TestSleep_send ( p , mem_ctx , & r ) ;
if ( ! req ) {
printf ( " Failed to send async sleep request \n " ) ;
goto failed ;
}
status = dcerpc_ndr_request_recv ( req ) ;
if ( ! NT_STATUS_EQUAL ( status , NT_STATUS_IO_TIMEOUT ) ) {
printf ( " request should have timed out - %s \n " , nt_errstr ( status ) ) ;
goto failed ;
}
printf ( " testing request destruction \n " ) ;
req = dcerpc_echo_TestSleep_send ( p , mem_ctx , & r ) ;
if ( ! req ) {
printf ( " Failed to send async sleep request \n " ) ;
goto failed ;
}
talloc_free ( req ) ;
req = dcerpc_echo_TestSleep_send ( p , mem_ctx , & r ) ;
if ( ! req ) {
printf ( " Failed to send async sleep request \n " ) ;
goto failed ;
}
status = dcerpc_ndr_request_recv ( req ) ;
if ( ! NT_STATUS_EQUAL ( status , NT_STATUS_IO_TIMEOUT ) ) {
printf ( " request should have timed out - %s \n " , nt_errstr ( status ) ) ;
goto failed ;
2005-06-17 03:30:02 +04:00
}
2005-06-17 03:28:24 +04:00
p - > request_timeout = timeout_saved ;
2005-06-17 03:30:02 +04:00
return test_addone ( p , mem_ctx ) ;
2005-06-17 03:28:24 +04:00
failed :
p - > request_timeout = timeout_saved ;
return False ;
}
2004-10-28 17:40:50 +04:00
BOOL torture_rpc_echo ( void )
2003-11-03 10:26:30 +03:00
{
2005-02-19 02:43:40 +03:00
NTSTATUS status ;
2005-03-22 11:00:45 +03:00
struct dcerpc_pipe * p ;
2003-11-03 10:26:30 +03:00
TALLOC_CTX * mem_ctx ;
2003-11-03 11:37:48 +03:00
BOOL ret = True ;
2003-11-03 10:26:30 +03:00
mem_ctx = talloc_init ( " torture_rpc_echo " ) ;
2005-03-22 11:00:45 +03:00
status = torture_rpc_connection ( mem_ctx ,
& p ,
2005-12-27 17:28:01 +03:00
& dcerpc_table_rpcecho ) ;
2003-11-03 10:26:30 +03:00
if ( ! NT_STATUS_IS_OK ( status ) ) {
return False ;
}
2003-11-03 11:37:48 +03:00
2005-01-21 09:56:57 +03:00
ret & = test_addone ( p , mem_ctx ) ;
ret & = test_sinkdata ( p , mem_ctx ) ;
ret & = test_echodata ( p , mem_ctx ) ;
ret & = test_sourcedata ( p , mem_ctx ) ;
ret & = test_testcall ( p , mem_ctx ) ;
ret & = test_testcall2 ( p , mem_ctx ) ;
ret & = test_enum ( p , mem_ctx ) ;
2005-02-19 02:30:26 +03:00
ret & = test_surrounding ( p , mem_ctx ) ;
2005-02-20 05:57:38 +03:00
ret & = test_doublepointer ( p , mem_ctx ) ;
2005-02-19 02:43:40 +03:00
ret & = test_sleep ( p , mem_ctx ) ;
2005-06-17 03:28:24 +04:00
ret & = test_timeout ( p , mem_ctx ) ;
2005-01-11 19:53:02 +03:00
2003-11-03 11:37:48 +03:00
printf ( " \n " ) ;
2004-09-27 12:41:39 +04:00
talloc_free ( mem_ctx ) ;
2003-11-22 11:11:32 +03:00
2003-11-03 11:37:48 +03:00
return ret ;
2003-11-03 10:26:30 +03:00
}