ReactOS  0.4.13-dev-99-g7e18b6d
bind.c
Go to the documentation of this file.
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 "ws2_32.h"
9 
10 CHAR LocalAddress[sizeof("255.255.255.255")];
11 #define PORT 58888
12 
13 static
14 VOID
16 {
17  const UCHAR b1 = Address.S_un.S_un_b.s_b1;
18  const UCHAR b2 = Address.S_un.S_un_b.s_b2;
19  const UCHAR b3 = Address.S_un.S_un_b.s_b3;
20  const UCHAR b4 = Address.S_un.S_un_b.s_b4;
21 
22  int Error;
23  struct
24  {
25  INT Type;
26  INT Proto;
27  struct sockaddr_in Addr;
28  INT ExpectedResult;
29  INT ExpectedWSAResult;
30  struct sockaddr_in ExpectedAddr;
31  } Tests[] =
32  {
33  { SOCK_STREAM, IPPROTO_TCP, { AF_INET, PORT, {{{ 0x7f, 0x00, 0x00, 0x01 }}} }, 0, 0, { AF_INET, PORT, {{{ 0x7f, 0x00, 0x00, 0x01 }}} } },
34  { SOCK_STREAM, IPPROTO_TCP, { AF_INET, PORT, {{{ 0x00, 0x00, 0x00, 0x00 }}} }, 0, 0, { AF_INET, PORT, {{{ 0x00, 0x00, 0x00, 0x00 }}} } },
35  { SOCK_STREAM, IPPROTO_TCP, { AF_INET, PORT, {{{ b1, b2, b3, b4 }}} }, 0, 0, { AF_INET, PORT, {{{ b1, b2, b3, b4 }}} } },
36  { SOCK_STREAM, IPPROTO_TCP, { AF_INET, PORT, {{{ 0xff, 0xff, 0xff, 0xff }}} }, SOCKET_ERROR, WSAEADDRNOTAVAIL },
37  { SOCK_STREAM, IPPROTO_TCP, { AF_INET, 0, {{{ 0x7f, 0x00, 0x00, 0x01 }}} }, 0, 0, { AF_INET, 0, {{{ 0x7f, 0x00, 0x00, 0x01 }}} } },
38  { SOCK_STREAM, IPPROTO_TCP, { AF_INET, 0, {{{ 0x00, 0x00, 0x00, 0x00 }}} } },
39  { SOCK_STREAM, IPPROTO_TCP, { AF_INET, 0, {{{ b1, b2, b3, b4 }}} }, 0, 0, { AF_INET, 0, {{{ b1, b2, b3, b4 }}} } },
40  { SOCK_STREAM, IPPROTO_TCP, { AF_INET, 0, {{{ 0xff, 0xff, 0xff, 0xff }}} }, SOCKET_ERROR, WSAEADDRNOTAVAIL },
41  { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, PORT, {{{ 0x7f, 0x00, 0x00, 0x01 }}} }, 0, 0, { AF_INET, PORT, {{{ 0x7f, 0x00, 0x00, 0x01 }}} } },
42  { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, PORT, {{{ 0x00, 0x00, 0x00, 0x00 }}} }, 0, 0, { AF_INET, PORT, {{{ 0x00, 0x00, 0x00, 0x00 }}} } },
43  { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, PORT, {{{ b1, b2, b3, b4 }}} }, 0, 0, { AF_INET, PORT, {{{ b1, b2, b3, b4 }}} } },
44  { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, PORT, {{{ 0xff, 0xff, 0xff, 0xff }}} }, SOCKET_ERROR, WSAEADDRNOTAVAIL },
45  { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, 0, {{{ 0x7f, 0x00, 0x00, 0x01 }}} }, 0, 0, { AF_INET, 0, {{{ 0x7f, 0x00, 0x00, 0x01 }}} } },
46  { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, 0, {{{ 0x00, 0x00, 0x00, 0x00 }}} } },
47  { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, 0, {{{ b1, b2, b3, b4 }}} }, 0, 0,{ AF_INET, 0, {{{ b1, b2, b3, b4 }}} } },
48  { SOCK_DGRAM, IPPROTO_UDP, { AF_INET, 0, {{{ 0xff, 0xff, 0xff, 0xff }}} }, SOCKET_ERROR, WSAEADDRNOTAVAIL },
49  };
50  const INT TestCount = _countof(Tests);
51  INT i, AddrSize;
52  SOCKET Socket;
53  struct sockaddr_in Addr;
54  BOOL Broadcast = TRUE;
55 
56  for (i = 0; i < TestCount; i++)
57  {
58  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);
59  Socket = socket(AF_INET, Tests[i].Type, Tests[i].Proto);
60  if (Socket == INVALID_SOCKET)
61  {
62  skip("Failed to create socket with error %d for test %d, skipping\n", WSAGetLastError(), i);
63  continue;
64  }
65  Error = bind(Socket, (const struct sockaddr *) &Tests[i].Addr, sizeof(Tests[i].Addr));
66  ok(Error == Tests[i].ExpectedResult, "Error %d differs from expected %d for test %d\n", Error, Tests[i].ExpectedResult, i);
67  if (Error)
68  {
69  ok(WSAGetLastError() == Tests[i].ExpectedWSAResult, "Error %d differs from expected %d for test %d\n", WSAGetLastError(), Tests[i].ExpectedWSAResult, i);
70  }
71  else
72  {
73  AddrSize = sizeof(Addr);
74  Error = getsockname(Socket, (struct sockaddr *) &Addr, &AddrSize);
75  ok(Error == 0, "Unexpected error %d %d on getsockname for test %d\n", Error, WSAGetLastError(), i);
76  ok(AddrSize == sizeof(Addr), "Returned size %d differs from expected %d for test %d\n", AddrSize, sizeof(Addr), i);
77  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);
78  if (Tests[i].ExpectedAddr.sin_port)
79  {
80  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);
81  }
82  else
83  {
84  ok(Addr.sin_port != 0, "Port remained zero for test %d\n", i);
85  }
86  }
87  Error = closesocket(Socket);
88  ok(Error == 0, "Unexpected error %d %d on closesocket for test %d\n", Error, WSAGetLastError(), i);
89  }
90  /* Check double bind */
91  Socket = socket(AF_INET, Tests[0].Type, Tests[0].Proto);
92  ok(Socket != INVALID_SOCKET, "Failed to create socket with error %d for double bind test, next tests might be wrong\n", WSAGetLastError());
93  Error = bind(Socket, (const struct sockaddr *) &Tests[0].Addr, sizeof(Tests[0].Addr));
94  ok(Error == Tests[0].ExpectedResult, "Error %d differs from expected %d for double bind test\n", Error, Tests[0].ExpectedResult);
95  if (Error)
96  {
97  ok(WSAGetLastError() == Tests[i].ExpectedWSAResult, "Error %d differs from expected %d for double bind test\n", WSAGetLastError(), Tests[0].ExpectedWSAResult);
98  }
99  else
100  {
101  AddrSize = sizeof(Addr);
102  Error = getsockname(Socket, (struct sockaddr *) &Addr, &AddrSize);
103  ok(Error == 0, "Unexpected error %d %d on getsockname for double bind test\n", Error, WSAGetLastError());
104  ok(AddrSize == sizeof(Addr), "Returned size %d differs from expected %d for double bind test\n", AddrSize, sizeof(Addr));
105  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);
106  if (Tests[0].ExpectedAddr.sin_port)
107  {
108  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);
109  }
110  else
111  {
112  ok(Addr.sin_port != 0, "Port remained zero for double bind test\n");
113  }
114  Error = bind(Socket, (const struct sockaddr *) &Tests[2].Addr, sizeof(Tests[2].Addr));
115  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);
116  }
117  Error = closesocket(Socket);
118  ok(Error == 0, "Unexpected error %d %d on closesocket for double bind test\n", Error, WSAGetLastError());
119  /* Check SO_BROADCAST and bind to broadcast address */
120  Socket = socket(AF_INET, Tests[10].Type, Tests[10].Proto);
121  ok(Socket != INVALID_SOCKET, "Failed to create socket with error %d for broadcast test, next tests might be wrong\n", WSAGetLastError());
122  Error = setsockopt(Socket, SOL_SOCKET, SO_BROADCAST, (const char *) &Broadcast, sizeof(Broadcast));
123  ok(Error == 0, "Unexpected error %d %d on setsockopt for broadcast test\n", Error, WSAGetLastError());
124  Error = bind(Socket, (const struct sockaddr *) &Tests[10].Addr, sizeof(Tests[10].Addr));
125  ok(Error == 0, "Unexpected error %d %d on bind for broadcast test\n", Error, WSAGetLastError());
126  Error = closesocket(Socket);
127  ok(Error == 0, "Unexpected error %d %d on closesocket for broadcast test\n", Error, WSAGetLastError());
128 }
129 
131 {
132  WSADATA WsaData;
133  int Error;
134  CHAR LocalHostName[128];
135  struct hostent *Hostent;
137  SOCKET Socket;
138  struct sockaddr_in Addr = { AF_INET };
139 
140  /* not yet initialized */
141  StartSeh()
142  Error = bind(INVALID_SOCKET, NULL, 0);
143  ok_dec(Error, -1);
145  StartSeh()
147  ok_dec(Error, -1);
149 
150  Error = WSAStartup(MAKEWORD(2, 2), &WsaData);
151  ok_dec(Error, 0);
152 
153  /* initialize LocalAddress for tests */
154  Error = gethostname(LocalHostName, sizeof(LocalHostName));
155  ok_dec(Error, 0);
156  ok_dec(WSAGetLastError(), 0);
157  trace("Local host name is '%s'\n", LocalHostName);
158  Hostent = gethostbyname(LocalHostName);
159  ok(Hostent != NULL, "gethostbyname failed with %d\n", WSAGetLastError());
160  if (Hostent && Hostent->h_addr_list[0] && Hostent->h_length == sizeof(IN_ADDR))
161  {
162  memcpy(&Address, Hostent->h_addr_list[0], sizeof(Address));
164  }
165  trace("Local address is '%s'\n", LocalAddress);
166  ok(LocalAddress[0] != '\0',
167  "Could not determine local address. Following test results may be wrong.\n");
168 
169  /* parameter tests */
170  StartSeh()
171  Error = bind(INVALID_SOCKET, NULL, 0);
175  StartSeh()
180  StartSeh()
182  Error = bind(Socket, NULL, 0);
185  closesocket(Socket);
187  StartSeh()
189  Error = bind(Socket, InvalidPointer, 0);
192  closesocket(Socket);
194  StartSeh()
196  Error = bind(Socket, NULL, sizeof(Addr));
199  closesocket(Socket);
201  StartSeh()
203  Error = bind(Socket, InvalidPointer, sizeof(Addr));
206  closesocket(Socket);
208  StartSeh()
210  Error = bind(Socket, (const struct sockaddr *) &Addr, 0);
213  closesocket(Socket);
215  StartSeh()
217  Error = bind(Socket, (const struct sockaddr *) &Addr, sizeof(Addr)-1);
220  closesocket(Socket);
222 
223  TestBind(Address);
224  /* TODO: test IPv6 */
225 
226  Error = WSACleanup();
227  ok_dec(Error, 0);
228 }
#define WSAENOTSOCK
Definition: winerror.h:1951
#define SOCKET_ERROR
Definition: winsock.h:333
#define trace(...)
Definition: kmt_test.h:217
char ** h_addr_list
Definition: winsock.h:138
#define TRUE
Definition: types.h:120
#define WSAEFAULT
Definition: winerror.h:1945
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
Type
Definition: Type.h:6
#define MAKEWORD(a, b)
Definition: typedefs.h:247
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
#define WSAEINVAL
Definition: winerror.h:1946
#define _countof(array)
Definition: fontsub.cpp:30
char CHAR
Definition: xmlstorage.h:175
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
#define PORT
Definition: bind.c:11
#define SOL_SOCKET
Definition: winsock.h:398
static VOID TestBind(IN_ADDR Address)
Definition: bind.c:15
#define StartSeh()
Definition: _sntprintf.h:16
int32_t INT
Definition: typedefs.h:56
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
#define closesocket
Definition: main.c:39
unsigned int BOOL
Definition: ntddk_ex.h:94
#define ok_dec(expression, result)
Definition: test.h:766
INT WSAAPI gethostname(OUT char FAR *name, IN INT namelen)
Definition: getxbyxx.c:397
#define SO_BROADCAST
Definition: winsock.h:183
INT WSAAPI setsockopt(IN SOCKET s, IN INT level, IN INT optname, IN CONST CHAR FAR *optval, IN INT optlen)
Definition: sockctrl.c:421
#define ok(value,...)
static CRYPT_DATA_BLOB b1[]
Definition: msg.c:573
short h_length
Definition: winsock.h:137
START_TEST(bind)
Definition: bind.c:130
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
#define InvalidPointer
#define inet_ntoa(addr)
Definition: inet.h:100
PHOSTENT WSAAPI gethostbyname(IN const char FAR *name)
Definition: getxbyxx.c:221
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define INVALID_SOCKET
Definition: winsock.h:332
Definition: tcpip.h:125
BOOL Error
Definition: chkdsk.c:66
unsigned char UCHAR
Definition: xmlstorage.h:181
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
struct in_addr sin_addr
Definition: winsock.h:512
#define WSAEADDRNOTAVAIL
Definition: winerror.h:1962
static CRYPT_DATA_BLOB b3[]
Definition: msg.c:592
#define skip(...)
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define AF_INET
Definition: tcpip.h:117
static CRYPT_DATA_BLOB b2[]
Definition: msg.c:582
static CRYPT_DATA_BLOB b4
Definition: msg.c:2284
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCK_STREAM
Definition: tcpip.h:118
#define SOCK_DGRAM
Definition: winsock.h:336
return STATUS_SUCCESS
Definition: btrfs.c:2725
INT WSAAPI getsockname(IN SOCKET s, OUT LPSOCKADDR name, IN OUT INT FAR *namelen)
Definition: sockctrl.c:213
struct test_data Tests[]
#define EndSeh(ExpectedStatus)
Definition: _sntprintf.h:17
_Must_inspect_result_ _In_ ULONG _In_ PSOCKADDR LocalAddress
Definition: wsk.h:170