ReactOS 0.4.16-dev-197-g92996da
getservbyname.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS API tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for getservbyname
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8#include "ws2_32.h"
9
11{
12 WSADATA WsaData;
13 struct servent *Serv;
14 const struct
15 {
16 PCSTR Name;
17 struct
18 {
19 PCSTR Proto;
20 int Port;
21 PCSTR Name;
22 PCSTR Aliases[5+1];
23 } Protos[3+1];
24 } Tests[] =
25 {
26 { "" },
27 { "xyzzy" },
28 { "http", { { "tcp", 80, "http", { "www", "www-http" } },
29 { "udp", 0 },
30 { "xyz", 0 } } },
31 { "smtp", { { "tcp", 25, "smtp", { "mail" } } } },
32 { "mail", { { "tcp", 25, "smtp", { "mail" } } } },
33 { "microsoft-ds", { { "tcp", 445, "microsoft-ds", },
34 { "udp", 445, "microsoft-ds", } } },
35 { "cmd", { { "tcp", 514, "cmd", { "shell" } },
36 { "udp", 0 } } },
37 { "syslog", { { "udp", 514, "syslog" },
38 { "tcp", 0 } } },
39 { "directplaysrvr", { { "tcp", 47624, "directplaysrvr" },
40 { "udp", 47624, "directplaysrvr" } } },
41 };
42 ULONG i, Proto, Alias;
43 int Error;
44 ULONG ExpectProto;
45
46 /* not yet initialized */
47 Serv = getservbyname(NULL, NULL);
49 ok(Serv == NULL, "Serv = %p\n", Serv);
50 ok(Error == WSANOTINITIALISED, "Error = %d\n", Error);
51
52 Error = WSAStartup(MAKEWORD(2, 2), &WsaData);
53 ok_dec(Error, 0);
54
55 for (i = 0; i < RTL_NUMBER_OF(Tests); i++)
56 {
57 Proto = 0;
58 do
59 {
60 Serv = getservbyname(Tests[i].Name, Tests[i].Protos[Proto].Proto);
62
63 /* For a NULL proto we expect the same as the first array entry */
64 ExpectProto = Proto;
65 if (Tests[i].Protos[Proto].Proto == NULL)
66 {
67 ExpectProto = 0;
68 }
69
70 if (Tests[i].Protos[ExpectProto].Port == 0)
71 {
72 ok(Serv == NULL, "[%s, %s] getservbyname succeeded unexpectedly\n",
73 Tests[i].Name, Tests[i].Protos[Proto].Proto);
74 ok(Error == WSANO_DATA, "[%s, %s] getservbyname returned error %d\n",
75 Tests[i].Name, Tests[i].Protos[Proto].Proto, Error);
76 continue;
77 }
78 else
79 {
80 ok(Serv != NULL, "[%s, %s] getservbyname failed with %d\n",
81 Tests[i].Name, Tests[i].Protos[Proto].Proto, Error);
82 }
83
84 if (Serv == NULL)
85 {
86 continue;
87 }
88
89 /* Check name */
90 ok(!strcmp(Serv->s_name, Tests[i].Protos[ExpectProto].Name),
91 "[%s, %s] s_name = '%s', expected '%s'\n",
92 Tests[i].Name, Tests[i].Protos[Proto].Proto, Serv->s_name, Tests[i].Protos[ExpectProto].Name);
93
94 /* Check aliases */
95 ok(Serv->s_aliases != NULL, "[%s, %s] s_aliases = NULL\n",
96 Tests[i].Name, Tests[i].Protos[Proto].Proto);
97 for (Alias = 0; Serv->s_aliases; Alias++)
98 {
99 if (Alias >= RTL_NUMBER_OF(Tests[i].Protos[ExpectProto].Aliases))
100 {
101 ok(0, "[%s, %s] Too many aliases\n",
102 Tests[i].Name, Tests[i].Protos[Proto].Proto);
103 break;
104 }
105 if (Serv->s_aliases[Alias] == NULL)
106 {
107 ok(Tests[i].Protos[ExpectProto].Aliases[Alias] == NULL,
108 "[%s, %s] getservbyname did not return expected alias '%s'\n",
109 Tests[i].Name, Tests[i].Protos[Proto].Proto, Tests[i].Protos[ExpectProto].Aliases[Alias]);
110 break;
111 }
112 if (Tests[i].Protos[ExpectProto].Aliases[Alias] == NULL)
113 {
114 ok(Serv->s_aliases[Alias] == NULL,
115 "[%s, %s] getservbyname returned additional alias '%s'\n",
116 Tests[i].Name, Tests[i].Protos[Proto].Proto, Serv->s_aliases[Alias]);
117 break;
118 }
119
120 ok(!strcmp(Serv->s_aliases[Alias], Tests[i].Protos[ExpectProto].Aliases[Alias]),
121 "[%s, %s] Got alias '%s', expected '%s'\n",
122 Tests[i].Name, Tests[i].Protos[Proto].Proto, Serv->s_aliases[Alias],Tests[i].Protos[ExpectProto].Aliases[Alias]);
123 }
124
125 /* Check port */
126 ok(ntohs(Serv->s_port) == Tests[i].Protos[ExpectProto].Port, "[%s, %s] s_port = %d\n",
127 Tests[i].Name, Tests[i].Protos[Proto].Proto, ntohs(Serv->s_port));
128
129 /* Check proto */
130 ok(!strcmp(Serv->s_proto, Tests[i].Protos[ExpectProto].Proto), "[%s, %s] s_proto = '%s', expected '%s'\n",
131 Tests[i].Name, Tests[i].Protos[Proto].Proto, Serv->s_proto, Tests[i].Protos[ExpectProto].Proto);
132 /* We want to include one NULL past the last proto in the array */
133 } while (Tests[i].Protos[Proto++].Proto != NULL);
134 }
135
136 Error = WSACleanup();
137 ok_dec(Error, 0);
138}
struct test_data Tests[]
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
struct NameRec_ * Name
Definition: cdprocs.h:460
#define ok(value,...)
Definition: atltest.h:57
#define ok_dec(expression, result)
Definition: atltest.h:101
#define START_TEST(x)
Definition: atltest.h:75
BOOL Error
Definition: chkdsk.c:66
#define NULL
Definition: types.h:112
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
PSERVENT WSAAPI getservbyname(IN const char FAR *name, IN const char FAR *proto)
Definition: getxbyxx.c:500
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
CPPORT Port[4]
Definition: headless.c:35
#define ntohs(x)
Definition: module.h:210
char * s_name
Definition: winsock.h:159
short s_port
Definition: winsock.h:165
char * s_proto
Definition: winsock.h:166
char ** s_aliases
Definition: winsock.h:160
#define MAKEWORD(a, b)
Definition: typedefs.h:248
const char * PCSTR
Definition: typedefs.h:52
uint32_t ULONG
Definition: typedefs.h:59
#define WSANOTINITIALISED
Definition: winerror.h:1987
#define WSANO_DATA
Definition: winerror.h:2003
int PASCAL FAR WSAGetLastError(void)
Definition: dllmain.c:112
int PASCAL FAR WSACleanup(void)
Definition: startup.c:60