ReactOS  0.4.15-dev-448-gd6c4411
ioctlsocket.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 ioctlsocket
5  * PROGRAMMERS: Colin Finck
6  */
7 
8 #include "ws2_32.h"
9 
11 {
12  LPSTR pszBuf;
13  int iResult;
14  SOCKET sck;
15  ULONG BytesAvailable;
16  ULONG BytesToRead;
17  WSADATA wdata;
18 
19  /* Start up Winsock */
20  iResult = WSAStartup(MAKEWORD(2, 2), &wdata);
21  ok(iResult == 0, "WSAStartup failed. iResult = %d\n", iResult);
22 
23  /* If we call ioctlsocket without a socket, it should return with an error and do nothing. */
24  BytesAvailable = 0xdeadbeef;
25  iResult = ioctlsocket(0, FIONREAD, &BytesAvailable);
26  ok(iResult == SOCKET_ERROR, "iResult = %d\n", iResult);
27  ok(BytesAvailable == 0xdeadbeef, "BytesAvailable = %ld\n", BytesAvailable);
28 
29  /* Create the socket */
30  if (!CreateSocket(&sck))
31  {
32  ok(0, "CreateSocket failed. Aborting test.\n");
33  return 0;
34  }
35 
36  /* Now we can pass at least a socket, but we have no connection yet. The function should return 0. */
37  BytesAvailable = 0xdeadbeef;
38  iResult = ioctlsocket(sck, FIONREAD, &BytesAvailable);
39  ok(iResult == 0, "iResult = %d\n", iResult);
40  ok(BytesAvailable == 0, "BytesAvailable = %ld\n", BytesAvailable);
41 
42  /* Connect to "www.reactos.org" */
43  if (!ConnectToReactOSWebsite(sck))
44  {
45  ok(0, "ConnectToReactOSWebsite failed. Aborting test.\n");
46  return 0;
47  }
48 
49  /* Even with a connection, there shouldn't be any bytes available. */
50  iResult = ioctlsocket(sck, FIONREAD, &BytesAvailable);
51  ok(iResult == 0, "iResult = %d\n", iResult);
52  ok(BytesAvailable == 0, "BytesAvailable = %ld\n", BytesAvailable);
53 
54  /* Send the GET request */
55  if (!GetRequestAndWait(sck))
56  {
57  ok(0, "GetRequestAndWait failed. Aborting test.\n");
58  return 0;
59  }
60 
61  /* Try ioctlsocket with FIONREAD. There should be bytes available now. */
62  SCKTEST(ioctlsocket(sck, FIONREAD, &BytesAvailable));
63  ok(BytesAvailable != 0, "BytesAvailable = %ld\n", BytesAvailable);
64 
65  /* Get half of the data */
66  BytesToRead = BytesAvailable / 2;
67  pszBuf = (LPSTR) HeapAlloc(GetProcessHeap(), 0, BytesToRead);
68  SCKTEST(recv(sck, pszBuf, BytesToRead, 0));
69  HeapFree(GetProcessHeap(), 0, pszBuf);
70 
71  BytesToRead = BytesAvailable - BytesToRead;
72 
73  /* Now try ioctlsocket again. BytesAvailable should be at the value saved in BytesToRead now. */
74  SCKTEST(ioctlsocket(sck, FIONREAD, &BytesAvailable));
75  ok(BytesAvailable == BytesToRead, "BytesAvailable = %ld\n", BytesAvailable);
76 
77  /* Read those bytes */
78  pszBuf = (LPSTR) HeapAlloc(GetProcessHeap(), 0, BytesToRead);
79  SCKTEST(recv(sck, pszBuf, BytesToRead, 0));
80  HeapFree(GetProcessHeap(), 0, pszBuf);
81 
82  /* Try it for the last time. BytesAvailable should be at 0 now. */
83  SCKTEST(ioctlsocket(sck, FIONREAD, &BytesAvailable));
84  ok(BytesAvailable == 0, "BytesAvailable = %ld\n", BytesAvailable);
85 
86  closesocket(sck);
87  WSACleanup();
88  return 1;
89 }
90 
92 {
94 }
95 
#define SOCKET_ERROR
Definition: winsock.h:333
int ConnectToReactOSWebsite(SOCKET sck)
Definition: helpers.c:27
#define SCKTEST(_cmd_)
Definition: ws2_32.h:25
int Test_ioctlsocket()
Definition: ioctlsocket.c:10
#define MAKEWORD(a, b)
Definition: typedefs.h:247
#define closesocket
Definition: precomp.h:57
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
char * LPSTR
Definition: xmlstorage.h:182
START_TEST(ioctlsocket)
Definition: ioctlsocket.c:91
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
#define GetProcessHeap()
Definition: compat.h:404
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define ioctlsocket
Definition: precomp.h:58
static void CreateSocket(void)
Definition: telnetd.c:102
#define ok(value,...)
Definition: atltest.h:57
int GetRequestAndWait(SOCKET sck)
Definition: helpers.c:45
unsigned int ULONG
Definition: retypes.h:1
UINT_PTR SOCKET
Definition: winsock.h:47
#define FIONREAD
Definition: winsock.h:247
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23
#define HeapFree(x, y, z)
Definition: compat.h:403