2005-01-19 06:20:20 +03:00
/*
Unix SMB / CIFS implementation .
local testing of socket routines .
Copyright ( C ) Andrew Tridgell 2005
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
2005-01-19 06:20:20 +03: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/>.
2005-01-19 06:20:20 +03:00
*/
# include "includes.h"
2005-02-10 09:59:29 +03:00
# include "lib/socket/socket.h"
2005-06-17 03:19:35 +04:00
# include "lib/events/events.h"
2006-03-07 14:07:23 +03:00
# include "system/network.h"
2006-08-17 17:37:04 +04:00
# include "lib/socket/netif.h"
2006-03-25 19:23:42 +03:00
# include "torture/torture.h"
2014-02-27 12:08:17 +04:00
# include "torture/local/proto.h"
2007-12-07 18:04:17 +03:00
# include "param/param.h"
2007-12-10 20:41:19 +03:00
# include "libcli/resolve/resolve.h"
2005-01-19 06:20:20 +03:00
2007-12-10 20:41:19 +03:00
/**
2005-01-19 06:20:20 +03:00
basic testing of udp routines
*/
2006-10-16 17:06:41 +04:00
static bool test_udp ( struct torture_context * tctx )
2005-01-19 06:20:20 +03:00
{
struct socket_context * sock1 , * sock2 ;
NTSTATUS status ;
2006-01-10 01:12:53 +03:00
struct socket_address * srv_addr , * from_addr , * localhost ;
2005-01-19 06:20:20 +03:00
size_t size = 100 + ( random ( ) % 100 ) ;
DATA_BLOB blob , blob2 ;
size_t sent , nread ;
2006-10-16 17:06:41 +04:00
TALLOC_CTX * mem_ctx = tctx ;
2007-12-12 00:23:14 +03:00
struct interface * ifaces ;
2011-06-02 09:40:28 +04:00
load_interface_list ( tctx , tctx - > lp_ctx , & ifaces ) ;
2005-01-19 06:20:20 +03:00
2018-02-15 18:43:59 +03:00
status = socket_create ( mem_ctx , " ip " , SOCKET_TYPE_DGRAM , & sock1 , 0 ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " creating DGRAM IP socket 1 " ) ;
2005-01-19 06:20:20 +03:00
2018-02-15 18:43:59 +03:00
status = socket_create ( mem_ctx , " ip " , SOCKET_TYPE_DGRAM , & sock2 , 0 ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " creating DGRAM IP socket 1 " ) ;
2005-01-19 06:20:20 +03:00
2006-01-10 01:12:53 +03:00
localhost = socket_address_from_strings ( sock1 , sock1 - > backend_name ,
2011-05-02 09:57:19 +04:00
iface_list_best_ip ( ifaces , " 127.0.0.1 " ) , 0 ) ;
2006-06-17 02:48:50 +04:00
2006-10-16 17:06:41 +04:00
torture_assert ( tctx , localhost , " Localhost not found " ) ;
2006-01-10 01:12:53 +03:00
status = socket_listen ( sock1 , localhost , 0 , 0 ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " listen on socket 1 " ) ;
2005-01-19 06:20:20 +03:00
2006-10-16 17:06:41 +04:00
srv_addr = socket_get_my_addr ( sock1 , mem_ctx ) ;
2007-12-07 01:57:22 +03:00
torture_assert ( tctx , srv_addr ! = NULL & &
2011-05-02 09:57:19 +04:00
strcmp ( srv_addr - > addr , iface_list_best_ip ( ifaces , " 127.0.0.1 " ) ) = = 0 ,
2006-10-16 17:06:41 +04:00
talloc_asprintf ( tctx ,
" Expected server address of %s but got %s " ,
2011-05-02 09:57:19 +04:00
iface_list_best_ip ( ifaces , " 127.0.0.1 " ) , srv_addr ? srv_addr - > addr : NULL ) ) ;
2005-01-19 06:20:20 +03:00
2006-10-16 17:06:41 +04:00
torture_comment ( tctx , " server port is %d \n " , srv_addr - > port ) ;
2005-01-19 06:20:20 +03:00
2006-10-16 17:06:41 +04:00
blob = data_blob_talloc ( mem_ctx , NULL , size ) ;
blob2 = data_blob_talloc ( mem_ctx , NULL , size ) ;
2005-01-19 06:20:20 +03:00
generate_random_buffer ( blob . data , blob . length ) ;
sent = size ;
2006-04-30 09:58:31 +04:00
status = socket_sendto ( sock2 , & blob , & sent , srv_addr ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " sendto() on socket 2 " ) ;
2005-01-19 06:20:20 +03:00
2006-04-30 09:58:31 +04:00
status = socket_recvfrom ( sock1 , blob2 . data , size , & nread ,
2006-01-10 01:12:53 +03:00
sock1 , & from_addr ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " recvfrom() on socket 1 " ) ;
torture_assert_str_equal ( tctx , from_addr - > addr , srv_addr - > addr ,
" different address " ) ;
torture_assert_int_equal ( tctx , nread , size , " Unexpected recvfrom size " ) ;
2008-04-14 00:00:36 +04:00
torture_assert_mem_equal ( tctx , blob2 . data , blob . data , size ,
2006-06-17 02:48:50 +04:00
" Bad data in recvfrom " ) ;
2005-01-19 06:20:20 +03:00
generate_random_buffer ( blob . data , blob . length ) ;
2006-04-30 09:58:31 +04:00
status = socket_sendto ( sock1 , & blob , & sent , from_addr ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " sendto() on socket 1 " ) ;
2005-01-19 06:20:20 +03:00
2006-04-30 09:58:31 +04:00
status = socket_recvfrom ( sock2 , blob2 . data , size , & nread ,
2006-01-10 01:12:53 +03:00
sock2 , & from_addr ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " recvfrom() on socket 2 " ) ;
torture_assert_str_equal ( tctx , from_addr - > addr , srv_addr - > addr ,
" Unexpected recvfrom addr " ) ;
2006-06-17 02:48:50 +04:00
2006-10-16 17:06:41 +04:00
torture_assert_int_equal ( tctx , nread , size , " Unexpected recvfrom size " ) ;
torture_assert_int_equal ( tctx , from_addr - > port , srv_addr - > port ,
" Unexpected recvfrom port " ) ;
2008-04-14 00:00:36 +04:00
torture_assert_mem_equal ( tctx , blob2 . data , blob . data , size ,
2006-06-17 02:48:50 +04:00
" Bad data in recvfrom " ) ;
2005-01-19 06:20:20 +03:00
talloc_free ( sock1 ) ;
talloc_free ( sock2 ) ;
2006-10-16 17:06:41 +04:00
return true ;
2005-01-19 06:20:20 +03:00
}
2005-01-19 06:33:49 +03:00
/*
basic testing of tcp routines
*/
2006-10-16 17:06:41 +04:00
static bool test_tcp ( struct torture_context * tctx )
2005-01-19 06:33:49 +03:00
{
struct socket_context * sock1 , * sock2 , * sock3 ;
NTSTATUS status ;
2006-01-10 01:12:53 +03:00
struct socket_address * srv_addr , * from_addr , * localhost ;
2005-01-19 06:33:49 +03:00
size_t size = 100 + ( random ( ) % 100 ) ;
DATA_BLOB blob , blob2 ;
size_t sent , nread ;
2006-10-16 17:06:41 +04:00
TALLOC_CTX * mem_ctx = tctx ;
2008-12-29 22:24:57 +03:00
struct tevent_context * ev = tctx - > ev ;
2007-12-12 00:23:14 +03:00
struct interface * ifaces ;
2005-01-19 06:33:49 +03:00
2018-02-15 18:43:59 +03:00
status = socket_create ( mem_ctx , " ip " , SOCKET_TYPE_STREAM , & sock1 , 0 ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " creating IP stream socket 1 " ) ;
2005-01-19 06:33:49 +03:00
2018-02-15 18:43:59 +03:00
status = socket_create ( mem_ctx , " ip " , SOCKET_TYPE_STREAM , & sock2 , 0 ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " creating IP stream socket 1 " ) ;
2005-01-19 06:33:49 +03:00
2011-06-02 09:40:28 +04:00
load_interface_list ( tctx , tctx - > lp_ctx , & ifaces ) ;
2006-01-10 01:12:53 +03:00
localhost = socket_address_from_strings ( sock1 , sock1 - > backend_name ,
2011-05-02 09:57:19 +04:00
iface_list_best_ip ( ifaces , " 127.0.0.1 " ) , 0 ) ;
2006-10-16 17:06:41 +04:00
torture_assert ( tctx , localhost , " Localhost not found " ) ;
2006-01-10 01:12:53 +03:00
status = socket_listen ( sock1 , localhost , 0 , 0 ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " listen on socket 1 " ) ;
2005-01-19 06:33:49 +03:00
2006-10-16 17:06:41 +04:00
srv_addr = socket_get_my_addr ( sock1 , mem_ctx ) ;
torture_assert ( tctx , srv_addr & & srv_addr - > addr ,
2006-06-17 02:48:50 +04:00
" Unexpected socket_get_my_addr NULL \n " ) ;
2006-01-10 01:12:53 +03:00
2011-05-02 09:57:19 +04:00
torture_assert_str_equal ( tctx , srv_addr - > addr , iface_list_best_ip ( ifaces , " 127.0.0.1 " ) ,
2006-10-16 17:06:41 +04:00
" Unexpected server address " ) ;
2005-01-19 06:33:49 +03:00
2006-10-16 17:06:41 +04:00
torture_comment ( tctx , " server port is %d \n " , srv_addr - > port ) ;
2005-01-19 06:33:49 +03:00
2008-12-18 01:13:44 +03:00
status = socket_connect_ev ( sock2 , NULL , srv_addr , 0 , ev ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " connect() on socket 2 " ) ;
2005-01-19 06:33:49 +03:00
status = socket_accept ( sock1 , & sock3 ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " accept() on socket 1 " ) ;
talloc_steal ( mem_ctx , sock3 ) ;
2005-01-19 06:33:49 +03:00
talloc_free ( sock1 ) ;
2006-10-16 17:06:41 +04:00
blob = data_blob_talloc ( mem_ctx , NULL , size ) ;
blob2 = data_blob_talloc ( mem_ctx , NULL , size ) ;
2005-01-19 06:33:49 +03:00
generate_random_buffer ( blob . data , blob . length ) ;
sent = size ;
2006-04-30 09:58:31 +04:00
status = socket_send ( sock2 , & blob , & sent ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " send() on socket 2 " ) ;
2005-01-19 06:33:49 +03:00
2006-04-30 09:58:31 +04:00
status = socket_recv ( sock3 , blob2 . data , size , & nread ) ;
2006-10-16 17:06:41 +04:00
torture_assert_ntstatus_ok ( tctx , status , " recv() on socket 3 " ) ;
2005-01-19 06:33:49 +03:00
2006-10-16 17:06:41 +04:00
from_addr = socket_get_peer_addr ( sock3 , mem_ctx ) ;
2006-06-17 02:48:50 +04:00
2006-10-16 17:06:41 +04:00
torture_assert ( tctx , from_addr & & from_addr - > addr ,
2006-06-17 02:48:50 +04:00
" Unexpected recvfrom addr NULL " ) ;
2005-01-19 06:33:49 +03:00
2006-10-16 17:06:41 +04:00
torture_assert_str_equal ( tctx , from_addr - > addr , srv_addr - > addr ,
" Unexpected recvfrom addr " ) ;
2005-01-19 06:33:49 +03:00
2006-10-16 17:06:41 +04:00
torture_assert_int_equal ( tctx , nread , size , " Unexpected recvfrom size " ) ;
2008-04-14 00:00:36 +04:00
torture_assert_mem_equal ( tctx , blob2 . data , blob . data , size ,
2006-10-16 17:06:41 +04:00
" Bad data in recv " ) ;
return true ;
2005-01-19 06:33:49 +03:00
}
2006-06-17 04:17:50 +04:00
struct torture_suite * torture_local_socket ( TALLOC_CTX * mem_ctx )
2005-01-19 06:20:20 +03:00
{
2010-12-11 05:26:31 +03:00
struct torture_suite * suite = torture_suite_create ( mem_ctx , " socket " ) ;
2005-01-19 06:20:20 +03:00
2006-10-16 17:06:41 +04:00
torture_suite_add_simple_test ( suite , " udp " , test_udp ) ;
torture_suite_add_simple_test ( suite , " tcp " , test_tcp ) ;
2005-01-19 06:20:20 +03:00
2006-06-17 04:17:50 +04:00
return suite ;
2005-01-19 06:20:20 +03:00
}