[WS2_32_APITESTS] Improve bind test output ROSTESTS-278
[reactos.git] / rostests / apitests / ws2_32 / bind.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for bind
5 * PROGRAMMER: Peter Hater
6 */
7
8 #include <apitest.h>
9
10 #define WIN32_NO_STATUS
11 #define _INC_WINDOWS
12 #define COM_NO_WINDOWS_H
13 #include <stdarg.h>
14 #include <windef.h>
15 #include <winbase.h>
16 #include <ws2tcpip.h>
17 #include <ndk/umtypes.h>
18
19 CHAR LocalAddress[sizeof("255.255.255.255")];
20 #define PORT 58888
21
22 static
23 VOID
24 TestBind(IN_ADDR Address)
25 {
26 int Error;
27 struct
28 {
29 INT Type;
30 INT Proto;
31 struct sockaddr_in Addr;
32 INT ExpectedResult;
33 INT ExpectedWSAResult;
34 struct sockaddr_in ExpectedAddr;
35 } Tests[] =
36 {
37 { SOCK_STREAM, IPPROTO_TCP, { AF_INET, PORT, {{{ 0x7f, 0x00, 0x00, 0x01 }}} }, 0, 0, { AF_INET, PORT, {{{ 0x7f, 0x00, 0x00, 0x01 }}} } },
38 { SOCK_STREAM, IPPROTO_TCP, { AF_INET, PORT, {{{ 0x00, 0x00, 0x00, 0x00 }}} }, 0, 0, { AF_INET, PORT, {{{ 0x00, 0x00, 0x00, 0x00 }}} } },
39 { SOCK_STREAM, IPPROTO_TCP, { AF_INET, PORT, Address }, 0, 0, { AF_INET, PORT, Address } },
40 { SOCK_STREAM, IPPROTO_TCP, { AF_INET, PORT, {{{ 0xff, 0xff, 0xff, 0xff }}} }, SOCKET_ERROR, WSAEADDRNOTAVAIL },
41 { SOCK_STREAM, IPPROTO_TCP, { AF_INET, 0, {{{ 0x7f, 0x00, 0x00, 0x01 }}} }, 0, 0, { AF_INET, 0, {{{ 0x7f, 0x00, 0x00, 0x01 }}} } },
42 { SOCK_STREAM, IPPROTO_TCP, { AF_INET, 0, {{{ 0x00, 0x00, 0x00, 0x00 }}} } },
43 { SOCK_STREAM, IPPROTO_TCP, { AF_INET, 0, Address }, 0, 0, { AF_INET, 0, Address } },
44 { SOCK_STREAM, IPPROTO_TCP, { AF_INET, 0, {{{ 0xff, 0xff, 0xff, 0xff }}} }, SOCKET_ERROR, WSAEADDRNOTAVAIL },
45 { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, PORT, {{{ 0x7f, 0x00, 0x00, 0x01 }}} }, 0, 0, { AF_INET, PORT, {{{ 0x7f, 0x00, 0x00, 0x01 }}} } },
46 { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, PORT, {{{ 0x00, 0x00, 0x00, 0x00 }}} }, 0, 0, { AF_INET, PORT, {{{ 0x00, 0x00, 0x00, 0x00 }}} } },
47 { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, PORT, Address }, 0, 0, { AF_INET, PORT, Address } },
48 { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, PORT, {{{ 0xff, 0xff, 0xff, 0xff }}} }, SOCKET_ERROR, WSAEADDRNOTAVAIL },
49 { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, 0, {{{ 0x7f, 0x00, 0x00, 0x01 }}} }, 0, 0, { AF_INET, 0, {{{ 0x7f, 0x00, 0x00, 0x01 }}} } },
50 { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, 0, {{{ 0x00, 0x00, 0x00, 0x00 }}} } },
51 { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, 0, Address }, 0, 0,{ AF_INET, 0, Address } },
52 { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, 0, {{{ 0xff, 0xff, 0xff, 0xff }}} }, SOCKET_ERROR, WSAEADDRNOTAVAIL },
53 };
54 const INT TestCount = _countof(Tests);
55 INT i, AddrSize;
56 SOCKET Socket;
57 struct sockaddr_in Addr;
58 #if 0
59 BOOL Broadcast = FALSE;
60 #endif
61
62 for (i = 0; i < TestCount; i++)
63 {
64 trace("%d: %s %d.%d.%d.%d:%d\n", i, Tests[i].Type == SOCK_STREAM ? "TCP" : "UDP", Tests[i].Addr.sin_addr.S_un.S_un_b.s_b1, Tests[i].Addr.sin_addr.S_un.S_un_b.s_b2, Tests[i].Addr.sin_addr.S_un.S_un_b.s_b3, Tests[i].Addr.sin_addr.S_un.S_un_b.s_b4, Tests[i].ExpectedAddr.sin_port);
65 Socket = socket(AF_INET, Tests[i].Type, Tests[i].Proto);
66 if (Socket == INVALID_SOCKET)
67 {
68 skip("Failed to create socket with error %d for test %d, skipping\n", WSAGetLastError(), i);
69 continue;
70 }
71 Error = bind(Socket, (const struct sockaddr *) &Tests[i].Addr, sizeof(Tests[i].Addr));
72 ok(Error == Tests[i].ExpectedResult, "Error %d differs from expected %d for test %d\n", Error, Tests[i].ExpectedResult, i);
73 if (Error)
74 {
75 ok(WSAGetLastError() == Tests[i].ExpectedWSAResult, "Error %d differs from expected %d for test %d\n", WSAGetLastError(), Tests[i].ExpectedWSAResult, i);
76 }
77 else
78 {
79 ok(WSAGetLastError() == 0, "Unexpected error %d on bind for test %d\n", WSAGetLastError(), i);
80 AddrSize = sizeof(Addr);
81 Error = getsockname(Socket, (struct sockaddr *) &Addr, &AddrSize);
82 ok(Error == 0, "Unexpected error %d on getsockname for test %d\n", Error, i);
83 ok(WSAGetLastError() == 0, "Unexpected error %d on getsockname for test %d\n", WSAGetLastError(), i);
84 ok(AddrSize == sizeof(Addr), "Returned size %d differs from expected %d for test %d\n", AddrSize, sizeof(Addr), i);
85 ok(Addr.sin_addr.s_addr == Tests[i].ExpectedAddr.sin_addr.s_addr, "Expected address %x differs from returned address %x for test %d\n", Tests[i].ExpectedAddr.sin_addr.s_addr, Addr.sin_addr.s_addr, i);
86 if (Tests[i].ExpectedAddr.sin_port)
87 {
88 ok(Addr.sin_port == Tests[i].ExpectedAddr.sin_port, "Returned port %d differs from expected %d for test %d\n", Addr.sin_port, Tests[i].ExpectedAddr.sin_port, i);
89 }
90 else
91 {
92 ok(Addr.sin_port != 0, "Port remained zero for test %d\n", i);
93 }
94 }
95 Error = closesocket(Socket);
96 ok(Error == 0, "Unexpected error %d on closesocket for test %d\n", Error, i);
97 ok(WSAGetLastError() == 0, "Unexpected error %d on closesocket for test %d\n", WSAGetLastError(), i);
98 }
99 #if 0
100 /* Check double bind */
101 Socket = socket(AF_INET, Tests[0].Type, Tests[0].Proto);
102 Error = bind(Socket, (const struct sockaddr *) &Tests[0].Addr, sizeof(Tests[0].Addr));
103 ok_dec(Error, Tests[0].ExpectedResult);
104 if (Error)
105 {
106 ok_dec(WSAGetLastError(), Tests[0].ExpectedWSAResult);
107 }
108 else
109 {
110 ok_dec(WSAGetLastError(), 0);
111 Error = getsockname(Socket, (struct sockaddr *) &Addr, &AddrSize);
112 ok_dec(Error, 0);
113 ok_dec(AddrSize, sizeof(Addr));
114 ok_dec(Addr.sin_addr.s_addr, Tests[0].ExpectedAddr.sin_addr.s_addr);
115 if (Tests[0].ExpectedAddr.sin_port)
116 {
117 ok_dec(Addr.sin_port, Tests[0].ExpectedAddr.sin_port);
118 }
119 else
120 {
121 ok(Addr.sin_port != 0, "Port remained zero\n");
122 }
123 Error = bind(Socket, (const struct sockaddr *) &Tests[2].Addr, sizeof(Tests[2].Addr));
124 ok_dec(Error, SOCKET_ERROR);
125 ok_dec(WSAGetLastError(), WSAEINVAL);
126 }
127 closesocket(Socket);
128 /* Check disabled SO_BROADCAST and bind to broadcast address */
129 Socket = socket(AF_INET, Tests[10].Type, Tests[10].Proto);
130 Error = setsockopt(Socket, SOL_SOCKET, SO_BROADCAST, (const char *) &Broadcast, sizeof(Broadcast));
131 ok_dec(Error, 0);
132 // FIXME: should be made properly broadcast address
133 Tests[10].Addr.sin_addr.S_un.S_un_b.s_b4 = 0xff;
134 Error = bind(Socket, (const struct sockaddr *) &Tests[10].Addr, sizeof(Tests[10].Addr));
135 ok_dec(Error, SOCKET_ERROR);
136 ok_dec(WSAGetLastError(), WSAEACCES);
137 closesocket(Socket);
138 #endif
139 }
140
141 START_TEST(bind)
142 {
143 WSADATA WsaData;
144 int Error;
145 CHAR LocalHostName[128];
146 struct hostent *Hostent;
147 IN_ADDR Address;
148 SOCKET Socket;
149 struct sockaddr_in Addr = { AF_INET };
150
151 /* not yet initialized */
152 StartSeh()
153 Error = bind(INVALID_SOCKET, NULL, 0);
154 ok_dec(Error, -1);
155 EndSeh(STATUS_SUCCESS);
156 StartSeh()
157 Error = bind(INVALID_SOCKET, InvalidPointer, 0);
158 ok_dec(Error, -1);
159 EndSeh(STATUS_SUCCESS);
160
161 Error = WSAStartup(MAKEWORD(2, 2), &WsaData);
162 ok_dec(Error, 0);
163
164 /* initialize LocalAddress for tests */
165 Error = gethostname(LocalHostName, sizeof(LocalHostName));
166 ok_dec(Error, 0);
167 ok_dec(WSAGetLastError(), 0);
168 trace("Local host name is '%s'\n", LocalHostName);
169 Hostent = gethostbyname(LocalHostName);
170 ok(Hostent != NULL, "gethostbyname failed with %d\n", WSAGetLastError());
171 if (Hostent && Hostent->h_addr_list[0] && Hostent->h_length == sizeof(IN_ADDR))
172 {
173 memcpy(&Address, Hostent->h_addr_list[0], sizeof(Address));
174 strcpy(LocalAddress, inet_ntoa(Address));
175 }
176 trace("Local address is '%s'\n", LocalAddress);
177 ok(LocalAddress[0] != '\0',
178 "Could not determine local address. Following test results may be wrong.\n");
179
180 /* parameter tests */
181 StartSeh()
182 Error = bind(INVALID_SOCKET, NULL, 0);
183 ok_dec(Error, SOCKET_ERROR);
184 ok_dec(WSAGetLastError(), WSAENOTSOCK);
185 EndSeh(STATUS_SUCCESS);
186 StartSeh()
187 Error = bind(INVALID_SOCKET, InvalidPointer, 0);
188 ok_dec(Error, SOCKET_ERROR);
189 ok_dec(WSAGetLastError(), WSAENOTSOCK);
190 EndSeh(STATUS_SUCCESS);
191 StartSeh()
192 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
193 Error = bind(Socket, NULL, 0);
194 ok_dec(Error, SOCKET_ERROR);
195 ok_dec(WSAGetLastError(), WSAEFAULT);
196 closesocket(Socket);
197 EndSeh(STATUS_SUCCESS);
198 StartSeh()
199 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
200 Error = bind(Socket, InvalidPointer, 0);
201 ok_dec(Error, SOCKET_ERROR);
202 ok_dec(WSAGetLastError(), WSAEFAULT);
203 closesocket(Socket);
204 EndSeh(STATUS_SUCCESS);
205 StartSeh()
206 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
207 Error = bind(Socket, NULL, sizeof(Addr));
208 ok_dec(Error, SOCKET_ERROR);
209 ok_dec(WSAGetLastError(), WSAEFAULT);
210 closesocket(Socket);
211 EndSeh(STATUS_SUCCESS);
212 StartSeh()
213 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
214 Error = bind(Socket, InvalidPointer, sizeof(Addr));
215 ok_dec(Error, SOCKET_ERROR);
216 ok_dec(WSAGetLastError(), WSAEFAULT);
217 closesocket(Socket);
218 EndSeh(STATUS_ACCESS_VIOLATION);
219 StartSeh()
220 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
221 Error = bind(Socket, (const struct sockaddr *) &Addr, 0);
222 ok_dec(Error, SOCKET_ERROR);
223 ok_dec(WSAGetLastError(), WSAEFAULT);
224 closesocket(Socket);
225 EndSeh(STATUS_SUCCESS);
226 StartSeh()
227 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
228 Error = bind(Socket, (const struct sockaddr *) &Addr, sizeof(Addr)-1);
229 ok_dec(Error, SOCKET_ERROR);
230 ok_dec(WSAGetLastError(), WSAEFAULT);
231 closesocket(Socket);
232 EndSeh(STATUS_SUCCESS);
233
234 TestBind(Address);
235 /* TODO: test IPv6 */
236
237 Error = WSACleanup();
238 ok_dec(Error, 0);
239 }