1 | /* $Id: tstRTNtPath-1.cpp 60481 2016-04-13 20:14:41Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTNtPath*.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2016 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 |
|
---|
40 |
|
---|
41 | /*********************************************************************************************************************************
|
---|
42 | * Structures and Typedefs *
|
---|
43 | *********************************************************************************************************************************/
|
---|
44 | typedef struct TSTRAVERSE
|
---|
45 | {
|
---|
46 | uint32_t cHits;
|
---|
47 | uint32_t cFirstClassHits;
|
---|
48 | uint32_t cEntries;
|
---|
49 | uint32_t cDirs;
|
---|
50 | UNICODE_STRING UniStr;
|
---|
51 | char szLongPath[RTPATH_MAX];
|
---|
52 | char szLongPathNt[RTPATH_MAX];
|
---|
53 | char szShortPath[RTPATH_MAX];
|
---|
54 | union
|
---|
55 | {
|
---|
56 | RTDIRENTRYEX EntryEx;
|
---|
57 | uint8_t abBuf[RTPATH_MAX + sizeof(RTDIRENTRYEX)];
|
---|
58 | } u;
|
---|
59 | size_t cbDirEntry;
|
---|
60 | } TSTRAVERSE;
|
---|
61 |
|
---|
62 |
|
---|
63 | void tstTraverse8dot3(TSTRAVERSE *pThis, size_t cchLong, size_t cchShort, uint32_t cShortNames)
|
---|
64 | {
|
---|
65 | pThis->cDirs++;
|
---|
66 |
|
---|
67 | uint32_t cLeftToTest = 2;
|
---|
68 | PRTDIR hDir;
|
---|
69 | int rc = RTDirOpen(&hDir, pThis->szLongPath);
|
---|
70 | if (RT_FAILURE(rc))
|
---|
71 | return;
|
---|
72 | while (pThis->cFirstClassHits < 256)
|
---|
73 | {
|
---|
74 | pThis->cbDirEntry = sizeof(pThis->u);
|
---|
75 | rc = RTDirReadEx(hDir, &pThis->u.EntryEx, &pThis->cbDirEntry, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
|
---|
76 | if (RT_FAILURE(rc))
|
---|
77 | break;
|
---|
78 | pThis->cEntries++;
|
---|
79 |
|
---|
80 | if (RTDirEntryExIsStdDotLink(&pThis->u.EntryEx))
|
---|
81 | continue;
|
---|
82 |
|
---|
83 | if ( cchLong + pThis->u.EntryEx.cbName + 1 >= sizeof(pThis->szLongPath)
|
---|
84 | || cchShort + RT_MAX(pThis->u.EntryEx.cbName, pThis->u.EntryEx.cwcShortName * 3) + 1 >= sizeof(pThis->szShortPath) )
|
---|
85 | continue; /* ignore obvious overflows */
|
---|
86 |
|
---|
87 | bool fHave8Dot3 = pThis->u.EntryEx.cwcShortName
|
---|
88 | && RTUtf16ICmpUtf8(pThis->u.EntryEx.wszShortName, pThis->u.EntryEx.szName) != 0;
|
---|
89 | if ( fHave8Dot3
|
---|
90 | || RTFS_IS_DIRECTORY(pThis->u.EntryEx.Info.Attr.fMode)
|
---|
91 | || cLeftToTest > 0)
|
---|
92 | {
|
---|
93 | if (!fHave8Dot3)
|
---|
94 | memcpy(&pThis->szShortPath[cchShort], pThis->u.EntryEx.szName, pThis->u.EntryEx.cbName + 1);
|
---|
95 | else
|
---|
96 | {
|
---|
97 | char *pszDst = &pThis->szShortPath[cchShort];
|
---|
98 | rc = RTUtf16ToUtf8Ex(pThis->u.EntryEx.wszShortName, pThis->u.EntryEx.cwcShortName, &pszDst,
|
---|
99 | sizeof(pThis->szShortPath) - cchShort, NULL);
|
---|
100 | if (RT_FAILURE(rc))
|
---|
101 | continue;
|
---|
102 | }
|
---|
103 | memcpy(&pThis->szLongPath[cchLong], pThis->u.EntryEx.szName, pThis->u.EntryEx.cbName + 1);
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Check it out.
|
---|
107 | */
|
---|
108 | HANDLE hRoot;
|
---|
109 | RTTESTI_CHECK_RC(rc = RTNtPathFromWinUtf8(&pThis->UniStr, &hRoot, pThis->szShortPath), VINF_SUCCESS);
|
---|
110 | if (RT_SUCCESS(rc))
|
---|
111 | {
|
---|
112 | RTTESTI_CHECK(pThis->UniStr.MaximumLength > pThis->UniStr.Length);
|
---|
113 | RTTESTI_CHECK(pThis->UniStr.Length == RTUtf16Len(pThis->UniStr.Buffer) * sizeof(RTUTF16));
|
---|
114 |
|
---|
115 | RTTESTI_CHECK_RC(rc = RTNtPathEnsureSpace(&pThis->UniStr, RTPATH_MAX + 256), VINF_SUCCESS);
|
---|
116 | if (RT_SUCCESS(rc))
|
---|
117 | {
|
---|
118 | RTTESTI_CHECK_RC(rc = RTNtPathExpand8dot3Path(&pThis->UniStr, false /*fPathOnly*/), VINF_SUCCESS);
|
---|
119 | if (RT_SUCCESS(rc))
|
---|
120 | {
|
---|
121 | RTTESTI_CHECK(pThis->UniStr.Length == RTUtf16Len(pThis->UniStr.Buffer) * sizeof(RTUTF16));
|
---|
122 |
|
---|
123 | /* Skip the win32 path prefix (it is usually \??\) so we can compare. Crude but works */
|
---|
124 | size_t offPrefix = 0;
|
---|
125 | while ( pThis->UniStr.Buffer[offPrefix] != pThis->szLongPath[0]
|
---|
126 | && pThis->UniStr.Buffer[offPrefix] != '\0')
|
---|
127 | offPrefix++;
|
---|
128 | if (!pThis->UniStr.Buffer[offPrefix])
|
---|
129 | offPrefix = 0;
|
---|
130 |
|
---|
131 | if (RTUtf16CmpUtf8(&pThis->UniStr.Buffer[offPrefix], pThis->szLongPath) == 0)
|
---|
132 | { /* ok */ }
|
---|
133 | else if (RTUtf16ICmpUtf8(&pThis->UniStr.Buffer[offPrefix], pThis->szLongPath) == 0)
|
---|
134 | RTTestIFailed("case mismatch: '%ls' vs '%s'", pThis->UniStr.Buffer, pThis->szLongPath);
|
---|
135 | else
|
---|
136 | RTTestIFailed("mismatch: '%ls' vs '%s'", pThis->UniStr.Buffer, pThis->szLongPath);
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * Update test efficiency hits.
|
---|
140 | */
|
---|
141 | if ( cLeftToTest > 0
|
---|
142 | && !pThis->u.EntryEx.cwcShortName
|
---|
143 | && !RTFS_IS_DIRECTORY(pThis->u.EntryEx.Info.Attr.fMode))
|
---|
144 | {
|
---|
145 | cLeftToTest--;
|
---|
146 | if (cShortNames >= 3)
|
---|
147 | pThis->cFirstClassHits++;
|
---|
148 | }
|
---|
149 | pThis->cHits++;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | RTNtPathFree(&pThis->UniStr, &hRoot);
|
---|
153 | }
|
---|
154 | //RTTestIPrintf(RTTESTLVL_ALWAYS, "debug: %u %u/%u %u/%u %s\n",
|
---|
155 | // cShortNames, pThis->cFirstClassHits, pThis->cHits, pThis->cDirs, pThis->cEntries, pThis->szShortPath);
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Decend into sub-directories. Must add slash first.
|
---|
159 | */
|
---|
160 | if (RTFS_IS_DIRECTORY(pThis->u.EntryEx.Info.Attr.fMode))
|
---|
161 | {
|
---|
162 | pThis->szLongPath[cchLong + pThis->u.EntryEx.cbName] = '\\';
|
---|
163 | pThis->szLongPath[cchLong + pThis->u.EntryEx.cbName + 1] = '\0';
|
---|
164 | strcat(&pThis->szShortPath[cchShort], "\\");
|
---|
165 |
|
---|
166 | tstTraverse8dot3(pThis,
|
---|
167 | cchLong + pThis->u.EntryEx.cbName + 1,
|
---|
168 | cchShort + strlen(&pThis->szShortPath[cchShort]),
|
---|
169 | cShortNames + (pThis->u.EntryEx.cwcShortName != 0));
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | RTDirClose(hDir);
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | int main()
|
---|
178 | {
|
---|
179 | RTTEST hTest;
|
---|
180 | int rc = RTTestInitAndCreate("tstRTNtPath-1", &hTest);
|
---|
181 | if (rc)
|
---|
182 | return rc;
|
---|
183 | RTTestBanner(hTest);
|
---|
184 |
|
---|
185 | /*
|
---|
186 | * Traverse the boot file system looking for short named and try locate an instance
|
---|
187 | * where we have at least 3 in a row.
|
---|
188 | */
|
---|
189 | RTTestSub(hTest, "8dot3");
|
---|
190 |
|
---|
191 | TSTRAVERSE This;
|
---|
192 | RT_ZERO(This);
|
---|
193 | rc = RTEnvGetEx(RTENV_DEFAULT, "SystemDrive", This.szLongPath, 64, NULL);
|
---|
194 | if (RT_SUCCESS(rc))
|
---|
195 | {
|
---|
196 | RTStrCat(This.szLongPath, sizeof(This.szLongPath), "\\");
|
---|
197 | size_t cch = strlen(This.szLongPath);
|
---|
198 | memcpy(This.szShortPath, This.szLongPath, cch + 1);
|
---|
199 |
|
---|
200 | tstTraverse8dot3(&This, cch, cch, 0);
|
---|
201 |
|
---|
202 |
|
---|
203 | }
|
---|
204 | else
|
---|
205 | RTTestSkipped(hTest, "failed to resolve %SystemDrive%: %Rrc", rc);
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Summary.
|
---|
209 | */
|
---|
210 | return RTTestSummaryAndDestroy(hTest);
|
---|
211 | }
|
---|
212 |
|
---|