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: _fullpath/test1/test1.c |
8 | ** |
9 | ** Purpose: Test to see if the _fullpath function returns the |
10 | ** proper values. A check is done to ensure NULL is returned |
11 | ** by _fullpath only for the condition where the length of the |
12 | ** created absolute path name (absPath) is greater than |
13 | ** maxLength. |
14 | ** |
15 | ** Dependencies: strlen |
16 | ** strncmp |
17 | ** SetCurrentDirectory |
18 | ** GetCurrentDirectory |
19 | ** |
20 | |
21 | ** |
22 | **=========================================================*/ |
23 | |
24 | #include <palsuite.h> |
25 | |
26 | struct testcase |
27 | { |
28 | char relPath[50]; /* relative path array */ |
29 | int maxLength; /* pathlength to pass */ |
30 | BOOL bRet; /* TRUE if testcase expects function to return NULL */ |
31 | }; |
32 | |
33 | int __cdecl main( int argc, char **argv ) |
34 | { |
35 | |
36 | DWORD dwOrigDirLength; |
37 | DWORD dwNewDirLength; |
38 | DWORD dwRetStrLength; |
39 | BOOL bRet; |
40 | char *retPath; |
41 | char szAbsPath[_MAX_PATH + 1]; |
42 | char szDirNameOWD[_MAX_DIR]; |
43 | char szDirNameNWD[_MAX_DIR]; |
44 | int i; |
45 | |
46 | struct testcase testcases[]= |
47 | { |
48 | {"." , _MAX_PATH, FALSE}, |
49 | {".." , _MAX_PATH, FALSE}, |
50 | {"..\\.." , _MAX_PATH, FALSE}, |
51 | {"..\\..\\.." , _MAX_PATH, FALSE}, |
52 | {".." , 1, TRUE} |
53 | }; |
54 | |
55 | if(0 != (PAL_Initialize(argc, argv))) |
56 | { |
57 | return ( FAIL ); |
58 | } |
59 | |
60 | for (i = 0; i < sizeof(testcases)/sizeof(struct testcase) ; i++) |
61 | { |
62 | |
63 | /* reset variables */ |
64 | memset(szAbsPath, 0, _MAX_PATH + 1); |
65 | memset(szDirNameOWD, 0, _MAX_DIR); |
66 | memset(szDirNameNWD, 0, _MAX_DIR); |
67 | |
68 | dwOrigDirLength = 0; |
69 | dwNewDirLength = 0; |
70 | dwRetStrLength = 0; |
71 | |
72 | /* Get the current directory name */ |
73 | dwOrigDirLength = GetCurrentDirectory(_MAX_PATH, szDirNameOWD); |
74 | if (0 == dwOrigDirLength) |
75 | { |
76 | Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed." |
77 | "\nGetCurrentDirectory (%d, %s) call failed. GetLastError" |
78 | " returned '%d'\n" , testcases[i].relPath, |
79 | testcases[i].maxLength, _MAX_PATH, szDirNameOWD, |
80 | GetLastError()); |
81 | } |
82 | |
83 | /* |
84 | * Set the current directory to relPath. |
85 | */ |
86 | bRet = SetCurrentDirectory(testcases[i].relPath); |
87 | if (0 == bRet) |
88 | { |
89 | Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed." |
90 | "\nSetCurrentDirectory (%s) call failed. GetLastError" |
91 | " returned '%d'\n" , testcases[i].relPath, |
92 | testcases[i].maxLength, testcases[i].relPath, |
93 | GetLastError()); |
94 | } |
95 | |
96 | /* Get the new current directory name */ |
97 | dwNewDirLength = GetCurrentDirectory(_MAX_PATH, szDirNameNWD); |
98 | if (0 == dwNewDirLength) |
99 | { |
100 | Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed." |
101 | "\nGetCurrentDirectory(%d, %s) call failed. GetLastError" |
102 | " returned '%d'\n" , testcases[i].relPath, |
103 | testcases[i].maxLength, _MAX_PATH, szDirNameNWD, |
104 | GetLastError()); |
105 | } |
106 | |
107 | /* Set the current directory back to the original one */ |
108 | bRet = SetCurrentDirectory(szDirNameOWD); |
109 | if (0 == bRet) |
110 | { |
111 | Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed." |
112 | "\nSetCurrentDirectory(%s) call failed. GetLastError" |
113 | " returned '%d'\n" , testcases[i].relPath, |
114 | testcases[i].maxLength, szDirNameOWD, GetLastError()); |
115 | } |
116 | |
117 | retPath = _fullpath( szAbsPath, |
118 | testcases[i].relPath, |
119 | testcases[i].maxLength ); |
120 | |
121 | if ( NULL == retPath ) |
122 | { |
123 | /* The function returned NULL when a value was expected */ |
124 | if ( FALSE == testcases[i].bRet ) |
125 | { |
126 | Fail("PALSUITE ERROR: test failed.\n" |
127 | "_fullpath (char *, %s, %d) returned NULL\n" |
128 | "when '%s' was expected\n" , testcases[i].relPath, |
129 | testcases[i].maxLength, szDirNameNWD ); |
130 | } |
131 | } |
132 | else |
133 | { |
134 | dwRetStrLength = strlen ( szAbsPath ); |
135 | |
136 | /* Check that the path lengths are identical. */ |
137 | if ( dwRetStrLength != dwNewDirLength ) |
138 | { |
139 | Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed." |
140 | "\ndwRetStringLength '%d' is not equal to " |
141 | "dwNewDirLength '%d'.\nszAbsPath is '%s' retPath is '%s'\n" |
142 | "szDirNameNWD is '%s'\n" , testcases[i].relPath, |
143 | testcases[i].maxLength, dwRetStrLength ,dwNewDirLength |
144 | ,szAbsPath ,retPath ,szDirNameNWD); |
145 | } |
146 | |
147 | /* |
148 | * Perform a string comparison on the path provided by |
149 | * GetCurrentDirectory and the path provided by _fullpath |
150 | * to ensure they are identical. |
151 | */ |
152 | if ( 0 != strncmp( szDirNameNWD, szAbsPath, dwNewDirLength )) |
153 | { |
154 | Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed." |
155 | "strncmp ( %s, %s, %d ) call failed.\n" , |
156 | testcases[i].relPath, testcases[i].maxLength, |
157 | szDirNameNWD, szAbsPath, dwNewDirLength ); |
158 | } |
159 | |
160 | /* |
161 | * Perform a string comparison on both paths provided by |
162 | * _fullpath to ensure they are identical. |
163 | */ |
164 | if ( 0 != strncmp( retPath, szAbsPath, dwNewDirLength )) |
165 | { |
166 | Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed." |
167 | "strncmp ( %s, %s, %d ) call failed.\n" , |
168 | testcases[i].relPath, testcases[i].maxLength, |
169 | szDirNameNWD, szAbsPath, dwNewDirLength ); |
170 | } |
171 | } |
172 | } |
173 | |
174 | PAL_Terminate(); |
175 | return ( PASS ); |
176 | } |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |