[FASTFAT]
[reactos.git] / rostests / apitests / ws2_32 / tests / ioctlsocket.c
1 /*
2 * PROJECT: ws2_32.dll API tests
3 * LICENSE: GPLv2 or any later version
4 * FILE: apitests/ws2_32/tests/ioctlsocket.c
5 * PURPOSE: Tests for the ioctlsocket function
6 * COPYRIGHT: Copyright 2008 Colin Finck <mail@colinfinck.de>
7 */
8
9 #include "../ws2_32.h"
10
11 /* For valid test results, the ReactOS Website needs to return at least 2 bytes on a "GET / HTTP/1.0" request. */
12 INT
13 Test_ioctlsocket(PTESTINFO pti)
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 TEST(WSAStartup(MAKEWORD(2, 2), &wdata) == 0);
24
25 /* If we call ioctlsocket without a socket, it should return with an error and do nothing. */
26 BytesAvailable = 0xdeadbeef;
27 TEST(ioctlsocket(0, FIONREAD, &BytesAvailable) == SOCKET_ERROR);
28 TEST(BytesAvailable == 0xdeadbeef);
29
30 /* Create the socket */
31 iResult = CreateSocket(pti, &sck);
32 if(iResult != APISTATUS_NORMAL)
33 return iResult;
34
35 /* Now we can pass at least a socket, but we have no connection yet. The function should return 0. */
36 BytesAvailable = 0xdeadbeef;
37 TEST(ioctlsocket(sck, FIONREAD, &BytesAvailable) == 0);
38 TEST(BytesAvailable == 0);
39
40 /* Connect to "www.reactos.org" */
41 iResult = ConnectToReactOSWebsite(pti, sck);
42 if(iResult != APISTATUS_NORMAL)
43 return iResult;
44
45 /* Even with a connection, there shouldn't be any bytes available. */
46 TEST(ioctlsocket(sck, FIONREAD, &BytesAvailable) == 0);
47 TEST(BytesAvailable == 0);
48
49 /* Send the GET request */
50 iResult = GetRequestAndWait(pti, sck);
51 if(iResult != APISTATUS_NORMAL)
52 return iResult;
53
54 /* Try ioctlsocket with FIONREAD. There should be bytes available now. */
55 SCKTEST(ioctlsocket(sck, FIONREAD, &BytesAvailable));
56 TEST(BytesAvailable != 0);
57
58 /* Get half of the data */
59 BytesToRead = BytesAvailable / 2;
60 pszBuf = (LPSTR) HeapAlloc(g_hHeap, 0, BytesToRead);
61 SCKTEST(recv(sck, pszBuf, BytesToRead, 0));
62 HeapFree(g_hHeap, 0, pszBuf);
63
64 BytesToRead = BytesAvailable - BytesToRead;
65
66 /* Now try ioctlsocket again. BytesAvailable should be at the value saved in BytesToRead now. */
67 SCKTEST(ioctlsocket(sck, FIONREAD, &BytesAvailable));
68 TEST(BytesAvailable == BytesToRead);
69
70 /* Read those bytes */
71 pszBuf = (LPSTR) HeapAlloc(g_hHeap, 0, BytesToRead);
72 SCKTEST(recv(sck, pszBuf, BytesToRead, 0));
73 HeapFree(g_hHeap, 0, pszBuf);
74
75 /* Try it for the last time. BytesAvailable should be at 0 now. */
76 SCKTEST(ioctlsocket(sck, FIONREAD, &BytesAvailable));
77 TEST(BytesAvailable == 0);
78
79 closesocket(sck);
80 WSACleanup();
81
82 return APISTATUS_NORMAL;
83 }