diff options
author | Andrew Tridgell <tridge@samba.org> | 2005-01-19 03:20:20 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 13:08:59 -0500 |
commit | 8783aa8ea57c3a6989e0722d5184e98d543352d4 (patch) | |
tree | 93df839740cf04b88ec6ff9f81fcde5c99e77951 /source4/torture | |
parent | 6c310003d21fe832da46bbfeb078f716d87be04f (diff) | |
download | samba-8783aa8ea57c3a6989e0722d5184e98d543352d4.tar.gz samba-8783aa8ea57c3a6989e0722d5184e98d543352d4.tar.bz2 samba-8783aa8ea57c3a6989e0722d5184e98d543352d4.zip |
r4831: added udp support to our generic sockets library.
I decided to incorporate the udp support into the socket_ipv4.c
backend (and later in socket_ipv6.c) rather than doing a separate
backend, as so much of the code is shareable. Basically this adds a
socket_sendto() and a socket_recvfrom() call and not much all.
For udp servers, I decided to keep the call as socket_listen(), even
though dgram servers don't actually call listen(). This keeps the API
consistent.
I also added a simple local sockets testsuite in smbtorture,
LOCAL-SOCKET
(This used to be commit 9f12a45a05c5c447fb4ec18c8dd28f70e90e32a5)
Diffstat (limited to 'source4/torture')
-rw-r--r-- | source4/torture/config.mk | 3 | ||||
-rw-r--r-- | source4/torture/local/socket.c | 134 | ||||
-rw-r--r-- | source4/torture/torture.c | 1 |
3 files changed, 137 insertions, 1 deletions
diff --git a/source4/torture/config.mk b/source4/torture/config.mk index 0e74d7c040..a31c2c0598 100644 --- a/source4/torture/config.mk +++ b/source4/torture/config.mk @@ -138,7 +138,8 @@ ADD_OBJ_FILES = \ lib/talloc/testsuite.o \ torture/local/messaging.o \ torture/local/binding_string.o \ - torture/local/idtree.o + torture/local/idtree.o \ + torture/local/socket.o REQUIRED_SUBSYSTEMS = \ LIBSMB \ MESSAGING diff --git a/source4/torture/local/socket.c b/source4/torture/local/socket.c new file mode 100644 index 0000000000..cdd379e934 --- /dev/null +++ b/source4/torture/local/socket.c @@ -0,0 +1,134 @@ +/* + 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 + 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 02139, USA. +*/ + +#include "includes.h" + +#define CHECK_STATUS(status, correct) do { \ + if (!NT_STATUS_EQUAL(status, correct)) { \ + printf("(%s) Incorrect status %s - should be %s\n", \ + __location__, nt_errstr(status), nt_errstr(correct)); \ + ret = False; \ + goto done; \ + }} while (0) + + +/* + basic testing of udp routines +*/ +static BOOL test_udp(TALLOC_CTX *mem_ctx) +{ + struct socket_context *sock1, *sock2; + NTSTATUS status; + int srv_port, from_port; + const char *srv_addr, *from_addr; + size_t size = 100 + (random() % 100); + DATA_BLOB blob, blob2; + size_t sent, nread; + BOOL ret = True; + + status = socket_create("ip", SOCKET_TYPE_DGRAM, &sock1, 0); + CHECK_STATUS(status, NT_STATUS_OK); + talloc_steal(mem_ctx, sock1); + + status = socket_create("ip", SOCKET_TYPE_DGRAM, &sock2, 0); + CHECK_STATUS(status, NT_STATUS_OK); + talloc_steal(mem_ctx, sock2); + + status = socket_listen(sock1, "127.0.0.1", 0, 0, 0); + CHECK_STATUS(status, NT_STATUS_OK); + + srv_addr = socket_get_my_addr(sock1, mem_ctx); + if (srv_addr == NULL || strcmp(srv_addr, "127.0.0.1") != 0) { + printf("Expected server address of 127.0.0.1 but got %s\n", srv_addr); + return False; + } + + srv_port = socket_get_my_port(sock1); + printf("server port is %d\n", srv_port); + + blob = data_blob_talloc(mem_ctx, NULL, size); + blob2 = data_blob_talloc(mem_ctx, NULL, size); + generate_random_buffer(blob.data, blob.length); + + sent = size; + status = socket_sendto(sock2, &blob, &sent, 0, srv_addr, srv_port); + CHECK_STATUS(status, NT_STATUS_OK); + + status = socket_recvfrom(sock1, blob2.data, size, &nread, 0, + &from_addr, &from_port); + CHECK_STATUS(status, NT_STATUS_OK); + + if (strcmp(from_addr, srv_addr) != 0) { + printf("Unexpected recvfrom addr %s\n", from_addr); + ret = False; + } + if (nread != size) { + printf("Unexpected recvfrom size %d should be %d\n", nread, size); + ret = False; + } + + if (memcmp(blob2.data, blob.data, size) != 0) { + printf("Bad data in recvfrom\n"); + ret = False; + } + + generate_random_buffer(blob.data, blob.length); + status = socket_sendto(sock1, &blob, &sent, 0, from_addr, from_port); + CHECK_STATUS(status, NT_STATUS_OK); + + status = socket_recvfrom(sock2, blob2.data, size, &nread, 0, + &from_addr, &from_port); + CHECK_STATUS(status, NT_STATUS_OK); + if (strcmp(from_addr, srv_addr) != 0) { + printf("Unexpected recvfrom addr %s\n", from_addr); + ret = False; + } + if (nread != size) { + printf("Unexpected recvfrom size %d should be %d\n", nread, size); + ret = False; + } + if (from_port != srv_port) { + printf("Unexpected recvfrom port %d should be %d\n", + from_port, srv_port); + ret = False; + } + if (memcmp(blob2.data, blob.data, size) != 0) { + printf("Bad data in recvfrom\n"); + ret = False; + } + +done: + talloc_free(sock1); + talloc_free(sock2); + + return ret; +} + +BOOL torture_local_socket(void) +{ + BOOL ret = True; + TALLOC_CTX *mem_ctx = talloc_new(NULL); + + ret &= test_udp(mem_ctx); + + return ret; +} diff --git a/source4/torture/torture.c b/source4/torture/torture.c index f9faf21957..b2219c617c 100644 --- a/source4/torture/torture.c +++ b/source4/torture/torture.c @@ -2432,6 +2432,7 @@ static struct { {"LOCAL-MESSAGING", torture_local_messaging, 0}, {"LOCAL-BINDING", torture_local_binding_string, 0}, {"LOCAL-IDTREE", torture_local_idtree, 0}, + {"LOCAL-SOCKET", torture_local_socket, 0}, /* ldap testers */ {"LDAP-BASIC", torture_ldap_basic, 0}, |