From d555f45389b76836323b826b5e5f190b7bc783a3 Mon Sep 17 00:00:00 2001 From: Thomas Faber Date: Sat, 13 Jun 2015 12:29:07 +0000 Subject: [PATCH] [WS2_32_APITEST] - Add a test for send/sendto with a read-only buffer CORE-9807 svn path=/trunk/; revision=68120 --- rostests/apitests/ws2_32/CMakeLists.txt | 1 + rostests/apitests/ws2_32/send.c | 185 ++++++++++++++++++++++++ rostests/apitests/ws2_32/testlist.c | 2 + 3 files changed, 188 insertions(+) create mode 100644 rostests/apitests/ws2_32/send.c diff --git a/rostests/apitests/ws2_32/CMakeLists.txt b/rostests/apitests/ws2_32/CMakeLists.txt index 13ef03bd221..d719c540ecc 100644 --- a/rostests/apitests/ws2_32/CMakeLists.txt +++ b/rostests/apitests/ws2_32/CMakeLists.txt @@ -5,6 +5,7 @@ list(APPEND SOURCE ioctlsocket.c nostartup.c recv.c + send.c WSAStartup.c testlist.c) diff --git a/rostests/apitests/ws2_32/send.c b/rostests/apitests/ws2_32/send.c new file mode 100644 index 00000000000..5e702239e94 --- /dev/null +++ b/rostests/apitests/ws2_32/send.c @@ -0,0 +1,185 @@ +/* + * PROJECT: ReactOS api tests + * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory + * PURPOSE: Test for send/sendto + * PROGRAMMER: Thomas Faber + */ + +#include +#include + +#define WIN32_NO_STATUS +#include +#include + +static +PVOID +AllocateReadOnly( + _In_ SIZE_T SizeRequested) +{ + NTSTATUS Status; + SIZE_T Size = PAGE_ROUND_UP(SizeRequested); + PVOID VirtualMemory = NULL; + + Status = NtAllocateVirtualMemory(NtCurrentProcess(), &VirtualMemory, 0, &Size, MEM_COMMIT, PAGE_READONLY); + if (!NT_SUCCESS(Status)) + return NULL; + + return VirtualMemory; +} + +static +VOID +FreeReadOnly( + _In_ PVOID VirtualMemory) +{ + NTSTATUS Status; + SIZE_T Size = 0; + + Status = NtFreeVirtualMemory(NtCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE); + ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status); +} + +static +VOID +test_send(void) +{ + SOCKET sock; + int ret; + int error; + PVOID buffer; + ULONG bufferSize; + struct sockaddr_in addr; + + bufferSize = 32; + buffer = AllocateReadOnly(bufferSize); + ok(buffer != NULL, "AllocateReadOnly failed\n"); + if (!buffer) + { + skip("No memory\n"); + return; + } + + ret = send(0, NULL, 0, 0); + error = WSAGetLastError(); + ok(ret == SOCKET_ERROR, "send returned %d\n", ret); + ok(error == WSAENOTSOCK, "error = %d\n", error); + + ret = send(0, buffer, bufferSize, 0); + error = WSAGetLastError(); + ok(ret == SOCKET_ERROR, "send returned %d\n", ret); + ok(error == WSAENOTSOCK, "error = %d\n", error); + + sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + ok(sock != INVALID_SOCKET, "socket failed\n"); + if (sock == INVALID_SOCKET) + { + skip("No socket\n"); + FreeReadOnly(buffer); + return; + } + + ret = send(sock, NULL, 0, 0); + error = WSAGetLastError(); + ok(ret == SOCKET_ERROR, "send returned %d\n", ret); + ok(error == WSAENOTCONN, "error = %d\n", error); + + ret = send(sock, buffer, bufferSize, 0); + error = WSAGetLastError(); + ok(ret == SOCKET_ERROR, "send returned %d\n", ret); + ok(error == WSAENOTCONN, "error = %d\n", error); + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr("8.8.8.8"); + addr.sin_port = htons(53); + ret = connect(sock, (const struct sockaddr *)&addr, sizeof(addr)); + error = WSAGetLastError(); + ok(ret == 0, "connect returned %d\n", ret); + ok(error == 0, "error = %d\n", error); + + ret = send(sock, NULL, 0, 0); + error = WSAGetLastError(); + ok(ret == 0, "send returned %d\n", ret); + ok(error == 0, "error = %d\n", error); + + ret = send(sock, buffer, bufferSize, 0); + error = WSAGetLastError(); + ok(ret == bufferSize, "send returned %d\n", ret); + ok(error == 0, "error = %d\n", error); + + closesocket(sock); + + FreeReadOnly(buffer); +} + +static +VOID +test_sendto(void) +{ + SOCKET sock; + int ret; + int error; + PVOID buffer; + ULONG bufferSize; + struct sockaddr_in addr; + + bufferSize = 32; + buffer = AllocateReadOnly(bufferSize); + ok(buffer != NULL, "AllocateReadOnly failed\n"); + if (!buffer) + { + skip("No memory\n"); + return; + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = inet_addr("8.8.8.8"); + addr.sin_port = htons(53); + + ret = sendto(0, NULL, 0, 0, (const struct sockaddr *)&addr, sizeof(addr)); + error = WSAGetLastError(); + ok(ret == SOCKET_ERROR, "sendto returned %d\n", ret); + ok(error == WSAENOTSOCK, "error = %d\n", error); + + ret = sendto(0, buffer, bufferSize, 0, (const struct sockaddr *)&addr, sizeof(addr)); + error = WSAGetLastError(); + ok(ret == SOCKET_ERROR, "sendto returned %d\n", ret); + ok(error == WSAENOTSOCK, "error = %d\n", error); + + sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + ok(sock != INVALID_SOCKET, "socket failed\n"); + if (sock == INVALID_SOCKET) + { + skip("No socket\n"); + FreeReadOnly(buffer); + return; + } + + ret = sendto(sock, NULL, 0, 0, (const struct sockaddr *)&addr, sizeof(addr)); + error = WSAGetLastError(); + ok(ret == 0, "sendto returned %d\n", ret); + ok(error == 0, "error = %d\n", error); + + ret = sendto(sock, buffer, bufferSize, 0, (const struct sockaddr *)&addr, sizeof(addr)); + error = WSAGetLastError(); + ok(ret == bufferSize, "sendto returned %d\n", ret); + ok(error == 0, "error = %d\n", error); + + closesocket(sock); + + FreeReadOnly(buffer); +} + +START_TEST(send) +{ + int ret; + WSADATA wsad; + + ret = WSAStartup(MAKEWORD(2, 2), &wsad); + ok(ret == 0, "WSAStartup failed with %d\n", ret); + test_send(); + test_sendto(); + WSACleanup(); +} diff --git a/rostests/apitests/ws2_32/testlist.c b/rostests/apitests/ws2_32/testlist.c index 7b0660f9bc9..e1363386979 100644 --- a/rostests/apitests/ws2_32/testlist.c +++ b/rostests/apitests/ws2_32/testlist.c @@ -6,6 +6,7 @@ extern void func_getaddrinfo(void); extern void func_ioctlsocket(void); extern void func_recv(void); +extern void func_send(void); extern void func_WSAStartup(void); extern void func_nostartup(void); @@ -15,6 +16,7 @@ const struct test winetest_testlist[] = { "ioctlsocket", func_ioctlsocket }, { "nostartup", func_nostartup }, { "recv", func_recv }, + { "send", func_send }, { "WSAStartup", func_WSAStartup }, { 0, 0 } };