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: test3.c |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the GetFullPathNameA API. |
10 | ** GetFullPathA will be passed a directory that contains '..'. |
11 | ** Example: test_directory\level1\..\testing.tmp. |
12 | ** To add to this test, we will also call SetCurrentDirectory to |
13 | ** ensure this is handled properly. |
14 | ** The test will create a file with in the parent directory |
15 | ** to verify that the returned directory is valid. |
16 | ** |
17 | ** Depends: SetCurrentDirectory, |
18 | ** CreateDirectory, |
19 | ** strcat, |
20 | ** memset, |
21 | ** CreateFile, |
22 | ** CloseHandle, |
23 | ** strcmp, |
24 | ** DeleteFileA, |
25 | ** RemoveDirectory. |
26 | ** |
27 | |
28 | ** |
29 | **===================================================================*/ |
30 | |
31 | #include <palsuite.h> |
32 | |
33 | #ifdef WIN32 |
34 | const char* szSeperator = "\\" ; |
35 | #else |
36 | const char* szSeperator = "//" ; |
37 | #endif |
38 | |
39 | const char* szDotDot = "..\\" ; |
40 | const char* szFileName = "testing.tmp" ; |
41 | |
42 | int __cdecl main(int argc, char *argv[]) |
43 | { |
44 | DWORD dwRc = 0; |
45 | char szReturnedPath[_MAX_DIR+1]; |
46 | char szFullFileName[_MAX_DIR+1]; |
47 | char szDirectory[256]; |
48 | char* szCreatedDir = {"test_directory" }; |
49 | char* szCreatedNextDir = {"level1" }; |
50 | WCHAR *szCreatedDirW; |
51 | LPSTR pPathPtr; |
52 | HANDLE hFile = NULL; |
53 | BOOL bRetVal = FAIL; |
54 | |
55 | /* Initialize the PAL. |
56 | */ |
57 | if (0 != PAL_Initialize(argc,argv)) |
58 | { |
59 | return (FAIL); |
60 | } |
61 | |
62 | /* Initialize the buffer. |
63 | */ |
64 | memset(szDirectory, '\0', 256); |
65 | |
66 | /* Change the current working directory. |
67 | */ |
68 | if (!SetCurrentDirectoryA(szDotDot)) |
69 | { |
70 | Fail("ERROR: SetCurrentDirectoryA failed with error code %u" |
71 | " when passed \"%s\".\n" , |
72 | GetLastError(), |
73 | szDotDot); |
74 | } |
75 | |
76 | /* Create the path to the next level of directory to create. |
77 | */ |
78 | strcat( szDirectory, szCreatedDir ); |
79 | |
80 | |
81 | /* Create a test directory. |
82 | */ |
83 | if ( !CreateDirectoryA( szDirectory, NULL ) ) |
84 | { |
85 | Fail("ERROR:%u: Unable to create directories \"%s\".\n" , |
86 | GetLastError(), |
87 | szDirectory); |
88 | } |
89 | |
90 | /* Create the path to the next level of directory to create. |
91 | */ |
92 | strcat( szDirectory, szSeperator ); |
93 | strcat( szDirectory, szCreatedNextDir ); |
94 | |
95 | /* Create a test directory. |
96 | */ |
97 | if ( !CreateDirectoryA( szDirectory, NULL ) ) |
98 | { |
99 | Trace("ERROR:%u: Unable to create directories \"%s\".\n" , |
100 | GetLastError(), |
101 | szDirectory); |
102 | bRetVal = FAIL; |
103 | goto cleanUpOne; |
104 | } |
105 | |
106 | /* Initialize the receiving char buffers. |
107 | */ |
108 | memset(szReturnedPath, 0, _MAX_DIR+1); |
109 | memset(szFullFileName, 0, _MAX_DIR+1); |
110 | |
111 | /* Create Full filename to pass, will include '..\' |
112 | * in the middle of the path. |
113 | */ |
114 | strcat(szFullFileName, szCreatedDir); |
115 | strcat(szFullFileName, szDotDot); |
116 | strcat(szFullFileName, szFileName); |
117 | |
118 | /* Get the full path to the filename. |
119 | */ |
120 | dwRc = GetFullPathNameA(szFullFileName, |
121 | _MAX_DIR, |
122 | szReturnedPath, |
123 | &pPathPtr); |
124 | if (dwRc == 0) |
125 | { |
126 | Trace("ERROR :%ld: GetFullPathName failed to " |
127 | "retrieve the path of \"%s\".\n" , |
128 | GetLastError(), |
129 | szFileName); |
130 | bRetVal = FAIL; |
131 | goto cleanUpTwo; |
132 | } |
133 | |
134 | /* The returned value should be the parent directory with the |
135 | * file name appended. */ |
136 | hFile = CreateFileA(szReturnedPath, |
137 | GENERIC_READ, |
138 | FILE_SHARE_READ, |
139 | NULL, |
140 | CREATE_ALWAYS, |
141 | FILE_ATTRIBUTE_NORMAL, |
142 | NULL); |
143 | |
144 | if (hFile == INVALID_HANDLE_VALUE) |
145 | { |
146 | Trace("ERROR :%ld: CreateFileA failed to create \"%s\".\n" , |
147 | GetLastError(), |
148 | szReturnedPath); |
149 | bRetVal = FAIL; |
150 | goto cleanUpTwo; |
151 | } |
152 | |
153 | /* Close the handle to the created file. |
154 | */ |
155 | if (CloseHandle(hFile) != TRUE) |
156 | { |
157 | Trace("ERROR :%ld: CloseHandle failed close hFile=0x%lx.\n" , |
158 | GetLastError()); |
159 | bRetVal = FAIL; |
160 | goto cleanUpThree; |
161 | } |
162 | |
163 | /* Verify that the file was created, attempt to create |
164 | * the file again. */ |
165 | hFile = CreateFileA(szReturnedPath, |
166 | GENERIC_READ, |
167 | FILE_SHARE_READ, |
168 | NULL, |
169 | CREATE_NEW, |
170 | FILE_ATTRIBUTE_NORMAL, |
171 | NULL); |
172 | if ((hFile != INVALID_HANDLE_VALUE) && |
173 | (GetLastError() != ERROR_ALREADY_EXISTS)) |
174 | { |
175 | Trace("ERROR :%ld: CreateFileA succeeded to create file " |
176 | "\"%s\", that already existed.\n" , |
177 | GetLastError(), |
178 | szFullFileName); |
179 | bRetVal = FAIL; |
180 | goto cleanUpThree; |
181 | } |
182 | |
183 | /* Verify that the returned filename is the same as the supplied. |
184 | */ |
185 | if (strcmp(pPathPtr, szFileName) != 0) |
186 | { |
187 | Trace("ERROR : Returned filename \"%s\" is not equal to " |
188 | "supplied filename \"%s\".\n" , |
189 | pPathPtr, |
190 | szFileName); |
191 | bRetVal = FAIL; |
192 | goto cleanUpThree; |
193 | } |
194 | |
195 | /* Successful test. |
196 | */ |
197 | bRetVal = PASS; |
198 | |
199 | cleanUpThree: |
200 | |
201 | /* Delete the create file. |
202 | */ |
203 | if (DeleteFileA(szReturnedPath) != TRUE) |
204 | { |
205 | Fail("ERROR :%ld: DeleteFileA failed to delete \"%s\".\n" , |
206 | GetLastError(), |
207 | szFileName); |
208 | } |
209 | |
210 | cleanUpTwo: |
211 | |
212 | /* Remove the empty directory. |
213 | */ |
214 | szCreatedDirW = convert((LPSTR)szDirectory); |
215 | if (!RemoveDirectoryW(szCreatedDirW)) |
216 | { |
217 | free (szCreatedDirW); |
218 | Fail("ERROR:%u: Unable to remove directory \"%s\".\n" , |
219 | GetLastError(), |
220 | szCreatedDir); |
221 | } |
222 | free (szCreatedDirW); |
223 | |
224 | cleanUpOne: |
225 | |
226 | /* Remove the empty directory. |
227 | */ |
228 | szCreatedDirW = convert((LPSTR)szCreatedDir); |
229 | if (!RemoveDirectoryW(szCreatedDirW)) |
230 | { |
231 | free (szCreatedDirW); |
232 | Fail("ERROR:%u: Unable to remove directory \"%s\".\n" , |
233 | GetLastError(), |
234 | szCreatedDir); |
235 | } |
236 | free (szCreatedDirW); |
237 | |
238 | /* Terminate the PAL.*/ |
239 | PAL_Terminate(); |
240 | return bRetVal; |
241 | } |
242 | |