aboutsummaryrefslogtreecommitdiff
path: root/test/unit/os/fileio_spec.lua
blob: fd30ca70da2d9b3027f75565ae329dfd73af2287 (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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
local luv = require('luv')

local helpers = require('test.unit.helpers')(after_each)
local itp = helpers.gen_itp(it)

local eq = helpers.eq
local ffi = helpers.ffi
local cimport = helpers.cimport
local cppimport = helpers.cppimport
local mkdir = helpers.mkdir

local m = cimport('./src/nvim/os/os.h', './src/nvim/os/fileio.h')
cppimport('fcntl.h')

local fcontents = ''
for i = 0, 255 do
  fcontents = fcontents .. (i == 0 and '\0' or ('%c'):format(i))
end
fcontents = fcontents:rep(16)

local dir = 'Xtest-unit-file_spec.d'
local file1 = dir .. '/file1.dat'
local file2 = dir .. '/file2.dat'
local linkf = dir .. '/file.lnk'
local linkb = dir .. '/broken.lnk'
local filec = dir .. '/created-file.dat'

before_each(function()
  mkdir(dir);

  local f1 = io.open(file1, 'w')
  f1:write(fcontents)
  f1:close()

  local f2 = io.open(file2, 'w')
  f2:write(fcontents)
  f2:close()

  luv.fs_symlink('file1.dat', linkf)
  luv.fs_symlink('broken.dat', linkb)
end)

after_each(function()
  os.remove(file1)
  os.remove(file2)
  os.remove(linkf)
  os.remove(linkb)
  os.remove(filec)
  luv.fs_rmdir(dir)
end)

local function file_open(fname, flags, mode)
  local ret2 = ffi.new('FileDescriptor')
  local ret1 = m.file_open(ret2, fname, flags, mode)
  return ret1, ret2
end

local function file_open_new(fname, flags, mode)
  local ret1 = ffi.new('int[?]', 1, {0})
  local ret2 = ffi.gc(m.file_open_new(ret1, fname, flags, mode), nil)
  return ret1[0], ret2
end

local function file_open_fd(fd, flags)
  local ret2 = ffi.new('FileDescriptor')
  local ret1 = m.file_open_fd(ret2, fd, flags)
  return ret1, ret2
end

local function file_open_fd_new(fd, flags)
  local ret1 = ffi.new('int[?]', 1, {0})
  local ret2 = ffi.gc(m.file_open_fd_new(ret1, fd, flags), nil)
  return ret1[0], ret2
end

local function file_write(fp, buf)
  return m.file_write(fp, buf, #buf)
end

local function msgpack_file_write(fp, buf)
  return m.msgpack_file_write(fp, buf, #buf)
end

local function file_read(fp, size)
  local buf = nil
  if size == nil then
    size = 0
  else
    -- For some reason if length of NUL-bytes-string is the same as `char[?]`
    -- size luajit garbage collector crashes. But it does not do so in
    -- os_read[v] tests in os/fs_spec.lua.
    buf = ffi.new('char[?]', size + 1, ('\0'):rep(size))
  end
  local ret1 = m.file_read(fp, buf, size)
  local ret2 = ''
  if buf ~= nil then
    ret2 = ffi.string(buf, size)
  end
  return ret1, ret2
end

local function file_flush(fp)
  return m.file_flush(fp)
end

local function file_fsync(fp)
  return m.file_fsync(fp)
end

local function file_skip(fp, size)
  return m.file_skip(fp, size)
end

describe('file_open_fd', function()
  itp('can use file descriptor returned by os_open for reading', function()
    local fd = m.os_open(file1, m.kO_RDONLY, 0)
    local err, fp = file_open_fd(fd, m.kFileReadOnly)
    eq(0, err)
    eq({#fcontents, fcontents}, {file_read(fp, #fcontents)})
    eq(0, m.file_close(fp, false))
  end)
  itp('can use file descriptor returned by os_open for writing', function()
    eq(nil, luv.fs_stat(filec))
    local fd = m.os_open(filec, m.kO_WRONLY + m.kO_CREAT, 384)
    local err, fp = file_open_fd(fd, m.kFileWriteOnly)
    eq(0, err)
    eq(4, file_write(fp, 'test'))
    eq(0, m.file_close(fp, false))
    eq(4, luv.fs_stat(filec).size)
    eq('test', io.open(filec):read('*a'))
  end)
end)

describe('file_open_fd_new', function()
  itp('can use file descriptor returned by os_open for reading', function()
    local fd = m.os_open(file1, m.kO_RDONLY, 0)
    local err, fp = file_open_fd_new(fd, m.kFileReadOnly)
    eq(0, err)
    eq({#fcontents, fcontents}, {file_read(fp, #fcontents)})
    eq(0, m.file_free(fp, false))
  end)
  itp('can use file descriptor returned by os_open for writing', function()
    eq(nil, luv.fs_stat(filec))
    local fd = m.os_open(filec, m.kO_WRONLY + m.kO_CREAT, 384)
    local err, fp = file_open_fd_new(fd, m.kFileWriteOnly)
    eq(0, err)
    eq(4, file_write(fp, 'test'))
    eq(0, m.file_free(fp, false))
    eq(4, luv.fs_stat(filec).size)
    eq('test', io.open(filec):read('*a'))
  end)
end)

describe('file_open', function()
  itp('can create a rwx------ file with kFileCreate', function()
    local err, fp = file_open(filec, m.kFileCreate, 448)
    eq(0, err)
    local attrs = luv.fs_stat(filec)
    eq(33216, attrs.mode)
    eq(0, m.file_close(fp, false))
  end)

  itp('can create a rw------- file with kFileCreate', function()
    local err, fp = file_open(filec, m.kFileCreate, 384)
    eq(0, err)
    local attrs = luv.fs_stat(filec)
    eq(33152, attrs.mode)
    eq(0, m.file_close(fp, false))
  end)

  itp('can create a rwx------ file with kFileCreateOnly', function()
    local err, fp = file_open(filec, m.kFileCreateOnly, 448)
    eq(0, err)
    local attrs = luv.fs_stat(filec)
    eq(33216, attrs.mode)
    eq(0, m.file_close(fp, false))
  end)

  itp('can create a rw------- file with kFileCreateOnly', function()
    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
    eq(0, err)
    local attrs = luv.fs_stat(filec)
    eq(33152, attrs.mode)
    eq(0, m.file_close(fp, false))
  end)

  itp('fails to open an existing file with kFileCreateOnly', function()
    local err, _ = file_open(file1, m.kFileCreateOnly, 384)
    eq(m.UV_EEXIST, err)
  end)

  itp('fails to open an symlink with kFileNoSymlink', function()
    local err, _ = file_open(linkf, m.kFileNoSymlink, 384)
    -- err is UV_EMLINK in FreeBSD, but if I use `ok(err == m.UV_ELOOP or err ==
    -- m.UV_EMLINK)`, then I loose the ability to see actual `err` value.
    if err ~= m.UV_ELOOP then eq(m.UV_EMLINK, err) end
  end)

  itp('can open an existing file write-only with kFileCreate', function()
    local err, fp = file_open(file1, m.kFileCreate, 384)
    eq(0, err)
    eq(true, fp.wr)
    eq(0, m.file_close(fp, false))
  end)

  itp('can open an existing file read-only with zero', function()
    local err, fp = file_open(file1, 0, 384)
    eq(0, err)
    eq(false, fp.wr)
    eq(0, m.file_close(fp, false))
  end)

  itp('can open an existing file read-only with kFileReadOnly', function()
    local err, fp = file_open(file1, m.kFileReadOnly, 384)
    eq(0, err)
    eq(false, fp.wr)
    eq(0, m.file_close(fp, false))
  end)

  itp('can open an existing file read-only with kFileNoSymlink', function()
    local err, fp = file_open(file1, m.kFileNoSymlink, 384)
    eq(0, err)
    eq(false, fp.wr)
    eq(0, m.file_close(fp, false))
  end)

  itp('can truncate an existing file with kFileTruncate', function()
    local err, fp = file_open(file1, m.kFileTruncate, 384)
    eq(0, err)
    eq(true, fp.wr)
    eq(0, m.file_close(fp, false))
    local attrs = luv.fs_stat(file1)
    eq(0, attrs.size)
  end)

  itp('can open an existing file write-only with kFileWriteOnly', function()
    local err, fp = file_open(file1, m.kFileWriteOnly, 384)
    eq(0, err)
    eq(true, fp.wr)
    eq(0, m.file_close(fp, false))
    local attrs = luv.fs_stat(file1)
    eq(4096, attrs.size)
  end)

  itp('fails to create a file with just kFileWriteOnly', function()
    local err, _ = file_open(filec, m.kFileWriteOnly, 384)
    eq(m.UV_ENOENT, err)
    local attrs = luv.fs_stat(filec)
    eq(nil, attrs)
  end)

  itp('can truncate an existing file with kFileTruncate when opening a symlink',
  function()
    local err, fp = file_open(linkf, m.kFileTruncate, 384)
    eq(0, err)
    eq(true, fp.wr)
    eq(0, m.file_close(fp, false))
    local attrs = luv.fs_stat(file1)
    eq(0, attrs.size)
  end)

  itp('fails to open a directory write-only', function()
    local err, _ = file_open(dir, m.kFileWriteOnly, 384)
    eq(m.UV_EISDIR, err)
  end)

  itp('fails to open a broken symbolic link write-only', function()
    local err, _ = file_open(linkb, m.kFileWriteOnly, 384)
    eq(m.UV_ENOENT, err)
  end)

  itp('fails to open a broken symbolic link read-only', function()
    local err, _ = file_open(linkb, m.kFileReadOnly, 384)
    eq(m.UV_ENOENT, err)
  end)
end)

describe('file_open_new', function()
  itp('can open a file read-only', function()
    local err, fp = file_open_new(file1, 0, 384)
    eq(0, err)
    eq(false, fp.wr)
    eq(0, m.file_free(fp, false))
  end)

  itp('fails to open an existing file with kFileCreateOnly', function()
    local err, fp = file_open_new(file1, m.kFileCreateOnly, 384)
    eq(m.UV_EEXIST, err)
    eq(nil, fp)
  end)
end)

describe('file_close', function()
  itp('can flush writes to disk also with true argument', function()
    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
    eq(0, err)
    local wsize = file_write(fp, 'test')
    eq(4, wsize)
    eq(0, luv.fs_stat(filec).size)
    eq(0, m.file_close(fp, true))
    eq(wsize, luv.fs_stat(filec).size)
  end)
end)

describe('file_free', function()
  itp('can flush writes to disk also with true argument', function()
    local err, fp = file_open_new(filec, m.kFileCreateOnly, 384)
    eq(0, err)
    local wsize = file_write(fp, 'test')
    eq(4, wsize)
    eq(0, luv.fs_stat(filec).size)
    eq(0, m.file_free(fp, true))
    eq(wsize, luv.fs_stat(filec).size)
  end)
end)

describe('file_fsync', function()
  itp('can flush writes to disk', function()
    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
    eq(0, file_fsync(fp))
    eq(0, err)
    eq(0, luv.fs_stat(filec).size)
    local wsize = file_write(fp, 'test')
    eq(4, wsize)
    eq(0, luv.fs_stat(filec).size)
    eq(0, file_fsync(fp))
    eq(wsize, luv.fs_stat(filec).size)
    eq(0, m.file_close(fp, false))
  end)
end)

describe('file_flush', function()
  itp('can flush writes to disk', function()
    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
    eq(0, file_flush(fp))
    eq(0, err)
    eq(0, luv.fs_stat(filec).size)
    local wsize = file_write(fp, 'test')
    eq(4, wsize)
    eq(0, luv.fs_stat(filec).size)
    eq(0, file_flush(fp))
    eq(wsize, luv.fs_stat(filec).size)
    eq(0, m.file_close(fp, false))
  end)
end)

describe('file_read', function()
  itp('can read small chunks of input until eof', function()
    local err, fp = file_open(file1, 0, 384)
    eq(0, err)
    eq(false, fp.wr)
    local shift = 0
    while shift < #fcontents do
      local size = 3
      local exp_err = size
      local exp_s = fcontents:sub(shift + 1, shift + size)
      if shift + size >= #fcontents then
        exp_err = #fcontents - shift
        exp_s = (fcontents:sub(shift + 1, shift + size)
                 .. (('\0'):rep(size - exp_err)))
      end
      eq({exp_err, exp_s}, {file_read(fp, size)})
      shift = shift + size
    end
    eq(0, m.file_close(fp, false))
  end)

  itp('can read the whole file at once', function()
    local err, fp = file_open(file1, 0, 384)
    eq(0, err)
    eq(false, fp.wr)
    eq({#fcontents, fcontents}, {file_read(fp, #fcontents)})
    eq({0, ('\0'):rep(#fcontents)}, {file_read(fp, #fcontents)})
    eq(0, m.file_close(fp, false))
  end)

  itp('can read more then 1024 bytes after reading a small chunk', function()
    local err, fp = file_open(file1, 0, 384)
    eq(0, err)
    eq(false, fp.wr)
    eq({5, fcontents:sub(1, 5)}, {file_read(fp, 5)})
    eq({#fcontents - 5, fcontents:sub(6) .. (('\0'):rep(5))},
       {file_read(fp, #fcontents)})
    eq(0, m.file_close(fp, false))
  end)

  itp('can read file by 768-byte-chunks', function()
    local err, fp = file_open(file1, 0, 384)
    eq(0, err)
    eq(false, fp.wr)
    local shift = 0
    while shift < #fcontents do
      local size = 768
      local exp_err = size
      local exp_s = fcontents:sub(shift + 1, shift + size)
      if shift + size >= #fcontents then
        exp_err = #fcontents - shift
        exp_s = (fcontents:sub(shift + 1, shift + size)
                 .. (('\0'):rep(size - exp_err)))
      end
      eq({exp_err, exp_s}, {file_read(fp, size)})
      shift = shift + size
    end
    eq(0, m.file_close(fp, false))
  end)
end)

describe('file_write', function()
  itp('can write the whole file at once', function()
    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
    eq(0, err)
    eq(true, fp.wr)
    local wr = file_write(fp, fcontents)
    eq(#fcontents, wr)
    eq(0, m.file_close(fp, false))
    eq(wr, luv.fs_stat(filec).size)
    eq(fcontents, io.open(filec):read('*a'))
  end)

  itp('can write the whole file by small chunks', function()
    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
    eq(0, err)
    eq(true, fp.wr)
    local shift = 0
    while shift < #fcontents do
      local size = 3
      local s = fcontents:sub(shift + 1, shift + size)
      local wr = file_write(fp, s)
      eq(wr, #s)
      shift = shift + size
    end
    eq(0, m.file_close(fp, false))
    eq(#fcontents, luv.fs_stat(filec).size)
    eq(fcontents, io.open(filec):read('*a'))
  end)

  itp('can write the whole file by 768-byte-chunks', function()
    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
    eq(0, err)
    eq(true, fp.wr)
    local shift = 0
    while shift < #fcontents do
      local size = 768
      local s = fcontents:sub(shift + 1, shift + size)
      local wr = file_write(fp, s)
      eq(wr, #s)
      shift = shift + size
    end
    eq(0, m.file_close(fp, false))
    eq(#fcontents, luv.fs_stat(filec).size)
    eq(fcontents, io.open(filec):read('*a'))
  end)
end)

describe('msgpack_file_write', function()
  itp('can write the whole file at once', function()
    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
    eq(0, err)
    eq(true, fp.wr)
    local wr = msgpack_file_write(fp, fcontents)
    eq(0, wr)
    eq(0, m.file_close(fp, false))
    eq(fcontents, io.open(filec):read('*a'))
  end)
end)

describe('file_skip', function()
  itp('can skip 3 bytes', function()
    local err, fp = file_open(file1, 0, 384)
    eq(0, err)
    eq(false, fp.wr)
    eq(3, file_skip(fp, 3))
    local rd, s = file_read(fp, 3)
    eq(3, rd)
    eq(fcontents:sub(4, 6), s)
    eq(0, m.file_close(fp, false))
  end)
end)