[APITESTS]
[reactos.git] / rostests / apitests / ws2_32 / ioctlsocket.c
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 <stdio.h>
9 #include <wine/test.h>
10 #include <windows.h>
11 #include "ws2_32.h"
12
13 int Test_ioctlsocket()
14 {
15 LPSTR pszBuf;
16 int iResult;
17 SOCKET sck;
18 ULONG BytesAvailable;
19 ULONG BytesToRead;
20 WSADATA wdata;
21
22 /* Start up Winsock */
23 iResult = WSAStartup(MAKEWORD(2, 2), &wdata);
24 ok(iResult == 0, "WSAStartup failed. iResult = %d\n", iResult);
25
26 /* If we call ioctlsocket without a socket, it should return with an error and do nothing. */
27 BytesAvailable = 0xdeadbeef;
28 iResult = ioctlsocket(0, FIONREAD, &BytesAvailable);
29 ok(iResult == SOCKET_ERROR, "iResult = %d\n", iResult);
30 ok(BytesAvailable == 0xdeadbeef, "BytesAvailable = %ld\n", BytesAvailable);
31
32 /* Create the socket */
33 if (!CreateSocket(&sck))
34 {
35 ok(0, "CreateSocket failed. Aborting test.\n");
36 return 0;
37 }
38
39 /* Now we can pass at least a socket, but we have no connection yet. The function should return 0. */
40 BytesAvailable = 0xdeadbeef;
41 iResult = ioctlsocket(sck, FIONREAD, &BytesAvailable);
42 ok(iResult == 0, "iResult = %d\n", iResult);
43 ok(BytesAvailable == 0, "BytesAvailable = %ld\n", BytesAvailable);
44
45 /* Connect to "www.reactos.org" */
46 if (!ConnectToReactOSWebsite(sck))
47 {
48 ok(0, "ConnectToReactOSWebsite failed. Aborting test.\n");
49 return 0;
50 }
51
52 /* Even with a connection, there shouldn't be any bytes available. */
53 iResult = ioctlsocket(sck, FIONREAD, &BytesAvailable);
54 ok(iResult == 0, "iResult = %d\n", iResult);
55 ok(BytesAvailable == 0, "BytesAvailable = %ld\n", BytesAvailable);
56
57 /* Send the GET request */
58 if (!GetRequestAndWait(sck))
59 {
60 ok(0, "GetRequestAndWait failed. Aborting test.\n");
61 return 0;
62 }
63
64 /* Try ioctlsocket with FIONREAD. There should be bytes available now. */
65 SCKTEST(ioctlsocket(sck, FIONREAD, &BytesAvailable));
66 ok(BytesAvailable != 0, "BytesAvailable = %ld\n", BytesAvailable);
67
68 /* Get half of the data */
69 BytesToRead = BytesAvailable / 2;
70 pszBuf = (LPSTR) HeapAlloc(GetProcessHeap(), 0, BytesToRead);
71 SCKTEST(recv(sck, pszBuf, BytesToRead, 0));
72 HeapFree(GetProcessHeap(), 0, pszBuf);
73
74 BytesToRead = BytesAvailable - BytesToRead;
75
76 /* Now try ioctlsocket again. BytesAvailable should be at the value saved in BytesToRead now. */
77 SCKTEST(ioctlsocket(sck, FIONREAD, &BytesAvailable));
78 ok(BytesAvailable == BytesToRead, "BytesAvailable = %ld\n", BytesAvailable);
79
80 /* Read those bytes */
81 pszBuf = (LPSTR) HeapAlloc(GetProcessHeap(), 0, BytesToRead);
82 SCKTEST(recv(sck, pszBuf, BytesToRead, 0));
83 HeapFree(GetProcessHeap(), 0, pszBuf);
84
85 /* Try it for the last time. BytesAvailable should be at 0 now. */
86 SCKTEST(ioctlsocket(sck, FIONREAD, &BytesAvailable));
87 ok(BytesAvailable == 0, "BytesAvailable = %ld\n", BytesAvailable);
88
89 closesocket(sck);
90 WSACleanup();
91 return 1;
92 }
93
94 START_TEST(ioctlsocket)
95 {
96 Test_ioctlsocket();
97 }
98