diff options
author | Andrew Tridgell <tridge@samba.org> | 2005-01-19 03:33:49 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 13:08:59 -0500 |
commit | a3c562b751637575941875d69b1c7fb395e8820b (patch) | |
tree | f78ff77c6db5d4618626eff8f26a767e17f4422d | |
parent | 8783aa8ea57c3a6989e0722d5184e98d543352d4 (diff) | |
download | samba-a3c562b751637575941875d69b1c7fb395e8820b.tar.gz samba-a3c562b751637575941875d69b1c7fb395e8820b.tar.bz2 samba-a3c562b751637575941875d69b1c7fb395e8820b.zip |
r4832: added simple testing of tcp sockets to LOCAL-SOCKET test
(This used to be commit c06b25c269fb34601e931079ff5658f6e19956bc)
-rw-r--r-- | source4/torture/local/socket.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/source4/torture/local/socket.c b/source4/torture/local/socket.c index cdd379e934..6a6f867fa0 100644 --- a/source4/torture/local/socket.c +++ b/source4/torture/local/socket.c @@ -45,6 +45,8 @@ static BOOL test_udp(TALLOC_CTX *mem_ctx) size_t sent, nread; BOOL ret = True; + printf("TESTING UDP SOCKETS\n"); + status = socket_create("ip", SOCKET_TYPE_DGRAM, &sock1, 0); CHECK_STATUS(status, NT_STATUS_OK); talloc_steal(mem_ctx, sock1); @@ -123,12 +125,118 @@ done: return ret; } +/* + basic testing of tcp routines +*/ +static BOOL test_tcp(TALLOC_CTX *mem_ctx) +{ + struct socket_context *sock1, *sock2, *sock3; + 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; + + printf("TESTING TCP SOCKETS\n"); + + status = socket_create("ip", SOCKET_TYPE_STREAM, &sock1, 0); + CHECK_STATUS(status, NT_STATUS_OK); + talloc_steal(mem_ctx, sock1); + + status = socket_create("ip", SOCKET_TYPE_STREAM, &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); + + status = socket_connect(sock2, NULL, 0, srv_addr, srv_port, 0); + CHECK_STATUS(status, NT_STATUS_OK); + + status = socket_accept(sock1, &sock3); + CHECK_STATUS(status, NT_STATUS_OK); + talloc_steal(mem_ctx, sock3); + talloc_free(sock1); + + 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_send(sock2, &blob, &sent, 0); + CHECK_STATUS(status, NT_STATUS_OK); + + status = socket_recv(sock3, blob2.data, size, &nread, 0); + CHECK_STATUS(status, NT_STATUS_OK); + + from_addr = socket_get_peer_addr(sock3, mem_ctx); + from_port = socket_get_peer_port(sock3); + + 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_send(sock3, &blob, &sent, 0); + CHECK_STATUS(status, NT_STATUS_OK); + + status = socket_recv(sock2, blob2.data, size, &nread, 0); + CHECK_STATUS(status, NT_STATUS_OK); + + from_addr = socket_get_peer_addr(sock2, mem_ctx); + from_port = socket_get_peer_port(sock2); + + 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: + + return ret; +} + BOOL torture_local_socket(void) { BOOL ret = True; TALLOC_CTX *mem_ctx = talloc_new(NULL); ret &= test_udp(mem_ctx); + ret &= test_tcp(mem_ctx); return ret; } |