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: GetFileSizeEx.c (test 1)
8**
9** Purpose: Tests the PAL implementation of the GetFileSizeEx function.
10**
11**
12**===================================================================*/
13
14#include <palsuite.h>
15
16const char* szTextFile = "text.txt";
17
18void CleanUp(HANDLE hFile)
19{
20 if (CloseHandle(hFile) != TRUE)
21 {
22 Fail("GetFileSizeEx: ERROR -> Unable to close file \"%s\".\n"
23 " Error is %d\n",
24 szTextFile, GetLastError());
25 }
26 if (!DeleteFileA(szTextFile))
27 {
28 Fail("GetFileSizeEx: ERROR -> Unable to delete file \"%s\".\n"
29 " Error is %d\n",
30 szTextFile, GetLastError());
31 }
32}
33
34void CheckFileSize(HANDLE hFile, DWORD dwOffset, DWORD dwHighOrder)
35{
36 DWORD dwRc = 0;
37 DWORD dwError = 0;
38 LARGE_INTEGER qwFileSize;
39
40 dwRc = SetFilePointer(hFile, dwOffset, (PLONG)&dwHighOrder, FILE_BEGIN);
41 if (dwRc == INVALID_SET_FILE_POINTER)
42 {
43 Trace("GetFileSizeEx: ERROR -> Call to SetFilePointer failed with %ld.\n",
44 GetLastError());
45 CleanUp(hFile);
46 Fail("");
47 }
48 else
49 {
50 if (!SetEndOfFile(hFile))
51 {
52 dwError = GetLastError();
53 CleanUp(hFile);
54 if (dwError == 112)
55 {
56 Fail("GetFileSizeEx: ERROR -> SetEndOfFile failed due to lack of "
57 "disk space\n");
58 }
59 else
60 {
61 Fail("GetFileSizeEx: ERROR -> SetEndOfFile call failed "
62 "with error %ld\n", dwError);
63 }
64 }
65 else
66 {
67 GetFileSizeEx(hFile, &qwFileSize);
68 if ((qwFileSize.u.LowPart != dwOffset) ||
69 (qwFileSize.u.HighPart != dwHighOrder))
70 {
71 CleanUp(hFile);
72 Fail("GetFileSizeEx: ERROR -> File sizes do not match up.\n");
73 }
74 }
75 }
76}
77
78int __cdecl main(int argc, char *argv[])
79{
80 HANDLE hFile = NULL;
81 BOOL bRc = FALSE;
82 DWORD lpNumberOfBytesWritten;
83 LARGE_INTEGER qwFileSize;
84 LARGE_INTEGER qwFileSize2;
85 char * data = "1234567890";
86
87 qwFileSize.QuadPart = 0;
88 qwFileSize2.QuadPart = 0;
89
90 if (0 != PAL_Initialize(argc,argv))
91 {
92 return FAIL;
93 }
94
95
96 /* test on a null file */
97 bRc = GetFileSizeEx(NULL, &qwFileSize);
98 if (bRc != FALSE)
99 {
100 Fail("GetFileSizeEx: ERROR -> Returned status as TRUE for "
101 "a null handle.\n");
102 }
103
104
105 /* test on an invalid file */
106 bRc = GetFileSizeEx(INVALID_HANDLE_VALUE, &qwFileSize);
107 if (bRc != FALSE)
108 {
109 Fail("GetFileSizeEx: ERROR -> Returned status as TRUE for "
110 "an invalid handle.\n");
111 }
112
113
114 /* create a test file */
115 hFile = CreateFile(szTextFile,
116 GENERIC_READ | GENERIC_WRITE,
117 FILE_SHARE_READ | FILE_SHARE_WRITE,
118 NULL,
119 CREATE_ALWAYS,
120 FILE_ATTRIBUTE_NORMAL,
121 NULL);
122
123 if(hFile == INVALID_HANDLE_VALUE)
124 {
125 Fail("GetFileSizeEx: ERROR -> Unable to create file \"%s\".\n",
126 szTextFile);
127 }
128
129 /* give the file a size */
130 CheckFileSize(hFile, 256, 0);
131
132 /* make the file large using the high order option */
133 CheckFileSize(hFile, 256, 1);
134
135
136 /* set the file size to zero */
137 CheckFileSize(hFile, 0, 0);
138
139 /* test if file size changes by writing to it. */
140 /* get file size */
141 GetFileSizeEx(hFile, &qwFileSize);
142
143 /* test writing to the file */
144 if(WriteFile(hFile, data, strlen(data), &lpNumberOfBytesWritten, NULL)==0)
145 {
146 Trace("GetFileSizeEx: ERROR -> Call to WriteFile failed with %ld.\n",
147 GetLastError());
148 CleanUp(hFile);
149 Fail("");
150 }
151
152 /* make sure the buffer flushed.*/
153 if(FlushFileBuffers(hFile)==0)
154 {
155 Trace("GetFileSizeEx: ERROR -> Call to FlushFileBuffers failed with %ld.\n",
156 GetLastError());
157 CleanUp(hFile);
158 Fail("");
159 }
160
161 /* get file size after writing some chars */
162 GetFileSizeEx(hFile, &qwFileSize2);
163 if((qwFileSize2.QuadPart-qwFileSize.QuadPart) !=strlen(data))
164 {
165 CleanUp(hFile);
166 Fail("GetFileSizeEx: ERROR -> File size did not increase properly after.\n"
167 "writing %d chars\n", strlen(data));
168 }
169
170 CleanUp(hFile);
171 PAL_Terminate();
172 return PASS;
173}
174