summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/test/units/sysutils/tfile2.pp
blob: 1e2e978f52d9ff48b1eb3bfdf736670600cd45b1 (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
{$ifdef fpc}
{$mode objfpc}
{$h+}
{$endif}

uses
  SysUtils;

{$ifndef fpc}
const
  fmsharecompat = cardinal(0);
  fsFromBeginning = cardinal(0);
{$endif}

var
  l,l2: longint;
begin
  try
    try
      l:=filecreate('tfile2.dat');
      if (l<0) then
        raise exception.create('unable to create file');
      fileclose(l);
      l:=fileopen('tfile2.dat',fmopenread);
      if (filewrite(l,l,sizeof(l))>0) then
        raise exception.create('writing to read-only file succeeded');
      fileclose(l);
      deletefile('tfile2.dat');


      l:=filecreate('tfile2.dat');
      if (l<0) then
        raise exception.create('unable to create file (2)');
      fileclose(l);
      l:=fileopen('tfile2.dat',fmopenwrite);
      if (filewrite(l,l,sizeof(l))<>sizeof(l)) then
        raise exception.create('writing to write-only file failed');
      if (fileseek(l,0,fsFromBeginning)<>0) then
        raise exception.create('seeking write-only file failed');
      if (fileread(l,l2,sizeof(l))>=0) then
        raise exception.create('reading from write-only file succeeded');
      fileclose(l);

      l:=fileopen('tfile2.dat',fmopenread or fmShareDenyWrite);
      if (l<0) then
        raise exception.create('unable to open file in read-only mode and fmShareDenyWrite mode');
      l2:=fileopen('tfile2.dat',fmopenread or fmShareDenyWrite);
      if (l2 < 0) then
        raise exception.create('opening two files as read-only with fmShareDenyWrite failed');
      fileclose(l2);
      l2:=fileopen('tfile2.dat',fmopenread or fmShareExclusive);
      if (l2 >= 0) then
        begin
          fileclose(l2);
          raise exception.create('opening file first as read-only with fmShareDenyWrite, and then again as fmopenread with fmShareExclusive succeeded');
        end;
      fileclose(l);


      l:=fileopen('tfile2.dat',fmopenwrite or fmShareExclusive);
      if (l<0) then
        raise exception.create('unable to open file in write-only and fmShareExclusive mode');
      l2:=fileopen('tfile2.dat',fmopenwrite or fmShareExclusive);
      if (l2 >= 0) then
        begin
          fileclose(l2);
          raise exception.create('opening two files as write-only with fmShareExclusive succeeded');
        end;
      l2:=fileopen('tfile2.dat',fmopenwrite or fmShareDenyWrite);
      if (l2 >= 0) then
        begin
          fileclose(l2);
          raise exception.create('opening file first as write-only with fmShareExclusive, and then again as fmopenwrite with fmShareDenyWrite succeeded');
        end;
      fileclose(l);


      l:=fileopen('tfile2.dat',fmopenread or fmShareExclusive);
      if (l<0) then
        raise exception.create('unable to open file in read-only and fmShareExclusive mode');
      l2:=fileopen('tfile2.dat',fmopenread or fmShareExclusive);
      if (l2 >= 0) then
        begin
          fileclose(l2);
          raise exception.create('opening two files as read-only with fmShareExclusive succeeded');
        end;
      l2:=fileopen('tfile2.dat',fmopenread or fmShareDenyWrite);
      if (l2 >= 0) then
        begin
          fileclose(l2);
          raise exception.create('opening file first as read-only with fmShareExclusive, and then again as fmopenread with fmShareDenyWrite succeeded');
        end;
      fileclose(l);


      l:=fileopen('tfile2.dat',fmopenread);
      if (l<0) then
        raise exception.create('unable to open file in read-only mode (2)');
      l2:=fileopen('tfile2.dat',fmopenread);
      if (l2 >= 0) then
        begin
          fileclose(l2);
          raise exception.create('opening two files as read-only without sharing specified succeeded (should not, file is by default locked)');
        end;
      l2:=fileopen('tfile2.dat',fmopenread or fmShareDenyWrite);
      if (l2 >= 0) then
        begin
          fileclose(l2);
          raise exception.create('opening two files as read-only with fmShareDenyWrite succeeded (should not, file is by default locked)');
        end;
      fileclose(l);


      { should be same as no locking specified }
      l:=fileopen('tfile2.dat',fmopenread or fmShareCompat);
      if (l<0) then
        raise exception.create('unable to open file in read-only mode (3)');
      l2:=fileopen('tfile2.dat',fmopenread or fmShareCompat);
      if (l2 >= 0) then
        begin
          fileclose(l2);
          raise exception.create('opening two files as read-only with fmShareCompat succeeded (should be locked)');
        end;
      l2:=fileopen('tfile2.dat',fmopenread or fmShareDenyWrite);
      if (l2 >= 0) then
        begin
          fileclose(l2);
          raise exception.create('opening file first as read-only fmShareCompat (should not have any effect), and then again as fmopenread with fmShareDenyWrite succeeded');
        end;
      fileclose(l);


      l:=fileopen('tfile2.dat',fmopenread or fmShareDenyNone);
      if (l<0) then
        raise exception.create('unable to open file in read-only mode and fmShareDenyNone mode');
      l2:=fileopen('tfile2.dat',fmopenread or fmShareDenyNone);
      if (l2 < 0) then
        raise exception.create('opening two files as read-only with fmShareDenyNone failed');
      fileclose(l2);
      l2:=fileopen('tfile2.dat',fmopenread or fmShareDenyWrite);
      if (l2 < 0) then
        raise exception.create('opening two files as read-only with fmShareDenyNone and then fmShareDenyWrite failed');
      fileclose(l2);
{ on Windows, fmShareExclusive checks whether the file is already open in any way by the current
  or another process. On Unix, that is not the case, and we also cannot check against a
  fmShareDenyNone mode
}
{$ifndef unix}
      l2:=fileopen('tfile2.dat',fmopenread or fmShareExclusive);
      if (l2 >= 0) then
        begin
          fileclose(l2);
          raise exception.create('opening two files as read-only with fmShareDenyNone and then fmShareExclusive succeeded');
        end;
{$endif}
      fileclose(l);

      l:=fileopen('tfile2.dat',fmopenread or fmShareDenyWrite);
      if (l<0) then
        raise exception.create('unable to open file in read-only mode and fmShareDenyWrite mode (2)');
      l2:=fileopen('tfile2.dat',fmopenread or fmShareDenyNone);
      if (l2 < 0) then
        raise exception.create('opening files as read-only with fmShareDenyWrite and then fmShareDenyNone failed');
      fileclose(l2);
      fileclose(l);


      l:=fileopen('tfile2.dat',fmopenwrite or fmShareDenyNone);
      if (l<0) then
        raise exception.create('unable to open file in write-only mode and fmShareDenyNone mode');
      l2:=fileopen('tfile2.dat',fmopenread or fmShareDenyNone);
      if (l2 < 0) then
        raise exception.create('opening two files as read/write-only with fmShareDenyNone failed');
      fileclose(l2);

    except
      on e: exception do
        begin
          writeln(e.message);
          exitcode:=1;
        end;
    end;
  finally
    if (l>=0) then
      fileclose(l);
    deletefile('tfile2.dat');
  end;
end.