summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/GetFileAttributesExW/test2/test2.c
blob: f244a3bf6a55952991874167485fc8fbe87161d1 (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
// 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:  getfileattributesexw.c (getfileattributesexw\test2)
**
** Purpose: Tests the PAL implementation of GetFileAttributesExW.
**          First get a file's attributes, modify the file, 
**          re-get its attributes
**          and compare the two sets of attributes.
**
**
**===================================================================*/
#include <palsuite.h>

/**
 * This is a helper function which takes two FILETIME structures and 
 * checks that the second time isn't before the first.
 */
static int IsFileTimeOk(FILETIME FirstTime, FILETIME SecondTime)
{
    
    ULONG64 TimeOne, TimeTwo;

    TimeOne = ((((ULONG64)FirstTime.dwHighDateTime)<<32) | 
               ((ULONG64)FirstTime.dwLowDateTime));
    
    TimeTwo = ((((ULONG64)SecondTime.dwHighDateTime)<<32) | 
               ((ULONG64)SecondTime.dwLowDateTime));
    
    return(TimeOne <= TimeTwo);
}

int __cdecl main(int argc, char **argv)
{
    DWORD res;
    char fileName[MAX_PATH] = "test_file";
    WCHAR *wFileName;
    WIN32_FILE_ATTRIBUTE_DATA beforeAttribs;
    WIN32_FILE_ATTRIBUTE_DATA afterAttribs;
    FILE *testFile;
    ULONG64 beforeFileSize;
    ULONG64 afterFileSize;

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

    /* Create the file */
    testFile = fopen(fileName, "w+");
    if( NULL == testFile )
    {
        Fail("Unexpected error: Unable to open file %S "
             "with fopen. \n",
             fileName);
    }

    if( EOF == fputs( "testing", testFile ) )
    {
        Fail("Unexpected error: Unable to write to file %S "
             "with fputs. \n",
             fileName);
    }

    if( 0 != fclose(testFile) )
    {
        Fail("Unexpected error: Unable to close file %S "
             "with fclose. \n",
             fileName);
    }

    /* Test the Values returned by GetFileAttributesExW 
     * before and after manipulating a file shouldn't be the same.
     */

    wFileName = convert(fileName);

    res = GetFileAttributesExW(wFileName,
                               GetFileExInfoStandard,
                               &beforeAttribs);
    
    if(res == 0)
    {
        Fail("ERROR: unable to get initial file attributes with "
             "GetFileAttributesEx that returned 0 with error %d.\n",
             GetLastError());
    }
    
    /* Make sure the time are different */
    Sleep(500);

    testFile = fopen(fileName, "w+");
    if( NULL == testFile )
    {
        Fail("Unexpected error: Unable to open file %S "
             "with fopen. \n",
             fileName);
    }

    if( EOF == fputs( "testing GetFileAttributesExW", testFile ) )
    {
        Fail("Unexpected error: Unable to write to file %S "
             "with fputs. \n",
             fileName);
    }

    if( 0 != fclose(testFile) )
    {
        Fail("Unexpected error: Unable to close file %S "
             "with fclose. \n",
             fileName);
    }

    res = GetFileAttributesExW(wFileName,
                               GetFileExInfoStandard,
                               &afterAttribs);
    
    if(res == 0)
    {
        Fail("ERROR: unable to get file attributes after operations with "
             "GetFileAttributesEx that returned 0 with error %d.\n",
             GetLastError());
    }

    /* Check the creation time */
    if(!IsFileTimeOk(beforeAttribs.ftCreationTime, 
                        afterAttribs.ftCreationTime))
    {
        Fail("ERROR: Creation time after the fputs operation "
             "is earlier than the creation time before the fputs.\n");
    }

    /* Check the last access time */
    if(!IsFileTimeOk(beforeAttribs.ftLastAccessTime, 
                        afterAttribs.ftLastAccessTime))
    {
        Fail("ERROR: Last access time after the fputs operation "
             "is earlier than the last access time before the fputs.\n");
    }

    /* Check the last write time */
    if(!IsFileTimeOk(beforeAttribs.ftLastWriteTime,
                        afterAttribs.ftLastWriteTime))
    {
        Fail("ERROR: Last write time after the fputs operation "
             "is earlier than the last write time before the fputs.\n");
    }

    beforeFileSize = ((ULONG64)beforeAttribs.nFileSizeHigh)<< 32 | 
                     ((ULONG64)beforeAttribs.nFileSizeLow);

    afterFileSize  = ((ULONG64)afterAttribs.nFileSizeHigh)<< 32 | 
                     ((ULONG64)afterAttribs.nFileSizeLow);
    
    /* Check the file size */
    if( afterFileSize <= beforeFileSize )
    {
        Fail("ERROR: the file should have had a bigger size "
             "after(%d) the operations than before(%d)\n",
             afterAttribs.nFileSizeLow,
             beforeAttribs.nFileSizeLow);
    }


    PAL_Terminate();
    return PASS;
}