[FASTFAT]
[reactos.git] / rostests / apitests / ws2_32 / tests / recv.c
1 /*
2 * PROJECT: ws2_32.dll API tests
3 * LICENSE: GPLv2 or any later version
4 * FILE: apitests/ws2_32/tests/recv.c
5 * PURPOSE: Tests for the recv function
6 * COPYRIGHT: Copyright 2008 Colin Finck <mail@colinfinck.de>
7 */
8
9 #include "../ws2_32.h"
10
11 #define RECV_BUF 4
12
13 /* For valid test results, the ReactOS Website needs to return at least 8 bytes on a "GET / HTTP/1.0" request.
14 Also the first 4 bytes and the last 4 bytes need to be different.
15 Both factors usually apply on standard HTTP responses. */
16 INT
17 Test_recv(PTESTINFO pti)
18 {
19 const char szDummyBytes[RECV_BUF] = {0xFF, 0x00, 0xFF, 0x00};
20
21 char szBuf1[RECV_BUF];
22 char szBuf2[RECV_BUF];
23 int iResult;
24 SOCKET sck;
25 WSADATA wdata;
26
27 /* Start up Winsock */
28 TEST(WSAStartup(MAKEWORD(2, 2), &wdata) == 0);
29
30 /* If we call recv without a socket, it should return with an error and do nothing. */
31 memcpy(szBuf1, szDummyBytes, RECV_BUF);
32 TEST(recv(0, szBuf1, RECV_BUF, 0) == SOCKET_ERROR);
33 TEST(!memcmp(szBuf1, szDummyBytes, RECV_BUF));
34
35 /* Create the socket */
36 iResult = CreateSocket(pti, &sck);
37 if(iResult != APISTATUS_NORMAL)
38 return iResult;
39
40 /* Now we can pass at least a socket, but we have no connection yet. Should return with an error and do nothing. */
41 memcpy(szBuf1, szDummyBytes, RECV_BUF);
42 TEST(recv(sck, szBuf1, RECV_BUF, 0) == SOCKET_ERROR);
43 TEST(!memcmp(szBuf1, szDummyBytes, RECV_BUF));
44
45 /* Connect to "www.reactos.org" */
46 iResult = ConnectToReactOSWebsite(pti, sck);
47 if(iResult != APISTATUS_NORMAL)
48 return iResult;
49
50 /* Send the GET request */
51 iResult = GetRequestAndWait(pti, sck);
52 if(iResult != APISTATUS_NORMAL)
53 return iResult;
54
55 /* Receive the data.
56 MSG_PEEK will not change the internal number of bytes read, so that a subsequent request should return the same bytes again. */
57 SCKTEST(recv(sck, szBuf1, RECV_BUF, MSG_PEEK));
58 SCKTEST(recv(sck, szBuf2, RECV_BUF, 0));
59 TEST(!memcmp(szBuf1, szBuf2, RECV_BUF));
60
61 /* The last recv() call moved the internal file pointer, so that the next request should return different data. */
62 SCKTEST(recv(sck, szBuf1, RECV_BUF, 0));
63 TEST(memcmp(szBuf1, szBuf2, RECV_BUF));
64
65 closesocket(sck);
66 WSACleanup();
67
68 return APISTATUS_NORMAL;
69 }