ReactOS 0.4.15-dev-7918-g2a2556c
getservbyport.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 getservbyport
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 int Port;
17 struct
18 {
19 PCSTR Proto;
20 PCSTR Name;
21 PCSTR Aliases[5+1];
22 } Protos[3+1];
23 } Tests[] =
24 {
25 { 0, },
26 { -1, },
27 { 80, { { "tcp", "http", { "www", "www-http" } },
28 { "udp", NULL },
29 { "xyz", NULL } } },
30 { 65536 + 80, { { "tcp", "http", { "www", "www-http" } } } },
31 { 0xffff0050, { { "tcp", "http", { "www", "www-http" } } } },
32 { 25, { { "tcp", "smtp", { "mail" } } } },
33 { 445, { { "tcp", "microsoft-ds" },
34 { "udp", "microsoft-ds" } } },
35 { 514, { { "tcp", "cmd", { "shell" } },
36 { "udp", "syslog" } } },
37 { 47624, { { "tcp", "directplaysrvr" },
38 { "udp", "directplaysrvr" } } },
39 };
40 ULONG i, Proto, Alias;
41 int Error;
42 ULONG ExpectProto;
43
44 /* not yet initialized */
45 Serv = getservbyport(0, NULL);
47 ok(Serv == NULL, "Serv = %p\n", Serv);
48 ok(Error == WSANOTINITIALISED, "Error = %d\n", Error);
49
50 Error = WSAStartup(MAKEWORD(2, 2), &WsaData);
51 ok_dec(Error, 0);
52
53 for (i = 0; i < RTL_NUMBER_OF(Tests); i++)
54 {
55 Proto = 0;
56 do
57 {
58 Serv = getservbyport(htons(Tests[i].Port), Tests[i].Protos[Proto].Proto);
60
61 /* For a NULL proto we expect the same as the first array entry */
62 ExpectProto = Proto;
63 if (Tests[i].Protos[Proto].Proto == NULL)
64 {
65 ExpectProto = 0;
66 }
67
68 if (Tests[i].Protos[ExpectProto].Name == NULL)
69 {
70 ok(Serv == NULL, "[%d, %s] getservbyport succeeded unexpectedly\n",
71 Tests[i].Port, Tests[i].Protos[Proto].Proto);
72 ok(Error == WSANO_DATA, "[%d, %s] getservbyport returned error %d\n",
73 Tests[i].Port, Tests[i].Protos[Proto].Proto, Error);
74 continue;
75 }
76 else
77 {
78 ok(Serv != NULL, "[%d, %s] getservbyport failed with %d\n",
79 Tests[i].Port, Tests[i].Protos[Proto].Proto, Error);
80 }
81
82 if (Serv == NULL)
83 {
84 continue;
85 }
86
87 /* Check name */
88 ok(!strcmp(Serv->s_name, Tests[i].Protos[ExpectProto].Name),
89 "[%d, %s] s_name = '%s', expected '%s'\n",
90 Tests[i].Port, Tests[i].Protos[Proto].Proto, Serv->s_name, Tests[i].Protos[ExpectProto].Name);
91
92 /* Check aliases */
93 ok(Serv->s_aliases != NULL, "[%d, %s] s_aliases = NULL\n",
94 Tests[i].Port, Tests[i].Protos[Proto].Proto);
95 for (Alias = 0; Serv->s_aliases; Alias++)
96 {
97 if (Alias >= RTL_NUMBER_OF(Tests[i].Protos[ExpectProto].Aliases))
98 {
99 ok(0, "[%d, %s] Too many aliases\n",
100 Tests[i].Port, Tests[i].Protos[Proto].Proto);
101 break;
102 }
103 if (Serv->s_aliases[Alias] == NULL)
104 {
105 ok(Tests[i].Protos[ExpectProto].Aliases[Alias] == NULL,
106 "[%d, %s] getservbyport did not return expected alias '%s'\n",
107 Tests[i].Port, Tests[i].Protos[Proto].Proto, Tests[i].Protos[ExpectProto].Aliases[Alias]);
108 break;
109 }
110 if (Tests[i].Protos[ExpectProto].Aliases[Alias] == NULL)
111 {
112 ok(Serv->s_aliases[Alias] == NULL,
113 "[%d, %s] getservbyport returned additional alias '%s'\n",
114 Tests[i].Port, Tests[i].Protos[Proto].Proto, Serv->s_aliases[Alias]);
115 break;
116 }
117
118 ok(!strcmp(Serv->s_aliases[Alias], Tests[i].Protos[ExpectProto].Aliases[Alias]),
119 "[%d, %s] Got alias '%s', expected '%s'\n",
120 Tests[i].Port, Tests[i].Protos[Proto].Proto, Serv->s_aliases[Alias],Tests[i].Protos[ExpectProto].Aliases[Alias]);
121 }
122
123 /* Port should be equal (upper bits are ignored) */
124 ok(ntohs(Serv->s_port) == (Tests[i].Port & 0xffff), "[%d, %s] s_port = %d\n",
125 Tests[i].Port, Tests[i].Protos[Proto].Proto, ntohs(Serv->s_port));
126
127 /* Check proto */
128 ok(!strcmp(Serv->s_proto, Tests[i].Protos[ExpectProto].Proto), "[%d, %s] s_proto = '%s', expected '%s'\n",
129 Tests[i].Port, Tests[i].Protos[Proto].Proto, Serv->s_proto, Tests[i].Protos[ExpectProto].Proto);
130 /* We want to include one NULL past the last proto in the array */
131 } while (Tests[i].Protos[Proto++].Proto != NULL);
132 }
133
134 Error = WSACleanup();
135 ok_dec(Error, 0);
136}
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 getservbyport(IN int port, IN const char FAR *proto)
Definition: getxbyxx.c:431
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 htons(x)
Definition: module.h:215
#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