Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
7.6 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. tlfn.c
  5. Abstract:
  6. Test program for GetShortPathName and GetLongPathName
  7. Author:
  8. William Hsieh (williamh) 26-Mar-1997
  9. Revision History:
  10. --*/
  11. #undef UNICODE
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <assert.h>
  16. #include <stdio.h>
  17. #include <conio.h>
  18. #include <string.h>
  19. #include <windows.h>
  20. #include "basedll.h"
  21. BOOL
  22. CreateTestDirs(LPSTR BasePath, DWORD Depth);
  23. BOOL
  24. DeleteTestDirs(LPSTR BasePath, DWORD Depth);
  25. DWORD
  26. DoTest(LPSTR BasePath, DWORD Depth);
  27. BOOL
  28. DoNullPathTest();
  29. LPSTR g_BasePath = "C:\\LongDirectoryForTesting";
  30. #define MAX_SUBDIRS 22
  31. // Note that NT file APIs strips trailing white chars automatically,
  32. // thus, path name such as "12345678.12 " will be created as
  33. // "12345678.12". Do not put something like this in the following table
  34. // or the test will fail.
  35. // The TestDepth controls the depth of the testing sub-dirs this program
  36. // creates. It could take a long time to complete the test if TestDepth
  37. // is set to larger than 3. With TestDepth set to 1, the program only tests
  38. // the BasePath. You can always manually test a particular pathname
  39. // by running "tlfn 1 yourpath". In this case, the given directory will not
  40. // be removed by this program.
  41. //
  42. LPSTR g_subdir[MAX_SUBDIRS] = {
  43. "12345678.123",
  44. "12345678.1",
  45. "8.1",
  46. "8.123",
  47. "12345678.1",
  48. "12345678",
  49. " 2345678. 3",
  50. " 8.1 3",
  51. "1 8. 23",
  52. "123 678.123",
  53. "123 678.1 3",
  54. ".1",
  55. "..2",
  56. "...3",
  57. "1 4 6 8........1 3",
  58. "1 8..123",
  59. "12345678..1 3",
  60. "12345678.12345678",
  61. "1234567890123456.1111",
  62. "123456789.123",
  63. "12345678.1234",
  64. "12345 1"
  65. };
  66. CHAR g_ShortName[MAX_PATH + 1];
  67. CHAR g_LongName[MAX_PATH + 1];
  68. DWORD g_TestDepth = 3;
  69. DWORD
  70. _cdecl
  71. main(
  72. int argc,
  73. char *argv[],
  74. char *envp[]
  75. )
  76. {
  77. DWORD FailCount;
  78. LPSTR BasePath;
  79. DWORD TestDepth;
  80. TestDepth = g_TestDepth;
  81. BasePath = g_BasePath;
  82. if (argc > 1 && argv[1])
  83. {
  84. TestDepth = atoi(argv[1]);
  85. }
  86. if (argc > 2 && argv[2])
  87. {
  88. BasePath = argv[2];
  89. }
  90. printf("Test depth = %d, Base path = %s\n", TestDepth, BasePath);
  91. if (!DoNullPathTest()) {
  92. printf("NullPath test Failed\n");
  93. return FALSE;
  94. }
  95. printf("Null Path test passed\n");
  96. if (TestDepth != 1 || BasePath == g_BasePath) {
  97. printf("\n\nCreating testing sub-directories.....\n\n\n");
  98. if (!CreateTestDirs(BasePath, TestDepth))
  99. {
  100. printf("Unable to create test path names\n");
  101. return FALSE;
  102. }
  103. }
  104. printf("Start testing....\n");
  105. FailCount = DoTest(BasePath, TestDepth);
  106. if (!FailCount)
  107. printf("Test passed\n");
  108. else
  109. printf("Test not passed, failed count = %ld\n", FailCount);
  110. if (TestDepth != 1 || BasePath == g_BasePath) {
  111. printf("Removing test sub-directories\n\n\n");
  112. DeleteTestDirs(BasePath, TestDepth);
  113. }
  114. return TRUE;
  115. }
  116. BOOL
  117. CreateTestDirs(
  118. LPTSTR BasePath,
  119. DWORD Depth
  120. )
  121. {
  122. int len;
  123. CHAR szBase[MAX_PATH + 1];
  124. BOOL Result;
  125. DWORD dw;
  126. Result = TRUE;
  127. dw = GetFileAttributes(BasePath);
  128. if (dw == 0xFFFFFFFF)
  129. {
  130. if (!CreateDirectory(BasePath, NULL))
  131. {
  132. printf("CreateTestDirs::Unable to create base directory, error = %ld\n",
  133. GetLastError());
  134. return FALSE;
  135. }
  136. }
  137. dw = GetFileAttributes(BasePath);
  138. if (dw == 0xFFFFFFFF || !(dw & FILE_ATTRIBUTE_DIRECTORY))
  139. return FALSE;
  140. if (Depth > 1)
  141. {
  142. int i;
  143. len = strlen(BasePath);
  144. strcpy(szBase, BasePath);
  145. for (i = 0; i < MAX_SUBDIRS; i++)
  146. {
  147. szBase[len] = '\\';
  148. strcpy((szBase + len + 1), g_subdir[(Depth & 1) ? i : MAX_SUBDIRS - i - 1]);
  149. if (!CreateTestDirs(szBase, Depth - 1))
  150. {
  151. Result = FALSE;
  152. break;
  153. }
  154. }
  155. }
  156. return Result;
  157. }
  158. BOOL
  159. DeleteTestDirs(
  160. LPSTR BasePath,
  161. DWORD Depth
  162. )
  163. {
  164. int i, len;
  165. CHAR Temp[MAX_PATH + 1];
  166. strcpy(Temp, BasePath);
  167. len = strlen(BasePath);
  168. if (Depth)
  169. {
  170. for (i = 0; i < MAX_SUBDIRS; i++)
  171. {
  172. Temp[len] = '\\';
  173. strcpy((Temp + len + 1), g_subdir[(Depth & 1) ?i : MAX_SUBDIRS - i - 1]);
  174. DeleteTestDirs(Temp, Depth - 1);
  175. }
  176. }
  177. RemoveDirectory(BasePath);
  178. return TRUE;
  179. }
  180. DWORD
  181. DoTest(
  182. LPSTR BasePath,
  183. DWORD Depth
  184. )
  185. {
  186. DWORD dw, dwTemp;
  187. DWORD FailCount;
  188. LPSTR ShortPath, LongPath;
  189. CHAR FunnyBuffer[1];
  190. ASSERT(Depth >= 1);
  191. ASSERT(BasePath);
  192. FailCount = 0;
  193. // this should fail
  194. dw = GetShortPathName(BasePath, NULL, 0);
  195. dwTemp = GetShortPathName(BasePath, FunnyBuffer, sizeof(FunnyBuffer) / sizeof(CHAR));
  196. ASSERT(dw == dwTemp);
  197. dwTemp = GetShortPathName(BasePath, NULL, 0xffff);
  198. ASSERT(dw == dwTemp);
  199. dwTemp = GetShortPathName(BasePath, FunnyBuffer, 0);
  200. ASSERT(dwTemp == dw);
  201. ShortPath = (LPTSTR)malloc(dw * sizeof(CHAR));
  202. if (!ShortPath) {
  203. printf("Not enough memory\n");
  204. return 0;
  205. }
  206. dwTemp = GetShortPathName(BasePath, ShortPath, dw - 1);
  207. ASSERT(dwTemp == dw);
  208. dwTemp = GetShortPathName(BasePath, ShortPath, dw);
  209. ASSERT(dwTemp && dwTemp + 1 == dw);
  210. printf("\"%s\" L-S -> \"%s\"\n", BasePath, ShortPath);
  211. strcpy(g_LongName, BasePath);
  212. dw = GetShortPathName(g_LongName, g_LongName, dwTemp - 1);
  213. if (_stricmp(BasePath, g_LongName))
  214. {
  215. printf("Overwrite source string\n");
  216. ASSERT(FALSE);
  217. strcpy(g_LongName, BasePath);
  218. }
  219. dw = GetShortPathName(g_LongName, g_LongName, dwTemp + 1);
  220. ASSERT(dw == dwTemp);
  221. if (_stricmp(ShortPath, g_LongName)) {
  222. printf("GetShortPathName mismatch with different desitination buffer\n");
  223. ASSERT(FALSE);
  224. }
  225. dw = GetLongPathName(ShortPath, NULL, 0);
  226. dwTemp = GetLongPathName(ShortPath, FunnyBuffer, sizeof(FunnyBuffer) / sizeof(CHAR));
  227. ASSERT(dw == dwTemp);
  228. dwTemp = GetLongPathName(ShortPath, NULL, 0xffff);
  229. ASSERT(dwTemp == dw);
  230. dwTemp = GetLongPathName(ShortPath, FunnyBuffer, 0);
  231. ASSERT(dw == dwTemp);
  232. LongPath = (LPSTR)malloc(dw * sizeof(CHAR));
  233. if (!LongPath) {
  234. printf("Not enough memory\n");
  235. free(ShortPath);
  236. return(0);
  237. }
  238. dwTemp = GetLongPathName(ShortPath, LongPath, dw - 1);
  239. ASSERT( dw == dwTemp);
  240. dwTemp = GetLongPathName(ShortPath, LongPath, dw);
  241. ASSERT(dwTemp && dwTemp + 1 == dw);
  242. printf("\"%s\" S-L -> \"%s\"\n", ShortPath, LongPath);
  243. strcpy(g_ShortName, ShortPath);
  244. dw = GetLongPathName(g_ShortName, g_ShortName, dwTemp - 1);
  245. if (_stricmp(ShortPath, g_ShortName))
  246. {
  247. printf("Overwrite source string\n");
  248. ASSERT(FALSE);
  249. strcpy(g_ShortName, ShortPath);
  250. }
  251. dw = GetLongPathName(g_ShortName, g_ShortName, dwTemp + 1);
  252. ASSERT(dw == dwTemp);
  253. if (_stricmp(LongPath, g_ShortName)) {
  254. printf("GetLongPathName mismatch with different desitination buffer\n");
  255. ASSERT(FALSE);
  256. }
  257. if (_stricmp(LongPath, BasePath))
  258. {
  259. FailCount++;
  260. printf("round trip does not match\n");
  261. ASSERT(FALSE);
  262. }
  263. if(Depth > 1) {
  264. int i, len;
  265. CHAR TempName[MAX_PATH + 1];
  266. len = strlen(BasePath);
  267. strcpy(TempName, BasePath);
  268. for (i = 0; i < MAX_SUBDIRS; i++)
  269. {
  270. TempName[len] = '\\';
  271. strcpy((TempName + len + 1), g_subdir[(Depth & 1) ?i : MAX_SUBDIRS - i - 1]);
  272. FailCount += DoTest(TempName, Depth - 1);
  273. }
  274. }
  275. free(ShortPath);
  276. free(LongPath);
  277. return FailCount;
  278. }
  279. BOOL
  280. DoNullPathTest()
  281. {
  282. CHAR ShortName[1];
  283. CHAR LongName[1];
  284. DWORD dw;
  285. LongName[0] = '\0';
  286. dw = GetShortPathName(LongName, ShortName, 0);
  287. ASSERT(dw == 1);
  288. dw = GetShortPathName(LongName, ShortName, 1);
  289. ASSERT(dw == 0);
  290. ShortName[0] = '\0';
  291. dw = GetLongPathName(ShortName, LongName, 0);
  292. ASSERT(dw == 1);
  293. dw = GetLongPathName(ShortName, LongName, 1);
  294. ASSERT(dw == 0);
  295. return (dw == 0);
  296. }