[BRANCHES]
[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 <apitest.h>
10
11 #include <stdio.h>
12 #include "ws2_32.h"
13
14 int CreateSocket(SOCKET* psck)
15 {
16 /* Create the socket */
17 *psck = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
18 ok(*psck != INVALID_SOCKET, "*psck = %d\n", *psck);
19
20 if(*psck == INVALID_SOCKET)
21 {
22 printf("Winsock error code is %u\n", WSAGetLastError());
23 WSACleanup();
24 return 0;
25 }
26
27 return 1;
28 }
29
30 int ConnectToReactOSWebsite(SOCKET sck)
31 {
32 int iResult;
33 struct hostent* host;
34 struct sockaddr_in sa;
35
36 /* Connect to "www.reactos.org" */
37 host = gethostbyname("www.reactos.org");
38
39 sa.sin_family = AF_INET;
40 sa.sin_addr.s_addr = *(u_long*)host->h_addr_list[0];
41 sa.sin_port = htons(80);
42
43 SCKTEST(connect(sck, (struct sockaddr *)&sa, sizeof(sa)));
44
45 return 1;
46 }
47
48 int GetRequestAndWait(SOCKET sck)
49 {
50 const char szGetRequest[] = "GET / HTTP/1.0\r\n\r\n";
51 int iResult;
52 struct fd_set readable;
53
54 /* Send the GET request */
55 SCKTEST(send(sck, szGetRequest, strlen(szGetRequest), 0));
56 ok(iResult == strlen(szGetRequest), "iResult = %d\n", iResult);
57
58 /* Shutdown the SEND connection */
59 SCKTEST(shutdown(sck, SD_SEND));
60
61 /* Wait until we're ready to read */
62 FD_ZERO(&readable);
63 FD_SET(sck, &readable);
64
65 SCKTEST(select(0, &readable, NULL, NULL, NULL));
66
67 return 1;
68 }