1 /*
2  * PROJECT:         ReactOS api tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Test for QueryPoints IOCTL
5  * PROGRAMMER:      Pierre Schweitzer
6  */
7 
8 #include "precomp.h"
9 
10 VOID
11 TraceMountPoint(PMOUNTMGR_MOUNT_POINTS MountPoints,
12                 PMOUNTMGR_MOUNT_POINT MountPoint)
13 {
14     trace("MountPoint: %p\n", MountPoint);
15     trace("\tSymbolicOffset: %ld\n", MountPoint->SymbolicLinkNameOffset);
16     trace("\tSymbolicLinkName: %.*S\n", MountPoint->SymbolicLinkNameLength / sizeof(WCHAR), (PWSTR)((ULONG_PTR)MountPoints + MountPoint->SymbolicLinkNameOffset));
17     trace("\tDeviceOffset: %ld\n", MountPoint->DeviceNameOffset);
18     trace("\tDeviceName: %.*S\n", MountPoint->DeviceNameLength / sizeof(WCHAR), (PWSTR)((ULONG_PTR)MountPoints + MountPoint->DeviceNameOffset));
19 }
20 
21 START_TEST(QueryPoints)
22 {
23     BOOL Ret;
24     HANDLE MountMgrHandle;
25     DWORD BytesReturned, Drives, i;
26     struct {
27         MOUNTMGR_MOUNT_POINT;
28         WCHAR Buffer[sizeof(L"\\DosDevice\\A:")];
29     } SinglePoint;
30     MOUNTMGR_MOUNT_POINTS MountPoints;
31     PMOUNTMGR_MOUNT_POINTS AllocatedPoints;
32 
33     MountMgrHandle = CreateFileW(MOUNTMGR_DOS_DEVICE_NAME, 0,
34                                  FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
35                                  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
36                                  INVALID_HANDLE_VALUE);
37     if (MountMgrHandle == INVALID_HANDLE_VALUE)
38     {
39         win_skip("MountMgr unavailable: %lx\n", GetLastError());
40         return;
41     }
42 
43     ZeroMemory(&SinglePoint, sizeof(SinglePoint));
44 
45     Ret = DeviceIoControl(MountMgrHandle, IOCTL_MOUNTMGR_QUERY_POINTS,
46                           &SinglePoint, sizeof(MOUNTMGR_MOUNT_POINT),
47                           &MountPoints, sizeof(MOUNTMGR_MOUNT_POINTS),
48                           &BytesReturned, NULL);
49     ok(Ret == FALSE, "IOCTL unexpectedly succeed\n");
50     ok(GetLastError() == ERROR_MORE_DATA, "Unexcepted failure: %lx\n", GetLastError());
51 
52     AllocatedPoints = RtlAllocateHeap(RtlGetProcessHeap(), 0, MountPoints.Size);
53     if (AllocatedPoints == NULL)
54     {
55         win_skip("Insufficiant memory\n");
56     }
57     else
58     {
59         AllocatedPoints->NumberOfMountPoints = 0;
60 
61         Ret = DeviceIoControl(MountMgrHandle, IOCTL_MOUNTMGR_QUERY_POINTS,
62                               &SinglePoint, sizeof(MOUNTMGR_MOUNT_POINT),
63                               AllocatedPoints, MountPoints.Size,
64                               &BytesReturned, NULL);
65         ok(Ret == TRUE, "IOCTL unexpectedly failed %lx\n", GetLastError());
66 
67         for (i = 0; i < AllocatedPoints->NumberOfMountPoints; ++i)
68         {
69             TraceMountPoint(AllocatedPoints, &AllocatedPoints->MountPoints[i]);
70         }
71 
72         RtlFreeHeap(RtlGetProcessHeap(), 0, AllocatedPoints);
73     }
74 
75     Drives = GetLogicalDrives();
76     if (Drives == 0)
77     {
78         win_skip("Drives map unavailable: %lx\n", GetLastError());
79         goto Done;
80     }
81 
82     for (i = 0; i < 26; i++)
83     {
84         if (!(Drives & (1 << i)))
85         {
86             break;
87         }
88     }
89 
90     if (i == 26)
91     {
92         win_skip("All the drive letters are in use, skipping\n");
93         goto Done;
94     }
95 
96     SinglePoint.SymbolicLinkNameOffset = sizeof(MOUNTMGR_MOUNT_POINT);
97     SinglePoint.SymbolicLinkNameLength = sizeof(L"\\DosDevice\\A:") - sizeof(UNICODE_NULL);
98     StringCbPrintfW((PWSTR)((ULONG_PTR)&SinglePoint + sizeof(MOUNTMGR_MOUNT_POINT)),
99                     sizeof(L"\\DosDevice\\A:"),
100                     L"\\DosDevice\\%C:",
101                     i + L'A');
102 
103     Ret = DeviceIoControl(MountMgrHandle, IOCTL_MOUNTMGR_QUERY_POINTS,
104                           &SinglePoint, sizeof(SinglePoint),
105                           &MountPoints, sizeof(MOUNTMGR_MOUNT_POINTS),
106                           &BytesReturned, NULL);
107     ok(Ret == FALSE, "IOCTL unexpectedly succeed\n");
108     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Unexcepted failure: %lx\n", GetLastError());
109 
110 Done:
111     CloseHandle(MountMgrHandle);
112 }
113