ReactOS  0.4.14-dev-98-gb0d4763
getaddrinfo.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPLv2+ - See COPYING in the top level directory
4  * PURPOSE: Test for getaddrinfo
5  * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include "ws2_32.h"
9 
10 #define ok_addrinfo(ai, flags, family, socktype, protocol, addrlen) do \
11 { \
12  ok_hex((ai)->ai_flags, flags); \
13  ok_dec((ai)->ai_family, family); \
14  ok_dec((ai)->ai_socktype, socktype); \
15  ok_dec((ai)->ai_protocol, protocol); \
16  ok_dec((ai)->ai_addrlen, addrlen); \
17 } while (0)
18 
19 #define ok_sockaddr_in(sockaddr, family, port, addr) do \
20 { \
21  int _i; \
22  ok_dec(((SOCKADDR_IN *)(sockaddr))->sin_family, family); \
23  ok_dec(ntohs(((SOCKADDR_IN *)(sockaddr))->sin_port), port); \
24  ok_hex(((SOCKADDR_IN *)(sockaddr))->sin_addr.S_un.S_addr, \
25  inet_addr(addr)); \
26  for (_i = 0; _i < 7; _i++) \
27  ok_dec(((SOCKADDR_IN *)(sockaddr))->sin_zero[_i], 0); \
28 } while (0)
29 
30 CHAR LocalAddress[sizeof("255.255.255.255")];
31 
32 static
33 VOID
35 {
36  int Error;
38  ADDRINFOA Hints;
39  struct
40  {
42  PCSTR ExpectedAddress;
43  INT Flags;
44  } Tests[] =
45  {
46  { "", LocalAddress },
47  { " ", NULL },
48  { "doesntexist.example.com", NULL },
49  { "localhost", "127.0.0.1" },
50  { "localhost:80", NULL },
51  { "7.8.9.10", "7.8.9.10", AI_NUMERICHOST },
52  { "0.0.0.0", "0.0.0.0", AI_NUMERICHOST },
53  { "255.255.255.255", "255.255.255.255", AI_NUMERICHOST },
54  { "0.0.0.0 ", "0.0.0.0", /* no AI_NUMERICHOST */ },
55  { "0.0.0.0:80", NULL },
56  { "0.0.0.0.0", NULL },
57  { "1.1.1.256", NULL },
58  { "1.2.3", NULL },
59  { "1.2.3.0x4", "1.2.3.4", AI_NUMERICHOST },
60  { "1.2.3.010", "1.2.3.8", AI_NUMERICHOST },
61  /* let's just assume this one doesn't change any time soon ;) */
62  { "google-public-dns-a.google.com", "8.8.8.8" },
63  };
64  const INT TestCount = sizeof(Tests) / sizeof(Tests[0]);
65  INT i;
66 
67  /* make sure we don't get IPv6 responses */
68  ZeroMemory(&Hints, sizeof(Hints));
69  Hints.ai_family = AF_INET;
70 
71  trace("Nodes\n");
72  for (i = 0; i < TestCount; i++)
73  {
74  trace("%d: '%s'\n", i, Tests[i].NodeName);
75  StartSeh()
77  Error = getaddrinfo(Tests[i].NodeName, NULL, &Hints, &AddrInfo);
78  if (Tests[i].ExpectedAddress)
79  {
80  ok_dec(Error, 0);
81  ok_dec(WSAGetLastError(), 0);
83  "AddrInfo = %p\n", AddrInfo);
84  }
85  else
86  {
90  }
91  if (!Error && AddrInfo && AddrInfo != InvalidPointer)
92  {
94  0, 0, sizeof(SOCKADDR_IN));
95  ok_ptr(AddrInfo->ai_canonname, NULL);
97  0, Tests[i].ExpectedAddress);
98  ok_ptr(AddrInfo->ai_next, NULL);
100  }
102  }
103 }
104 
105 static
106 VOID
108 {
109  int Error;
111  ADDRINFOA Hints;
112  struct
113  {
115  INT ExpectedPort;
116  INT SockType;
117  } Tests[] =
118  {
119  { "", 0 },
120  { "0", 0 },
121  { "1", 1 },
122  { "a", -1 },
123  { "010", 10 },
124  { "0x1a", -1 },
125  { "http", 80, SOCK_STREAM },
126  { "smtp", 25, SOCK_STREAM },
127  { "mail", 25, SOCK_STREAM }, /* alias for smtp */
128  { "router", 520, SOCK_DGRAM },
129  { "domain", 53, 0 /* DNS supports both UDP and TCP */ },
130  { ":0", -1 },
131  { "123", 123 },
132  { " 123", 123 },
133  { " 123", 123 },
134  { "32767", 32767 },
135  { "32768", 32768 },
136  { "65535", 65535 },
137  { "65536", 0 },
138  { "65537", 1 },
139  { "65540", 4 },
140  { "65536", 0 },
141  { "4294967295", 65535 },
142  { "4294967296", 65535 },
143  { "9999999999", 65535 },
144  { "999999999999999999999999999999999999", 65535 },
145  { "+5", 5 },
146  { "-1", 65535 },
147  { "-4", 65532 },
148  { "-65534", 2 },
149  { "-65535", 1 },
150  { "-65536", 0 },
151  { "-65537", 65535 },
152  { "28a", -1 },
153  { "28 ", -1 },
154  { "a28", -1 },
155  };
156  const INT TestCount = sizeof(Tests) / sizeof(Tests[0]);
157  INT i;
158 
159  /* make sure we don't get IPv6 responses */
160  ZeroMemory(&Hints, sizeof(Hints));
161  Hints.ai_family = AF_INET;
162 
163  trace("Services\n");
164  for (i = 0; i < TestCount; i++)
165  {
166  trace("%d: '%s'\n", i, Tests[i].ServiceName);
167  StartSeh()
170  if (Tests[i].ExpectedPort != -1)
171  {
172  ok_dec(Error, 0);
173  ok_dec(WSAGetLastError(), 0);
175  "AddrInfo = %p\n", AddrInfo);
176  }
177  else
178  {
181  ok_ptr(AddrInfo, NULL);
182  }
183  if (!Error && AddrInfo && AddrInfo != InvalidPointer)
184  {
186  Tests[i].SockType, 0, sizeof(SOCKADDR_IN));
187  ok_ptr(AddrInfo->ai_canonname, NULL);
188  ok_sockaddr_in(AddrInfo->ai_addr, AF_INET,
189  Tests[i].ExpectedPort, "127.0.0.1");
190  ok_ptr(AddrInfo->ai_next, NULL);
192  }
194  }
195 }
196 
198 {
199  WSADATA WsaData;
200  int Error;
202  ADDRINFOA Hints;
203  CHAR LocalHostName[128];
204  struct hostent *Hostent;
205 
206  /* not yet initialized */
207  StartSeh()
211  StartSeh()
217 
218  Error = getaddrinfo("127.0.0.1", "80", NULL, &AddrInfo);
220 
221  Error = WSAStartup(MAKEWORD(2, 2), &WsaData);
222  ok_dec(Error, 0);
223 
224  /* initialize LocalAddress for tests */
225  Error = gethostname(LocalHostName, sizeof(LocalHostName));
226  ok_dec(Error, 0);
227  ok_dec(WSAGetLastError(), 0);
228  trace("Local host name is '%s'\n", LocalHostName);
229  Hostent = gethostbyname(LocalHostName);
230  ok(Hostent != NULL, "gethostbyname failed with %d\n", WSAGetLastError());
231  if (Hostent && Hostent->h_addr_list[0] && Hostent->h_length == sizeof(IN_ADDR))
232  {
234  memcpy(&Address, Hostent->h_addr_list[0], sizeof(Address));
236  }
237  trace("Local address is '%s'\n", LocalAddress);
238  ok(LocalAddress[0] != '\0',
239  "Could not determine local address. Following test results may be wrong.\n");
240 
241  ZeroMemory(&Hints, sizeof(Hints));
242  /* parameter tests */
245  StartSeh()
250  ok_ptr(AddrInfo, NULL);
252 
253  TestNodeName();
254  TestServiceName();
255  /* TODO: test passing both node name and service name */
256  /* TODO: test hints */
257  /* TODO: test IPv6 */
258 
259  Error = WSACleanup();
260  ok_dec(Error, 0);
261 
262  /* not initialized anymore */
263  Error = getaddrinfo("127.0.0.1", "80", NULL, &AddrInfo);
265 }
#define ok_addrinfo(ai, flags, family, socktype, protocol, addrlen)
Definition: getaddrinfo.c:10
char ** h_addr_list
Definition: winsock.h:138
#define WSAHOST_NOT_FOUND
Definition: winerror.h:2000
#define freeaddrinfo
Definition: wspiapi.h:46
static VOID TestServiceName(VOID)
Definition: getaddrinfo.c:107
#define MAKEWORD(a, b)
Definition: typedefs.h:247
char CHAR
Definition: xmlstorage.h:175
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
START_TEST(getaddrinfo)
Definition: getaddrinfo.c:197
#define ZeroMemory
Definition: winbase.h:1642
#define ok_ptr(expression, result)
Definition: atltest.h:108
#define StartSeh()
Definition: _sntprintf.h:16
int32_t INT
Definition: typedefs.h:56
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
INT WSAAPI gethostname(OUT char FAR *name, IN INT namelen)
Definition: getxbyxx.c:397
short h_length
Definition: winsock.h:137
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
#define InvalidPointer
#define inet_ntoa(addr)
Definition: inet.h:100
#define WSANOTINITIALISED
Definition: winerror.h:1987
PHOSTENT WSAAPI gethostbyname(IN const char FAR *name)
Definition: getxbyxx.c:221
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define trace
Definition: atltest.h:70
Definition: tcpip.h:125
static VOID TestNodeName(VOID)
Definition: getaddrinfo.c:34
BOOL Error
Definition: chkdsk.c:66
_In_ PADDRINFOEXW AddrInfo
Definition: wsk.h:458
static const char * NodeName(const NOTIFICATIONLIST *item)
Definition: changenotify.c:145
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define AI_NUMERICHOST
Definition: ws2def.h:295
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:228
#define ok(value,...)
Definition: atltest.h:57
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define WSATYPE_NOT_FOUND
Definition: winerror.h:1996
#define AF_INET
Definition: tcpip.h:117
#define ok_sockaddr_in(sockaddr, family, port, addr)
Definition: getaddrinfo.c:19
const char * PCSTR
Definition: typedefs.h:51
#define SOCK_STREAM
Definition: tcpip.h:118
#define SOCK_DGRAM
Definition: winsock.h:336
return STATUS_SUCCESS
Definition: btrfs.c:2966
#define ok_dec(expression, result)
Definition: atltest.h:101
struct test_data Tests[]
#define EndSeh(ExpectedStatus)
Definition: _sntprintf.h:17
#define getaddrinfo
Definition: wspiapi.h:44
_Must_inspect_result_ _In_ ULONG _In_ PSOCKADDR LocalAddress
Definition: wsk.h:170
int ai_family
Definition: ws2def.h:666