summaryrefslogtreecommitdiffstats
path: root/private/nw/test/lock3.c
blob: eff9d0d55cdf8ea34c6b5b1e2a2dab9e9394c2db (plain) (blame)
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
/*++

Copyright (c) 1990  Microsoft Corporation

Module Name:

    tfile.c

Abstract:

    Test program for Win32 Base File API calls

Author:

    Mark Lucovsky (markl) 26-Sep-1990

Revision History:

--*/

#include <windows.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

VOID
TryLock(
    int Type,
    HANDLE Handle,
    DWORD Offset,
    DWORD StartOffset,
    DWORD Range,
    BOOL ExpectedResult
    );

#define  Lock   1
#define  Unlock 2

DWORD
_cdecl
main(
    int argc,
    char *argv[],
    char *envp[]
    )
{

    HANDLE iFile;
    BOOL success;
    STARTUPINFO startupInfo;
    PROCESS_INFORMATION processInfo;
    SECURITY_ATTRIBUTES security;
    char commandLine[80];
    int offset;

    printf( "Number of arguments = %d\n", argc );

    if ( argc == 1 ) {

        security.nLength = sizeof( security );
        security.lpSecurityDescriptor = NULL;
        security.bInheritHandle = TRUE;

        iFile = CreateFile(
                    "testfile.txt",
                    GENERIC_READ,
                    FILE_SHARE_READ,
                    &security,
                    OPEN_EXISTING,
                    0,
                    NULL
                    );

        printf("Created file, handle = %d\n", iFile );

        memset( &startupInfo, 0, sizeof( startupInfo ) );
        sprintf( commandLine, "%s %d\n", argv[0], iFile );

        success = CreateProcess(
                      "D:\\444\\nt\\system32\\lock3.exe",
                      commandLine,
                      NULL,    // Process security
                      NULL,    // Thread security
                      TRUE,    // Inherit Handles
                      0,       // Create flags
                      NULL,    // Environment
                      NULL,    // Current Directory
                      &startupInfo,
                      &processInfo );

        if ( !success ) {
            printf("Create process failed, err = %d\n", GetLastError() );
        }
        offset = 0;
    } else {

        iFile = (HANDLE)atoi( argv[1] );
        printf("Inherited file handle = %d\n", iFile );
        offset = 100;
    }

    assert(iFile != INVALID_HANDLE_VALUE);

    TryLock( Lock, iFile, offset, 0, 10, TRUE );
    TryLock( Lock, iFile, offset, 10, 10, TRUE );
    TryLock( Lock, iFile, offset, 1, 1, FALSE );
    TryLock( Lock, iFile, offset, 0, 11, FALSE );
    TryLock( Lock, iFile, offset, 0, 20, FALSE );
    TryLock( Unlock, iFile, offset, 1, 1, FALSE );
    TryLock( Unlock, iFile, offset, 0, 12, FALSE );
    TryLock( Unlock, iFile, offset, 0, 20, FALSE );
    TryLock( Unlock, iFile, offset, 0, 10, TRUE );
    TryLock( Unlock, iFile, offset, 10, 10, TRUE );
    TryLock( Lock, iFile, offset, 0, 10, TRUE );
    TryLock( Lock, iFile, offset, 10, 10, TRUE );

    Sleep(5 * argc * 1000);

    CloseHandle(iFile);
    printf("Done\n");
    return 1;

}

VOID
TryLock(
    int Type,
    HANDLE Handle,
    DWORD Offset,
    DWORD StartOffset,
    DWORD Range,
    BOOL ExpectedResult
    )
{
    BOOL result;
    DWORD err;

    if ( Type == Lock ) {
        result = LockFile( Handle, StartOffset + Offset, 0, Range, 0 );
    } else {
        result = UnlockFile( Handle, StartOffset + Offset, 0, Range, 0 );
    }

    if ( result == ExpectedResult ) {
        return;
    } else {
        err = GetLastError();
        printf("Expected %d got %d for %slock at %d L%d\n",
                ExpectedResult, result,
                Type == Lock ? "" : "un",
                StartOffset + Offset, Range );
        if ( !result ) {
            printf("Unexpected error = %08lx\n", err );
        }
    }
}