robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
11.3 KiB402 linesraw
1local uv = vim.uv
2
3local t = require('test.unit.testutil')
4local describe, before_each, after_each = t.describe, t.before_each, t.after_each
5local itp = t.gen_itp(t.it)
6
7local eq = t.eq
8local ffi = t.ffi
9local cimport = t.cimport
10local cppimport = t.cppimport
11local mkdir = t.mkdir
12
13local m = cimport('./src/nvim/os/os.h', './src/nvim/os/fileio.h')
14cppimport('fcntl.h')
15
16local fcontents = ''
17for i = 0, 255 do
18  fcontents = fcontents .. (i == 0 and '\0' or ('%c'):format(i))
19end
20fcontents = fcontents:rep(16)
21
22local dir = 'Xtest-unit-file_spec.d'
23local file1 = dir .. '/file1.dat'
24local file2 = dir .. '/file2.dat'
25local linkf = dir .. '/file.lnk'
26local linkb = dir .. '/broken.lnk'
27local filec = dir .. '/created-file.dat'
28
29before_each(function()
30  mkdir(dir)
31
32  local f1 = io.open(file1, 'w')
33  f1:write(fcontents)
34  f1:close()
35
36  local f2 = io.open(file2, 'w')
37  f2:write(fcontents)
38  f2:close()
39
40  uv.fs_symlink('file1.dat', linkf)
41  uv.fs_symlink('broken.dat', linkb)
42end)
43
44after_each(function()
45  os.remove(file1)
46  os.remove(file2)
47  os.remove(linkf)
48  os.remove(linkb)
49  os.remove(filec)
50  uv.fs_rmdir(dir)
51end)
52
53local function file_open(fname, flags, mode)
54  local ret2 = ffi.new('FileDescriptor')
55  local ret1 = m.file_open(ret2, fname, flags, mode)
56  return ret1, ret2
57end
58
59local function file_open_fd(fd, flags)
60  local ret2 = ffi.new('FileDescriptor')
61  local ret1 = m.file_open_fd(ret2, fd, flags)
62  return ret1, ret2
63end
64
65local function file_write(fp, buf)
66  return m.file_write(fp, buf, #buf)
67end
68
69local function file_read(fp, size)
70  local buf = nil
71  if size == nil then
72    size = 0
73  else
74    -- For some reason if length of NUL-bytes-string is the same as `char[?]`
75    -- size luajit garbage collector crashes. But it does not do so in
76    -- os_read[v] tests in os/fs_spec.lua.
77    buf = ffi.new('char[?]', size + 1, ('\0'):rep(size))
78  end
79  local ret1 = m.file_read(fp, buf, size)
80  local ret2 = ''
81  if buf ~= nil then
82    ret2 = ffi.string(buf, size)
83  end
84  return ret1, ret2
85end
86
87local function file_flush(fp)
88  return m.file_flush(fp)
89end
90
91local function file_fsync(fp)
92  return m.file_fsync(fp)
93end
94
95local function file_skip(fp, size)
96  return m.file_skip(fp, size)
97end
98
99describe('file_open_fd', function()
100  itp('can use file descriptor returned by os_open for reading', function()
101    local fd = m.os_open(file1, m.kO_RDONLY, 0)
102    local err, fp = file_open_fd(fd, m.kFileReadOnly)
103    eq(0, err)
104    eq({ #fcontents, fcontents }, { file_read(fp, #fcontents) })
105    eq(0, m.file_close(fp, false))
106  end)
107  itp('can use file descriptor returned by os_open for writing', function()
108    eq(nil, uv.fs_stat(filec))
109    local fd = m.os_open(filec, m.kO_WRONLY + m.kO_CREAT, 384)
110    local err, fp = file_open_fd(fd, m.kFileWriteOnly)
111    eq(0, err)
112    eq(4, file_write(fp, 'test'))
113    eq(0, m.file_close(fp, false))
114    eq(4, uv.fs_stat(filec).size)
115    eq('test', io.open(filec):read('*a'))
116  end)
117end)
118
119describe('file_open', function()
120  itp('can create a rwx------ file with kFileCreate', function()
121    local err, fp = file_open(filec, m.kFileCreate, 448)
122    eq(0, err)
123    local attrs = uv.fs_stat(filec)
124    eq(33216, attrs.mode)
125    eq(0, m.file_close(fp, false))
126  end)
127
128  itp('can create a rw------- file with kFileCreate', function()
129    local err, fp = file_open(filec, m.kFileCreate, 384)
130    eq(0, err)
131    local attrs = uv.fs_stat(filec)
132    eq(33152, attrs.mode)
133    eq(0, m.file_close(fp, false))
134  end)
135
136  itp('can create a rwx------ file with kFileCreateOnly', function()
137    local err, fp = file_open(filec, m.kFileCreateOnly, 448)
138    eq(0, err)
139    local attrs = uv.fs_stat(filec)
140    eq(33216, attrs.mode)
141    eq(0, m.file_close(fp, false))
142  end)
143
144  itp('can create a rw------- file with kFileCreateOnly', function()
145    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
146    eq(0, err)
147    local attrs = uv.fs_stat(filec)
148    eq(33152, attrs.mode)
149    eq(0, m.file_close(fp, false))
150  end)
151
152  itp('fails to open an existing file with kFileCreateOnly', function()
153    local err, _ = file_open(file1, m.kFileCreateOnly, 384)
154    eq(m.UV_EEXIST, err)
155  end)
156
157  itp('fails to open an symlink with kFileNoSymlink', function()
158    local err, _ = file_open(linkf, m.kFileNoSymlink, 384)
159    -- err is UV_EMLINK in FreeBSD, but if I use `ok(err == m.UV_ELOOP or err ==
160    -- m.UV_EMLINK)`, then I loose the ability to see actual `err` value.
161    if err ~= m.UV_ELOOP then
162      eq(m.UV_EMLINK, err)
163    end
164  end)
165
166  itp('can open an existing file write-only with kFileCreate', function()
167    local err, fp = file_open(file1, m.kFileCreate, 384)
168    eq(0, err)
169    eq(true, fp.wr)
170    eq(0, m.file_close(fp, false))
171  end)
172
173  itp('can open an existing file read-only with zero', function()
174    local err, fp = file_open(file1, 0, 384)
175    eq(0, err)
176    eq(false, fp.wr)
177    eq(0, m.file_close(fp, false))
178  end)
179
180  itp('can open an existing file read-only with kFileReadOnly', function()
181    local err, fp = file_open(file1, m.kFileReadOnly, 384)
182    eq(0, err)
183    eq(false, fp.wr)
184    eq(0, m.file_close(fp, false))
185  end)
186
187  itp('can open an existing file read-only with kFileNoSymlink', function()
188    local err, fp = file_open(file1, m.kFileNoSymlink, 384)
189    eq(0, err)
190    eq(false, fp.wr)
191    eq(0, m.file_close(fp, false))
192  end)
193
194  itp('can truncate an existing file with kFileTruncate', function()
195    local err, fp = file_open(file1, m.kFileTruncate, 384)
196    eq(0, err)
197    eq(true, fp.wr)
198    eq(0, m.file_close(fp, false))
199    local attrs = uv.fs_stat(file1)
200    eq(0, attrs.size)
201  end)
202
203  itp('can open an existing file write-only with kFileWriteOnly', function()
204    local err, fp = file_open(file1, m.kFileWriteOnly, 384)
205    eq(0, err)
206    eq(true, fp.wr)
207    eq(0, m.file_close(fp, false))
208    local attrs = uv.fs_stat(file1)
209    eq(4096, attrs.size)
210  end)
211
212  itp('fails to create a file with just kFileWriteOnly', function()
213    local err, _ = file_open(filec, m.kFileWriteOnly, 384)
214    eq(m.UV_ENOENT, err)
215    local attrs = uv.fs_stat(filec)
216    eq(nil, attrs)
217  end)
218
219  itp('can truncate an existing file with kFileTruncate when opening a symlink', function()
220    local err, fp = file_open(linkf, m.kFileTruncate, 384)
221    eq(0, err)
222    eq(true, fp.wr)
223    eq(0, m.file_close(fp, false))
224    local attrs = uv.fs_stat(file1)
225    eq(0, attrs.size)
226  end)
227
228  itp('fails to open a directory write-only', function()
229    local err, _ = file_open(dir, m.kFileWriteOnly, 384)
230    eq(m.UV_EISDIR, err)
231  end)
232
233  itp('fails to open a broken symbolic link write-only', function()
234    local err, _ = file_open(linkb, m.kFileWriteOnly, 384)
235    eq(m.UV_ENOENT, err)
236  end)
237
238  itp('fails to open a broken symbolic link read-only', function()
239    local err, _ = file_open(linkb, m.kFileReadOnly, 384)
240    eq(m.UV_ENOENT, err)
241  end)
242end)
243
244describe('file_close', function()
245  itp('can flush writes to disk also with true argument', function()
246    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
247    eq(0, err)
248    local wsize = file_write(fp, 'test')
249    eq(4, wsize)
250    eq(0, uv.fs_stat(filec).size)
251    eq(0, m.file_close(fp, true))
252    eq(wsize, uv.fs_stat(filec).size)
253  end)
254end)
255
256describe('file_fsync', function()
257  itp('can flush writes to disk', function()
258    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
259    eq(0, file_fsync(fp))
260    eq(0, err)
261    eq(0, uv.fs_stat(filec).size)
262    local wsize = file_write(fp, 'test')
263    eq(4, wsize)
264    eq(0, uv.fs_stat(filec).size)
265    eq(0, file_fsync(fp))
266    eq(wsize, uv.fs_stat(filec).size)
267    eq(0, m.file_close(fp, false))
268  end)
269end)
270
271describe('file_flush', function()
272  itp('can flush writes to disk', function()
273    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
274    eq(0, file_flush(fp))
275    eq(0, err)
276    eq(0, uv.fs_stat(filec).size)
277    local wsize = file_write(fp, 'test')
278    eq(4, wsize)
279    eq(0, uv.fs_stat(filec).size)
280    eq(0, file_flush(fp))
281    eq(wsize, uv.fs_stat(filec).size)
282    eq(0, m.file_close(fp, false))
283  end)
284end)
285
286describe('file_read', function()
287  itp('can read small chunks of input until eof', function()
288    local err, fp = file_open(file1, 0, 384)
289    eq(0, err)
290    eq(false, fp.wr)
291    local shift = 0
292    while shift < #fcontents do
293      local size = 3
294      local exp_err = size
295      local exp_s = fcontents:sub(shift + 1, shift + size)
296      if shift + size >= #fcontents then
297        exp_err = #fcontents - shift
298        exp_s = (fcontents:sub(shift + 1, shift + size) .. (('\0'):rep(size - exp_err)))
299      end
300      eq({ exp_err, exp_s }, { file_read(fp, size) })
301      shift = shift + size
302    end
303    eq(0, m.file_close(fp, false))
304  end)
305
306  itp('can read the whole file at once', function()
307    local err, fp = file_open(file1, 0, 384)
308    eq(0, err)
309    eq(false, fp.wr)
310    eq({ #fcontents, fcontents }, { file_read(fp, #fcontents) })
311    eq({ 0, ('\0'):rep(#fcontents) }, { file_read(fp, #fcontents) })
312    eq(0, m.file_close(fp, false))
313  end)
314
315  itp('can read more then 1024 bytes after reading a small chunk', function()
316    local err, fp = file_open(file1, 0, 384)
317    eq(0, err)
318    eq(false, fp.wr)
319    eq({ 5, fcontents:sub(1, 5) }, { file_read(fp, 5) })
320    eq({ #fcontents - 5, fcontents:sub(6) .. (('\0'):rep(5)) }, { file_read(fp, #fcontents) })
321    eq(0, m.file_close(fp, false))
322  end)
323
324  itp('can read file by 768-byte-chunks', function()
325    local err, fp = file_open(file1, 0, 384)
326    eq(0, err)
327    eq(false, fp.wr)
328    local shift = 0
329    while shift < #fcontents do
330      local size = 768
331      local exp_err = size
332      local exp_s = fcontents:sub(shift + 1, shift + size)
333      if shift + size >= #fcontents then
334        exp_err = #fcontents - shift
335        exp_s = (fcontents:sub(shift + 1, shift + size) .. (('\0'):rep(size - exp_err)))
336      end
337      eq({ exp_err, exp_s }, { file_read(fp, size) })
338      shift = shift + size
339    end
340    eq(0, m.file_close(fp, false))
341  end)
342end)
343
344describe('file_write', function()
345  itp('can write the whole file at once', function()
346    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
347    eq(0, err)
348    eq(true, fp.wr)
349    local wr = file_write(fp, fcontents)
350    eq(#fcontents, wr)
351    eq(0, m.file_close(fp, false))
352    eq(wr, uv.fs_stat(filec).size)
353    eq(fcontents, io.open(filec):read('*a'))
354  end)
355
356  itp('can write the whole file by small chunks', function()
357    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
358    eq(0, err)
359    eq(true, fp.wr)
360    local shift = 0
361    while shift < #fcontents do
362      local size = 3
363      local s = fcontents:sub(shift + 1, shift + size)
364      local wr = file_write(fp, s)
365      eq(wr, #s)
366      shift = shift + size
367    end
368    eq(0, m.file_close(fp, false))
369    eq(#fcontents, uv.fs_stat(filec).size)
370    eq(fcontents, io.open(filec):read('*a'))
371  end)
372
373  itp('can write the whole file by 768-byte-chunks', function()
374    local err, fp = file_open(filec, m.kFileCreateOnly, 384)
375    eq(0, err)
376    eq(true, fp.wr)
377    local shift = 0
378    while shift < #fcontents do
379      local size = 768
380      local s = fcontents:sub(shift + 1, shift + size)
381      local wr = file_write(fp, s)
382      eq(wr, #s)
383      shift = shift + size
384    end
385    eq(0, m.file_close(fp, false))
386    eq(#fcontents, uv.fs_stat(filec).size)
387    eq(fcontents, io.open(filec):read('*a'))
388  end)
389end)
390
391describe('file_skip', function()
392  itp('can skip 3 bytes', function()
393    local err, fp = file_open(file1, 0, 384)
394    eq(0, err)
395    eq(false, fp.wr)
396    eq(3, file_skip(fp, 3))
397    local rd, s = file_read(fp, 3)
398    eq(3, rd)
399    eq(fcontents:sub(4, 6), s)
400    eq(0, m.file_close(fp, false))
401  end)
402end)