1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=====================================================================
6**
7** Source: test2.c
8**
9** Purpose: Tests the PAL implementation of the GetFullPathNameW function.
10** Get the full path for a file name and verify the results.
11** This test will use a relative path, containing '..\'. To
12** add to this test, we will also call SetCurrentDirectory to
13** ensure this is handled properly.
14**
15**
16**===================================================================*/
17
18#include <palsuite.h>
19
20WCHAR szwDotDot[] = {'.','.','\\','\0'};
21WCHAR szwFileName[] = {'t','e','s','t','i','n','g','.','t','m','p','\0'};
22
23int __cdecl main(int argc, char *argv[])
24{
25 DWORD dwRc = 0;
26 WCHAR szwReturnedPath[_MAX_DIR+1];
27 WCHAR szwFullFileName[_MAX_DIR+1];
28 char *szReturnedPath;
29 char *szFileName;
30 LPWSTR pPathPtr;
31 HANDLE hFile = NULL;
32
33 if (0 != PAL_Initialize(argc,argv))
34 {
35 return FAIL;
36 }
37
38 /* change the directory */
39 if (!SetCurrentDirectoryW(szwDotDot))
40 {
41 Fail("ERROR: SetCurrentDirectoryW failed with error code %u"
42 " when passed \"%S\".\n",
43 GetLastError(),
44 szwDotDot);
45 }
46
47 /* Initialize the receiving char buffers.
48 */
49 memset(szwReturnedPath, 0, _MAX_DIR+1);
50 memset(szwFullFileName, 0, _MAX_DIR+1);
51
52 /* Create Full filename to pass, will include '..\'
53 * as a pre-fix. */
54 wcscat(szwFullFileName, szwDotDot);
55 wcscat(szwFullFileName, szwFileName);
56
57 /* Convert wide char strings to multibyte, to us
58 * incase of error messages.*/
59 szFileName = convertC(szwFileName);
60
61 /* Get the full path to the filename.
62 */
63 dwRc = GetFullPathNameW(szwFullFileName,
64 _MAX_DIR,
65 szwReturnedPath,
66 &pPathPtr);
67
68 szReturnedPath = convertC(szwReturnedPath);
69
70 if (dwRc == 0)
71 {
72 Trace("ERROR :%ld: Failed to get path to \"%s\".\n",
73 GetLastError(),
74 szReturnedPath);
75 free(szReturnedPath);
76 free(szFileName);
77 Fail("");
78 }
79
80 /*
81 * The returned value should be the parent directory with the
82 * file name appended.
83 */
84 hFile = CreateFileW(szwReturnedPath,
85 GENERIC_READ,
86 FILE_SHARE_READ,
87 NULL,
88 CREATE_ALWAYS,
89 FILE_ATTRIBUTE_NORMAL,
90 NULL);
91 if (hFile == INVALID_HANDLE_VALUE)
92 {
93 Trace("ERROR :%ld: CreateFileW failed to create file \"%s\".\n",
94 GetLastError(),
95 szReturnedPath);
96 free(szFileName);
97 free(szReturnedPath);
98 Fail("");
99 }
100
101 /* Close the handle to the create file.*/
102 if (CloseHandle(hFile) != TRUE)
103 {
104 Trace("ERROR :%ld: Failed to close handle hFile=0x%lx.\n",
105 GetLastError(),
106 hFile);
107 goto terminate;
108 }
109
110 /* Verify that the file was created, attempt to create
111 * the file again. */
112 hFile = CreateFileW(szwReturnedPath,
113 GENERIC_READ,
114 FILE_SHARE_READ,
115 NULL,
116 CREATE_NEW,
117 FILE_ATTRIBUTE_NORMAL,
118 NULL);
119 if ((hFile != INVALID_HANDLE_VALUE) &&
120 (GetLastError() != ERROR_ALREADY_EXISTS))
121 {
122 Trace("ERROR :%ld: CreateFileW succeeded to create file "
123 "\"%s\", that already existed.\n",
124 GetLastError(),
125 szReturnedPath);
126 goto terminate;
127 }
128
129 /* Verify that the returned filename is the same as the supplied.
130 */
131 if (wcsncmp(pPathPtr, szwFileName, wcslen(szwFileName)) != 0)
132 {
133 Trace("ERROR : Returned filename is not equal to \"%s\".\n",
134 szFileName);
135 goto terminate;
136 }
137
138terminate:
139 /* Delete the create file.
140 */
141 if (DeleteFileW(szwFullFileName) != TRUE)
142 {
143 Trace("ERROR :%ld: DeleteFileW failed to delete \"%s\".\n",
144 szFileName,
145 GetLastError());
146 free(szFileName);
147 free(szReturnedPath);
148 Fail("");
149 }
150
151 free(szFileName);
152 free(szReturnedPath);
153
154 /* Terminate the PAL.
155 */
156 PAL_Terminate();
157 return PASS;
158}
159
160