summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/filemapping_memmgt/LockFile/test1/helper.c
blob: 5c64d456ecc23efe451706d1a21678c79bcf4848 (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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. 
//

/*=============================================================
**
** Source: helper.c
**
** Purpose: A child process which will attempt to read and write to files
** which were locked in the parent.
**
**
**============================================================*/

#include <palsuite.h>

#define BUF_SIZE 128

int __cdecl main(int argc, char *argv[])
{
    HANDLE TheFile;
    int result = 0;
    char DataBuffer[BUF_SIZE];
    DWORD BytesRead, BytesWritten;
    char fileName[] = "testfile.tmp";
    
    if(0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }
    
    /* Open the same file that the parent has opened and locked */
    TheFile = CreateFile(fileName,     
                         GENERIC_READ|GENERIC_WRITE, 
                         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.",fileName); 
    }

    /* Attempt to Read 5 bytes from this file.  Since it is locked, this
       should fail.
    */
    
    if(ReadFile(TheFile, DataBuffer, 5, &BytesRead, NULL) != 0)
    {
        Trace("ERROR: ReadFile should have failed!  It was called on "
              "a locked file. But, it returned non-zero indicating success.");
        result = 1;
    }

    /* Attempt to Write 5 bytes to this file.  Since it is locked this should
       fail.
    */

    memset(DataBuffer,'X',BUF_SIZE);

    if(WriteFile(TheFile, DataBuffer, 5,&BytesWritten, NULL) != 0)
    {
        Trace("ERROR: WriteFile should have failed!  It was called on "
              "a locked file. But, it returned non-zero indicating success.");
        result = 1;
    } 
   
    /* Check to ensure that the number of Bytes read/written is still 0, 
       since nothing should have been read or written.
    */
    
    if(BytesRead != 0 || BytesWritten !=0)
    {
        Trace("ERROR: The number of bytes read is %d and written is %d.  "
              "These should both be 0, as the file was locked.",
              BytesRead,BytesWritten);
        result = 1;
    }

    PAL_TerminateEx(result);
    return result;
}