56d4f41f99a28967d21e5f3b9524c2de592f1c68
[reactos.git] / modules / 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 BOOL Broadcast = TRUE;
59
60 for (i = 0; i < TestCount; i++)
61 {
62 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);
63 Socket = socket(AF_INET, Tests[i].Type, Tests[i].Proto);
64 if (Socket == INVALID_SOCKET)
65 {
66 skip("Failed to create socket with error %d for test %d, skipping\n", WSAGetLastError(), i);
67 continue;
68 }
69 Error = bind(Socket, (const struct sockaddr *) &Tests[i].Addr, sizeof(Tests[i].Addr));
70 ok(Error == Tests[i].ExpectedResult, "Error %d differs from expected %d for test %d\n", Error, Tests[i].ExpectedResult, i);
71 if (Error)
72 {
73 ok(WSAGetLastError() == Tests[i].ExpectedWSAResult, "Error %d differs from expected %d for test %d\n", WSAGetLastError(), Tests[i].ExpectedWSAResult, i);
74 }
75 else
76 {
77 AddrSize = sizeof(Addr);
78 Error = getsockname(Socket, (struct sockaddr *) &Addr, &AddrSize);
79 ok(Error == 0, "Unexpected error %d %d on getsockname for test %d\n", Error, WSAGetLastError(), i);
80 ok(AddrSize == sizeof(Addr), "Returned size %d differs from expected %d for test %d\n", AddrSize, sizeof(Addr), i);
81 ok(Addr.sin_addr.s_addr == Tests[i].ExpectedAddr.sin_addr.s_addr, "Expected address %lx differs from returned address %lx for test %d\n", Tests[i].ExpectedAddr.sin_addr.s_addr, Addr.sin_addr.s_addr, i);
82 if (Tests[i].ExpectedAddr.sin_port)
83 {
84 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);
85 }
86 else
87 {
88 ok(Addr.sin_port != 0, "Port remained zero for test %d\n", i);
89 }
90 }
91 Error = closesocket(Socket);
92 ok(Error == 0, "Unexpected error %d %d on closesocket for test %d\n", Error, WSAGetLastError(), i);
93 }
94 /* Check double bind */
95 Socket = socket(AF_INET, Tests[0].Type, Tests[0].Proto);
96 ok(Socket != INVALID_SOCKET, "Failed to create socket with error %d for double bind test, next tests might be wrong\n", WSAGetLastError());
97 Error = bind(Socket, (const struct sockaddr *) &Tests[0].Addr, sizeof(Tests[0].Addr));
98 ok(Error == Tests[0].ExpectedResult, "Error %d differs from expected %d for double bind test\n", Error, Tests[0].ExpectedResult);
99 if (Error)
100 {
101 ok(WSAGetLastError() == Tests[i].ExpectedWSAResult, "Error %d differs from expected %d for double bind test\n", WSAGetLastError(), Tests[0].ExpectedWSAResult);
102 }
103 else
104 {
105 AddrSize = sizeof(Addr);
106 Error = getsockname(Socket, (struct sockaddr *) &Addr, &AddrSize);
107 ok(Error == 0, "Unexpected error %d %d on getsockname for double bind test\n", Error, WSAGetLastError());
108 ok(AddrSize == sizeof(Addr), "Returned size %d differs from expected %d for double bind test\n", AddrSize, sizeof(Addr));
109 ok(Addr.sin_addr.s_addr == Tests[0].ExpectedAddr.sin_addr.s_addr, "Expected address %lx differs from returned address %lx for double bind test\n", Tests[0].ExpectedAddr.sin_addr.s_addr, Addr.sin_addr.s_addr);
110 if (Tests[0].ExpectedAddr.sin_port)
111 {
112 ok(Addr.sin_port == Tests[0].ExpectedAddr.sin_port, "Returned port %d differs from expected %d for double bind test\n", Addr.sin_port, Tests[0].ExpectedAddr.sin_port);
113 }
114 else
115 {
116 ok(Addr.sin_port != 0, "Port remained zero for double bind test\n");
117 }
118 Error = bind(Socket, (const struct sockaddr *) &Tests[2].Addr, sizeof(Tests[2].Addr));
119 ok(Error == SOCKET_ERROR && WSAGetLastError() == WSAEINVAL, "Unexpected result %d expected %d and wsa result %d expected %ld for double bind test\n", Error, SOCKET_ERROR, WSAGetLastError(), WSAEINVAL);
120 }
121 Error = closesocket(Socket);
122 ok(Error == 0, "Unexpected error %d %d on closesocket for double bind test\n", Error, WSAGetLastError());
123 /* Check SO_BROADCAST and bind to broadcast address */
124 Socket = socket(AF_INET, Tests[10].Type, Tests[10].Proto);
125 ok(Socket != INVALID_SOCKET, "Failed to create socket with error %d for broadcast test, next tests might be wrong\n", WSAGetLastError());
126 Error = setsockopt(Socket, SOL_SOCKET, SO_BROADCAST, (const char *) &Broadcast, sizeof(Broadcast));
127 ok(Error == 0, "Unexpected error %d %d on setsockopt for broadcast test\n", Error, WSAGetLastError());
128 Error = bind(Socket, (const struct sockaddr *) &Tests[10].Addr, sizeof(Tests[10].Addr));
129 ok(Error == 0, "Unexpected error %d %d on bind for broadcast test\n", Error, WSAGetLastError());
130 Error = closesocket(Socket);
131 ok(Error == 0, "Unexpected error %d %d on closesocket for broadcast test\n", Error, WSAGetLastError());
132 }
133
134 START_TEST(bind)
135 {
136 WSADATA WsaData;
137 int Error;
138 CHAR LocalHostName[128];
139 struct hostent *Hostent;
140 IN_ADDR Address;
141 SOCKET Socket;
142 struct sockaddr_in Addr = { AF_INET };
143
144 /* not yet initialized */
145 StartSeh()
146 Error = bind(INVALID_SOCKET, NULL, 0);
147 ok_dec(Error, -1);
148 EndSeh(STATUS_SUCCESS);
149 StartSeh()
150 Error = bind(INVALID_SOCKET, InvalidPointer, 0);
151 ok_dec(Error, -1);
152 EndSeh(STATUS_SUCCESS);
153
154 Error = WSAStartup(MAKEWORD(2, 2), &WsaData);
155 ok_dec(Error, 0);
156
157 /* initialize LocalAddress for tests */
158 Error = gethostname(LocalHostName, sizeof(LocalHostName));
159 ok_dec(Error, 0);
160 ok_dec(WSAGetLastError(), 0);
161 trace("Local host name is '%s'\n", LocalHostName);
162 Hostent = gethostbyname(LocalHostName);
163 ok(Hostent != NULL, "gethostbyname failed with %d\n", WSAGetLastError());
164 if (Hostent && Hostent->h_addr_list[0] && Hostent->h_length == sizeof(IN_ADDR))
165 {
166 memcpy(&Address, Hostent->h_addr_list[0], sizeof(Address));
167 strcpy(LocalAddress, inet_ntoa(Address));
168 }
169 trace("Local address is '%s'\n", LocalAddress);
170 ok(LocalAddress[0] != '\0',
171 "Could not determine local address. Following test results may be wrong.\n");
172
173 /* parameter tests */
174 StartSeh()
175 Error = bind(INVALID_SOCKET, NULL, 0);
176 ok_dec(Error, SOCKET_ERROR);
177 ok_dec(WSAGetLastError(), WSAENOTSOCK);
178 EndSeh(STATUS_SUCCESS);
179 StartSeh()
180 Error = bind(INVALID_SOCKET, InvalidPointer, 0);
181 ok_dec(Error, SOCKET_ERROR);
182 ok_dec(WSAGetLastError(), WSAENOTSOCK);
183 EndSeh(STATUS_SUCCESS);
184 StartSeh()
185 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
186 Error = bind(Socket, NULL, 0);
187 ok_dec(Error, SOCKET_ERROR);
188 ok_dec(WSAGetLastError(), WSAEFAULT);
189 closesocket(Socket);
190 EndSeh(STATUS_SUCCESS);
191 StartSeh()
192 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
193 Error = bind(Socket, InvalidPointer, 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, NULL, sizeof(Addr));
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, InvalidPointer, 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, (const struct sockaddr *) &Addr, 0);
215 ok_dec(Error, SOCKET_ERROR);
216 ok_dec(WSAGetLastError(), WSAEFAULT);
217 closesocket(Socket);
218 EndSeh(STATUS_SUCCESS);
219 StartSeh()
220 Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
221 Error = bind(Socket, (const struct sockaddr *) &Addr, sizeof(Addr)-1);
222 ok_dec(Error, SOCKET_ERROR);
223 ok_dec(WSAGetLastError(), WSAEFAULT);
224 closesocket(Socket);
225 EndSeh(STATUS_SUCCESS);
226
227 TestBind(Address);
228 /* TODO: test IPv6 */
229
230 Error = WSACleanup();
231 ok_dec(Error, 0);
232 }