robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
37.2 KiB1114 linesraw
1local uv = vim.uv
2local bit = require('bit')
3
4local t = require('test.unit.testutil')
5local describe, before_each, after_each, pending =
6  t.describe, t.before_each, t.after_each, t.pending
7local itp = t.gen_itp(t.it)
8
9local cimport = t.cimport
10local cppimport = t.cppimport
11local internalize = t.internalize
12local ok = t.ok
13local eq = t.eq
14local neq = t.neq
15local ffi = t.ffi
16local cstr = t.cstr
17local to_cstr = t.to_cstr
18local OK = t.OK
19local FAIL = t.FAIL
20local NULL = t.NULL
21local mkdir = t.mkdir
22local endswith = vim.endswith
23
24local NODE_NORMAL = 0
25local NODE_WRITABLE = 1
26
27local fs = cimport('./src/nvim/os/os.h', './src/nvim/path.h')
28cppimport('sys/stat.h')
29cppimport('fcntl.h')
30cimport('uv.h')
31
32local s = ''
33for i = 0, 255 do
34  s = s .. (i == 0 and '\0' or ('%c'):format(i))
35end
36local fcontents = s:rep(16)
37
38local directory = nil
39local absolute_executable = nil
40local executable_name = nil
41
42local function set_bit(number, to_set)
43  return bit.bor(number, to_set)
44end
45
46local function unset_bit(number, to_unset)
47  return bit.band(number, (bit.bnot(to_unset)))
48end
49
50local function assert_file_exists(filepath)
51  neq(nil, uv.fs_stat(filepath))
52end
53
54local function assert_file_does_not_exist(filepath)
55  eq(nil, uv.fs_stat(filepath))
56end
57
58local function os_setperm(filename, perm)
59  return fs.os_setperm((to_cstr(filename)), perm)
60end
61
62local function os_getperm(filename)
63  local perm = fs.os_getperm((to_cstr(filename)))
64  return tonumber(perm)
65end
66
67describe('fs.c', function()
68  local function os_isdir(name)
69    return fs.os_isdir(to_cstr(name))
70  end
71
72  before_each(function()
73    mkdir('unit-test-directory')
74
75    io.open('unit-test-directory/test.file', 'w'):close()
76
77    io.open('unit-test-directory/test_2.file', 'w'):close()
78    uv.fs_symlink('test.file', 'unit-test-directory/test_link.file')
79
80    uv.fs_symlink('non_existing_file.file', 'unit-test-directory/test_broken_link.file')
81    -- The tests are invoked with an absolute executable path in arg[0].
82    absolute_executable = vim.fs.normalize(assert(uv.exepath()))
83    -- Split the absolute_executable path into a directory and filename.
84    directory, executable_name = string.match(absolute_executable, '^(.*)/(.*)$')
85  end)
86
87  after_each(function()
88    os.remove('unit-test-directory/test.file')
89    os.remove('unit-test-directory/test_2.file')
90    os.remove('unit-test-directory/test_link.file')
91    os.remove('unit-test-directory/test_hlink.file')
92    os.remove('unit-test-directory/test_broken_link.file')
93    uv.fs_rmdir('unit-test-directory')
94  end)
95
96  describe('os_dirname', function()
97    itp('returns OK and writes current directory to the buffer', function()
98      local length = string.len(uv.cwd()) + 1
99      local buf = cstr(length, '')
100      eq(OK, fs.os_dirname(buf, length))
101      eq(uv.cwd(), ffi.string(buf))
102    end)
103
104    itp('returns FAIL if the buffer is too small', function()
105      local length = string.len(uv.cwd()) + 1
106      local buf = cstr(length - 1, '')
107      eq(FAIL, fs.os_dirname(buf, length - 1))
108    end)
109  end)
110
111  describe('os_chdir', function()
112    itp('fails with path="~"', function()
113      eq(false, os_isdir('~'), 'sanity check: no literal "~" directory')
114      local length = 4096
115      local expected_cwd = cstr(length, '')
116      local cwd = cstr(length, '')
117      eq(OK, fs.os_dirname(expected_cwd, length))
118
119      -- os_chdir returns 0 for success, not OK (1).
120      neq(0, fs.os_chdir('~')) -- fail
121      neq(0, fs.os_chdir('~/')) -- fail
122
123      eq(OK, fs.os_dirname(cwd, length))
124      -- CWD did not change.
125      eq(ffi.string(expected_cwd), ffi.string(cwd))
126    end)
127  end)
128
129  describe('os_isdir', function()
130    itp('returns false if an empty string is given', function()
131      eq(false, (os_isdir('')))
132    end)
133
134    itp('returns false if a nonexisting directory is given', function()
135      eq(false, (os_isdir('non-existing-directory')))
136    end)
137
138    itp('returns false if a nonexisting absolute directory is given', function()
139      eq(false, (os_isdir('/non-existing-directory')))
140    end)
141
142    itp('returns false if an existing file is given', function()
143      eq(false, (os_isdir('unit-test-directory/test.file')))
144    end)
145
146    itp('returns true if the current directory is given', function()
147      eq(true, (os_isdir('.')))
148    end)
149
150    itp('returns true if the parent directory is given', function()
151      eq(true, (os_isdir('..')))
152    end)
153
154    itp('returns true if an arbitrary directory is given', function()
155      eq(true, (os_isdir('unit-test-directory')))
156    end)
157
158    itp('returns true if an absolute directory is given', function()
159      eq(true, (os_isdir(directory)))
160    end)
161  end)
162
163  describe('os_can_exe', function()
164    local function os_can_exe(name)
165      local buf = ffi.new('char *[1]')
166      buf[0] = NULL
167      local ce_ret = fs.os_can_exe(to_cstr(name), buf, true)
168
169      -- When os_can_exe returns true, it must set the path.
170      -- When it returns false, the path must be NULL.
171      if ce_ret then
172        neq(NULL, buf[0])
173        return internalize(buf[0])
174      else
175        eq(NULL, buf[0])
176        return nil
177      end
178    end
179
180    local function cant_exe(name)
181      eq(nil, os_can_exe(name))
182    end
183
184    local function exe(name)
185      return os_can_exe(name)
186    end
187
188    itp('returns false when given a directory', function()
189      cant_exe('./unit-test-directory')
190    end)
191
192    itp('returns false when given a regular file without executable bit set', function()
193      cant_exe('unit-test-directory/test.file')
194    end)
195
196    itp('returns false when the given file does not exists', function()
197      cant_exe('does-not-exist.file')
198    end)
199
200    itp('returns the absolute path when given an executable inside $PATH', function()
201      local fullpath = exe('ls')
202      eq(true, fs.path_is_absolute(to_cstr(fullpath)))
203    end)
204
205    itp('returns the absolute path when given an executable relative to the current dir', function()
206      local old_dir = uv.cwd()
207
208      uv.chdir(directory)
209
210      -- Rely on currentdir to resolve symlinks, if any. Testing against
211      -- the absolute path taken from arg[0] may result in failure where
212      -- the path has a symlink in it.
213      local canonical = uv.cwd() .. '/' .. executable_name
214      local expected = exe(canonical)
215      local relative_executable = './' .. executable_name
216      local res = exe(relative_executable)
217
218      -- Don't test yet; we need to chdir back first.
219      uv.chdir(old_dir)
220      eq(expected, res)
221    end)
222  end)
223
224  describe('file permissions', function()
225    local function os_fchown(filename, user_id, group_id)
226      local fd = ffi.C.open(filename, 0)
227      local res = fs.os_fchown(fd, user_id, group_id)
228      ffi.C.close(fd)
229      return res
230    end
231
232    local function os_file_is_readable(filename)
233      return fs.os_file_is_readable((to_cstr(filename)))
234    end
235
236    local function os_file_is_writable(filename)
237      return fs.os_file_is_writable((to_cstr(filename)))
238    end
239
240    local function bit_set(number, check_bit)
241      return 0 ~= (bit.band(number, check_bit))
242    end
243
244    describe('os_getperm', function()
245      itp('returns UV_ENOENT when the given file does not exist', function()
246        eq(ffi.C.UV_ENOENT, (os_getperm('non-existing-file')))
247      end)
248
249      itp('returns a perm > 0 when given an existing file', function()
250        t.ok((os_getperm('unit-test-directory')) > 0)
251      end)
252
253      itp('returns S_IRUSR when the file is readable', function()
254        local perm = os_getperm('unit-test-directory')
255        t.ok((bit_set(perm, ffi.C.kS_IRUSR)))
256      end)
257    end)
258
259    describe('os_setperm', function()
260      itp('can set and unset the executable bit of a file', function()
261        local perm = os_getperm('unit-test-directory/test.file')
262        perm = unset_bit(perm, ffi.C.kS_IXUSR)
263        eq(OK, (os_setperm('unit-test-directory/test.file', perm)))
264        perm = os_getperm('unit-test-directory/test.file')
265        assert(not (bit_set(perm, ffi.C.kS_IXUSR)))
266        perm = set_bit(perm, ffi.C.kS_IXUSR)
267        eq(OK, os_setperm('unit-test-directory/test.file', perm))
268        perm = os_getperm('unit-test-directory/test.file')
269        t.ok((bit_set(perm, ffi.C.kS_IXUSR)))
270      end)
271
272      itp('fails if given file does not exist', function()
273        local perm = ffi.C.kS_IXUSR
274        eq(FAIL, (os_setperm('non-existing-file', perm)))
275      end)
276    end)
277
278    describe('os_fchown', function()
279      local filename = 'unit-test-directory/test.file'
280      itp('does not change owner and group if respective IDs are equal to -1', function()
281        local uid = uv.fs_stat(filename).uid
282        local gid = uv.fs_stat(filename).gid
283        eq(0, os_fchown(filename, -1ULL, -1ULL))
284        eq(uid, uv.fs_stat(filename).uid)
285        return eq(gid, uv.fs_stat(filename).gid)
286      end)
287
288      -- Some systems may not have `id` utility.
289      if os.execute('id -G > /dev/null 2>&1') ~= 0 then
290        pending('skipped (missing `id` utility)', function() end)
291      else
292        itp(
293          'owner of a file may change the group of the file to any group of which that owner is a member',
294          function()
295            local file_gid = uv.fs_stat(filename).gid
296
297            -- Gets ID of any group of which current user is a member except the
298            -- group that owns the file.
299            local id_fd = io.popen('id -G')
300            local new_gid = id_fd:read('*n')
301            if new_gid == file_gid then
302              new_gid = id_fd:read('*n')
303            end
304            id_fd:close()
305
306            -- User can be a member of only one group.
307            -- In that case we can not perform this test.
308            if new_gid then
309              eq(0, (os_fchown(filename, -1ULL, new_gid)))
310              eq(new_gid, uv.fs_stat(filename).gid)
311            end
312          end
313        )
314      end
315
316      if ffi.os == 'Windows' or ffi.C.geteuid() == 0 then
317        pending('skipped (uv_fs_chown is no-op on Windows)', function() end)
318      else
319        itp('returns nonzero if process has not enough permissions', function()
320          -- chown to root
321          neq(0, os_fchown(filename, 0, 0))
322        end)
323      end
324    end)
325
326    describe('os_file_is_readable', function()
327      itp('returns false if the file is not readable', function()
328        local perm = os_getperm('unit-test-directory/test.file')
329        perm = unset_bit(perm, ffi.C.kS_IRUSR)
330        perm = unset_bit(perm, ffi.C.kS_IRGRP)
331        perm = unset_bit(perm, ffi.C.kS_IROTH)
332        eq(OK, (os_setperm('unit-test-directory/test.file', perm)))
333        eq(false, os_file_is_readable('unit-test-directory/test.file'))
334      end)
335
336      itp('returns false if the file does not exist', function()
337        eq(false, os_file_is_readable('unit-test-directory/what_are_you_smoking.gif'))
338      end)
339
340      itp('returns true if the file is readable', function()
341        eq(true, os_file_is_readable('unit-test-directory/test.file'))
342      end)
343    end)
344
345    describe('os_file_is_writable', function()
346      itp('returns 0 if the file is readonly', function()
347        local perm = os_getperm('unit-test-directory/test.file')
348        perm = unset_bit(perm, ffi.C.kS_IWUSR)
349        perm = unset_bit(perm, ffi.C.kS_IWGRP)
350        perm = unset_bit(perm, ffi.C.kS_IWOTH)
351        eq(OK, (os_setperm('unit-test-directory/test.file', perm)))
352        eq(0, os_file_is_writable('unit-test-directory/test.file'))
353      end)
354
355      itp('returns 1 if the file is writable', function()
356        eq(1, os_file_is_writable('unit-test-directory/test.file'))
357      end)
358
359      itp('returns 2 when given a folder with rights to write into', function()
360        eq(2, os_file_is_writable('unit-test-directory'))
361      end)
362    end)
363  end)
364
365  describe('file operations', function()
366    local function os_path_exists(filename)
367      return fs.os_path_exists((to_cstr(filename)))
368    end
369    local function os_rename(path, new_path)
370      return fs.os_rename((to_cstr(path)), (to_cstr(new_path)))
371    end
372    local function os_remove(path)
373      return fs.os_remove((to_cstr(path)))
374    end
375    local function os_open(path, flags, mode)
376      return fs.os_open((to_cstr(path)), flags, mode)
377    end
378    local function os_close(fd)
379      return fs.os_close(fd)
380    end
381    -- For some reason if length of NUL-bytes-string is the same as `char[?]`
382    -- size luajit crashes. Though it does not do so in this test suite, better
383    -- be cautious and allocate more elements then needed. I only did this to
384    -- strings.
385    local function os_read(fd, size)
386      local buf = nil
387      if size == nil then
388        size = 0
389      else
390        buf = ffi.new('char[?]', size + 1, ('\0'):rep(size))
391      end
392      local eof = ffi.new('bool[?]', 1, { true })
393      local ret2 = fs.os_read(fd, eof, buf, size, false)
394      local ret1 = eof[0]
395      local ret3 = ''
396      if buf ~= nil then
397        ret3 = ffi.string(buf, size)
398      end
399      return ret1, ret2, ret3
400    end
401    local function os_readv(fd, sizes)
402      local bufs = {}
403      for i, size in ipairs(sizes) do
404        bufs[i] = {
405          iov_base = ffi.new('char[?]', size + 1, ('\0'):rep(size)),
406          iov_len = size,
407        }
408      end
409      local iov = ffi.new('struct iovec[?]', #sizes, bufs)
410      local eof = ffi.new('bool[?]', 1, { true })
411      local ret2 = fs.os_readv(fd, eof, iov, #sizes, false)
412      local ret1 = eof[0]
413      local ret3 = {}
414      for i = 1, #sizes do
415        -- Warning: iov may not be used.
416        ret3[i] = ffi.string(bufs[i].iov_base, bufs[i].iov_len)
417      end
418      return ret1, ret2, ret3
419    end
420    local function os_write(fd, data)
421      return fs.os_write(fd, data, data and #data or 0, false)
422    end
423
424    describe('os_path_exists', function()
425      itp('returns false when given a non-existing file', function()
426        eq(false, (os_path_exists('non-existing-file')))
427      end)
428
429      itp('returns true when given an existing file', function()
430        eq(true, (os_path_exists('unit-test-directory/test.file')))
431      end)
432
433      itp('returns false when given a broken symlink', function()
434        eq(false, (os_path_exists('unit-test-directory/test_broken_link.file')))
435      end)
436
437      itp('returns true when given a directory', function()
438        eq(true, (os_path_exists('unit-test-directory')))
439      end)
440    end)
441
442    describe('os_rename', function()
443      local test = 'unit-test-directory/test.file'
444      local not_exist = 'unit-test-directory/not_exist.file'
445
446      itp('can rename file if destination file does not exist', function()
447        eq(OK, (os_rename(test, not_exist)))
448        eq(false, (os_path_exists(test)))
449        eq(true, (os_path_exists(not_exist)))
450        eq(OK, (os_rename(not_exist, test))) -- restore test file
451      end)
452
453      itp('fail if source file does not exist', function()
454        eq(FAIL, (os_rename(not_exist, test)))
455      end)
456
457      itp('can overwrite destination file if it exists', function()
458        local other = 'unit-test-directory/other.file'
459        local file = io.open(other, 'w')
460        file:write('other')
461        file:flush()
462        file:close()
463
464        eq(OK, (os_rename(other, test)))
465        eq(false, (os_path_exists(other)))
466        eq(true, (os_path_exists(test)))
467        file = io.open(test, 'r')
468        eq('other', (file:read('*all')))
469        file:close()
470      end)
471    end)
472
473    describe('os_remove', function()
474      before_each(function()
475        io.open('unit-test-directory/test_remove.file', 'w'):close()
476      end)
477
478      after_each(function()
479        os.remove('unit-test-directory/test_remove.file')
480      end)
481
482      itp('returns non-zero when given a non-existing file', function()
483        neq(0, (os_remove('non-existing-file')))
484      end)
485
486      itp('removes the given file and returns 0', function()
487        local f = 'unit-test-directory/test_remove.file'
488        assert_file_exists(f)
489        eq(0, (os_remove(f)))
490        assert_file_does_not_exist(f)
491      end)
492    end)
493
494    describe('os_dup', function()
495      itp('returns new file descriptor', function()
496        local dup0 = fs.os_dup(0)
497        local dup1 = fs.os_dup(1)
498        local dup2 = fs.os_dup(2)
499        local tbl = {
500          [0] = true,
501          [1] = true,
502          [2] = true,
503          [tonumber(dup0)] = true,
504          [tonumber(dup1)] = true,
505          [tonumber(dup2)] = true,
506        }
507        local i = 0
508        for _, _ in pairs(tbl) do
509          i = i + 1
510        end
511        eq(6, i) -- All fds must be unique
512      end)
513    end)
514
515    describe('os_open', function()
516      local new_file = 'test_new_file'
517      local existing_file = 'unit-test-directory/test_existing.file'
518
519      before_each(function()
520        (io.open(existing_file, 'w')):close()
521      end)
522
523      after_each(function()
524        os.remove(existing_file)
525        os.remove(new_file)
526      end)
527
528      itp('returns UV_ENOENT for O_RDWR on a non-existing file', function()
529        eq(ffi.C.UV_ENOENT, (os_open('non-existing-file', ffi.C.kO_RDWR, 0)))
530      end)
531
532      itp(
533        'returns non-negative for O_CREAT on a non-existing file which then can be closed',
534        function()
535          assert_file_does_not_exist(new_file)
536          local fd = os_open(new_file, ffi.C.kO_CREAT, 0)
537          t.ok(0 <= fd)
538          eq(0, os_close(fd))
539        end
540      )
541
542      itp('returns non-negative for O_CREAT on a existing file which then can be closed', function()
543        assert_file_exists(existing_file)
544        local fd = os_open(existing_file, ffi.C.kO_CREAT, 0)
545        t.ok(0 <= fd)
546        eq(0, os_close(fd))
547      end)
548
549      itp('returns UV_EEXIST for O_CREAT|O_EXCL on a existing file', function()
550        assert_file_exists(existing_file)
551        eq(ffi.C.UV_EEXIST, (os_open(existing_file, (bit.bor(ffi.C.kO_CREAT, ffi.C.kO_EXCL)), 0)))
552      end)
553
554      itp('sets `rwx` permissions for O_CREAT 700 which then can be closed', function()
555        assert_file_does_not_exist(new_file)
556        --create the file
557        local fd = os_open(new_file, ffi.C.kO_CREAT, tonumber('700', 8))
558        --verify permissions
559        eq(33216, uv.fs_stat(new_file).mode)
560        eq(0, os_close(fd))
561      end)
562
563      itp('sets `rw` permissions for O_CREAT 600 which then can be closed', function()
564        assert_file_does_not_exist(new_file)
565        --create the file
566        local fd = os_open(new_file, ffi.C.kO_CREAT, tonumber('600', 8))
567        --verify permissions
568        eq(33152, uv.fs_stat(new_file).mode)
569        eq(0, os_close(fd))
570      end)
571
572      itp(
573        'returns a non-negative file descriptor for an existing file which then can be closed',
574        function()
575          local fd = os_open(existing_file, ffi.C.kO_RDWR, 0)
576          t.ok(0 <= fd)
577          eq(0, os_close(fd))
578        end
579      )
580    end)
581
582    describe('os_close', function()
583      itp('returns EBADF for negative file descriptors', function()
584        eq(ffi.C.UV_EBADF, os_close(-1))
585        eq(ffi.C.UV_EBADF, os_close(-1000))
586      end)
587    end)
588
589    describe('os_read', function()
590      local file = 'test-unit-os-fs_spec-os_read.dat'
591
592      before_each(function()
593        local f = io.open(file, 'w')
594        f:write(fcontents)
595        f:close()
596      end)
597
598      after_each(function()
599        os.remove(file)
600      end)
601
602      itp('can read zero bytes from a file', function()
603        local fd = os_open(file, ffi.C.kO_RDONLY, 0)
604        ok(fd >= 0)
605        eq({ false, 0, '' }, { os_read(fd, nil) })
606        eq({ false, 0, '' }, { os_read(fd, 0) })
607        eq(0, os_close(fd))
608      end)
609
610      itp('can read from a file multiple times', function()
611        local fd = os_open(file, ffi.C.kO_RDONLY, 0)
612        ok(fd >= 0)
613        eq({ false, 2, '\000\001' }, { os_read(fd, 2) })
614        eq({ false, 2, '\002\003' }, { os_read(fd, 2) })
615        eq(0, os_close(fd))
616      end)
617
618      itp('can read the whole file at once and then report eof', function()
619        local fd = os_open(file, ffi.C.kO_RDONLY, 0)
620        ok(fd >= 0)
621        eq({ false, #fcontents, fcontents }, { os_read(fd, #fcontents) })
622        eq({ true, 0, ('\0'):rep(#fcontents) }, { os_read(fd, #fcontents) })
623        eq(0, os_close(fd))
624      end)
625
626      itp('can read the whole file in two calls, one partially', function()
627        local fd = os_open(file, ffi.C.kO_RDONLY, 0)
628        ok(fd >= 0)
629        eq(
630          { false, #fcontents * 3 / 4, fcontents:sub(1, #fcontents * 3 / 4) },
631          { os_read(fd, #fcontents * 3 / 4) }
632        )
633        eq({
634          true,
635          (#fcontents * 1 / 4),
636          fcontents:sub(#fcontents * 3 / 4 + 1) .. ('\0'):rep(#fcontents * 2 / 4),
637        }, { os_read(fd, #fcontents * 3 / 4) })
638        eq(0, os_close(fd))
639      end)
640    end)
641
642    describe('os_readv', function()
643      -- Function may be absent
644      if not pcall(function()
645        return fs.os_readv
646      end) then
647        return
648      end
649      local file = 'test-unit-os-fs_spec-os_readv.dat'
650
651      before_each(function()
652        local f = io.open(file, 'w')
653        f:write(fcontents)
654        f:close()
655      end)
656
657      after_each(function()
658        os.remove(file)
659      end)
660
661      itp('can read zero bytes from a file', function()
662        local fd = os_open(file, ffi.C.kO_RDONLY, 0)
663        ok(fd >= 0)
664        eq({ false, 0, {} }, { os_readv(fd, {}) })
665        eq({ false, 0, { '', '', '' } }, { os_readv(fd, { 0, 0, 0 }) })
666        eq(0, os_close(fd))
667      end)
668
669      itp('can read from a file multiple times to a differently-sized buffers', function()
670        local fd = os_open(file, ffi.C.kO_RDONLY, 0)
671        ok(fd >= 0)
672        eq({ false, 2, { '\000\001' } }, { os_readv(fd, { 2 }) })
673        eq({ false, 5, { '\002\003', '\004\005\006' } }, { os_readv(fd, { 2, 3 }) })
674        eq(0, os_close(fd))
675      end)
676
677      itp('can read the whole file at once and then report eof', function()
678        local fd = os_open(file, ffi.C.kO_RDONLY, 0)
679        ok(fd >= 0)
680        eq({
681          false,
682          #fcontents,
683          {
684            fcontents:sub(1, #fcontents * 1 / 4),
685            fcontents:sub(#fcontents * 1 / 4 + 1, #fcontents * 3 / 4),
686            fcontents:sub(#fcontents * 3 / 4 + 1, #fcontents * 15 / 16),
687            fcontents:sub(#fcontents * 15 / 16 + 1, #fcontents),
688          },
689        }, {
690          os_readv(
691            fd,
692            { #fcontents * 1 / 4, #fcontents * 2 / 4, #fcontents * 3 / 16, #fcontents * 1 / 16 }
693          ),
694        })
695        eq({ true, 0, { '\0' } }, { os_readv(fd, { 1 }) })
696        eq(0, os_close(fd))
697      end)
698
699      itp('can read the whole file in two calls, one partially', function()
700        local fd = os_open(file, ffi.C.kO_RDONLY, 0)
701        ok(fd >= 0)
702        eq(
703          { false, #fcontents * 3 / 4, { fcontents:sub(1, #fcontents * 3 / 4) } },
704          { os_readv(fd, { #fcontents * 3 / 4 }) }
705        )
706        eq({
707          true,
708          (#fcontents * 1 / 4),
709          { fcontents:sub(#fcontents * 3 / 4 + 1) .. ('\0'):rep(#fcontents * 2 / 4) },
710        }, { os_readv(fd, { #fcontents * 3 / 4 }) })
711        eq(0, os_close(fd))
712      end)
713    end)
714
715    describe('os_write', function()
716      -- Function may be absent
717      local file = 'test-unit-os-fs_spec-os_write.dat'
718
719      before_each(function()
720        local f = io.open(file, 'w')
721        f:write(fcontents)
722        f:close()
723      end)
724
725      after_each(function()
726        os.remove(file)
727      end)
728
729      itp('can write zero bytes to a file', function()
730        local fd = os_open(file, ffi.C.kO_WRONLY, 0)
731        ok(fd >= 0)
732        eq(0, os_write(fd, ''))
733        eq(0, os_write(fd, nil))
734        eq(fcontents, io.open(file, 'r'):read('*a'))
735        eq(0, os_close(fd))
736      end)
737
738      itp('can write some data to a file', function()
739        local fd = os_open(file, ffi.C.kO_WRONLY, 0)
740        ok(fd >= 0)
741        eq(3, os_write(fd, 'abc'))
742        eq(4, os_write(fd, ' def'))
743        eq('abc def' .. fcontents:sub(8), io.open(file, 'r'):read('*a'))
744        eq(0, os_close(fd))
745      end)
746    end)
747
748    describe('os_nodetype', function()
749      before_each(function()
750        os.remove('non-existing-file')
751      end)
752
753      itp('returns NODE_NORMAL for non-existing file', function()
754        eq(NODE_NORMAL, fs.os_nodetype(to_cstr('non-existing-file')))
755      end)
756
757      itp('returns NODE_WRITABLE for /dev/stderr', function()
758        eq(NODE_WRITABLE, fs.os_nodetype(to_cstr('/dev/stderr')))
759      end)
760    end)
761  end)
762
763  describe('folder operations', function()
764    local function os_mkdir(path, mode)
765      return fs.os_mkdir(to_cstr(path), mode)
766    end
767
768    local function os_rmdir(path)
769      return fs.os_rmdir(to_cstr(path))
770    end
771
772    local function os_mkdir_recurse(path, mode)
773      local failed_str = ffi.new('char *[1]', { nil })
774      local created_str = ffi.new('char *[1]', { nil })
775      local ret = fs.os_mkdir_recurse(path, mode, failed_str, created_str)
776      local failed_dir = failed_str[0]
777      if failed_dir ~= nil then
778        failed_dir = ffi.string(failed_dir)
779      end
780      local created_dir = created_str[0]
781      if created_dir ~= nil then
782        created_dir = ffi.string(created_dir)
783      end
784      return ret, failed_dir, created_dir
785    end
786
787    describe('os_mkdir', function()
788      itp('returns non-zero when given an already existing directory', function()
789        local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
790        neq(0, (os_mkdir('unit-test-directory', mode)))
791      end)
792
793      itp('creates a directory and returns 0', function()
794        local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
795        eq(false, (os_isdir('unit-test-directory/new-dir')))
796        eq(0, (os_mkdir('unit-test-directory/new-dir', mode)))
797        eq(true, (os_isdir('unit-test-directory/new-dir')))
798        uv.fs_rmdir('unit-test-directory/new-dir')
799      end)
800    end)
801
802    describe('os_mkdir_recurse', function()
803      itp('returns zero when given an already existing directory', function()
804        local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
805        local ret, failed_dir, created_dir = os_mkdir_recurse('unit-test-directory', mode)
806        eq(0, ret)
807        eq(nil, failed_dir)
808        eq(nil, created_dir)
809      end)
810
811      itp('fails to create a directory where there is a file', function()
812        local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
813        local ret, failed_dir, created_dir = os_mkdir_recurse('unit-test-directory/test.file', mode)
814        neq(0, ret)
815        eq('unit-test-directory/test.file', failed_dir)
816        eq(nil, created_dir)
817      end)
818
819      itp('fails to create a directory where there is a file in path', function()
820        local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
821        local ret, failed_dir, created_dir =
822          os_mkdir_recurse('unit-test-directory/test.file/test', mode)
823        neq(0, ret)
824        eq('unit-test-directory/test.file', failed_dir)
825        eq(nil, created_dir)
826      end)
827
828      itp('succeeds to create a directory', function()
829        local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
830        local ret, failed_dir, created_dir =
831          os_mkdir_recurse('unit-test-directory/new-dir-recurse', mode)
832        eq(0, ret)
833        eq(nil, failed_dir)
834        ok(endswith(created_dir, 'unit-test-directory/new-dir-recurse'))
835        eq(true, os_isdir('unit-test-directory/new-dir-recurse'))
836        uv.fs_rmdir('unit-test-directory/new-dir-recurse')
837        eq(false, os_isdir('unit-test-directory/new-dir-recurse'))
838      end)
839
840      itp('succeeds to create a directory ending with ///', function()
841        local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
842        local ret, failed_dir, created_dir =
843          os_mkdir_recurse('unit-test-directory/new-dir-recurse///', mode)
844        eq(0, ret)
845        eq(nil, failed_dir)
846        ok(endswith(created_dir, 'unit-test-directory/new-dir-recurse'))
847        eq(true, os_isdir('unit-test-directory/new-dir-recurse'))
848        uv.fs_rmdir('unit-test-directory/new-dir-recurse')
849        eq(false, os_isdir('unit-test-directory/new-dir-recurse'))
850      end)
851
852      itp('succeeds to create a directory ending with /', function()
853        local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
854        local ret, failed_dir, created_dir =
855          os_mkdir_recurse('unit-test-directory/new-dir-recurse/', mode)
856        eq(0, ret)
857        eq(nil, failed_dir)
858        ok(endswith(created_dir, 'unit-test-directory/new-dir-recurse'))
859        eq(true, os_isdir('unit-test-directory/new-dir-recurse'))
860        uv.fs_rmdir('unit-test-directory/new-dir-recurse')
861        eq(false, os_isdir('unit-test-directory/new-dir-recurse'))
862      end)
863
864      itp('succeeds to create a directory tree', function()
865        local mode = ffi.C.kS_IRUSR + ffi.C.kS_IWUSR + ffi.C.kS_IXUSR
866        local ret, failed_dir, created_dir =
867          os_mkdir_recurse('unit-test-directory/new-dir-recurse/1/2/3', mode)
868        eq(0, ret)
869        eq(nil, failed_dir)
870        ok(endswith(created_dir, 'unit-test-directory/new-dir-recurse'))
871        eq(true, os_isdir('unit-test-directory/new-dir-recurse'))
872        eq(true, os_isdir('unit-test-directory/new-dir-recurse/1'))
873        eq(true, os_isdir('unit-test-directory/new-dir-recurse/1/2'))
874        eq(true, os_isdir('unit-test-directory/new-dir-recurse/1/2/3'))
875        uv.fs_rmdir('unit-test-directory/new-dir-recurse/1/2/3')
876        uv.fs_rmdir('unit-test-directory/new-dir-recurse/1/2')
877        uv.fs_rmdir('unit-test-directory/new-dir-recurse/1')
878        uv.fs_rmdir('unit-test-directory/new-dir-recurse')
879        eq(false, os_isdir('unit-test-directory/new-dir-recurse'))
880      end)
881    end)
882
883    describe('os_rmdir', function()
884      itp('returns non_zero when given a non-existing directory', function()
885        neq(0, (os_rmdir('non-existing-directory')))
886      end)
887
888      itp('removes the given directory and returns 0', function()
889        mkdir('unit-test-directory/new-dir')
890        eq(0, os_rmdir('unit-test-directory/new-dir'))
891        eq(false, (os_isdir('unit-test-directory/new-dir')))
892      end)
893    end)
894  end)
895
896  describe('FileInfo', function()
897    local function file_info_new()
898      local info = ffi.new('FileInfo[1]')
899      info[0].stat.st_ino = 0
900      info[0].stat.st_dev = 0
901      return info
902    end
903
904    -- Returns true if the FileInfo object has non-empty fields.
905    local function has_fileinfo(info)
906      return info[0].stat.st_ino > 0 and info[0].stat.st_dev > 0
907    end
908
909    local function file_id_new()
910      local info = ffi.new('FileID[1]')
911      info[0].inode = 0
912      info[0].device_id = 0
913      return info
914    end
915
916    describe('os_fileinfo', function()
917      itp('returns false if path=NULL', function()
918        local info = file_info_new()
919        assert(not (fs.os_fileinfo(nil, info)))
920      end)
921
922      itp('returns false if given a non-existing file', function()
923        local info = file_info_new()
924        assert(not (fs.os_fileinfo('/non-existent', info)))
925      end)
926
927      itp('returns true if given an existing file and fills FileInfo', function()
928        local info = file_info_new()
929        local path = 'unit-test-directory/test.file'
930        t.ok((fs.os_fileinfo(path, info)))
931        t.ok((has_fileinfo(info)))
932      end)
933
934      itp('returns the FileInfo of the linked file, not the link', function()
935        local info = file_info_new()
936        local path = 'unit-test-directory/test_link.file'
937        t.ok((fs.os_fileinfo(path, info)))
938        t.ok((has_fileinfo(info)))
939        local mode = tonumber(info[0].stat.st_mode)
940        return eq(ffi.C.kS_IFREG, (bit.band(mode, ffi.C.kS_IFMT)))
941      end)
942    end)
943
944    describe('os_fileinfo_link', function()
945      itp('returns false for non-existing file', function()
946        local info = file_info_new()
947        assert(not (fs.os_fileinfo_link('/non-existent', info)))
948      end)
949
950      itp('returns true and fills FileInfo for existing file', function()
951        local info = file_info_new()
952        local path = 'unit-test-directory/test.file'
953        t.ok((fs.os_fileinfo_link(path, info)))
954        t.ok((has_fileinfo(info)))
955      end)
956
957      itp('returns FileInfo of the link, not its target', function()
958        local info = file_info_new()
959        local link = 'unit-test-directory/test_link.file'
960        t.ok((fs.os_fileinfo_link(link, info)))
961        t.ok((has_fileinfo(info)))
962        local mode = tonumber(info[0].stat.st_mode)
963        eq(ffi.C.kS_IFLNK, (bit.band(mode, ffi.C.kS_IFMT)))
964      end)
965    end)
966
967    describe('os_fileinfo_fd', function()
968      itp('returns false if given an invalid file descriptor', function()
969        local info = file_info_new()
970        assert(not (fs.os_fileinfo_fd(-1, info)))
971      end)
972
973      itp('returns true if given a file descriptor and fills FileInfo', function()
974        local info = file_info_new()
975        local path = 'unit-test-directory/test.file'
976        local fd = ffi.C.open(path, 0)
977        t.ok((fs.os_fileinfo_fd(fd, info)))
978        t.ok((has_fileinfo(info)))
979        ffi.C.close(fd)
980      end)
981    end)
982
983    describe('os_fileinfo_id_equal', function()
984      itp('returns false if file infos represent different files', function()
985        local file_info_1 = file_info_new()
986        local file_info_2 = file_info_new()
987        local path_1 = 'unit-test-directory/test.file'
988        local path_2 = 'unit-test-directory/test_2.file'
989        t.ok((fs.os_fileinfo(path_1, file_info_1)))
990        t.ok((fs.os_fileinfo(path_2, file_info_2)))
991        assert(not (fs.os_fileinfo_id_equal(file_info_1, file_info_2)))
992      end)
993
994      itp('returns true if file infos represent the same file', function()
995        local file_info_1 = file_info_new()
996        local file_info_2 = file_info_new()
997        local path = 'unit-test-directory/test.file'
998        t.ok((fs.os_fileinfo(path, file_info_1)))
999        t.ok((fs.os_fileinfo(path, file_info_2)))
1000        t.ok((fs.os_fileinfo_id_equal(file_info_1, file_info_2)))
1001      end)
1002
1003      itp('returns true if file infos represent the same file (symlink)', function()
1004        local file_info_1 = file_info_new()
1005        local file_info_2 = file_info_new()
1006        local path_1 = 'unit-test-directory/test.file'
1007        local path_2 = 'unit-test-directory/test_link.file'
1008        t.ok((fs.os_fileinfo(path_1, file_info_1)))
1009        t.ok((fs.os_fileinfo(path_2, file_info_2)))
1010        t.ok((fs.os_fileinfo_id_equal(file_info_1, file_info_2)))
1011      end)
1012    end)
1013
1014    describe('os_fileinfo_id', function()
1015      itp('extracts ino/dev from FileInfo into file_id', function()
1016        local info = file_info_new()
1017        local file_id = file_id_new()
1018        local path = 'unit-test-directory/test.file'
1019        t.ok((fs.os_fileinfo(path, info)))
1020        fs.os_fileinfo_id(info, file_id)
1021        eq(info[0].stat.st_ino, file_id[0].inode)
1022        eq(info[0].stat.st_dev, file_id[0].device_id)
1023      end)
1024    end)
1025
1026    describe('os_fileinfo_inode', function()
1027      itp('returns the inode from FileInfo', function()
1028        local info = file_info_new()
1029        local path = 'unit-test-directory/test.file'
1030        t.ok((fs.os_fileinfo(path, info)))
1031        local inode = fs.os_fileinfo_inode(info)
1032        eq(info[0].stat.st_ino, inode)
1033      end)
1034    end)
1035
1036    describe('os_fileinfo_size', function()
1037      itp('returns the correct size of a file', function()
1038        local path = 'unit-test-directory/test.file'
1039        local file = io.open(path, 'w')
1040        file:write('some bytes to get filesize != 0')
1041        file:flush()
1042        file:close()
1043        local size = uv.fs_stat(path).size
1044        local info = file_info_new()
1045        t.ok(fs.os_fileinfo(path, info))
1046        eq(size, fs.os_fileinfo_size(info))
1047      end)
1048    end)
1049
1050    describe('os_fileinfo_hardlinks', function()
1051      itp('returns the correct number of hardlinks', function()
1052        local path = 'unit-test-directory/test.file'
1053        local path_link = 'unit-test-directory/test_hlink.file'
1054        local info = file_info_new()
1055        t.ok(fs.os_fileinfo(path, info))
1056        eq(1, fs.os_fileinfo_hardlinks(info))
1057        uv.fs_link(path, path_link)
1058        t.ok(fs.os_fileinfo(path, info))
1059        eq(2, fs.os_fileinfo_hardlinks(info))
1060      end)
1061    end)
1062
1063    describe('os_fileinfo_blocksize', function()
1064      itp('returns the correct blocksize of a file', function()
1065        local path = 'unit-test-directory/test.file'
1066        local blksize = uv.fs_stat(path).blksize
1067        local info = file_info_new()
1068        t.ok(fs.os_fileinfo(path, info))
1069        if blksize then
1070          eq(blksize, fs.os_fileinfo_blocksize(info))
1071        else
1072          -- luafs doesn't support blksize on windows
1073          -- libuv on windows returns a constant value as blocksize
1074          -- checking for this constant value should be enough
1075          eq(2048, fs.os_fileinfo_blocksize(info))
1076        end
1077      end)
1078    end)
1079
1080    describe('os_fileid', function()
1081      itp('returns false if given an non-existing file', function()
1082        local file_id = file_id_new()
1083        assert(not (fs.os_fileid('/non-existent', file_id)))
1084      end)
1085
1086      itp('returns true if given an existing file and fills file_id', function()
1087        local file_id = file_id_new()
1088        local path = 'unit-test-directory/test.file'
1089        t.ok((fs.os_fileid(path, file_id)))
1090        t.ok(0 < file_id[0].inode)
1091        t.ok(0 < file_id[0].device_id)
1092      end)
1093    end)
1094
1095    describe('os_fileid_equal', function()
1096      itp('returns true if two FileIDs are equal', function()
1097        local file_id = file_id_new()
1098        local path = 'unit-test-directory/test.file'
1099        t.ok((fs.os_fileid(path, file_id)))
1100        t.ok((fs.os_fileid_equal(file_id, file_id)))
1101      end)
1102
1103      itp('returns false if two FileIDs are not equal', function()
1104        local file_id_1 = file_id_new()
1105        local file_id_2 = file_id_new()
1106        local path_1 = 'unit-test-directory/test.file'
1107        local path_2 = 'unit-test-directory/test_2.file'
1108        t.ok((fs.os_fileid(path_1, file_id_1)))
1109        t.ok((fs.os_fileid(path_2, file_id_2)))
1110        assert(not (fs.os_fileid_equal(file_id_1, file_id_2)))
1111      end)
1112    end)
1113  end)
1114end)