summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/GetFileTime/test3/GetFileTime.c
blob: a3f46c2bf84225823d55f8e52cf9d28d11617d58 (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
// 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: Tests the PAL implementation of the GetFileTime function
** Test to see that creation time is changed when two different files
** are created. 
**
** Depends:
**         CreateFile
**         ReadFile
**         CloseHandle
**
**
**===================================================================*/


#include <palsuite.h>


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

    FILETIME Creation;
    HANDLE TheFileHandle, SecondFileHandle;
    ULONG64 FirstCreationTime, SecondCreationTime;
    BOOL result;
    
    if (0 != PAL_Initialize(argc,argv))
    {
        return FAIL;
    }
    
    /* Open the file to get a HANDLE */
    TheFileHandle = 
        CreateFile(
            "the_file",                 
            GENERIC_READ,  
            0,                           
            NULL,                        
            OPEN_ALWAYS,                 
            FILE_ATTRIBUTE_NORMAL,       
            NULL);                       

	
    if(TheFileHandle == INVALID_HANDLE_VALUE) 
    {
        Fail("ERROR: Failed to open the file.  The error number "
               "returned was %d.",GetLastError());
    }
    

    /* Get the Creation time of the File */
    if(GetFileTime(TheFileHandle,&Creation,NULL,NULL)==0)
    {
        Fail("ERROR: GetFileTime returned 0, indicating failure. "
               "Two of the params were NULL in this case, did they "
               "cause the probleM?");
    }
  
    /* Convert the structure to an ULONG64 */

    FirstCreationTime = ( (((ULONG64)Creation.dwHighDateTime)<<32) | 
                          ((ULONG64)Creation.dwLowDateTime));


    /* Close the File, so the changes are recorded */
    result = CloseHandle(TheFileHandle);
  
    if(result == 0) 
    {
        Fail("ERROR: Failed to close the file handle.");
    }


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


 
    /* Open another file */
    SecondFileHandle = 
        CreateFile("the_other_file",                  /* file name */ 
                   GENERIC_READ,                /* access mode */
                   0,                           /* share mode */
                   NULL,                        /* SD */ 
                   CREATE_ALWAYS,                 /* how to create */
                   FILE_ATTRIBUTE_NORMAL,       /* file attributes */
                   NULL                         /* handle to template file */
            );
    
    if(SecondFileHandle == INVALID_HANDLE_VALUE) 
    {
        Fail("ERROR: Failed to open the second file.  The error number "
               "returned was %d.",GetLastError());
    }

    
    /* Call GetFileTime again  */
    if(GetFileTime(SecondFileHandle,&Creation,NULL,NULL) == 0)
    {
        Fail("ERROR: GetFileTime returned 0, indicating failure. "
               "Perhaps the NULLs in the function broke it?");
    }

    /* Close the File*/
    result = CloseHandle(SecondFileHandle);
    
    if(result == 0) 
    {
        Fail("ERROR: Failed to close the file handle.");
    }


    /* Store the results in a ULONG64 */

    SecondCreationTime = ( (((ULONG64)Creation.dwHighDateTime)<<32) | 
                           ((ULONG64)Creation.dwLowDateTime));
  
  
  
    /* Now -- to test. We ensure that the FirstCreationTime is
       less than the SecondCreationTime
    */

   
    if(FirstCreationTime >= SecondCreationTime) 
    {
        Fail("ERROR: The creation time of the two files should be "
               "different.  The first file should have a creation "
               "time less than the second.");
    }

  

    PAL_Terminate();
    return PASS;
}