[APITESTS]
[reactos.git] / rostests / apitests / ws2_32 / recv.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for recv
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 #define RECV_BUF 4
14
15 /* For valid test results, the ReactOS Website needs to return at least 8 bytes on a "GET / HTTP/1.0" request.
16 Also the first 4 bytes and the last 4 bytes need to be different.
17 Both factors usually apply on standard HTTP responses. */
18
19 int Test_recv()
20 {
21 const char szDummyBytes[RECV_BUF] = {0xFF, 0x00, 0xFF, 0x00};
22
23 char szBuf1[RECV_BUF];
24 char szBuf2[RECV_BUF];
25 int iResult;
26 SOCKET sck;
27 WSADATA wdata;
28
29 /* Start up Winsock */
30 iResult = WSAStartup(MAKEWORD(2, 2), &wdata);
31 ok(iResult == 0, "WSAStartup failed, iResult == %d\n", iResult);
32
33 /* If we call recv without a socket, it should return with an error and do nothing. */
34 memcpy(szBuf1, szDummyBytes, RECV_BUF);
35 iResult = recv(0, szBuf1, RECV_BUF, 0);
36 ok(iResult == SOCKET_ERROR, "iRseult = %d\n", iResult);
37 ok(!memcmp(szBuf1, szDummyBytes, RECV_BUF), "not equal\n");
38
39 /* Create the socket */
40 if (!CreateSocket(&sck))
41 {
42 ok(0, "CreateSocket failed. Aborting test.\n");
43 return 0;
44 }
45
46 /* Now we can pass at least a socket, but we have no connection yet. Should return with an error and do nothing. */
47 memcpy(szBuf1, szDummyBytes, RECV_BUF);
48 iResult = recv(sck, szBuf1, RECV_BUF, 0);
49 ok(iResult == SOCKET_ERROR, "iResult = %d\n", iResult);
50 ok(!memcmp(szBuf1, szDummyBytes, RECV_BUF), "not equal\n");
51
52 /* Connect to "www.reactos.org" */
53 if (!ConnectToReactOSWebsite(sck))
54 {
55 ok(0, "ConnectToReactOSWebsite failed. Aborting test.\n");
56 return 0;
57 }
58
59 /* Send the GET request */
60 if (!GetRequestAndWait(sck))
61 {
62 ok(0, "GetRequestAndWait failed. Aborting test.\n");
63 return 0;
64 }
65
66 /* Receive the data.
67 MSG_PEEK will not change the internal number of bytes read, so that a subsequent request should return the same bytes again. */
68 SCKTEST(recv(sck, szBuf1, RECV_BUF, MSG_PEEK));
69 SCKTEST(recv(sck, szBuf2, RECV_BUF, 0));
70 ok(!memcmp(szBuf1, szBuf2, RECV_BUF), "not equal\n");
71
72 /* The last recv() call moved the internal file pointer, so that the next request should return different data. */
73 SCKTEST(recv(sck, szBuf1, RECV_BUF, 0));
74 ok(memcmp(szBuf1, szBuf2, RECV_BUF), "equal\n");
75
76 closesocket(sck);
77 WSACleanup();
78 return 1;
79 }
80
81 START_TEST(recv)
82 {
83 Test_recv();
84 }
85