fileio_spec.lua 11 KB

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