fs_spec.lua 38 KB

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