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

#include <palsuite.h>

const char* szFileName = "testing.tmp";

int __cdecl main(int argc, char *argv[])
{
    DWORD dwRc = 0;
    WCHAR szwReturnedPath[_MAX_DIR+1];
    WCHAR szwShortBuff[2];
    LPWSTR pPathPtr;
    HANDLE hFile = NULL;
    WCHAR* szwFileName = NULL;


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

    szwFileName = convert((char*)szFileName);

    /* perform a short buffer test */
    if (GetFullPathNameW(szwFileName, 2, szwShortBuff, &pPathPtr) <= 2)
    {
        free(szwFileName);
        /* this test should have failed but didn't */
        Fail("GetFullPathNameW: ERROR -> The API was passed a buffer that was"
            " too small for the path name and yet it apparently passed.\n");
    }


    memset(szwReturnedPath, 0, _MAX_DIR+1);
    dwRc = GetFullPathNameW(szwFileName,
        _MAX_DIR,
        szwReturnedPath,
        &pPathPtr);

    if (dwRc == 0)
    {
        /* this test should have passed but didn't */
        free(szwFileName);
        Fail("GetFullPathNameW: ERROR -> Function failed for the "
            "file \"%s\" with error code: %ld.\n", szFileName, GetLastError());
    }
    /*
     * the returned value should be the current directory with the
     * file name appended
     */
    hFile = CreateFileW(szwFileName,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        free(szwFileName);
        Fail("GetFullPathNameW: ERROR -> CreateFileW failed to create "
            "file \"%s\" with error code: %ld.\n",
            szFileName,
            GetLastError());
    }
    if (CloseHandle(hFile) != TRUE)
    {
        free(szwFileName);
        Trace("GetFullPathNameW: ERROR -> CloseHandle failed with error "
            "code: %ld.\n", GetLastError());
        if (DeleteFileA(szFileName) != TRUE)
        {
            Trace("GetFullPathNameW: ERROR -> DeleteFileW failed to "
                "delete the test file with error code: %ld.\n",
                GetLastError());
        }
        PAL_TerminateEx(FAIL);
        return FAIL;
    }

    /*
     * now try to create the file based on the returned value with the
     * CREATE_NEW option which should fail since the file should
     * already exist
     */
    hFile = CreateFileW(szwReturnedPath,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        CREATE_NEW,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
    if (hFile != INVALID_HANDLE_VALUE)
    {
        Trace("GetFullPathNameW: ERROR -> CreateFileW was able to "
            "CREATE_NEW the returned file \"%s\". The returned file "
            "name is therefore apparently wrong.\n",
            szwReturnedPath);
        if (CloseHandle(hFile) != TRUE)
        {
            Trace("GetFullPathNameW: ERROR -> CloseHandle failed with "
                "error code: %ld.\n", GetLastError());
        }
        if ((DeleteFileW(szwReturnedPath) != TRUE) ||
            (DeleteFileW(szwFileName) != TRUE))
        {
            Trace("GetFullPathNameW: ERROR -> DeleteFileW failed to "
                "delete the test files with error code: %ld.\n",
                GetLastError());
        }
        free(szwFileName);
        PAL_TerminateEx(FAIL);
        return FAIL;
    }

    /* now make sure the pPathPtr is the same as the file name */
    if (wcsncmp(pPathPtr, szwFileName, wcslen(szwFileName)) != 0)
    {
        Trace("GetFullPathNameW: ERROR -> %s != %s\n",
            pPathPtr, szFileName);
        if ((DeleteFileW(szwReturnedPath) != TRUE) ||
            (DeleteFileW(szwFileName) != TRUE))
        {
            Trace("GetFullPathNameW: ERROR -> DeleteFileW failed to "
                "delete the test files with error code: %ld.\n",
                GetLastError());
        }
        free(szwFileName);
        PAL_TerminateEx(FAIL);
        return FAIL;
    }

    /* clean up */
    free(szwFileName);
    if (DeleteFileA(szFileName) != TRUE)
    {
        Fail("GetFullPathNameW: ERROR -> DeleteFileW failed to "
            "delete \"%s\" with error code: %ld.\n",
            szFileName,
            GetLastError());
    }

    PAL_Terminate();
    return PASS;
}