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 10 int Test_ioctlsocket() 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 91 START_TEST(ioctlsocket) 92 { 93 Test_ioctlsocket(); 94 } 95 96