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 
10 START_TEST(getservbyname)
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);
48     Error = WSAGetLastError();
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);
61             Error = WSAGetLastError();
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 }
139