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 GetFullPathNameA API. |
10 | ** GetFullPathA will be passed a directory that contains '..'. |
11 | ** To add to this test, we will also call SetCurrentDirectory to |
12 | ** ensure this is handled properly. |
13 | ** The test will create a file with in the parent directory |
14 | ** to verify that the returned directory is valid. |
15 | ** |
16 | ** |
17 | **===================================================================*/ |
18 | |
19 | #include <palsuite.h> |
20 | |
21 | const char* szDotDot = "..\\" ; |
22 | const char* szFileName = "testing.tmp" ; |
23 | |
24 | int __cdecl main(int argc, char *argv[]) |
25 | { |
26 | DWORD dwRc = 0; |
27 | char szReturnedPath[_MAX_DIR+1]; |
28 | char szFullFileName[_MAX_DIR+1]; |
29 | LPSTR pPathPtr; |
30 | HANDLE hFile = NULL; |
31 | |
32 | /* Initialize the PAL. |
33 | */ |
34 | if (0 != PAL_Initialize(argc,argv)) |
35 | { |
36 | return (FAIL); |
37 | } |
38 | |
39 | /* change the directory */ |
40 | if (!SetCurrentDirectoryA(szDotDot)) |
41 | { |
42 | Fail("ERROR: SetCurrentDirectoryA failed with error code %u" |
43 | " when passed \"%s\".\n" , |
44 | GetLastError(), |
45 | szDotDot); |
46 | } |
47 | |
48 | /* Initialize the receiving char buffers. |
49 | */ |
50 | memset(szReturnedPath, 0, _MAX_DIR+1); |
51 | memset(szFullFileName, 0, _MAX_DIR+1); |
52 | |
53 | /* Create Full filename to pass, will include '..\' |
54 | * as a pre-fix. */ |
55 | strcat(szFullFileName, szDotDot); |
56 | strcat(szFullFileName, szFileName); |
57 | |
58 | /* Get the full path to the filename. |
59 | */ |
60 | dwRc = GetFullPathNameA(szFullFileName, |
61 | _MAX_DIR, |
62 | szReturnedPath, |
63 | &pPathPtr); |
64 | if (dwRc == 0) |
65 | { |
66 | Fail("ERROR :%ld: GetFullPathName failed to " |
67 | "retrieve the path of \"%s\".\n" , |
68 | GetLastError(), |
69 | szFileName); |
70 | } |
71 | |
72 | /* The returned value should be the parent directory with the |
73 | * file name appended. */ |
74 | hFile = CreateFileA(szReturnedPath, |
75 | GENERIC_READ, |
76 | FILE_SHARE_READ, |
77 | NULL, |
78 | CREATE_ALWAYS, |
79 | FILE_ATTRIBUTE_NORMAL, |
80 | NULL); |
81 | |
82 | if (hFile == INVALID_HANDLE_VALUE) |
83 | { |
84 | Fail("ERROR :%ld: CreateFileA failed to create \"%s\".\n" , |
85 | GetLastError(), |
86 | szReturnedPath); |
87 | } |
88 | |
89 | /* Close the handle to the created file. |
90 | */ |
91 | if (CloseHandle(hFile) != TRUE) |
92 | { |
93 | Trace("ERROR :%ld: CloseHandle failed close hFile=0x%lx.\n" , |
94 | GetLastError()); |
95 | goto terminate; |
96 | } |
97 | |
98 | /* Verify that the file was created, attempt to create |
99 | * the file again. */ |
100 | hFile = CreateFileA(szReturnedPath, |
101 | GENERIC_READ, |
102 | FILE_SHARE_READ, |
103 | NULL, |
104 | CREATE_NEW, |
105 | FILE_ATTRIBUTE_NORMAL, |
106 | NULL); |
107 | if ((hFile != INVALID_HANDLE_VALUE) && |
108 | (GetLastError() != ERROR_ALREADY_EXISTS)) |
109 | { |
110 | Fail("ERROR :%ld: CreateFileA succeeded to create file " |
111 | "\"%s\", that already existed.\n" , |
112 | GetLastError(), |
113 | szFullFileName); |
114 | } |
115 | |
116 | |
117 | /* Verify that the returned filename is the same as the supplied. |
118 | */ |
119 | if (strcmp(pPathPtr, szFileName) != 0) |
120 | { |
121 | Trace("ERROR : Returned filename \"%s\" is not equal to " |
122 | "supplied filename \"%s\".\n" , |
123 | pPathPtr, |
124 | szFileName); |
125 | goto terminate; |
126 | } |
127 | |
128 | terminate: |
129 | /* Delete the create file. |
130 | */ |
131 | if (DeleteFileA(szReturnedPath) != TRUE) |
132 | { |
133 | Fail("ERROR :%ld: DeleteFileA failed to delete \"%s\".\n" , |
134 | GetLastError(), |
135 | szFileName); |
136 | } |
137 | |
138 | /* Terminate the PAL.*/ |
139 | PAL_Terminate(); |
140 | return PASS; |
141 | } |
142 | |
143 | |
144 | |