summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/file_io/FlushFileBuffers/test1/FlushFileBuffers.c
blob: 246be64847fde2a164ebb24d6f1f29b672a9a8e0 (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
// 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:  FlushFileBuffers.c
**
** Purpose: Tests the PAL implementation of the FlushFileBuffers function
** This tests checks the return values of FlushFileBuffers -- once on an
** open handle and once on a closed handle.  
**  
** Depends:
**       CreateFile
**       WriteFile
**       CloseHandle
**       DeleteFileA
**
**
**===================================================================*/

#include <palsuite.h>


int __cdecl main(int argc, char **argv)
{
    
    int TheReturn;
    HANDLE TheFileHandle;
    DWORD temp;
    DWORD originalSize=10000;
    DWORD finalSize=10000;
    const char* fileName="the_file";

    /*                          1         2         3         4*/
    char * SomeText = "1234567890123456789012345678901234567890";
    

    if (0 != PAL_Initialize(argc,argv))
    {
        return FAIL;
    }
  
    
    /* Open the file to get a HANDLE */    
    TheFileHandle = 
        CreateFile(
            fileName,                 
            GENERIC_READ|GENERIC_WRITE,  
            FILE_SHARE_READ,                           
            NULL,                        
            OPEN_ALWAYS,                 
            FILE_ATTRIBUTE_NORMAL,       
            NULL);                       

    if(TheFileHandle == INVALID_HANDLE_VALUE) 
    {
        Fail("ERROR: CreateFile failed.  Test depends on this function.");
    }
  
    /* get the file size */
    originalSize = GetFileSize (TheFileHandle, NULL) ; 
    if(originalSize == INVALID_FILE_SIZE)
    {
         Fail("ERROR: call to GetFileSize faild with error "
             "The GetLastError is %d.",GetLastError());
    }
    
    /* Write something too the HANDLE.  Should be buffered */    
    TheReturn = WriteFile(TheFileHandle,     
                          SomeText,        
                           strlen(SomeText),
                          &temp,  
                          NULL);
    
    if(TheReturn == 0) 
    {
        Fail("ERROR: WriteFile failed.  Test depends on this function.");
    }

    /* Test to see that FlushFileBuffers returns a success value */
    TheReturn = FlushFileBuffers(TheFileHandle);
    if(TheReturn == 0) 
    {
        Fail("ERROR: The FlushFileBuffers function returned 0, which "
               "indicates failure, when trying to flush a valid HANDLE.  "
               "The GetLastError is %d.",GetLastError());
    }
  
    /* test if flush modified the file */
    finalSize = GetFileSize (TheFileHandle, NULL) ; 
    if(finalSize==INVALID_FILE_SIZE)
    {
        Fail("ERROR: call to GetFileSize faild with error "
             "The GetLastError is %d.",GetLastError());
    }
    if(finalSize!=(originalSize+strlen(SomeText)))
    {
        Fail("ERROR: FlushFileBuffers failed. data was not written to the file");
    }
  
  
    /* Close the Handle */
    TheReturn = CloseHandle(TheFileHandle);
    if(TheReturn == 0) 
    {
        Fail("ERROR: CloseHandle failed.  This function depends "
               "upon it.");
    }
    

    /* Test to see that FlushFileBuffers returns a failure value */
    TheReturn = FlushFileBuffers(TheFileHandle);
    if(TheReturn != 0) 
    {
        Fail("ERROR: The FlushFileBuffers function returned non-zero, "
               "which indicates success, when trying to flush an invalid "
               "HANDLE.");
    }  

    /* make sure file does not exist */
    if(DeleteFileA(fileName)== 0 )
    {
         Fail("ERROR: call to DeleteFileA faild with error "
             "The GetLastError is %d.",GetLastError());
    }
    PAL_Terminate();
    return PASS;
}