98d5b9ddd6fd5e511e1721e49b1374a88557c219
[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, skipping");
69 continue;
70 }
71 Error = bind(Socket, (const struct sockaddr *) &Tests[i].Addr, sizeof(Tests[i].Addr));
72 ok_dec(Error, Tests[i].ExpectedResult);
73 if (Error)
74 {
75 ok_dec(WSAGetLastError(), Tests[i].ExpectedWSAResult);
76 }
77 else
78 {
79 ok_dec(WSAGetLastError(), 0);
80 Error = getsockname(Socket, (struct sockaddr *) &Addr, &AddrSize);
81 ok(Error == 0, "Error getsockname for test %d, Error %d\n", i, Error);
82 ok_dec(WSAGetLastError(), 0);
83 ok_dec(AddrSize, sizeof(Addr));
84 ok_dec(Addr.sin_addr.s_addr, Tests[i].ExpectedAddr.sin_addr.s_addr);
85 if (Tests[i].ExpectedAddr.sin_port)
86 {
87 ok_dec(Addr.sin_port, Tests[i].ExpectedAddr.sin_port);
88 }
89 else
90 {
91 ok(Addr.sin_port != 0, "Port remained zero\n");
92 }
93 }
94 Error = closesocket(Socket);
95 ok_dec(Error, 0);
96 }
97 #if 0
98 /* Check double bind */
99 Socket = socket(AF_INET, Tests[0].Type, Tests[0].Proto);
100 Error = bind(Socket, (const struct sockaddr *) &Tests[0].Addr, sizeof(Tests[0].Addr));
101 ok_dec(Error, Tests[0].ExpectedResult);
102 if (Error)
103 {
104 ok_dec(WSAGetLastError(), Tests[0].ExpectedWSAResult);
105 }
106 else
107 {
108 ok_dec(WSAGetLastError(), 0);
109 Error = getsockname(Socket, (struct sockaddr *) &Addr, &AddrSize);
110 ok_dec(Error, 0);
111 ok_dec(AddrSize, sizeof(Addr));
112 ok_dec(Addr.sin_addr.s_addr, Tests[0].ExpectedAddr.sin_addr.s_addr);
113 if (Tests[0].ExpectedAddr.sin_port)
114 {
115 ok_dec(Addr.sin_port, Tests[0].ExpectedAddr.sin_port);
116 }
117 else
118 {
119 ok(Addr.sin_port != 0, "Port remained zero\n");
120 }
121 Error = bind(Socket, (const struct sockaddr *) &Tests[2].Addr, sizeof(Tests[2].Addr));
122 ok_dec(Error, SOCKET_ERROR);
123 ok_dec(WSAGetLastError(), WSAEINVAL);
124 }
125 closesocket(Socket);
126 /* Check disabled SO_BROADCAST and bind to broadcast address */
127 Socket = socket(AF_INET, Tests[10].Type, Tests[10].Proto);
128 Error = setsockopt(Socket, SOL_SOCKET, SO_BROADCAST, (const char *) &Broadcast, sizeof(Broadcast));
129 ok_dec(Error, 0);
130 // FIXME: should be made properly broadcast address
131 Tests[10].Addr.sin_addr.S_un.S_un_b.s_b4 = 0xff;
132 Error = bind(Socket, (const struct sockaddr *) &Tests[10].Addr, sizeof(Tests[10].Addr));
133 ok_dec(Error, SOCKET_ERROR);
134 ok_dec(WSAGetLastError(), WSAEACCES);
135 closesocket(Socket);
136 #endif
137 }
138
139 START_TEST(bind)
140 {
141 WSADATA WsaData;
142 int Error;
143 CHAR LocalHostName[128];
144 struct hostent *Hostent;
145 IN_ADDR Address;
146 SOCKET Socket;
147 struct sockaddr_in Addr = { AF_INET };
148
149 /* not yet initialized */
150 StartSeh()
151 Error = bind(INVALID_SOCKET, NULL, 0);
152 ok_dec(Error, -1);
153 EndSeh(STATUS_SUCCESS);
154 StartSeh()
155 Error = bind(INVALID_SOCKET, InvalidPointer, 0);
156 ok_dec(Error, -1);
157 EndSeh(STATUS_SUCCESS);
158
159 Error = WSAStartup(MAKEWORD(2, 2), &WsaData);
160 ok_dec(Error, 0);
161
162 /* initialize LocalAddress for tests */
163 Error = gethostname(LocalHostName, sizeof(LocalHostName));
164 ok_dec(Error, 0);
165 ok_dec(WSAGetLastError(), 0);
166 trace("Local host name is '%s'\n", LocalHostName);
167 Hostent = gethostbyname(LocalHostName);
168 ok(Hostent != NULL, "gethostbyname failed with %d\n", WSAGetLastError());
169 if (Hostent && Hostent->h_addr_list[0] && Hostent->h_length == sizeof(IN_ADDR))
170 {
171 memcpy(&Address, Hostent->h_addr_list[0], sizeof(Address));
172 strcpy(LocalAddress, inet_ntoa(Address));
173 }
174 trace("Local address is '%s'\n", LocalAddress);
175 ok(LocalAddress[0] != '\0',
176 "Could not determine local address. Following test results may be wrong.\n");
177
178 /* parameter tests */
179 StartSeh()
180 Error = bind(INVALID_SOCKET, NULL, 0);
181 ok_dec(Error, SOCKET_ERROR);
182 ok_dec(WSAGetLastError(), WSAENOTSOCK);
183 EndSeh(STATUS_SUCCESS);
184 StartSeh()
185 Error = bind(INVALID_SOCKET, InvalidPointer, 0);
186 ok_dec(Error, SOCKET_ERROR);
187 ok_dec(WSAGetLastError(), WSAENOTSOCK);
188 EndSeh(STATUS_SUCCESS);
189 StartSeh()
190 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
191 Error = bind(Socket, NULL, 0);
192 ok_dec(Error, SOCKET_ERROR);
193 ok_dec(WSAGetLastError(), WSAEFAULT);
194 closesocket(Socket);
195 EndSeh(STATUS_SUCCESS);
196 StartSeh()
197 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
198 Error = bind(Socket, InvalidPointer, 0);
199 ok_dec(Error, SOCKET_ERROR);
200 ok_dec(WSAGetLastError(), WSAEFAULT);
201 closesocket(Socket);
202 EndSeh(STATUS_SUCCESS);
203 StartSeh()
204 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
205 Error = bind(Socket, NULL, sizeof(Addr));
206 ok_dec(Error, SOCKET_ERROR);
207 ok_dec(WSAGetLastError(), WSAEFAULT);
208 closesocket(Socket);
209 EndSeh(STATUS_SUCCESS);
210 StartSeh()
211 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
212 Error = bind(Socket, InvalidPointer, sizeof(Addr));
213 ok_dec(Error, SOCKET_ERROR);
214 ok_dec(WSAGetLastError(), WSAEFAULT);
215 closesocket(Socket);
216 EndSeh(STATUS_ACCESS_VIOLATION);
217 StartSeh()
218 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
219 Error = bind(Socket, (const struct sockaddr *) &Addr, 0);
220 ok_dec(Error, SOCKET_ERROR);
221 ok_dec(WSAGetLastError(), WSAEFAULT);
222 closesocket(Socket);
223 EndSeh(STATUS_SUCCESS);
224 StartSeh()
225 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
226 Error = bind(Socket, (const struct sockaddr *) &Addr, sizeof(Addr)-1);
227 ok_dec(Error, SOCKET_ERROR);
228 ok_dec(WSAGetLastError(), WSAEFAULT);
229 closesocket(Socket);
230 EndSeh(STATUS_SUCCESS);
231
232 TestBind(Address);
233 /* TODO: test IPv6 */
234
235 Error = WSACleanup();
236 ok_dec(Error, 0);
237 }