[WS2_32_APITEST] Add a test showing that GetFileType / _open_osfhandle should succeed.
[reactos.git] / modules / rostests / apitests / ws2_32 / open_osfhandle.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Test for open_osfhandle on WSASocket
5 * COPYRIGHT: Copyright 2018 Mark Jansen (mark.jansen@reactos.org)
6 */
7
8 #include "ws2_32.h"
9 #include <io.h>
10 #include <fcntl.h>
11
12
13 static void run_open_osfhandle(void)
14 {
15 DWORD type;
16 int handle, err;
17 SOCKET fd = WSASocketA(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
18 ok (fd != INVALID_SOCKET, "Invalid socket\n");
19 if (fd == INVALID_SOCKET)
20 return;
21
22 type = GetFileType((HANDLE)fd);
23 ok(type == FILE_TYPE_PIPE, "Expected type FILE_TYPE_PIPE, was: %lu\n", type);
24
25 handle = _open_osfhandle(fd, _O_BINARY | _O_RDWR);
26 err = *_errno();
27
28 ok(handle != -1, "Expected a valid handle (%i)\n", err);
29 if (handle != -1)
30 {
31 /* To close a file opened with _open_osfhandle, call _close. The underlying handle is also closed by
32 a call to _close, so it is not necessary to call the Win32 function CloseHandle on the original handle. */
33 _close(handle);
34 }
35 else
36 {
37 closesocket(fd);
38 }
39 }
40
41 START_TEST(open_osfhandle)
42 {
43 WSADATA wdata;
44 int iResult = WSAStartup(MAKEWORD(2, 2), &wdata);
45 ok(iResult == 0, "WSAStartup failed, iResult == %d\n", iResult);
46 run_open_osfhandle();
47 WSACleanup();
48 }
49