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: FlushFileBuffers.c
8**
9** Purpose: Tests the PAL implementation of the FlushFileBuffers function
10** This tests checks the return values of FlushFileBuffers -- once on an
11** open handle and once on a closed handle.
12**
13** Depends:
14** CreateFile
15** WriteFile
16** CloseHandle
17** DeleteFileA
18**
19**
20**===================================================================*/
21
22#include <palsuite.h>
23
24
25int __cdecl main(int argc, char **argv)
26{
27
28 int TheReturn;
29 HANDLE TheFileHandle;
30 DWORD temp;
31 DWORD originalSize=10000;
32 DWORD finalSize=10000;
33 const char* fileName="the_file";
34
35 /* 1 2 3 4*/
36 char * SomeText = "1234567890123456789012345678901234567890";
37
38
39 if (0 != PAL_Initialize(argc,argv))
40 {
41 return FAIL;
42 }
43
44
45 /* Open the file to get a HANDLE */
46 TheFileHandle =
47 CreateFile(
48 fileName,
49 GENERIC_READ|GENERIC_WRITE,
50 FILE_SHARE_READ,
51 NULL,
52 OPEN_ALWAYS,
53 FILE_ATTRIBUTE_NORMAL,
54 NULL);
55
56 if(TheFileHandle == INVALID_HANDLE_VALUE)
57 {
58 Fail("ERROR: CreateFile failed. Test depends on this function.");
59 }
60
61 /* get the file size */
62 originalSize = GetFileSize (TheFileHandle, NULL) ;
63 if(originalSize == INVALID_FILE_SIZE)
64 {
65 Fail("ERROR: call to GetFileSize faild with error "
66 "The GetLastError is %d.",GetLastError());
67 }
68
69 /* Write something too the HANDLE. Should be buffered */
70 TheReturn = WriteFile(TheFileHandle,
71 SomeText,
72 strlen(SomeText),
73 &temp,
74 NULL);
75
76 if(TheReturn == 0)
77 {
78 Fail("ERROR: WriteFile failed. Test depends on this function.");
79 }
80
81 /* Test to see that FlushFileBuffers returns a success value */
82 TheReturn = FlushFileBuffers(TheFileHandle);
83 if(TheReturn == 0)
84 {
85 Fail("ERROR: The FlushFileBuffers function returned 0, which "
86 "indicates failure, when trying to flush a valid HANDLE. "
87 "The GetLastError is %d.",GetLastError());
88 }
89
90 /* test if flush modified the file */
91 finalSize = GetFileSize (TheFileHandle, NULL) ;
92 if(finalSize==INVALID_FILE_SIZE)
93 {
94 Fail("ERROR: call to GetFileSize faild with error "
95 "The GetLastError is %d.",GetLastError());
96 }
97 if(finalSize!=(originalSize+strlen(SomeText)))
98 {
99 Fail("ERROR: FlushFileBuffers failed. data was not written to the file");
100 }
101
102
103 /* Close the Handle */
104 TheReturn = CloseHandle(TheFileHandle);
105 if(TheReturn == 0)
106 {
107 Fail("ERROR: CloseHandle failed. This function depends "
108 "upon it.");
109 }
110
111
112 /* Test to see that FlushFileBuffers returns a failure value */
113 TheReturn = FlushFileBuffers(TheFileHandle);
114 if(TheReturn != 0)
115 {
116 Fail("ERROR: The FlushFileBuffers function returned non-zero, "
117 "which indicates success, when trying to flush an invalid "
118 "HANDLE.");
119 }
120
121 /* make sure file does not exist */
122 if(DeleteFileA(fileName)== 0 )
123 {
124 Fail("ERROR: call to DeleteFileA faild with error "
125 "The GetLastError is %d.",GetLastError());
126 }
127 PAL_Terminate();
128 return PASS;
129}
130
131