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: test4.c |
8 | ** |
9 | ** Purpose: Tests the PAL implementation of the GetFullPathNameA API. |
10 | ** GetFullPathA will be passed a directory that begins with '..'. |
11 | ** Example: ..\test_directory\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 | WCHAR *szCreatedDirW; |
50 | LPSTR pPathPtr; |
51 | HANDLE hFile = NULL; |
52 | BOOL bRetVal = FAIL; |
53 | |
54 | /* Initialize the PAL. |
55 | */ |
56 | if (0 != PAL_Initialize(argc,argv)) |
57 | { |
58 | return (FAIL); |
59 | } |
60 | |
61 | /* Initialize the buffer. |
62 | */ |
63 | memset(szDirectory, '\0', 256); |
64 | |
65 | /* Create the path to the next level of directory to create. |
66 | */ |
67 | strcat( szDirectory, szDotDot ); /* .. */ |
68 | strcat( szDirectory, szSeperator ); /* ../ */ |
69 | strcat( szDirectory, szCreatedDir ); /* ../test_directory */ |
70 | |
71 | /* Create a test directory. |
72 | */ |
73 | if ( !CreateDirectoryA( szDirectory, NULL ) ) |
74 | { |
75 | Fail("ERROR:%u: Unable to create directories \"%s\".\n" , |
76 | GetLastError(), |
77 | szDirectory); |
78 | } |
79 | |
80 | /* Initialize the receiving char buffers. |
81 | */ |
82 | memset(szReturnedPath, 0, _MAX_DIR+1); |
83 | memset(szFullFileName, 0, _MAX_DIR+1); |
84 | |
85 | /* Create Full filename to pass, will include '..\' |
86 | * in the middle of the path. |
87 | */ |
88 | strcat( szFullFileName, szDotDot ); /* .. */ |
89 | strcat( szFullFileName, szSeperator ); /* ../ */ |
90 | strcat( szFullFileName, szCreatedDir ); /* ../test_directory */ |
91 | strcat( szFullFileName, szSeperator ); /* ../test_directory/ */ |
92 | strcat( szFullFileName, szFileName ); /* ../test_directory/testing.tmp */ |
93 | |
94 | /* Get the full path to the filename. |
95 | */ |
96 | dwRc = GetFullPathNameA(szFullFileName, |
97 | _MAX_DIR, |
98 | szReturnedPath, |
99 | &pPathPtr); |
100 | if (dwRc == 0) |
101 | { |
102 | Trace("ERROR :%ld: GetFullPathName failed to " |
103 | "retrieve the path of \"%s\".\n" , |
104 | GetLastError(), |
105 | szFileName); |
106 | bRetVal = FAIL; |
107 | goto cleanUpOne; |
108 | } |
109 | |
110 | /* The returned value should be the parent directory with the |
111 | * file name appended. */ |
112 | hFile = CreateFileA(szReturnedPath, |
113 | GENERIC_READ, |
114 | FILE_SHARE_READ, |
115 | NULL, |
116 | CREATE_ALWAYS, |
117 | FILE_ATTRIBUTE_NORMAL, |
118 | NULL); |
119 | |
120 | if (hFile == INVALID_HANDLE_VALUE) |
121 | { |
122 | Trace("ERROR :%ld: CreateFileA failed to create \"%s\".\n" , |
123 | GetLastError(), |
124 | szReturnedPath); |
125 | bRetVal = FAIL; |
126 | goto cleanUpOne; |
127 | } |
128 | |
129 | /* Close the handle to the created file. |
130 | */ |
131 | if (CloseHandle(hFile) != TRUE) |
132 | { |
133 | Trace("ERROR :%ld: CloseHandle failed close hFile=0x%lx.\n" , |
134 | GetLastError()); |
135 | bRetVal = FAIL; |
136 | goto cleanUpTwo; |
137 | } |
138 | |
139 | /* Verify that the file was created, attempt to create |
140 | * the file again. */ |
141 | hFile = CreateFileA(szReturnedPath, |
142 | GENERIC_READ, |
143 | FILE_SHARE_READ, |
144 | NULL, |
145 | CREATE_NEW, |
146 | FILE_ATTRIBUTE_NORMAL, |
147 | NULL); |
148 | if ((hFile != INVALID_HANDLE_VALUE) && |
149 | (GetLastError() != ERROR_ALREADY_EXISTS)) |
150 | { |
151 | Trace("ERROR :%ld: CreateFileA succeeded to create file " |
152 | "\"%s\", that already existed.\n" , |
153 | GetLastError(), |
154 | szFullFileName); |
155 | bRetVal = FAIL; |
156 | goto cleanUpTwo; |
157 | } |
158 | |
159 | /* Verify that the returned filename is the same as the supplied. |
160 | */ |
161 | if (strcmp(pPathPtr, szFileName) != 0) |
162 | { |
163 | Trace("ERROR : Returned filename \"%s\" is not equal to " |
164 | "supplied filename \"%s\".\n" , |
165 | pPathPtr, |
166 | szFileName); |
167 | bRetVal = FAIL; |
168 | goto cleanUpTwo; |
169 | } |
170 | |
171 | /* Successful test. |
172 | */ |
173 | bRetVal = PASS; |
174 | |
175 | cleanUpTwo: |
176 | |
177 | /* Delete the create file. |
178 | */ |
179 | if (DeleteFileA(szReturnedPath) != TRUE) |
180 | { |
181 | Fail("ERROR :%ld: DeleteFileA failed to delete \"%s\".\n" , |
182 | GetLastError(), |
183 | szFileName); |
184 | } |
185 | |
186 | cleanUpOne: |
187 | |
188 | /* Remove the empty directory. |
189 | */ |
190 | szCreatedDirW = convert((LPSTR)szDirectory); |
191 | if (!RemoveDirectoryW(szCreatedDirW)) |
192 | { |
193 | free (szCreatedDirW); |
194 | Fail("ERROR:%u: Unable to remove directory \"%s\".\n" , |
195 | GetLastError(), |
196 | szCreatedDir); |
197 | } |
198 | free (szCreatedDirW); |
199 | |
200 | /* Terminate the PAL.*/ |
201 | PAL_Terminate(); |
202 | return bRetVal; |
203 | } |
204 | |