ReactOS  0.4.14-dev-614-gbfd8a84
close.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPL - See COPYING in the top level directory
4  * PURPOSE: Test for WSASocket
5  * PROGRAMMERS: Peter Hater
6  */
7 
8 #include "ws2_32.h"
9 
11 {
12  char szBuf[10];
13  int err;
14  SOCKET sck, dup_sck;
15  WSAPROTOCOL_INFOW ProtocolInfo;
16  struct sockaddr_in to = { AF_INET, 2222, {{{ 0x7f, 0x00, 0x00, 0x01 }}} };
17 
18  /* Create the socket */
20  if(sck == INVALID_SOCKET)
21  {
22  skip("socket failed %d. Aborting test.\n", WSAGetLastError());
23  return;
24  }
25 
26  err = sendto(sck, szBuf, _countof(szBuf), 0, (struct sockaddr *)&to, sizeof(to));
27  ok(err == _countof(szBuf), "sendto err = %d %d\n", err, WSAGetLastError());
28 
29  err = WSADuplicateSocketW(sck, GetCurrentProcessId(), &ProtocolInfo);
30  ok(err == 0, "WSADuplicateSocketW err = %d %d\n", err, WSAGetLastError());
31 
32  dup_sck = WSASocketW(0, 0, 0, &ProtocolInfo, 0, 0);
33  if (dup_sck == INVALID_SOCKET)
34  {
35  skip("WSASocketW failed %d. Aborting test.\n", WSAGetLastError());
36  closesocket(sck);
37  return;
38  }
39 
40  err = sendto(dup_sck, szBuf, _countof(szBuf), 0, (struct sockaddr *)&to, sizeof(to));
41  ok(err == _countof(szBuf), "sendto err = %d %d\n", err, WSAGetLastError());
42 
43  err = closesocket(sck);
44  ok(err == 0, "closesocket sck err = %d %d\n", err, WSAGetLastError());
45 
46  err = sendto(dup_sck, szBuf, _countof(szBuf), 0, (struct sockaddr *)&to, sizeof(to));
47  ok(err == _countof(szBuf), "sendto err = %d %d\n", err, WSAGetLastError());
48 
49  err = closesocket(dup_sck);
50  ok(err == 0, "closesocket dup_sck err = %d %d\n", err, WSAGetLastError());
51  return;
52 }
53 
54 // 100 ms
55 #define TIMEOUT_SEC 0
56 #define TIMEOUT_USEC 100000
57 
58 // 250 ms
59 #define TIME_SLEEP1 250
60 
61 #define THREAD_PROC_LOOPS 5
62 
63 #define LISTEN_PORT 22222
64 #define LISTEN_BACKLOG 5
65 
67 {
69  struct timeval tval;
71  int i;
72 
73  tval.tv_sec = TIMEOUT_SEC;
74  tval.tv_usec = TIMEOUT_USEC;
75 
76  for (i = 0; i < THREAD_PROC_LOOPS; ++i)
77  {
79  // write will be empty
81 
82  select(0, &read, &write, &except, &tval);
83  }
84 
85  return 0;
86 }
87 
89 {
90  int err;
91  SOCKET sock;
92  struct sockaddr_in addrin;
93  HANDLE hthread;
94 
95  /* Create the socket */
97  if (sock == INVALID_SOCKET)
98  {
99  skip("socket failed %d. Aborting test.\n", WSAGetLastError());
100  return;
101  }
102 
103  memset(&addrin, 0, sizeof(struct sockaddr_in));
104  addrin.sin_family = AF_INET;
105  addrin.sin_addr.s_addr = inet_addr("127.0.0.1");
106  addrin.sin_port = htons(LISTEN_PORT);
107 
108  err = bind(sock, (struct sockaddr*)(&addrin), sizeof(struct sockaddr_in));
109  ok(err == 0, "bind err = %d %d\n", err, WSAGetLastError());
111  ok(err == 0, "listen err = %d %d\n", err, WSAGetLastError());
112 
113  hthread = CreateThread(NULL, 0, thread_proc, (void*)sock, 0, NULL);
114  ok(hthread != NULL, "CreateThread %ld\n", GetLastError());
115 
117  err = closesocket(sock);
118  ok(err == 0, "closesocket err = %d %d\n", err, WSAGetLastError());
119 
120  WaitForSingleObject(hthread, INFINITE);
121  CloseHandle(hthread);
122  return;
123 }
124 
126 {
127  int err;
128  SOCKET sock, dup_sock;
129  WSAPROTOCOL_INFOW ProtocolInfo;
130  struct sockaddr_in addrin;
131  HANDLE hthread;
132 
133  /* Create the socket */
135  if (sock == INVALID_SOCKET)
136  {
137  skip("socket failed %d. Aborting test.\n", WSAGetLastError());
138  return;
139  }
140 
141  memset(&addrin, 0, sizeof(struct sockaddr_in));
142  addrin.sin_family = AF_INET;
143  addrin.sin_addr.s_addr = inet_addr("127.0.0.1");
144  addrin.sin_port = htons(LISTEN_PORT);
145 
146  err = bind(sock, (struct sockaddr*)(&addrin), sizeof(struct sockaddr_in));
147  ok(err == 0, "bind err = %d %d\n", err, WSAGetLastError());
149  ok(err == 0, "listen err = %d %d\n", err, WSAGetLastError());
150 
151  err = WSADuplicateSocketW(sock, GetCurrentProcessId(), &ProtocolInfo);
152  ok(err == 0, "WSADuplicateSocketW err = %d %d\n", err, WSAGetLastError());
153 
154  dup_sock = WSASocketW(0, 0, 0, &ProtocolInfo, 0, 0);
155  if (dup_sock == INVALID_SOCKET)
156  {
157  skip("WSASocketW failed %d. Aborting test.\n", WSAGetLastError());
158  closesocket(sock);
159  return;
160  }
161 
162  hthread = CreateThread(NULL, 0, thread_proc, (void*)dup_sock, 0, NULL);
163  ok(hthread != NULL, "CreateThread %ld\n", GetLastError());
164 
165  err = closesocket(sock);
166  ok(err == 0, "closesocket err = %d %d\n", err, WSAGetLastError());
167 
169  err = closesocket(dup_sock);
170  ok(err == 0, "closesocket err = %d %d\n", err, WSAGetLastError());
171 
172  WaitForSingleObject(hthread, INFINITE);
173  CloseHandle(hthread);
174  return;
175 }
176 
178 {
179  int err;
180  WSADATA wdata;
181 
182  /* Start up Winsock */
183  err = WSAStartup(MAKEWORD(2, 2), &wdata);
184  ok(err == 0, "WSAStartup failed, iResult == %d %d\n", err, WSAGetLastError());
185 
189 
190  WSACleanup();
191 }
192 
Definition: winsock.h:66
#define CloseHandle
Definition: compat.h:406
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
static SOCKET sock
Definition: syslog.c:45
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
u_short sin_port
Definition: winsock.h:511
#define inet_addr(cp)
Definition: inet.h:98
#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 _countof(array)
Definition: fontsub.cpp:30
unsigned long tv_sec
Definition: linux.h:1738
#define closesocket
Definition: precomp.h:57
#define TIMEOUT_SEC
Definition: close.c:55
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
#define FD_ZERO(set)
Definition: winsock.h:96
#define FD_SET(fd, set)
Definition: winsock.h:89
#define TIME_SLEEP1
Definition: close.c:59
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define write
Definition: acwin.h:97
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
DWORD WINAPI thread_proc(void *param)
Definition: close.c:66
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
SOCKET WSAAPI WSASocketW(IN INT af, IN INT type, IN INT protocol, IN LPWSAPROTOCOL_INFOW lpProtocolInfo, IN GROUP g, IN DWORD dwFlags)
Definition: socklife.c:490
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:136
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
smooth NULL
Definition: ftsmooth.c:416
unsigned long tv_usec
Definition: linux.h:1739
void Test_CloseDuplicatedSocket()
Definition: close.c:10
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define INVALID_SOCKET
Definition: winsock.h:332
#define THREAD_PROC_LOOPS
Definition: close.c:61
START_TEST(close)
Definition: close.c:177
GLfloat param
Definition: glext.h:5796
#define WINAPI
Definition: msvc.h:6
unsigned long DWORD
Definition: ntddk_ex.h:95
INT WSAAPI WSADuplicateSocketW(IN SOCKET s, IN DWORD dwProcessId, OUT LPWSAPROTOCOL_INFOW lpProtocolInfo)
Definition: dupsock.c:68
#define TIMEOUT_USEC
Definition: close.c:56
void Test_CloseWhileSelectSameSocket()
Definition: close.c:88
#define except(x)
Definition: btrfs_drv.h:135
#define close
Definition: acwin.h:98
#define err(...)
struct in_addr sin_addr
Definition: winsock.h:512
#define LISTEN_PORT
Definition: close.c:63
#define ok(value,...)
Definition: atltest.h:57
void Test_CloseWhileSelectDuplicatedSocket()
Definition: close.c:125
INT WSAAPI sendto(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags, IN CONST struct sockaddr *to, IN INT tolen)
Definition: send.c:82
INT WSAAPI listen(IN SOCKET s, IN INT backlog)
Definition: sockctrl.c:123
Definition: tcpcore.h:1455
#define skip(...)
Definition: atltest.h:64
#define AF_INET
Definition: tcpip.h:117
UINT_PTR SOCKET
Definition: winsock.h:47
#define LISTEN_BACKLOG
Definition: close.c:64
#define SOCK_STREAM
Definition: tcpip.h:118
#define htons(x)
Definition: module.h:213
#define INFINITE
Definition: serial.h:102
#define SOCK_DGRAM
Definition: winsock.h:336
#define memset(x, y, z)
Definition: compat.h:39
short sin_family
Definition: winsock.h:510
_CRTIMP int __cdecl read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
DWORD WINAPI GetCurrentProcessId(VOID)
Definition: proc.c:1158