ReactOS  0.4.15-dev-1033-gd7d716a
WSARecv.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPL - See COPYING in the top level directory
4  * PURPOSE: Test for WSARecv
5  * PROGRAMMERS: Peter Hater
6  */
7 
8 #include "ws2_32.h"
9 
10 #define RECV_BUF 4
11 #define WSARecv_TIMEOUT 2000
12 
13 static int count = 0;
14 
15 void
17  DWORD dwError,
18  DWORD cbTransferred,
20  DWORD dwFlags)
21 {
22  //trace("completion called dwFlags %ld cbTransferred %ld lpOverlapped %p dwFlags %ld\n", dwError, cbTransferred, lpOverlapped, dwFlags);
23  count++;
24  ok(count == 1, "completion sould be called only once count = %d\n", count);
25  ok(dwError == 0, "dwError = %ld\n", dwError);
26  ok(cbTransferred == RECV_BUF, "cbTransferred %ld != %d\n", cbTransferred, RECV_BUF);
27  ok(lpOverlapped != NULL, "lpOverlapped %p\n", lpOverlapped);
28  if (lpOverlapped)
29  {
30  ok(lpOverlapped->hEvent != INVALID_HANDLE_VALUE, "lpOverlapped->hEvent %p\n", lpOverlapped->hEvent);
31  if (lpOverlapped->hEvent != INVALID_HANDLE_VALUE)
32  WSASetEvent(lpOverlapped->hEvent);
33  }
34 }
35 
37 {
38  const char szDummyBytes[RECV_BUF] = { 0xFF, 0x00, 0xFF, 0x00 };
39 
40  char szBuf[RECV_BUF];
41  char szRecvBuf[RECV_BUF];
42  int iResult, err;
43  SOCKET sck;
44  WSADATA wdata;
46  DWORD dwRecv, dwSent, dwFlags;
48  char szGetRequest[] = "GET / HTTP/1.0\r\n\r\n";
49  struct fd_set readable;
50  BOOL ret;
51 
52  /* Start up Winsock */
53  iResult = WSAStartup(MAKEWORD(2, 2), &wdata);
54  ok(iResult == 0, "WSAStartup failed, iResult == %d\n", iResult);
55 
56  /* If we call recv without a socket, it should return with an error and do nothing. */
57  memcpy(szBuf, szDummyBytes, RECV_BUF);
58  buffers.buf = szBuf;
59  buffers.len = sizeof(szBuf);
60  dwFlags = 0;
61  dwRecv = 0;
62  iResult = WSARecv(0, &buffers, 1, &dwRecv, &dwFlags, NULL, NULL);
63  ok(iResult == SOCKET_ERROR, "iRseult = %d\n", iResult);
64  ok(!memcmp(szBuf, szDummyBytes, RECV_BUF), "not equal\n");
65 
66  /* Create the socket */
68  if(sck == INVALID_SOCKET)
69  {
70  WSACleanup();
71  skip("CreateSocket failed. Aborting test.\n");
72  return;
73  }
74 
75  /* Now we can pass at least a socket, but we have no connection yet. Should return with an error and do nothing. */
76  memcpy(szBuf, szDummyBytes, RECV_BUF);
77  buffers.buf = szBuf;
78  buffers.len = sizeof(szBuf);
79  dwFlags = 0;
80  dwRecv = 0;
81  iResult = WSARecv(sck, &buffers, 1, &dwRecv, &dwFlags, NULL, NULL);
82  ok(iResult == SOCKET_ERROR, "iResult = %d\n", iResult);
83  ok(!memcmp(szBuf, szDummyBytes, RECV_BUF), "not equal\n");
84 
85  /* Connect to "www.reactos.org" */
86  if (!ConnectToReactOSWebsite(sck))
87  {
88  WSACleanup();
89  skip("ConnectToReactOSWebsite failed. Aborting test.\n");
90  return;
91  }
92 
93  /* prepare overlapped */
94  memset(&overlapped, 0, sizeof(overlapped));
95  overlapped.hEvent = WSACreateEvent();
96 
97  /* Send the GET request */
98  buffers.buf = szGetRequest;
99  buffers.len = lstrlenA(szGetRequest);
100  dwSent = 0;
101  WSASetLastError(0xdeadbeef);
102  iResult = WSASend(sck, &buffers, 1, &dwSent, 0, &overlapped, NULL);
103  err = WSAGetLastError();
104  ok(iResult == 0 || (iResult == SOCKET_ERROR && err == WSA_IO_PENDING), "iResult = %d, %d\n", iResult, err);
105  if (err == WSA_IO_PENDING)
106  {
108  ok(iResult == WSA_WAIT_EVENT_0, "WSAWaitForMultipleEvents failed %d\n", iResult);
109  ret = WSAGetOverlappedResult(sck, &overlapped, &dwSent, TRUE, &dwFlags);
110  ok(ret, "WSAGetOverlappedResult failed %d\n", WSAGetLastError());
111  }
112  ok(dwSent == strlen(szGetRequest), "dwSent %ld != %d\n", dwSent, strlen(szGetRequest));
113 #if 0 /* break windows too */
114  /* Shutdown the SEND connection */
115  iResult = shutdown(sck, SD_SEND);
116  ok(iResult != SOCKET_ERROR, "iResult = %d\n", iResult);
117 #endif
118  /* Wait until we're ready to read */
119  FD_ZERO(&readable);
120  FD_SET(sck, &readable);
121 
122  iResult = select(0, &readable, NULL, NULL, NULL);
123  ok(iResult != SOCKET_ERROR, "iResult = %d\n", iResult);
124 
125  /* Receive the data. */
126  buffers.buf = szBuf;
127  buffers.len = sizeof(szBuf);
128  dwRecv = sizeof(szBuf);
129  iResult = WSARecv(sck, &buffers, 1, &dwRecv, &dwFlags, NULL, NULL);
130  ok(iResult != SOCKET_ERROR, "iResult = %d\n", iResult);
131  ok(dwRecv == sizeof(szBuf), "dwRecv %ld != %d\n", dwRecv, sizeof(szBuf));
132  /* MSG_PEEK is invalid for overlapped (MSDN), but passes??? */
133  buffers.buf = szRecvBuf;
134  buffers.len = sizeof(szRecvBuf);
135  dwFlags = MSG_PEEK;
136  dwRecv = sizeof(szRecvBuf);
137  ok(overlapped.hEvent != NULL, "WSACreateEvent failed %d\n", WSAGetLastError());
138  WSASetLastError(0xdeadbeef);
139  iResult = WSARecv(sck, &buffers, 1, &dwRecv, &dwFlags, &overlapped, NULL);
140  err = WSAGetLastError();
141  ok(iResult == 0 || (iResult == SOCKET_ERROR && err == WSA_IO_PENDING), "iResult = %d, %d\n", iResult, err);
142  if (err == WSA_IO_PENDING)
143  {
145  ok(iResult == WSA_WAIT_EVENT_0, "WSAWaitForMultipleEvents failed %d\n", iResult);
146  ret = WSAGetOverlappedResult(sck, &overlapped, &dwRecv, TRUE, &dwFlags);
147  ok(ret, "WSAGetOverlappedResult failed %d\n", WSAGetLastError());
148  }
149  ok(dwRecv == sizeof(szRecvBuf), "dwRecv %ld != %d\n", dwRecv, sizeof(szRecvBuf));
150  /* normal overlapped, no completion */
151  buffers.buf = szBuf;
152  buffers.len = sizeof(szBuf);
153  dwFlags = 0;
154  dwRecv = sizeof(szBuf);
155  WSAResetEvent(overlapped.hEvent);
156  WSASetLastError(0xdeadbeef);
157  iResult = WSARecv(sck, &buffers, 1, &dwRecv, &dwFlags, &overlapped, NULL);
158  err = WSAGetLastError();
159  ok(iResult == 0 || (iResult == SOCKET_ERROR && err == WSA_IO_PENDING), "iResult = %d, %d\n", iResult, err);
160  if (err == WSA_IO_PENDING)
161  {
163  ok(iResult == WSA_WAIT_EVENT_0, "WSAWaitForMultipleEvents failed %d\n", iResult);
164  ret = WSAGetOverlappedResult(sck, &overlapped, &dwRecv, TRUE, &dwFlags);
165  ok(ret, "WSAGetOverlappedResult failed %d\n", WSAGetLastError());
166  }
167  ok(dwRecv == sizeof(szBuf), "dwRecv %ld != %d\n", dwRecv, sizeof(szBuf));
168  ok(memcmp(szRecvBuf, szBuf, sizeof(szBuf)) == 0, "MSG_PEEK shouldn't have moved the pointer\n");
169  /* overlapped with completion */
170  dwFlags = 0;
171  dwRecv = sizeof(szBuf);
172  WSAResetEvent(overlapped.hEvent);
173  WSASetLastError(0xdeadbeef);
174  iResult = WSARecv(sck, &buffers, 1, &dwRecv, &dwFlags, &overlapped, &completion);
175  err = WSAGetLastError();
176  ok(iResult == 0 || (iResult == SOCKET_ERROR && err == WSA_IO_PENDING), "iResult = %d, %d\n", iResult, err);
177  if (err == WSA_IO_PENDING)
178  {
180  ok(iResult == WSA_WAIT_EVENT_0, "WSAWaitForMultipleEvents failed %d\n", iResult);
181  ret = WSAGetOverlappedResult(sck, &overlapped, &dwRecv, TRUE, &dwFlags);
182  ok(ret, "WSAGetOverlappedResult failed %d\n", WSAGetLastError());
183  }
184  ret = WSACloseEvent(overlapped.hEvent);
185  ok(ret, "WSACloseEvent failed %d\n", WSAGetLastError());
186  ok(dwRecv == sizeof(szBuf), "dwRecv %ld != %d\n", dwRecv, sizeof(szBuf));
187  /* no overlapped with completion */
188  dwFlags = 0;
189  dwRecv = sizeof(szBuf);
190  WSASetLastError(0xdeadbeef);
191  /* call doesn't fail, but completion is not called */
192  iResult = WSARecv(sck, &buffers, 1, &dwRecv, &dwFlags, NULL, &completion);
193  err = WSAGetLastError();
194  ok(iResult == 0 || (iResult == SOCKET_ERROR && err == WSA_IO_PENDING), "iResult = %d, %d\n", iResult, err);
195  ok(err == 0, "WSARecv failed %d\n", err);
196  ok(dwRecv == sizeof(szBuf), "dwRecv %ld != %d and 0\n", dwRecv, sizeof(szBuf));
197 
198  closesocket(sck);
199  WSACleanup();
200  return;
201 }
202 
204 {
205  Test_WSARecv();
206 }
207 
Definition: winsock.h:66
#define SOCKET_ERROR
Definition: winsock.h:333
#define WSA_WAIT_EVENT_0
Definition: winsock2.h:598
int ConnectToReactOSWebsite(SOCKET sck)
Definition: helpers.c:27
#define WSA_FLAG_OVERLAPPED
Definition: winsock2.h:466
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define MAKEWORD(a, b)
Definition: typedefs.h:248
#define TRUE
Definition: types.h:120
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define closesocket
Definition: precomp.h:57
#define CALLBACK
Definition: compat.h:35
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
#define INVALID_HANDLE_VALUE
Definition: compat.h:479
const GLuint * buffers
Definition: glext.h:5916
DWORD WSAAPI WSAWaitForMultipleEvents(IN DWORD cEvents, IN CONST WSAEVENT FAR *lphEvents, IN BOOL fWaitAll, IN DWORD dwTimeout, IN BOOL fAlertable)
Definition: event.c:75
#define FD_ZERO(set)
Definition: winsock.h:96
BOOL WSAAPI WSAGetOverlappedResult(IN SOCKET s, IN LPWSAOVERLAPPED lpOverlapped, OUT LPDWORD lpcbTransfer, IN BOOL fWait, OUT LPDWORD lpdwFlags)
Definition: sockctrl.c:603
#define FD_SET(fd, set)
Definition: winsock.h:89
void Test_WSARecv()
Definition: WSARecv.c:36
BOOL WSAAPI WSAResetEvent(IN WSAEVENT hEvent)
Definition: event.c:53
#define WSARecv_TIMEOUT
Definition: WSARecv.c:11
unsigned int BOOL
Definition: ntddk_ex.h:94
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
SOCKET WSAAPI WSASocketW(IN INT af, IN INT type, IN INT protocol, IN LPWSAPROTOCOL_INFOW lpProtocolInfo, IN GROUP g, IN DWORD dwFlags)
Definition: socklife.c:490
BOOL WSAAPI WSACloseEvent(IN WSAEVENT hEvent)
Definition: event.c:23
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
smooth NULL
Definition: ftsmooth.c:416
#define WSA_IO_PENDING
Definition: winsock2.h:589
VOID WSAAPI WSASetLastError(IN INT iError)
Definition: dllmain.c:123
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define INVALID_SOCKET
Definition: winsock.h:332
INT WSAAPI shutdown(IN SOCKET s, IN INT how)
Definition: sockctrl.c:506
START_TEST(WSARecv)
Definition: WSARecv.c:203
unsigned long DWORD
Definition: ntddk_ex.h:95
#define RECV_BUF
Definition: WSARecv.c:10
BOOL WSAAPI WSASetEvent(IN WSAEVENT hEvent)
Definition: event.c:64
int ret
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1175
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define err(...)
INT WSAAPI WSASend(IN SOCKET s, IN LPWSABUF lpBuffers, IN DWORD dwBufferCount, OUT LPDWORD lpNumberOfBytesSent, IN DWORD dwFlags, IN LPWSAOVERLAPPED lpOverlapped, IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
Definition: send.c:145
namespace GUID const ADDRINFOEXW ADDRINFOEXW struct timeval OVERLAPPED * overlapped
Definition: sock.c:82
INT WSAAPI WSARecv(IN SOCKET s, IN OUT LPWSABUF lpBuffers, IN DWORD dwBufferCount, OUT LPDWORD lpNumberOfBytesRecvd, IN OUT LPDWORD lpFlags, IN LPWSAOVERLAPPED lpOverlapped, IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
Definition: recv.c:155
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
WSAEVENT WSAAPI WSACreateEvent(VOID)
Definition: event.c:42
_In_ HANDLE _In_ DWORD _In_ DWORD _Inout_opt_ LPOVERLAPPED lpOverlapped
Definition: mswsock.h:90
#define SD_SEND
Definition: winsock.h:55
#define ok(value,...)
Definition: atltest.h:57
void CALLBACK completion(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags)
Definition: WSARecv.c:16
#define WSAOVERLAPPED
Definition: winsock2.h:587
#define skip(...)
Definition: atltest.h:64
#define AF_INET
Definition: tcpip.h:117
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCK_STREAM
Definition: tcpip.h:118
#define memset(x, y, z)
Definition: compat.h:39
#define MSG_PEEK
Definition: winsock.h:222