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 <ndk/iofuncs.h>
10 #include <ndk/obfuncs.h>
11 #include <io.h>
12 #include <fcntl.h>
13 
14 #define WINVER_WIN8    0x0602
15 
16 static void run_open_osfhandle(void)
17 {
18     DWORD type;
19     int handle, err;
20     FILE_FS_DEVICE_INFORMATION DeviceInfo;
21     IO_STATUS_BLOCK StatusBlock;
22     NTSTATUS Status;
23 
24     SOCKET fd = WSASocketA(AF_INET, SOCK_STREAM, 0, NULL, 0, 0);
25     ok (fd != INVALID_SOCKET, "Invalid socket\n");
26     if (fd == INVALID_SOCKET)
27         return;
28 
29     type = GetFileType((HANDLE)fd);
30     ok(type == FILE_TYPE_PIPE, "Expected type FILE_TYPE_PIPE, was: %lu\n", type);
31 
32     Status = NtQueryVolumeInformationFile((HANDLE)fd, &StatusBlock, &DeviceInfo, sizeof(DeviceInfo), FileFsDeviceInformation);
33     ok(Status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got 0x%lx\n", Status);
34     if (Status == STATUS_SUCCESS)
35     {
36         RTL_OSVERSIONINFOEXW rtlinfo = { sizeof(rtlinfo), 0 };
37         ULONG Characteristics;
38         DWORD dwWinVersion;
39         ok(DeviceInfo.DeviceType == FILE_DEVICE_NAMED_PIPE, "Expected FILE_DEVICE_NAMED_PIPE, got: 0x%lx\n", DeviceInfo.DeviceType);
40 
41         RtlGetVersion((PRTL_OSVERSIONINFOW)&rtlinfo);
42         dwWinVersion = (rtlinfo.dwMajorVersion << 8) | rtlinfo.dwMinorVersion;
43         Characteristics = dwWinVersion >= WINVER_WIN8 ? 0x20000 : 0;
44         ok(DeviceInfo.Characteristics == Characteristics, "Expected 0x%lx, got: 0x%lx\n", Characteristics, DeviceInfo.Characteristics);
45     }
46 
47     handle = _open_osfhandle(fd, _O_BINARY | _O_RDWR);
48     err = *_errno();
49 
50     ok(handle != -1, "Expected a valid handle (%i)\n", err);
51     if (handle != -1)
52     {
53         /* To close a file opened with _open_osfhandle, call _close. The underlying handle is also closed by
54            a call to _close, so it is not necessary to call the Win32 function CloseHandle on the original handle. */
55         _close(handle);
56     }
57     else
58     {
59         closesocket(fd);
60     }
61 }
62 
63 START_TEST(open_osfhandle)
64 {
65     WSADATA wdata;
66     int iResult = WSAStartup(MAKEWORD(2, 2), &wdata);
67     ok(iResult == 0, "WSAStartup failed, iResult == %d\n", iResult);
68     run_open_osfhandle();
69     WSACleanup();
70 }
71 
72