summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/_fullpath/test1/test1.cpp
blob: f390f4309bba54bfdfcb43e2420c21f20248c164 (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
// 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: _fullpath/test1/test1.c
**
** Purpose: Test to see if the _fullpath function returns the
** proper values.  A check is done to ensure NULL is returned
** by _fullpath only for the condition where the length of the 
** created absolute path name (absPath) is greater than 
** maxLength.  
**
** Dependencies: strlen
**               strncmp
**               SetCurrentDirectory
**               GetCurrentDirectory
** 

**
**=========================================================*/

#include <palsuite.h>

struct testcase 
{
    char relPath[50]; /* relative path array */
    int maxLength;    /* pathlength to pass */
    BOOL bRet;        /* TRUE if testcase expects function to return NULL */
};

int __cdecl main( int argc, char **argv ) 
{

    DWORD dwOrigDirLength;
    DWORD dwNewDirLength;
    DWORD dwRetStrLength;
    BOOL bRet;
    char *retPath;
    char szAbsPath[_MAX_PATH + 1];
    char szDirNameOWD[_MAX_DIR];
    char szDirNameNWD[_MAX_DIR];
    int i;

    struct testcase testcases[]=
    {
        {"." , _MAX_PATH, FALSE},
        {".." , _MAX_PATH, FALSE},
        {"..\\..", _MAX_PATH, FALSE},
        {"..\\..\\..", _MAX_PATH, FALSE},
        {"..", 1, TRUE}
    };

    if(0 != (PAL_Initialize(argc, argv)))
    {
        return ( FAIL );
    }
     
    for (i = 0; i < sizeof(testcases)/sizeof(struct testcase) ; i++)
    {

        /* reset variables */
        memset(szAbsPath, 0, _MAX_PATH + 1);
        memset(szDirNameOWD, 0, _MAX_DIR);
        memset(szDirNameNWD, 0, _MAX_DIR);

        dwOrigDirLength = 0;
        dwNewDirLength = 0;
        dwRetStrLength = 0;
       
        /* Get the current directory name */
        dwOrigDirLength = GetCurrentDirectory(_MAX_PATH, szDirNameOWD);
        if (0 == dwOrigDirLength) 
        {
            Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed."
                  "\nGetCurrentDirectory (%d, %s) call failed. GetLastError"
                  " returned '%d'\n", testcases[i].relPath, 
                  testcases[i].maxLength, _MAX_PATH, szDirNameOWD, 
                  GetLastError());
        }

        /* 
         * Set the current directory to relPath.
         */
        bRet = SetCurrentDirectory(testcases[i].relPath);
        if (0 == bRet) 
        {
            Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed."
                  "\nSetCurrentDirectory (%s) call failed. GetLastError"
                  " returned '%d'\n", testcases[i].relPath, 
                  testcases[i].maxLength, testcases[i].relPath, 
                  GetLastError());
        }
            
        /* Get the new current directory name */
        dwNewDirLength = GetCurrentDirectory(_MAX_PATH, szDirNameNWD);
        if (0 == dwNewDirLength) 
        {
            Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed."
                  "\nGetCurrentDirectory(%d, %s) call failed. GetLastError"
                   " returned '%d'\n", testcases[i].relPath, 
                  testcases[i].maxLength, _MAX_PATH, szDirNameNWD,
                  GetLastError());
        }
            
        /* Set the current directory back to the original one */
        bRet = SetCurrentDirectory(szDirNameOWD);
        if (0 == bRet) 
        {
            Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed."
                  "\nSetCurrentDirectory(%s) call failed. GetLastError"
                  " returned '%d'\n", testcases[i].relPath, 
                  testcases[i].maxLength, szDirNameOWD, GetLastError());
        }

        retPath = _fullpath( szAbsPath, 
                             testcases[i].relPath, 
                             testcases[i].maxLength );
        
        if ( NULL == retPath )
        {
            /* The function returned NULL when a value was expected */
            if ( FALSE == testcases[i].bRet )
            {
                Fail("PALSUITE ERROR: test failed.\n"
                     "_fullpath (char *, %s, %d) returned NULL\n" 
                     "when '%s' was expected\n", testcases[i].relPath, 
                      testcases[i].maxLength, szDirNameNWD );
            }
        } 
        else
        {
            dwRetStrLength = strlen ( szAbsPath );

            /* Check that the path lengths are identical. */
            if ( dwRetStrLength != dwNewDirLength )
            {
                Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed."
                      "\ndwRetStringLength '%d' is not equal to "
                      "dwNewDirLength '%d'.\nszAbsPath is '%s' retPath is '%s'\n"
                      "szDirNameNWD is '%s'\n" , testcases[i].relPath, 
                      testcases[i].maxLength, dwRetStrLength ,dwNewDirLength
                      ,szAbsPath ,retPath ,szDirNameNWD);
            }

            /* 
             * Perform a string comparison on the path provided by 
             * GetCurrentDirectory and the path provided by _fullpath 
             * to ensure they are identical.
             */
            if ( 0 != strncmp( szDirNameNWD, szAbsPath, dwNewDirLength ))
            {
                Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed."
                      "strncmp ( %s, %s, %d ) call failed.\n", 
                      testcases[i].relPath, testcases[i].maxLength, 
                      szDirNameNWD, szAbsPath, dwNewDirLength );
            }

            /* 
             * Perform a string comparison on both paths provided by 
             * _fullpath to ensure they are identical.
             */
            if ( 0 != strncmp( retPath, szAbsPath, dwNewDirLength ))
            {
                Fail ("PALSUITE ERROR: _fullpath (char *, %s, %d) test failed."
                      "strncmp ( %s, %s, %d ) call failed.\n", 
                      testcases[i].relPath, testcases[i].maxLength, 
                      szDirNameNWD, szAbsPath, dwNewDirLength );
            }
        } 
    }

    PAL_Terminate();
    return ( PASS );
}