summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/GetFileTime/test7/getfiletime.cpp
blob: d33175b8ec5cd6aa8b90f83d2d5df8c8d2f6ecfa (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
// 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:  GetFileTime.c
**
** Purpose: Test the PAL implementation of GetFileTime. This test
**          creates a file and compares create and write times after
**          the buffers are flushed, but before the close, and verifies 
**          the results are as expected
**
** Depends:
**          CreateFile
**          WriteFile
**          FlushFileBuffers
**          CloseHandle
**
**
**===================================================================*/


#include <palsuite.h>


int __cdecl main(int argc, char **argv)
{
    FILETIME Creation;
    FILETIME LastAccess;
    FILETIME LastWrite;
    HANDLE hFile;
    ULONG64 FirstWrite;
    ULONG64 SecondWrite;
    ULONG64 FirstAccess;
    ULONG64 SecondAccess;
    ULONG64 FirstCreationTime;
    ULONG64 SecondCreationTime;
    DWORD temp;
    const char* someText = "1234567890123456789012345678901234567890";

    if (0 != PAL_Initialize(argc,argv))
    {
        return FAIL;
    }
  
    /* Open the file to get a HANDLE */
    hFile = CreateFile("test.tmp",
        GENERIC_READ|GENERIC_WRITE,  
        0,                           
        NULL,                        
        CREATE_ALWAYS,                 
        FILE_ATTRIBUTE_NORMAL,       
        NULL);                       
    
    if(hFile == INVALID_HANDLE_VALUE) 
    {
        Fail("ERROR: Failed to create the file.  The error number "
               "returned was %u.\n",
               GetLastError());
    }

     /* Write to the file -- this should change write access and
       last access 
    */
    if(!WriteFile(hFile, someText, strlen(someText), &temp, NULL)) 
    {
        Trace("ERROR: Failed to write to file. The file must be "
               "written to in order to test that the write time is "
               "updated. GetLastError returned %u.\n", 
                GetLastError());
        /* Close the File */
        if(!CloseHandle(hFile)) 
        {
            Trace("ERROR: Failed to close the file handle. "
                "GetLastError returned %u.\n", 
                GetLastError());
        }
        Fail("");
    }

    /* Flush the buffers */
    if(!FlushFileBuffers(hFile)) 
    {
        Trace("ERROR: The FlushFileBuffers function failed. "
               "GetLastError returned %u.\n",
               GetLastError());
        /* Close the File */
        if(!CloseHandle(hFile)) 
        {
            Trace("ERROR: Failed to close the file handle. "
                "GetLastError returned %u.\n", 
                GetLastError());
        }
        Fail("");
    }

    /* Get the Last Write, Creation and Access File time of that File */
    if(!GetFileTime(hFile, &Creation, &LastAccess, &LastWrite))
    {
        Trace("ERROR: GetFileTime returned 0, indicating failure."
            " GetLastError returned %u\n",
            GetLastError());
        /* Close the File */
        if(!CloseHandle(hFile)) 
        {
            Trace("ERROR: Failed to close the file handle. "
                "GetLastError returned %u.\n", 
                GetLastError());
        }
        Fail("");
    }

    /* Convert the structures to an ULONG64 */
    FirstCreationTime = ((((ULONG64)Creation.dwHighDateTime)<<32) | 
                         ((ULONG64)Creation.dwLowDateTime));
    
    FirstWrite =        ((((ULONG64)LastWrite.dwHighDateTime)<<32) | 
                         ((ULONG64)LastWrite.dwLowDateTime));

    FirstAccess =        ((((ULONG64)LastAccess.dwHighDateTime)<<32) | 
                         ((ULONG64)LastAccess.dwLowDateTime));

    /* Sleep for 3 seconds, this will ensure the time changes */
    Sleep(3000);

    /* Write to the file again so we have something to flush */
    if(!WriteFile(hFile, someText, strlen(someText), &temp, NULL)) 
    {
        Trace("ERROR: Failed to write to file. The file must be "
               "written to in order to test that the write time is "
               "updated. GetLastError returned %u.\n", 
                GetLastError());
        /* Close the File */
        if(!CloseHandle(hFile)) 
        {
            Trace("ERROR: Failed to close the file handle. "
                "GetLastError returned %u.\n", 
                GetLastError());
        }
        Fail("");
    }
  
    /* Flush the buffers forcing the access/mod time to change */
    if(!FlushFileBuffers(hFile)) 
    {
        Trace("ERROR: The FlushFileBuffers function failed. "
               "GetLastError returned %u.\n",
               GetLastError());
        /* Close the File */
        if(!CloseHandle(hFile)) 
        {
            Trace("ERROR: Failed to close the file handle. "
                "GetLastError returned %u.\n", 
                GetLastError());
        }
        Fail("");
    }


    /* Call GetFileTime again */
    if(!GetFileTime(hFile,&Creation,&LastAccess,&LastWrite))
    {
        Trace("ERROR: GetFileTime returned 0, indicating failure."
            "GetLastError returned %u.\n", 
            GetLastError());
        /* Close the File */
        if(!CloseHandle(hFile)) 
        {
            Trace("ERROR: Failed to close the file handle. "
                "GetLastError returned %u.\n", 
                GetLastError());
        }
        Fail("");
    }  
    
    /* Store the results in a ULONG64 */
    
    SecondCreationTime = ( (((ULONG64)Creation.dwHighDateTime)<<32) | 
                           ((ULONG64)Creation.dwLowDateTime));
  
    SecondWrite = ( (((ULONG64)LastWrite.dwHighDateTime)<<32) | 
                    ((ULONG64)LastWrite.dwLowDateTime));
  
    SecondAccess = ((((ULONG64)LastAccess.dwHighDateTime)<<32) | 
                    ((ULONG64)LastAccess.dwLowDateTime));

  
    /* Now -- to test.  We'll ensure that the Second
       LastWrite and access times are larger than the first.
       It tells us that time is passing, which is good! 
    */

    if(FirstWrite >= SecondWrite) 
    {
        /* Close the File */
        if(!CloseHandle(hFile)) 
        {
            Trace("ERROR: Failed to close the file handle. "
                "GetLastError returned %u.\n", 
                GetLastError());
        }
        Fail("ERROR: The write-file-time (%I64d) after the first flush "
            "should be less than the write-file-time (%I64d) after the second "
               "flush.\n",
               FirstWrite, 
               LastWrite);

    }

    
    if(SecondAccess < FirstAccess) 
    {
        /* Close the File */
        if(!CloseHandle(hFile)) 
        {
            Trace("ERROR: Failed to close the file handle. "
                "GetLastError returned %u.\n", 
                GetLastError());
        }
        Fail("ERROR: The access-file-time (%I64d) after the first flush "
            "should be less than or equal to the access-file-time (%I64d) "
               "after the second flush.\n",
               FirstAccess, 
               LastAccess);
    }

#if WIN32
    /* Then we can check to make sure that the creation time
       hasn't changed.  This should always stay the same.
    */
    
    if(FirstCreationTime != SecondCreationTime) 
    {
        /* Close the File */
        if(!CloseHandle(hFile)) 
        {
            Trace("ERROR: Failed to close the file handle. "
                "GetLastError returned %u.\n", 
                GetLastError());
        }
        Fail("ERROR: The creation time after writing should not "
               "not change from the original.  The second value should be "
               "equal.\n");
    }
#else
    /* Then we can check to make sure that the creation time
       has changed.  Under FreeBSD it changes whenever the file is
       access or written.
    */
    
    if(FirstCreationTime >= SecondCreationTime) 
    {
        /* Close the File */
        if(!CloseHandle(hFile)) 
        {
            Trace("ERROR: Failed to close the file handle. "
                "GetLastError returned %u.\n", 
                GetLastError());
        }
        Fail("ERROR: The creation time after writing should be "
               "greater than the original.  The second value should be "
               "larger.\n");
    }
    
#endif
    
    /* Close the File */
    if(!CloseHandle(hFile)) 
    {
        Fail("ERROR: Failed to close the file handle. "
            "GetLastError returned %u.\n", 
            GetLastError());
    }

    PAL_Terminate();
    return PASS;
}