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>
11 /* For valid test results, the ReactOS Website needs to return at least 2 bytes on a "GET / HTTP/1.0" request. */
13 Test_ioctlsocket(PTESTINFO pti
)
22 /* Start up Winsock */
23 TEST(WSAStartup(MAKEWORD(2, 2), &wdata
) == 0);
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);
30 /* Create the socket */
31 iResult
= CreateSocket(pti
, &sck
);
32 if(iResult
!= APISTATUS_NORMAL
)
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);
40 /* Connect to "www.reactos.org" */
41 iResult
= ConnectToReactOSWebsite(pti
, sck
);
42 if(iResult
!= APISTATUS_NORMAL
)
45 /* Even with a connection, there shouldn't be any bytes available. */
46 TEST(ioctlsocket(sck
, FIONREAD
, &BytesAvailable
) == 0);
47 TEST(BytesAvailable
== 0);
49 /* Send the GET request */
50 iResult
= GetRequestAndWait(pti
, sck
);
51 if(iResult
!= APISTATUS_NORMAL
)
54 /* Try ioctlsocket with FIONREAD. There should be bytes available now. */
55 SCKTEST(ioctlsocket(sck
, FIONREAD
, &BytesAvailable
));
56 TEST(BytesAvailable
!= 0);
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
);
64 BytesToRead
= BytesAvailable
- BytesToRead
;
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
);
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
);
75 /* Try it for the last time. BytesAvailable should be at 0 now. */
76 SCKTEST(ioctlsocket(sck
, FIONREAD
, &BytesAvailable
));
77 TEST(BytesAvailable
== 0);
82 return APISTATUS_NORMAL
;