summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/GetFileAttributesW/test1/GetFileAttributesW.cpp
blob: 9d00da4bd42dd57b609f8c5e64bc9776d1fb6d01 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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:  GetFileAttributesW.c
**
** Purpose: Tests the PAL implementation of the GetFileAttributesW function by
**          checking the attributes of:
**          - a normal directory and file
**          - a read only directory and file
**          - a read write directory and file
**          - a hidden directory and file 
**          - a read only hidden directory and file
**          - a directory and a file with no attributes
**          - an invalid file name
**
**
**===========================================================================*/
#include <palsuite.h>

const int TYPE_DIR = 0;
const int TYPE_FILE = 1;
/* Structure defining a test case */
typedef struct 
{
    char *name;     /* name of the file/directory */
    DWORD expectedAttribs;  /* expected attributes */
    HANDLE hFile;  /* Handle to the file */
    int isFile;    /* is file (1) or dir (0) */
}TestCaseFile;

typedef struct 
{
    char *name;     /* name of the file/directory */
    DWORD expectedAttribs;  /* expected attributes */
    HANDLE hFile;  /* Handle to the file */
    int isFile;    /* is file (1) or dir (0) */
}TestCaseDir;

DWORD desiredAccessFile = GENERIC_READ | GENERIC_WRITE;
DWORD shareModeFile  =  FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE;
LPSECURITY_ATTRIBUTES lpAttrFile = NULL;
DWORD dwCreationDispFile  = CREATE_NEW;
DWORD dwFlagsAttribFile = FILE_ATTRIBUTE_NORMAL;
HANDLE hTemplateFile  = NULL;

int numFileTests = 6;
TestCaseFile gfaTestsFile[6]; /* GetFileAttributes tests list */

int numDirTests = 6;
TestCaseDir gfaTestsDir[6]; /* GetFileAttributes tests list */

BOOL CleanUpFiles()
{
    DWORD dwAtt;
    int i;
    BOOL result = TRUE;
    for (i = 0; i < numFileTests - 1 ; i++ )
    {
        dwAtt = GetFileAttributesA(gfaTestsFile[i].name);
 
        if( dwAtt != INVALID_FILE_ATTRIBUTES )
        {
            //Trace("Files iteration %d\n", i);
            if(!SetFileAttributesA (gfaTestsFile[i].name, FILE_ATTRIBUTE_NORMAL))
            {
                result = FALSE;
                Trace("ERROR:%d: Error setting attributes [%s][%d]\n", GetLastError(), gfaTestsFile[i].name, FILE_ATTRIBUTE_NORMAL); 
            } 

            if(!DeleteFileA (gfaTestsFile[i].name))
            {
                result = FALSE;
                Trace("ERROR:%d: Error deleting file [%s][%d]\n", GetLastError(), gfaTestsFile[i].name, dwAtt);   
            }
            
        }
    }
//    Trace("Value of result is %d\n", result);
    return result;
}
BOOL SetUpFiles()
{
    int i = 0;
    BOOL result = TRUE;
    for (i = 0; i < numFileTests - 1 ; i++ )
    {
        gfaTestsFile[i].hFile = CreateFile(gfaTestsFile[i].name,
                        desiredAccessFile,
                        shareModeFile,
                        lpAttrFile,
                        dwCreationDispFile,
                        dwFlagsAttribFile,
                        hTemplateFile);

        if( gfaTestsFile[i].hFile == NULL )
        {
            Fail("Error while creating files for iteration %d\n", i);
        }

        if(!SetFileAttributesA (gfaTestsFile[i].name, gfaTestsFile[i].expectedAttribs))
        {
            result = FALSE;
            Trace("ERROR:%d: Error setting attributes [%s][%d]\n", GetLastError(), gfaTestsFile[i].name, gfaTestsFile[i].expectedAttribs); 
        } 
    }

    return result;
}

BOOL CleanUpDirs()
{
    DWORD dwAtt;
    int i;
    BOOL result = TRUE;
    for (i = 0; i < numDirTests - 1; i++ )
    {
        dwAtt = GetFileAttributesA(gfaTestsDir[i].name);
 
        if( dwAtt != INVALID_FILE_ATTRIBUTES )
        {
            
            if(!SetFileAttributesA (gfaTestsDir[i].name, FILE_ATTRIBUTE_DIRECTORY))
            {
                result = FALSE;
                Trace("ERROR:%d: Error setting attributes [%s][%d]\n", GetLastError(), gfaTestsDir[i].name, (FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY)); 
            } 

            if(!RemoveDirectoryA (gfaTestsDir[i].name))
            {
                result = FALSE;
                Trace("ERROR:%d: Error deleting file [%s][%d]\n", GetLastError(), gfaTestsDir[i].name, dwAtt);   
            }
            
        }
    }

    return result;
}

BOOL SetUpDirs()
{
    int i = 0;
    BOOL result = TRUE;
    DWORD ret = 0;
    for (i = 0; i < numDirTests - 1; i++ )
    {
        result = CreateDirectory(gfaTestsDir[i].name,
                         NULL);

        if(!result )
        {
            result = FALSE;
            Fail("Error while creating directory for iteration %d\n", i);
        }

        if(!SetFileAttributesA (gfaTestsDir[i].name, gfaTestsDir[i].expectedAttribs))
        {
            result = FALSE;
            Trace("ERROR:%d: Error setting attributes [%s][%d]\n", GetLastError(), gfaTestsDir[i].name, gfaTestsDir[i].expectedAttribs); 
        } 

        ret = GetFileAttributesA (gfaTestsDir[i].name);
        if(ret != gfaTestsDir[i].expectedAttribs)
        {
            result = FALSE;
            Trace("ERROR: Error setting attributes [%s][%d]\n", gfaTestsDir[i].name, gfaTestsDir[i].expectedAttribs); 
        } 
        // Trace("Setup Dir setting attr [%d], returned [%d]\n", gfaTestsDir[i].expectedAttribs, ret);

    }
//    Trace("Setup dirs returning %d\n", result);
    return result;
}
int __cdecl main(int argc, char **argv)
{
    int i;
    BOOL  bFailed = FALSE;
    DWORD result;

    char * NormalDirectoryName          = "normal_test_directory";
    char * ReadOnlyDirectoryName        = "ro_test_directory";
    char * ReadWriteDirectoryName       = "rw_directory";
    char * HiddenDirectoryName          = ".hidden_directory";
    char * HiddenReadOnlyDirectoryName  = ".hidden_ro_directory";
    char * NoDirectoryName              = "no_directory";

    char * NormalFileName               = "normal_test_file";
    char * ReadOnlyFileName             = "ro_test_file";  
    char * ReadWriteFileName            = "rw_file";
    char * HiddenFileName               = ".hidden_file";
    char * HiddenReadOnlyFileName       = ".hidden_ro_file";
    char * NotReallyAFileName           = "not_really_a_file";

    WCHAR *WStr;
    /* Tests on directory */
    gfaTestsDir[0].name    = NormalDirectoryName;
    gfaTestsDir[0].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY;
    gfaTestsDir[0].isFile  = TYPE_DIR;
    
    gfaTestsDir[1].name    = ReadOnlyDirectoryName;
    gfaTestsDir[1].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY | 
                          FILE_ATTRIBUTE_READONLY;
    gfaTestsDir[1].isFile  = TYPE_DIR;

    gfaTestsDir[2].name    = ReadWriteDirectoryName;
    gfaTestsDir[2].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY;
    gfaTestsDir[2].isFile  = TYPE_DIR;

    gfaTestsDir[3].name    = HiddenDirectoryName;
    gfaTestsDir[3].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY; //| 
                          //FILE_ATTRIBUTE_HIDDEN;
    gfaTestsDir[3].isFile  = TYPE_DIR;
    
    gfaTestsDir[4].name    = HiddenReadOnlyDirectoryName;
    gfaTestsDir[4].expectedAttribs = FILE_ATTRIBUTE_DIRECTORY | 
                          FILE_ATTRIBUTE_READONLY; //|
                          //FILE_ATTRIBUTE_HIDDEN;
    gfaTestsDir[4].isFile  = TYPE_DIR;

    gfaTestsDir[5].name    = NoDirectoryName;
    gfaTestsDir[5].expectedAttribs = INVALID_FILE_ATTRIBUTES;
    gfaTestsDir[5].isFile  = TYPE_DIR;

    /* Tests on file */
    gfaTestsFile[0].name    = NormalFileName;
    gfaTestsFile[0].expectedAttribs = FILE_ATTRIBUTE_NORMAL;
    gfaTestsFile[0].isFile  = TYPE_FILE;


    gfaTestsFile[1].name    = ReadOnlyFileName;
    gfaTestsFile[1].expectedAttribs = FILE_ATTRIBUTE_READONLY;
    gfaTestsFile[1].isFile  = TYPE_FILE;

    gfaTestsFile[2].name    = ReadWriteFileName;
    gfaTestsFile[2].expectedAttribs = FILE_ATTRIBUTE_NORMAL;
    gfaTestsFile[2].isFile  = TYPE_FILE;

    gfaTestsFile[3].name    = HiddenFileName;
    gfaTestsFile[3].expectedAttribs = FILE_ATTRIBUTE_NORMAL; //FILE_ATTRIBUTE_HIDDEN;
    gfaTestsFile[3].isFile  = TYPE_FILE;

    gfaTestsFile[4].name    = HiddenReadOnlyFileName;
    gfaTestsFile[4].expectedAttribs = FILE_ATTRIBUTE_READONLY; //|
                           //FILE_ATTRIBUTE_HIDDEN;
    gfaTestsFile[4].isFile  = TYPE_FILE;


    gfaTestsFile[5].name    = NotReallyAFileName;
    gfaTestsFile[5].expectedAttribs =  INVALID_FILE_ATTRIBUTES;
    gfaTestsFile[5].isFile  = TYPE_FILE;    

    /* Initialize PAL environment */
    if (0 != PAL_Initialize(argc,argv))
    {
        return FAIL;
    }

    if(!CleanUpFiles())
    {
        Fail("GetFileAttributesW: Pre-Clean Up Files Failed\n");
    }

    if(0 == SetUpFiles())
    {
        Fail("GetFileAttributesW: SetUp Files Failed\n");
    }

    if(!CleanUpDirs())
    {
        Fail("GetFileAttributesW: Pre-Clean Up Directories Failed\n");
    }

    if(!SetUpDirs())
    {
        Fail("GetFileAttributesW: SetUp Directories Failed\n");
    }

    /* 
     * Go through all the test cases above,
     * call GetFileAttributesW on the name and
     * make sure the return value is the one expected
     */
    for( i = 0; i < numFileTests; i++ )
    {
        WStr = convert(gfaTestsFile[i].name);
        result = GetFileAttributesW(WStr);

        if( result != gfaTestsFile[i].expectedAttribs )
        {
            bFailed = TRUE;

            Trace("ERROR: GetFileAttributesW Test#%u on %s "
                  "returned %u instead of %u. \n",
                  i,
                  gfaTestsFile[i].name,
                  result,
                  gfaTestsFile[i].expectedAttribs);

        }
        free(WStr);
    }


    for( i = 0; i < numDirTests; i++ )
    {
        WStr = convert(gfaTestsDir[i].name);
        result = GetFileAttributesW(WStr);

        if( result != gfaTestsDir[i].expectedAttribs )
        {
            bFailed = TRUE;

            Trace("ERROR: GetFileAttributesW on Directories Test#%u on %s "
                  "returned %u instead of %u. \n",
                  i,
                  gfaTestsDir[i].name,
                  result,
                  gfaTestsDir[i].expectedAttribs);

        }
        free(WStr);
    }

    if(!CleanUpFiles())
    {
        Fail("GetFileAttributesW: Post-Clean Up Files Failed\n");
    }

    if(!CleanUpDirs())
    {
        Fail("GetFileAttributesW: Post-Clean Up Directories Failed\n");
    }

    /* If any errors, just call Fail() */
    if( bFailed )
    {
        Fail("");
    }

    PAL_Terminate();
    return PASS;
}