1 | /* $Id: tstRTNtPath-1.cpp 76409 2018-12-23 18:27:21Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTNtPath*.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/nt/nt-and-windows.h>
|
---|
32 |
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/dir.h>
|
---|
35 | #include <iprt/env.h>
|
---|
36 | #include <iprt/path.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/test.h>
|
---|
39 | #include <iprt/utf16.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Structures and Typedefs *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | typedef struct TSTRAVERSE
|
---|
46 | {
|
---|
47 | uint32_t cHits;
|
---|
48 | uint32_t cFirstClassHits;
|
---|
49 | uint32_t cEntries;
|
---|
50 | uint32_t cDirs;
|
---|
51 | UNICODE_STRING UniStr;
|
---|
52 | char szLongPath[RTPATH_MAX];
|
---|
53 | char szLongPathNt[RTPATH_MAX];
|
---|
54 | char szShortPath[RTPATH_MAX];
|
---|
55 | union
|
---|
56 | {
|
---|
57 | RTDIRENTRYEX EntryEx;
|
---|
58 | uint8_t abBuf[RTPATH_MAX + sizeof(RTDIRENTRYEX)];
|
---|
59 | } u;
|
---|
60 | size_t cbDirEntry;
|
---|
61 | } TSTRAVERSE;
|
---|
62 |
|
---|
63 |
|
---|
64 | void tstTraverse8dot3(TSTRAVERSE *pThis, size_t cchLong, size_t cchShort, uint32_t cShortNames)
|
---|
65 | {
|
---|
66 | pThis->cDirs++;
|
---|
67 |
|
---|
68 | uint32_t cLeftToTest = 2;
|
---|
69 | RTDIR hDir;
|
---|
70 | int rc = RTDirOpen(&hDir, pThis->szLongPath);
|
---|
71 | if (RT_FAILURE(rc))
|
---|
72 | return;
|
---|
73 | while (pThis->cFirstClassHits < 256)
|
---|
74 | {
|
---|
75 | pThis->cbDirEntry = sizeof(pThis->u);
|
---|
76 | rc = RTDirReadEx(hDir, &pThis->u.EntryEx, &pThis->cbDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
77 | if (RT_FAILURE(rc))
|
---|
78 | break;
|
---|
79 | pThis->cEntries++;
|
---|
80 |
|
---|
81 | if (RTDirEntryExIsStdDotLink(&pThis->u.EntryEx))
|
---|
82 | continue;
|
---|
83 |
|
---|
84 | if ( cchLong + pThis->u.EntryEx.cbName + 1 >= sizeof(pThis->szLongPath)
|
---|
85 | || cchShort + RT_MAX(pThis->u.EntryEx.cbName, pThis->u.EntryEx.cwcShortName * 3) + 1 >= sizeof(pThis->szShortPath) )
|
---|
86 | continue; /* ignore obvious overflows */
|
---|
87 |
|
---|
88 | bool fHave8Dot3 = pThis->u.EntryEx.cwcShortName
|
---|
89 | && RTUtf16ICmpUtf8(pThis->u.EntryEx.wszShortName, pThis->u.EntryEx.szName) != 0;
|
---|
90 | if ( fHave8Dot3
|
---|
91 | || RTFS_IS_DIRECTORY(pThis->u.EntryEx.Info.Attr.fMode)
|
---|
92 | || cLeftToTest > 0)
|
---|
93 | {
|
---|
94 | if (!fHave8Dot3)
|
---|
95 | memcpy(&pThis->szShortPath[cchShort], pThis->u.EntryEx.szName, pThis->u.EntryEx.cbName + 1);
|
---|
96 | else
|
---|
97 | {
|
---|
98 | char *pszDst = &pThis->szShortPath[cchShort];
|
---|
99 | rc = RTUtf16ToUtf8Ex(pThis->u.EntryEx.wszShortName, pThis->u.EntryEx.cwcShortName, &pszDst,
|
---|
100 | sizeof(pThis->szShortPath) - cchShort, NULL);
|
---|
101 | if (RT_FAILURE(rc))
|
---|
102 | continue;
|
---|
103 | }
|
---|
104 | memcpy(&pThis->szLongPath[cchLong], pThis->u.EntryEx.szName, pThis->u.EntryEx.cbName + 1);
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Check it out.
|
---|
108 | */
|
---|
109 | HANDLE hRoot;
|
---|
110 | RTTESTI_CHECK_RC(rc = RTNtPathFromWinUtf8(&pThis->UniStr, &hRoot, pThis->szShortPath), VINF_SUCCESS);
|
---|
111 | if (RT_SUCCESS(rc))
|
---|
112 | {
|
---|
113 | RTTESTI_CHECK(pThis->UniStr.MaximumLength > pThis->UniStr.Length);
|
---|
114 | RTTESTI_CHECK(pThis->UniStr.Length == RTUtf16Len(pThis->UniStr.Buffer) * sizeof(RTUTF16));
|
---|
115 |
|
---|
116 | RTTESTI_CHECK_RC(rc = RTNtPathEnsureSpace(&pThis->UniStr, RTPATH_MAX + 256), VINF_SUCCESS);
|
---|
117 | if (RT_SUCCESS(rc))
|
---|
118 | {
|
---|
119 | RTTESTI_CHECK_RC(rc = RTNtPathExpand8dot3Path(&pThis->UniStr, false /*fPathOnly*/), VINF_SUCCESS);
|
---|
120 | if (RT_SUCCESS(rc))
|
---|
121 | {
|
---|
122 | RTTESTI_CHECK(pThis->UniStr.Length == RTUtf16Len(pThis->UniStr.Buffer) * sizeof(RTUTF16));
|
---|
123 |
|
---|
124 | /* Skip the win32 path prefix (it is usually \??\) so we can compare. Crude but works */
|
---|
125 | size_t offPrefix = 0;
|
---|
126 | while ( pThis->UniStr.Buffer[offPrefix] != pThis->szLongPath[0]
|
---|
127 | && pThis->UniStr.Buffer[offPrefix] != '\0')
|
---|
128 | offPrefix++;
|
---|
129 | if (!pThis->UniStr.Buffer[offPrefix])
|
---|
130 | offPrefix = 0;
|
---|
131 |
|
---|
132 | if (RTUtf16CmpUtf8(&pThis->UniStr.Buffer[offPrefix], pThis->szLongPath) == 0)
|
---|
133 | { /* ok */ }
|
---|
134 | else if (RTUtf16ICmpUtf8(&pThis->UniStr.Buffer[offPrefix], pThis->szLongPath) == 0)
|
---|
135 | RTTestIFailed("case mismatch: '%ls' vs '%s'", pThis->UniStr.Buffer, pThis->szLongPath);
|
---|
136 | else
|
---|
137 | RTTestIFailed("mismatch: '%ls' vs '%s'", pThis->UniStr.Buffer, pThis->szLongPath);
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * Update test efficiency hits.
|
---|
141 | */
|
---|
142 | if ( cLeftToTest > 0
|
---|
143 | && !pThis->u.EntryEx.cwcShortName
|
---|
144 | && !RTFS_IS_DIRECTORY(pThis->u.EntryEx.Info.Attr.fMode))
|
---|
145 | {
|
---|
146 | cLeftToTest--;
|
---|
147 | if (cShortNames >= 3)
|
---|
148 | pThis->cFirstClassHits++;
|
---|
149 | }
|
---|
150 | pThis->cHits++;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | RTNtPathFree(&pThis->UniStr, &hRoot);
|
---|
154 | }
|
---|
155 | //RTTestIPrintf(RTTESTLVL_ALWAYS, "debug: %u %u/%u %u/%u %s\n",
|
---|
156 | // cShortNames, pThis->cFirstClassHits, pThis->cHits, pThis->cDirs, pThis->cEntries, pThis->szShortPath);
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * Decend into sub-directories. Must add slash first.
|
---|
160 | */
|
---|
161 | if (RTFS_IS_DIRECTORY(pThis->u.EntryEx.Info.Attr.fMode))
|
---|
162 | {
|
---|
163 | pThis->szLongPath[cchLong + pThis->u.EntryEx.cbName] = '\\';
|
---|
164 | pThis->szLongPath[cchLong + pThis->u.EntryEx.cbName + 1] = '\0';
|
---|
165 | strcat(&pThis->szShortPath[cchShort], "\\");
|
---|
166 |
|
---|
167 | tstTraverse8dot3(pThis,
|
---|
168 | cchLong + pThis->u.EntryEx.cbName + 1,
|
---|
169 | cchShort + strlen(&pThis->szShortPath[cchShort]),
|
---|
170 | cShortNames + (pThis->u.EntryEx.cwcShortName != 0));
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 | RTDirClose(hDir);
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | int main()
|
---|
179 | {
|
---|
180 | RTTEST hTest;
|
---|
181 | int rc = RTTestInitAndCreate("tstRTNtPath-1", &hTest);
|
---|
182 | if (rc)
|
---|
183 | return rc;
|
---|
184 | RTTestBanner(hTest);
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Traverse the boot file system looking for short named and try locate an instance
|
---|
188 | * where we have at least 3 in a row.
|
---|
189 | */
|
---|
190 | RTTestSub(hTest, "8dot3");
|
---|
191 |
|
---|
192 | TSTRAVERSE This;
|
---|
193 | RT_ZERO(This);
|
---|
194 | rc = RTEnvGetEx(RTENV_DEFAULT, "SystemDrive", This.szLongPath, 64, NULL);
|
---|
195 | if (RT_SUCCESS(rc))
|
---|
196 | {
|
---|
197 | RTStrCat(This.szLongPath, sizeof(This.szLongPath), "\\");
|
---|
198 | size_t cch = strlen(This.szLongPath);
|
---|
199 | memcpy(This.szShortPath, This.szLongPath, cch + 1);
|
---|
200 |
|
---|
201 | tstTraverse8dot3(&This, cch, cch, 0);
|
---|
202 | RTTestIPrintf(RTTESTLVL_ALWAYS, "info: cEntries=%u cHits=%u cFirstClassHits=%u\n",
|
---|
203 | This.cEntries, This.cHits, This.cFirstClassHits);
|
---|
204 | }
|
---|
205 | else
|
---|
206 | RTTestSkipped(hTest, "failed to resolve %SystemDrive%: %Rrc", rc);
|
---|
207 |
|
---|
208 | /*
|
---|
209 | * Summary.
|
---|
210 | */
|
---|
211 | return RTTestSummaryAndDestroy(hTest);
|
---|
212 | }
|
---|
213 |
|
---|