summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/GetCurrentDirectoryW/test1/GetCurrentDirectoryW.c
blob: 4f4697b0a26b519d14743701ef43749241463356 (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
// 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:  GetCurrentDirectoryW.c (test 1)
**
** Purpose: Tests the PAL implementation of the GetCurrentDirectoryW function.
**
**
**===================================================================*/

#include <palsuite.h>


int __cdecl main(int argc, char *argv[])
{
    DWORD dwRc = 0;
    DWORD dwRc2 = 0;
    WCHAR szwReturnedPath[_MAX_PATH+1];
    WCHAR szwCurrentDir[_MAX_PATH+1];
    WCHAR szwFileName[_MAX_PATH] = {'b','l','a','h','\0'};
    LPWSTR pPathPtr;
    size_t nCount = 0;

    if (0 != PAL_Initialize(argc,argv))
    {
        return FAIL;
    }

    /* use GetFullPathName to to get the current path by stripping 
     * the file name off the end */
    memset(szwReturnedPath, 0, sizeof(WCHAR)*(_MAX_PATH+1));
    dwRc = GetFullPathNameW(szwFileName, _MAX_PATH, szwReturnedPath, &pPathPtr);
    if (dwRc == 0)
    {
        /* GetFullPathName failed */
        Fail("GetCurrentDirectoryW: ERROR -> GetFullPathNameW failed "
            "with error code: %ld.\n", GetLastError());
    }
    else if(dwRc >_MAX_PATH)
    {
        Fail("GetCurrentDirectoryW: ERROR -> The path name GetFullPathNameW "
            "returned is longer than _MAX_PATH characters.\n");
    }

    /* strip the file name from the full path to get the current path */
    nCount = wcslen(szwReturnedPath) - wcslen(szwFileName) - 1;
    memset(szwCurrentDir, 0, sizeof(WCHAR)*(_MAX_PATH+1));
    memcpy(szwCurrentDir, szwReturnedPath, nCount*sizeof(WCHAR));

    /* compare the results of GetCurrentDirectoryW with the above */
    memset(szwReturnedPath, 0, sizeof(WCHAR)*(_MAX_PATH+1));
    dwRc = GetCurrentDirectoryW((sizeof(WCHAR)*(_MAX_PATH+1)), szwReturnedPath);
    if (dwRc == 0)
    {
        Fail("GetCurrentDirectoryW: ERROR -> GetCurrentDirectoryW failed "
            "with error code: %ld.\n", GetLastError());
    }
    else if(dwRc >_MAX_PATH)
    {
        Fail("GetCurrentDirectoryW: ERROR -> The path name "
            "returned is longer than _MAX_PATH characters.\n");
    }

    /* check to see whether the length of the returned string is equal to
     * the DWORD returned by GetCurrentDirectoryW.
     */
    if(wcslen(szwReturnedPath) != dwRc)
    {
        Fail("GetCurrentDirectoryW: ERROR -> The Length of the path name "
            "returned \"%u\" is not equal to the return value of the "
            "function \"%u\".\n" , wcslen(szwReturnedPath), dwRc);
    }



    /* test case  the passed buffer size  is not big enough
     * function should return the size required + 1 for a terminating null character
     */

    /* good buffer size */
    dwRc = GetCurrentDirectoryW((sizeof(WCHAR)*(_MAX_PATH+1)), szwReturnedPath);

    /* small buffer (0 size)*/
    dwRc2 = GetCurrentDirectoryW(0, szwReturnedPath);
    if (dwRc2 != (dwRc+1) )
    {
        Fail("GetCurrentDirectoryW: ERROR -> failed to give the correct "
             "return value when passed a buffer not big enough. "
             "Expected %u while result is %u ",(dwRc+1),dwRc2);

    }

    if (wcsncmp(szwReturnedPath, szwCurrentDir, wcslen(szwReturnedPath)) != 0)
    {
        Fail("GetCurrentDirectoryW: ERROR -> The computed and returned "
            "directories do not compare.\n");
    }


    PAL_Terminate();
    return PASS;
}