summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/filemapping_memmgt/LockFile/test4/test4.c
blob: f5cd359fb5e1a1a58bbf82239725a5089571b970 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// 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: test4.c
**
** Purpose: 
** - Attempt to call LockFile on a file without GENERIC_READ or
** GENERIC_WRITE  (this should fail)
** - Attempt to overlap two locks, this should fail.
**
**
**============================================================*/

#include <palsuite.h>

char fileName[] = "testfile.tmp";

void OverlapTest() 
{
    HANDLE TheFile = NULL;
    DWORD FileStart = 0;
    const char lpBuffer[] = "This is a test file.";
    DWORD bytesWritten;
    BOOL bRc = TRUE;
    
    TheFile = CreateFile(fileName,     
                         GENERIC_READ|GENERIC_WRITE, 
                         FILE_SHARE_READ|FILE_SHARE_WRITE,
                         NULL,                          
                         CREATE_ALWAYS,                 
                         FILE_ATTRIBUTE_NORMAL, 
                         NULL);
    
    if (TheFile == INVALID_HANDLE_VALUE) 
    { 
        Fail("ERROR: Could not open file '%s' with CreateFile. "
             "GetLastError() returned %d.",fileName,GetLastError()); 
    } 

    bRc = WriteFile(TheFile,
                    lpBuffer,
                    (DWORD)sizeof(lpBuffer),
                    &bytesWritten,
                    NULL);

    if(!bRc)
    {      
        Trace("ERROR: Could not write to file '%s' with WriteFile.",fileName);

        if(CloseHandle(TheFile) == 0)
        {
            Fail("ERROR: CloseHandle failed to close the file.");
        }
        Fail("");

    }
    else if(bytesWritten != (DWORD)sizeof(lpBuffer))
    {
        Trace("ERROR: Could not write the correct number of bytes to the "
        "file '%s' with WriteFile.",fileName);

        if(CloseHandle(TheFile) == 0)
        {
            Fail("ERROR: CloseHandle failed to close the file.");
        }
        Fail("");
    }

    /* Lock the First 5 bytes of the File */
    
    if(LockFile(TheFile, FileStart, 0, 5, 0) == 0)
    {
        Trace("ERROR: LockFile failed in Overlap test.  "
             "GetLastError returns %d.",
             GetLastError());

        if(CloseHandle(TheFile) == 0)
        {
            Fail("ERROR: CloseHandle failed to close the file.");
        }
        Fail("");
    }

    /* Lock from Byte 2 until 7 -- this overlaps and should return failure. */
    if(LockFile(TheFile,FileStart+2, 0, 5, 0) != 0)
    {
        Trace("ERROR: LockFile returned success when it was overlapped on "
             "an already locked region of the file.");

        if(CloseHandle(TheFile) == 0)
        {
            Fail("ERROR: CloseHandle failed to close the file.");
    }
        Fail("");
    }

    /* Unlock the file */
    if(UnlockFile(TheFile, FileStart, 0, 5, 0) == 0)
    {
        Trace("ERROR: UnlockFile failed in Overlap test.  GetLastError "
             "returns %d.",GetLastError());

        if(CloseHandle(TheFile) == 0)
        {
            Fail("ERROR: CloseHandle failed to close the file.");
        }
        Fail("");
    }

    /* Close the File */
    if(CloseHandle(TheFile) == 0)
    {
        Fail("ERROR: CloseHandle failed to close the file in the Overlap "
             "test.  GetLastError() returned %d.",GetLastError());
    }
}

void FlagsTest(DWORD TheFlags, int ExpectedResult) 
{    
    HANDLE TheFile = NULL;
    DWORD FileStart = 0;
    int result;

    TheFile = CreateFile(fileName,     
                         TheFlags, 
                         FILE_SHARE_READ|FILE_SHARE_WRITE,
                         NULL,                          
                         OPEN_EXISTING,                 
                         FILE_ATTRIBUTE_NORMAL, 
                         NULL);
    
    if (TheFile == INVALID_HANDLE_VALUE) 
    { 
        Fail("ERROR: Could not open file '%s' with CreateFile. "
             "GetLastError() returned %d.",fileName,GetLastError()); 
    } 

    /* Lock the First 5 bytes of the File.  The result of this depends 
       upon which flags were set with the CreateFile.
    */
    
    result = LockFile(TheFile, FileStart, 0, 5, 0);
    
    /* If the expected result is 1, check to ensure the result is non-zero, 
       as non-zero is returned on success 
    */
    if(ExpectedResult == 1)
    {
        if(result == 0)
        {
            Trace("ERROR: LockFile returned zero when the expected result "
                 "was non-zero.  It was passed the flag value %d.",
                 TheFlags);   

            if(CloseHandle(TheFile) == 0)
            {
                Fail("ERROR: CloseHandle failed to close the file.");
            }
            Fail("");
        }
    }
    /* If the expected result is 0, check to ensure the result is 0 */
    else 
    {
        if(result != 0)
        {
            Trace("ERROR: LockFile returned %d when the expected result "
                 "was zero.  It was passed the flag value %d.",
                 result, TheFlags);

            if(CloseHandle(TheFile) == 0)
            {
                Fail("ERROR: CloseHandle failed to close the file.");
        }
            Fail("");
    }
    }
    
    /* Only unlock the file if we expect it to be successfully locked */
    if(ExpectedResult)
    {
        if(UnlockFile(TheFile,FileStart,0, 5, 0) == 0)
        {
            Fail("ERROR: UnlockFile failed in the Flags Test.  GetLastError() "
                 "returned %d.",GetLastError());

            if(CloseHandle(TheFile) == 0)
            {
                Fail("ERROR: CloseHandle failed to close the file.");
            }
            Fail("");
        }
    }
    
    /* Close the File */
    if(CloseHandle(TheFile) == 0)
    {
        Fail("ERROR: CloseHandle failed to close the file in the Flags "
             "test. GetLastError() returned %d.",GetLastError());
    }
}

int __cdecl main(int argc, char *argv[])
{
 
    if(0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }

    /* This test opens a file, then calls lock twice, overlapping the 
       regions and checking to ensure that this causes an error.
    */
    OverlapTest();
    
    /* Test that LockFile fails if no flags are set */
    FlagsTest(0,0);
    
    /* Test that LockFile passes if only GENERIC_READ is set */
    FlagsTest(GENERIC_READ,1);

    /* Test that LockFile passes if only GENERIC_WRITE is set */
    FlagsTest(GENERIC_WRITE,1);
    
    PAL_Terminate();
    return PASS;
}