1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=====================================================================
6**
7** Source: getfileattributesexw.c (getfileattributesexw\test2)
8**
9** Purpose: Tests the PAL implementation of GetFileAttributesExW.
10** First get a file's attributes, modify the file,
11** re-get its attributes
12** and compare the two sets of attributes.
13**
14**
15**===================================================================*/
16#include <palsuite.h>
17
18/**
19 * This is a helper function which takes two FILETIME structures and
20 * checks that the second time isn't before the first.
21 */
22static int IsFileTimeOk(FILETIME FirstTime, FILETIME SecondTime)
23{
24
25 ULONG64 TimeOne, TimeTwo;
26
27 TimeOne = ((((ULONG64)FirstTime.dwHighDateTime)<<32) |
28 ((ULONG64)FirstTime.dwLowDateTime));
29
30 TimeTwo = ((((ULONG64)SecondTime.dwHighDateTime)<<32) |
31 ((ULONG64)SecondTime.dwLowDateTime));
32
33 return(TimeOne <= TimeTwo);
34}
35
36int __cdecl main(int argc, char **argv)
37{
38 DWORD res;
39 char fileName[MAX_PATH] = "test_file";
40 WCHAR *wFileName;
41 WIN32_FILE_ATTRIBUTE_DATA beforeAttribs;
42 WIN32_FILE_ATTRIBUTE_DATA afterAttribs;
43 FILE *testFile;
44 ULONG64 beforeFileSize;
45 ULONG64 afterFileSize;
46
47 if (0 != PAL_Initialize(argc,argv))
48 {
49 return FAIL;
50 }
51
52 /* Create the file */
53 testFile = fopen(fileName, "w+");
54 if( NULL == testFile )
55 {
56 Fail("Unexpected error: Unable to open file %S "
57 "with fopen. \n",
58 fileName);
59 }
60
61 if( EOF == fputs( "testing", testFile ) )
62 {
63 Fail("Unexpected error: Unable to write to file %S "
64 "with fputs. \n",
65 fileName);
66 }
67
68 if( 0 != fclose(testFile) )
69 {
70 Fail("Unexpected error: Unable to close file %S "
71 "with fclose. \n",
72 fileName);
73 }
74
75 /* Test the Values returned by GetFileAttributesExW
76 * before and after manipulating a file shouldn't be the same.
77 */
78
79 wFileName = convert(fileName);
80
81 res = GetFileAttributesExW(wFileName,
82 GetFileExInfoStandard,
83 &beforeAttribs);
84
85 if(res == 0)
86 {
87 Fail("ERROR: unable to get initial file attributes with "
88 "GetFileAttributesEx that returned 0 with error %d.\n",
89 GetLastError());
90 }
91
92 /* Make sure the time are different */
93 Sleep(500);
94
95 testFile = fopen(fileName, "w+");
96 if( NULL == testFile )
97 {
98 Fail("Unexpected error: Unable to open file %S "
99 "with fopen. \n",
100 fileName);
101 }
102
103 if( EOF == fputs( "testing GetFileAttributesExW", testFile ) )
104 {
105 Fail("Unexpected error: Unable to write to file %S "
106 "with fputs. \n",
107 fileName);
108 }
109
110 if( 0 != fclose(testFile) )
111 {
112 Fail("Unexpected error: Unable to close file %S "
113 "with fclose. \n",
114 fileName);
115 }
116
117 res = GetFileAttributesExW(wFileName,
118 GetFileExInfoStandard,
119 &afterAttribs);
120
121 if(res == 0)
122 {
123 Fail("ERROR: unable to get file attributes after operations with "
124 "GetFileAttributesEx that returned 0 with error %d.\n",
125 GetLastError());
126 }
127
128 /* Check the creation time */
129 if(!IsFileTimeOk(beforeAttribs.ftCreationTime,
130 afterAttribs.ftCreationTime))
131 {
132 Fail("ERROR: Creation time after the fputs operation "
133 "is earlier than the creation time before the fputs.\n");
134 }
135
136 /* Check the last access time */
137 if(!IsFileTimeOk(beforeAttribs.ftLastAccessTime,
138 afterAttribs.ftLastAccessTime))
139 {
140 Fail("ERROR: Last access time after the fputs operation "
141 "is earlier than the last access time before the fputs.\n");
142 }
143
144 /* Check the last write time */
145 if(!IsFileTimeOk(beforeAttribs.ftLastWriteTime,
146 afterAttribs.ftLastWriteTime))
147 {
148 Fail("ERROR: Last write time after the fputs operation "
149 "is earlier than the last write time before the fputs.\n");
150 }
151
152 beforeFileSize = ((ULONG64)beforeAttribs.nFileSizeHigh)<< 32 |
153 ((ULONG64)beforeAttribs.nFileSizeLow);
154
155 afterFileSize = ((ULONG64)afterAttribs.nFileSizeHigh)<< 32 |
156 ((ULONG64)afterAttribs.nFileSizeLow);
157
158 /* Check the file size */
159 if( afterFileSize <= beforeFileSize )
160 {
161 Fail("ERROR: the file should have had a bigger size "
162 "after(%d) the operations than before(%d)\n",
163 afterAttribs.nFileSizeLow,
164 beforeAttribs.nFileSizeLow);
165 }
166
167
168 PAL_Terminate();
169 return PASS;
170}
171