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