summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/filemapping_memmgt/LockFile/test6/test6.c
blob: ba01b9710ad917efc7cb2813e8428f5ecfc9e396 (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
// 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: test6.c
**
** Purpose: 
** Append to a file which is locked until the end of the file, and
** append to a file which is locked past the end of the file.  (The first
** should succeed, while the second should fail)
**
**
**============================================================*/

#include <palsuite.h>
#include "../LockFile.h"

#define HELPER "helper"
#define FILENAME "testfile.txt"

/* This test checks that you can append to a file which is locked from Start
   to EOF.
*/
void Test1()
{
    HANDLE TheFile = NULL;
    DWORD FileStart = 0;
    DWORD FileEnd = 0;
    int result;
    char* WriteBuffer = "12345678901234567890123456"; 
      
    /* Call the helper function to Create a file, write 'WriteBuffer' to
       the file, and lock the file.
    */
    
    FileEnd = strlen(WriteBuffer);
    TheFile = CreateAndLockFile(TheFile,FILENAME, WriteBuffer, 
                                FileStart, FileEnd);
    
    
    /*
      Launch another proccess which will attempt to append to the 
      end of the file.  Note: This returns -1 if the setup failed in some way.
    */

    result = RunHelper(HELPER);

    if(result == -1)
    {
        Fail("ERROR: The Helper program failed in setting up the "
             "test, so it could never be run.");
    }
    else if(result == 0)
    {
        Fail("ERROR: Failed to append to the file which was Locked from "
             "start until EOF.  Should have been able to append to this "
             "file still.  GetLastError() is %d.",GetLastError());
    }

    if(UnlockFile(TheFile, FileStart, 0, FileEnd, 0) == 0)
    {
        Fail("ERROR: UnlockFile failed.  GetLastError returns %d.",
             GetLastError());
    }
    
    if(CloseHandle(TheFile) == 0)
    {
        Fail("ERROR: CloseHandle failed to close the file. "
             "GetLastError() returned %d.",GetLastError());
    }

}

/* This test checks that you can't append to a file which is locked beyond 
   EOF.
*/
void Test2()
{
    HANDLE TheFile = NULL;
    DWORD FileStart = 0;
    DWORD FileEnd = 0;
    int result;
    char* WriteBuffer = "12345678901234567890123456"; 
    
    /* Call the helper function to Create a file, write 'WriteBuffer' to
       the file, and lock the file.
    */
    
    FileEnd = strlen(WriteBuffer);
    TheFile = CreateAndLockFile(TheFile,FILENAME, WriteBuffer, 
                                FileStart, FileEnd+20);
    
    
    /*
      Launch another proccess which will attempt to append to the 
      end of the file.
    */

    result = RunHelper(HELPER);

    if(result == -1)
    {
        Fail("ERROR: The Helper program failed in setting up the "
             "test, so it could never be run.");
    }
    else if(result > 0)
    {
        Fail("ERROR: The Helper program successfully appended to the "
             "end of the file, even though it was locked beyond EOF.  This "
             "should have failed.");
    }

    if(UnlockFile(TheFile, FileStart, 0, FileEnd+20, 0) == 0)
    {
        Fail("ERROR: UnlockFile failed.  GetLastError returns %d.",
             GetLastError());
    }
    
    if(CloseHandle(TheFile) == 0)
    {
        Fail("ERROR: CloseHandle failed to close the file. "
             "GetLastError() returned %d.",GetLastError());
    }

}


int __cdecl main(int argc, char *argv[])
{
    
    if(0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }
    
    /* Test a file which is locked until EOF to see if you can append */
    Test1();

    /* Test a file which is locked past EOF to ensure you can't append */
    Test2();
    
    PAL_Terminate();
    return PASS;
}