summaryrefslogtreecommitdiff
path: root/spec/ruby/core/io/buffer/map_spec.rb
blob: d980eb0ae0451ab0285079e97b1f7127087a77da (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
require_relative '../../../spec_helper'

describe "IO::Buffer.map" do
  before :all do
    @big_file_name = tmp("big_file")
    # Usually 4 kibibytes + 16 bytes
    File.write(@big_file_name, "12345678" * (IO::Buffer::PAGE_SIZE / 8 + 2))
  end

  after :all do
    File.delete(@big_file_name)
  end

  def open_fixture
    File.open("#{__dir__}/../fixtures/read_text.txt", "r+")
  end

  def open_big_file_fixture
    File.open(@big_file_name, "r+")
  end

  after :each do
    @buffer&.free
    @buffer = nil
    @file&.close
    @file = nil
  end

  it "creates a new buffer mapped from a file" do
    @file = open_fixture
    @buffer = IO::Buffer.map(@file)

    @buffer.size.should == 9
    @buffer.get_string.should == "abcâdef\n".b
  end

  it "allows to close the file after creating buffer, retaining mapping" do
    file = open_fixture
    @buffer = IO::Buffer.map(file)
    file.close

    @buffer.get_string.should == "abcâdef\n".b
  end

  it "creates a mapped, external, shared buffer" do
    @file = open_fixture
    @buffer = IO::Buffer.map(@file)

    @buffer.should_not.internal?
    @buffer.should.mapped?
    @buffer.should.external?

    @buffer.should_not.empty?
    @buffer.should_not.null?

    @buffer.should.shared?
    @buffer.should_not.private?
    @buffer.should_not.readonly?

    @buffer.should_not.locked?
    @buffer.should.valid?
  end

  platform_is_not :windows do
    it "is shareable across processes" do
      file_name = tmp("shared_buffer")
      @file = File.open(file_name, "w+")
      @file << "I'm private"
      @file.rewind
      @buffer = IO::Buffer.map(@file)

      IO.popen("-") do |child_pipe|
        if child_pipe
          # Synchronize on child's output.
          child_pipe.readlines.first.chomp.should == @buffer.to_s
          @buffer.get_string.should == "I'm shared!"

          @file.read.should == "I'm shared!"
        else
          @buffer.set_string("I'm shared!")
          puts @buffer
        end
      ensure
        child_pipe&.close
      end
    ensure
      File.unlink(file_name)
    end
  end

  context "with an empty file" do
    ruby_version_is ""..."4.0" do
      it "raises a SystemCallError" do
        @file = File.open("#{__dir__}/../fixtures/empty.txt", "r+")
        -> { IO::Buffer.map(@file) }.should raise_error(SystemCallError)
      end
    end

    ruby_version_is "4.0" do
      it "raises ArgumentError" do
        @file = File.open("#{__dir__}/../fixtures/empty.txt", "r+")
        -> { IO::Buffer.map(@file) }.should raise_error(ArgumentError, "Invalid negative or zero file size!")
      end
    end
  end

  context "with a file opened only for reading" do
    it "raises a SystemCallError if no flags are used" do
      @file = File.open("#{__dir__}/../fixtures/read_text.txt", "r")
      -> { IO::Buffer.map(@file) }.should raise_error(SystemCallError)
    end
  end

  context "with size argument" do
    it "limits the buffer to the specified size in bytes, starting from the start of the file" do
      @file = open_fixture
      @buffer = IO::Buffer.map(@file, 4)

      @buffer.size.should == 4
      @buffer.get_string.should == "abc\xC3".b
    end

    it "maps the whole file if size is nil" do
      @file = open_fixture
      @buffer = IO::Buffer.map(@file, nil)

      @buffer.size.should == 9
    end

    context "if size is 0" do
      ruby_version_is ""..."4.0" do
        platform_is_not :windows do
          it "raises a SystemCallError" do
            @file = open_fixture
            -> { IO::Buffer.map(@file, 0) }.should raise_error(SystemCallError)
          end
        end
      end

      ruby_version_is "4.0" do
        it "raises ArgumentError" do
          @file = open_fixture
          -> { IO::Buffer.map(@file, 0) }.should raise_error(ArgumentError, "Size can't be zero!")
        end
      end
    end

    it "raises TypeError if size is not an Integer or nil" do
      @file = open_fixture
      -> { IO::Buffer.map(@file, "10") }.should raise_error(TypeError, "not an Integer")
      -> { IO::Buffer.map(@file, 10.0) }.should raise_error(TypeError, "not an Integer")
    end

    it "raises ArgumentError if size is negative" do
      @file = open_fixture
      -> { IO::Buffer.map(@file, -1) }.should raise_error(ArgumentError, "Size can't be negative!")
    end

    ruby_version_is ""..."4.0" do
      # May or may not cause a crash on access.
      it "is undefined behavior if size is larger than file size"
    end

    ruby_version_is "4.0" do
      it "raises ArgumentError if size is larger than file size" do
        @file = open_fixture
        -> { IO::Buffer.map(@file, 8192) }.should raise_error(ArgumentError, "Size can't be larger than file size!")
      end
    end
  end

  context "with size and offset arguments" do
    # Neither Windows nor macOS have clear, stable behavior with non-zero offset.
    # https://bugs.ruby-lang.org/issues/21700
    platform_is :linux do
      context "if offset is an allowed value for system call" do
        it "maps the span specified by size starting from the offset" do
          @file = open_big_file_fixture
          @buffer = IO::Buffer.map(@file, 14, IO::Buffer::PAGE_SIZE)

          @buffer.size.should == 14
          @buffer.get_string(0, 14).should == "12345678123456"
        end

        context "if size is nil" do
          ruby_version_is ""..."4.0" do
            it "maps the rest of the file" do
              @file = open_big_file_fixture
              @buffer = IO::Buffer.map(@file, nil, IO::Buffer::PAGE_SIZE)

              @buffer.get_string(0, 1).should == "1"
            end

            it "incorrectly sets buffer's size to file's full size" do
              @file = open_big_file_fixture
              @buffer = IO::Buffer.map(@file, nil, IO::Buffer::PAGE_SIZE)

              @buffer.size.should == @file.size
            end
          end

          ruby_version_is "4.0" do
            it "maps the rest of the file" do
              @file = open_big_file_fixture
              @buffer = IO::Buffer.map(@file, nil, IO::Buffer::PAGE_SIZE)

              @buffer.get_string(0, 1).should == "1"
            end

            it "sets buffer's size to file's remaining size" do
              @file = open_big_file_fixture
              @buffer = IO::Buffer.map(@file, nil, IO::Buffer::PAGE_SIZE)

              @buffer.size.should == (@file.size - IO::Buffer::PAGE_SIZE)
            end
          end
        end
      end
    end

    it "maps the file from the start if offset is 0" do
      @file = open_fixture
      @buffer = IO::Buffer.map(@file, 4, 0)

      @buffer.size.should == 4
      @buffer.get_string.should == "abc\xC3".b
    end

    ruby_version_is ""..."4.0" do
      # May or may not cause a crash on access.
      it "is undefined behavior if offset+size is larger than file size"
    end

    ruby_version_is "4.0" do
      it "raises ArgumentError if offset+size is larger than file size" do
        @file = open_big_file_fixture
        -> { IO::Buffer.map(@file, 17, IO::Buffer::PAGE_SIZE) }.should raise_error(ArgumentError, "Offset too large!")
      ensure
        # Windows requires the file to be closed before deletion.
        @file.close unless @file.closed?
      end
    end

    it "raises TypeError if offset is not convertible to Integer" do
      @file = open_fixture
      -> { IO::Buffer.map(@file, 4, "4096") }.should raise_error(TypeError, /no implicit conversion/)
      -> { IO::Buffer.map(@file, 4, nil) }.should raise_error(TypeError, /no implicit conversion/)
    end

    it "raises a SystemCallError if offset is not an allowed value" do
      @file = open_fixture
      -> { IO::Buffer.map(@file, 4, 3) }.should raise_error(SystemCallError)
    end

    ruby_version_is ""..."4.0" do
      it "raises a SystemCallError if offset is negative" do
        @file = open_fixture
        -> { IO::Buffer.map(@file, 4, -1) }.should raise_error(SystemCallError)
      end
    end

    ruby_version_is "4.0" do
      it "raises ArgumentError if offset is negative" do
        @file = open_fixture
        -> { IO::Buffer.map(@file, 4, -1) }.should raise_error(ArgumentError, "Offset can't be negative!")
      end
    end
  end

  context "with flags argument" do
    context "when READONLY flag is specified" do
      it "sets readonly flag on the buffer, allowing only reads" do
        @file = open_fixture
        @buffer = IO::Buffer.map(@file, nil, 0, IO::Buffer::READONLY)

        @buffer.should.readonly?

        @buffer.get_string.should == "abc\xC3\xA2def\n".b
      end

      it "allows mapping read-only files" do
        @file = File.open("#{__dir__}/../fixtures/read_text.txt", "r")
        @buffer = IO::Buffer.map(@file, nil, 0, IO::Buffer::READONLY)

        @buffer.should.readonly?

        @buffer.get_string.should == "abc\xC3\xA2def\n".b
      end

      it "causes IO::Buffer::AccessError on write" do
        @file = open_fixture
        @buffer = IO::Buffer.map(@file, nil, 0, IO::Buffer::READONLY)

        -> { @buffer.set_string("test") }.should raise_error(IO::Buffer::AccessError, "Buffer is not writable!")
      end
    end

    context "when PRIVATE is specified" do
      it "sets private flag on the buffer, making it freely modifiable" do
        @file = open_fixture
        @buffer = IO::Buffer.map(@file, nil, 0, IO::Buffer::PRIVATE)

        @buffer.should.private?
        @buffer.should_not.shared?
        @buffer.should_not.external?

        @buffer.get_string.should == "abc\xC3\xA2def\n".b
        @buffer.set_string("test12345")
        @buffer.get_string.should == "test12345".b

        @file.read.should == "abcâdef\n"
      end

      it "allows mapping read-only files and modifying the buffer" do
        @file = File.open("#{__dir__}/../fixtures/read_text.txt", "r")
        @buffer = IO::Buffer.map(@file, nil, 0, IO::Buffer::PRIVATE)

        @buffer.should.private?
        @buffer.should_not.shared?
        @buffer.should_not.external?

        @buffer.get_string.should == "abc\xC3\xA2def\n".b
        @buffer.set_string("test12345")
        @buffer.get_string.should == "test12345".b

        @file.read.should == "abcâdef\n"
      end

      platform_is_not :windows do
        it "is not shared across processes" do
          file_name = tmp("shared_buffer")
          @file = File.open(file_name, "w+")
          @file << "I'm private"
          @file.rewind
          @buffer = IO::Buffer.map(@file, nil, 0, IO::Buffer::PRIVATE)

          IO.popen("-") do |child_pipe|
            if child_pipe
              # Synchronize on child's output.
              child_pipe.readlines.first.chomp.should == @buffer.to_s
              @buffer.get_string.should == "I'm private"

              @file.read.should == "I'm private"
            else
              @buffer.set_string("I'm shared!")
              puts @buffer
            end
          ensure
            child_pipe&.close
          end
        ensure
          File.unlink(file_name)
        end
      end
    end
  end
end