summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/GetLongPathNameW/test2/getlongpathnamew.cpp
blob: 3b033e9a645b1fcb9435668c242588ec164c9e89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=====================================================================
**
** Source:  GetLongPathNameW.c FreeBSD version(test 2)
**
** Purpose: Tests the PAL implementation of the GetLongPathNameW function
**          as expected under FreeBSD
**
** Depends on:
**      CreateDirectoryA
**      RemoveDirectoryW
**
**
**===================================================================*/
/*
tests:
    - test invalid path names
    - test an already short path name
    - test a long path name
    - test with buffer size too small
*/

#include <palsuite.h>


int __cdecl main(int argc, char *argv[])
{
/* Since GetLongPathNameW operates differently under FreeBSD and Win32 this test
   is for freeBSD only. It runs the same tests as the Win32 version but checks for
   different results
*/
#if !(WIN32) /* Only execute if the is Free BSD */

    DWORD dwRc = 0;
    WCHAR szwReturnedPath[MAX_LONGPATH];
    WCHAR szwSmallBuff[3];
    const char szShortPathName[] = {"testing"};
    const char szLongPathName[] = {"This_is_a_long_directory_name"};
    /* since BSD doesn't shorten long dir names, it will only use the long name */
    const char szShortenedPathName[] = {"This_is_a_long_directory_name"};
    WCHAR* wLongPathPtr = NULL;
    WCHAR* wShortPathPtr = NULL;
    int StringLen        = 0;



    if (0 != PAL_Initialize(argc,argv))
    {
        return FAIL;
    }
    memset(szwReturnedPath, 0, MAX_LONGPATH*sizeof(WCHAR));
    memset(szwSmallBuff, 0, 3*sizeof(WCHAR));
    wLongPathPtr = convert((char*)szLongPathName);
    wShortPathPtr = convert((char*)szShortenedPathName);
   

    /* do some clean up just to be safe */
    RemoveDirectoryW(wLongPathPtr);
    RemoveDirectoryW(wShortPathPtr);


    /* attempt call on an invalid short path name */
    dwRc = GetLongPathNameW(wShortPathPtr, szwReturnedPath, MAX_LONGPATH);
    if (dwRc != 0)
    {
        Trace("GetLongPathNameW: ERROR -> Call made with an invalid short "
            "path \"%S\" returned \"%S\"\n",
            wShortPathPtr,
            szwReturnedPath);
        free (wLongPathPtr);
        free (wShortPathPtr);
        Fail("");
    }


    /* attempt call on an invalid long path name */
    dwRc = GetLongPathNameW(wLongPathPtr, szwReturnedPath, MAX_LONGPATH);
    if (dwRc != 0)
    {
        Trace("GetLongPathNameW: ERROR -> Call made with an invalid long "
            "path \"%S\" returned \"%S\"\n",
			wLongPathPtr,
            szwReturnedPath);
        free (wLongPathPtr);
        free (wShortPathPtr);
        Fail("");
    }


    /* create a long directory name */
    if (TRUE != CreateDirectoryW(wLongPathPtr, NULL))
    {
        free(wLongPathPtr);
        free(wShortPathPtr);
        Fail("GetLongPathNameW: ERROR -> CreateDirectoryW failed with an error"
            " code of %ld when asked to create a directory called \"%s\".\n",
            GetLastError(),
            szLongPathName);
    }


    /* get the long path name */
    memset(szwReturnedPath, 0, MAX_LONGPATH*sizeof(WCHAR));
    dwRc = GetLongPathNameW(wShortPathPtr, szwReturnedPath, MAX_LONGPATH);
    StringLen = wcslen(wShortPathPtr);

   if (dwRc !=  StringLen)
    {
        Trace("GetLongPathNameW: ERROR -> Under FreeBSD, this test should"
            " have returned %d but instead returned %d.\n",
            StringLen, dwRc);
        if (RemoveDirectoryW(wLongPathPtr) != TRUE)
        {
            Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to "
                " remove the directory \"%S\" with an error code of %ld.\n",
                wLongPathPtr,
                GetLastError());
        }
        free(wLongPathPtr);
        free(wShortPathPtr);
        Fail("");
    }
    if (wcsncmp(wShortPathPtr,szwReturnedPath, StringLen) != 0)
    {
      Trace("GetLongPathNameW: ERROR -> Under Unix,"
	    "the lpszLongPath \"%s\" should have been,"
	    "but was \"%s\".\n",
	    wShortPathPtr, szwReturnedPath);

        if (RemoveDirectoryW(wLongPathPtr) != TRUE)
        {
            Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to "
                " remove the directory \"%S\" with an error code of %ld.\n",
                wLongPathPtr,
                GetLastError());
        }
        free(wLongPathPtr);
        free(wShortPathPtr);
        Fail("");
    }

    if (RemoveDirectoryW(wLongPathPtr) != TRUE)
    {
        Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to "
            " remove the directory \"%S\" with an error code of %ld.\n",
            wLongPathPtr,
            GetLastError());
        free(wLongPathPtr);
        free(wShortPathPtr);
        Fail("");
    }
    free(wShortPathPtr);
    free(wLongPathPtr);


    /* test an actual short name */
    /* create a long directory name */
    wShortPathPtr = convert((char*)szShortPathName);
    RemoveDirectoryW(wShortPathPtr);

    if (TRUE != CreateDirectoryW(wShortPathPtr, NULL))
    {
        free(wShortPathPtr);
        Fail("GetLongPathNameW: ERROR -> CreateDirectoryW failed with an error"
            " code of %ld when asked to create a directory called \"%s\".\n",
            GetLastError(),
            szShortPathName);
    }


    /* get the long path name */
    memset(szwReturnedPath, 0, MAX_LONGPATH*sizeof(WCHAR));
    dwRc = GetLongPathNameW(wShortPathPtr, szwReturnedPath, MAX_LONGPATH);
    StringLen = wcslen (wShortPathPtr);

    if (dwRc != StringLen)
    {
        Trace("GetLongPathNameW: ERROR -> Under FreeBSD, this test should"
            " have returned %d but instead returned %d.\n",
            StringLen, dwRc);
        if (RemoveDirectoryW(wShortPathPtr) != TRUE)
        {
            Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to "
                " remove the directory \"%S\" with an error code of %ld.\n",
                wShortPathPtr,
                GetLastError());
        }
        free(wShortPathPtr);
        Fail("");
    }
    if (wcsncmp(wShortPathPtr, szwReturnedPath, StringLen) != 0)
    {
       Trace("GetLongPathNameW: ERROR -> Under Unix, the lpszLongPath"
	    "\"%s\" should have been,"
	    "but was \"%s\".\n",
	    wShortPathPtr, szwReturnedPath);     
      
        if (RemoveDirectoryW(wShortPathPtr) != TRUE)
        {
            Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to "
                " remove the directory \"%S\" with an error code of %ld.\n",
                wShortPathPtr,
                GetLastError());
        }
        free(wShortPathPtr);
        Fail("");
    }


    /* test using a too small return buffer */
    dwRc = GetLongPathNameW(wShortPathPtr, szwSmallBuff, 3);
    StringLen = wcslen (wShortPathPtr);


    if (dwRc != (StringLen + 1)) //Return size includes NULL char
    {
        Trace("GetLongPathNameW: ERROR -> Under FreeBSD, this test should"
            " have returned %d but instead returned %d.\n",
            StringLen, dwRc);
        if (RemoveDirectoryW(wShortPathPtr) != TRUE)
        {
            Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to "
                " remove the directory \"%S\" with an error code of %ld.\n",
                wShortPathPtr,
                GetLastError());
        }
        free(wShortPathPtr);
        Fail("");
    }
    if (szwSmallBuff[0] != 0)
    {
        Trace("GetLongPathNameW: ERROR -> Under FreeBSD, this test should"
            " not have touched lpszLongPath but instead it returned\"%s\".\n",
            szwReturnedPath);
        if (RemoveDirectoryW(wShortPathPtr) != TRUE)
        {
            Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to "
                " remove the directory \"%S\" with an error code of %ld.\n",
                wShortPathPtr,
                GetLastError());
        }
        free(wShortPathPtr);
        Fail("");
    }

    /* clean up */
    if (RemoveDirectoryW(wShortPathPtr) != TRUE)
    {
        Trace("GetLongPathNameW: ERROR -> RemoveDirectoryW failed to "
            " remove the directory \"%S\" with an error code of %ld.\n",
            wShortPathPtr,
            GetLastError());
        free(wShortPathPtr);
        Fail("");
    }
    free(wShortPathPtr);

    PAL_Terminate();

#endif /* Free BSD */
    return PASS;
}