9033aed184f0ddab3cf7aa7284b65e1feb89ac9d
[reactos.git] / rostests / apitests / ws2_32 / helpers.c
1 /*
2 * PROJECT: ws2_32.dll API tests
3 * LICENSE: GPLv2 or any later version
4 * FILE: apitests/ws2_32/helpers.c
5 * PURPOSE: Helper functions for the socket tests
6 * COPYRIGHT: Copyright 2008 Colin Finck <mail@colinfinck.de>
7 */
8
9 #include "ws2_32.h"
10
11 int CreateSocket(PTESTINFO pti, SOCKET* psck)
12 {
13 /* Create the socket */
14 *psck = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
15 TEST(*psck != INVALID_SOCKET);
16
17 if(*psck == INVALID_SOCKET)
18 {
19 printf("Winsock error code is %u\n", WSAGetLastError());
20 WSACleanup();
21 return APISTATUS_ASSERTION_FAILED;
22 }
23
24 return APISTATUS_NORMAL;
25 }
26
27 int ConnectToReactOSWebsite(PTESTINFO pti, SOCKET sck)
28 {
29 int iResult;
30 struct hostent* host;
31 struct sockaddr_in sa;
32
33 /* Connect to "www.reactos.org" */
34 host = gethostbyname("www.reactos.org");
35
36 sa.sin_family = AF_INET;
37 sa.sin_addr.s_addr = *(u_long*)host->h_addr_list[0];
38 sa.sin_port = htons(80);
39
40 SCKTEST(connect(sck, (struct sockaddr *)&sa, sizeof(sa)));
41
42 return APISTATUS_NORMAL;
43 }
44
45 int GetRequestAndWait(PTESTINFO pti, SOCKET sck)
46 {
47 const char szGetRequest[] = "GET / HTTP/1.0\r\n\r\n";
48 int iResult;
49 struct fd_set readable;
50
51 /* Send the GET request */
52 SCKTEST(send(sck, szGetRequest, strlen(szGetRequest), 0));
53 TEST(iResult == strlen(szGetRequest));
54
55 /* Shutdown the SEND connection */
56 SCKTEST(shutdown(sck, SD_SEND));
57
58 /* Wait until we're ready to read */
59 FD_ZERO(&readable);
60 FD_SET(sck, &readable);
61
62 SCKTEST(select(0, &readable, NULL, NULL, NULL));
63
64 return APISTATUS_NORMAL;
65 }