-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathtest-fs.lua
444 lines (400 loc) · 13 KB
/
test-fs.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
return require('lib/tap')(function (test)
test("read a file sync", function (print, p, expect, uv)
local fd = assert(uv.fs_open('README.md', 'r', tonumber('644', 8)))
p{fd=fd}
local stat = assert(uv.fs_fstat(fd))
p{stat=stat}
local chunk = assert(uv.fs_read(fd, stat.size, 0))
assert(#chunk == stat.size)
assert(uv.fs_close(fd))
end)
test("read a file sync in chunks", function (print, p, expect, uv)
local fd = assert(uv.fs_open('README.md', 'r', tonumber('644', 8)))
local stat = assert(uv.fs_fstat(fd))
local chunks = {}
local numchunks = 8
local chunksize = math.ceil(stat.size/numchunks)
while true do
local chunk, err = uv.fs_read(fd, chunksize)
assert(not err, err)
if #chunk == 0 then
break
end
table.insert(chunks, chunk)
end
assert(#chunks == numchunks)
assert(#table.concat(chunks) == stat.size)
assert(uv.fs_close(fd))
end)
test("read a file async", function (print, p, expect, uv)
uv.fs_open('README.md', 'r', tonumber('644', 8), expect(function (err, fd)
assert(not err, err)
p{fd=fd}
uv.fs_fstat(fd, expect(function (err, stat)
assert(not err, err)
p{stat=stat}
uv.fs_read(fd, stat.size, 0, expect(function (err, chunk)
assert(not err, err)
p{chunk=#chunk}
assert(#chunk == stat.size)
uv.fs_close(fd, expect(function (err)
assert(not err, err)
end))
end))
end))
end))
end)
test("fs.write", function (print, p, expect, uv)
local path = "_test_"
local fd = assert(uv.fs_open(path, "w", 438))
-- a mix of async and sync
uv.fs_write(fd, "Hello World\n", expect(function(err, n)
assert(not err, err)
assert(uv.fs_write(fd, {"with\n", "more\n", "lines\n"}))
assert(uv.fs_close(fd))
assert(uv.fs_unlink(path))
end))
end)
-- collect garbage after uv.fs_write but before the write callback
-- is called in order to potentially garbage collect the strings that
-- are being sent. See https://github.com/luvit/luv/issues/397
test("fs.write data refs", function (print, p, expect, uv)
local path = "_test_"
local fd = assert(uv.fs_open(path, "w+", tonumber("0666", 8)))
do
-- the number here gets coerced into a string
local t = {"with", 600, "lines"}
uv.fs_write(fd, t, function()
local expectedContents = table.concat(t)
local stat = assert(uv.fs_fstat(fd))
assert(stat.size == #expectedContents)
local chunk = assert(uv.fs_read(fd, stat.size, 0))
assert(chunk == expectedContents)
assert(uv.fs_close(fd))
assert(uv.fs_unlink(path))
end)
end
local count = collectgarbage("count")
collectgarbage("collect")
assert(count - collectgarbage("count") > 0)
end)
test("fs.stat sync", function (print, p, expect, uv)
local stat = assert(uv.fs_stat("README.md"))
assert(stat.size)
end)
test("fs.stat async", function (print, p, expect, uv)
assert(uv.fs_stat("README.md", expect(function (err, stat)
assert(not err, err)
assert(stat.size)
end)))
end)
test("fs.stat sync error", function (print, p, expect, uv)
local stat, err, code = uv.fs_stat("BAD_FILE!")
p{err=err,code=code,stat=stat}
assert(not stat)
assert(err)
assert(code == "ENOENT")
end)
test("fs.stat async error", function (print, p, expect, uv)
assert(uv.fs_stat("BAD_FILE@", expect(function (err, stat)
p{err=err,stat=stat}
assert(err)
assert(not stat)
end)))
end)
test("fs.scandir", function (print, p, expect, uv)
local req = uv.fs_scandir('.')
local function iter()
return uv.fs_scandir_next(req)
end
for name, ftype in iter do
p{name=name, ftype=ftype}
assert(name)
-- ftype is not available in all filesystems; for example it's
-- provided for HFS+ (OSX), NTFS (Windows) but not for ext4 (Linux).
end
end)
test("fs.scandir sync error", function (print, p, expect, uv)
local req, err, code = uv.fs_scandir('BAD_FILE!')
p{err=err,code=code,req=req}
assert(not req)
assert(err)
assert(code == "ENOENT")
end)
test("fs.scandir async error", function (print, p, expect, uv)
local _req, _err = uv.fs_scandir('BAD_FILE!', expect(function(err, req)
p{err=err,req=req}
assert(not req)
assert(err)
end))
-- Note: when using the async version, the initial return only errors
-- if there is an error when setting up the internal Libuv call
-- (e.g. if there's an out-of-memory error when copying the path).
-- So even though the callback will have an error, the initial call
-- should return a valid uv_fs_t userdata without an error.
assert(_req)
assert(not _err)
end)
test("fs.scandir async", function (print, p, expect, uv)
assert(uv.fs_scandir('.', function(err, req)
assert(not err)
local function iter()
return uv.fs_scandir_next(req)
end
for name, ftype in iter do
p{name=name, ftype=ftype}
assert(name)
-- ftype is not available in all filesystems; for example it's
-- provided for HFS+ (OSX), NTFS (Windows) but not for ext4 (Linux).
end
end))
end)
-- this test does nothing on its own, but when run with a leak checker,
-- it will check that the memory allocated by Libuv for req is cleaned up
-- even if its not iterated fully (or at all)
test("fs.scandir with no iteration", function(print, p, expect, uv)
local req = uv.fs_scandir('.')
assert(req)
end)
-- this previously hit a use-after-free
-- see https://github.com/luvit/luv/pull/696
test("fs.scandir given to new_work", function(print, p, expect, uv)
local req = assert(uv.fs_scandir('.'))
local work
work = assert(uv.new_work(function(_entries)
local _uv = require('luv')
while true do
if not _uv.fs_scandir_next(_entries) then break end
end
end, function() end))
work:queue(req)
end)
test("fs.realpath", function (print, p, expect, uv)
p(assert(uv.fs_realpath('.')))
assert(uv.fs_realpath('.', expect(function (err, path)
assert(not err, err)
p(path)
end)))
end, "1.8.0")
test("fs.copyfile", function (print, p, expect, uv)
local path = "_test_"
local path2 = "_test2_"
local fd = assert(uv.fs_open(path, "w", 438))
uv.fs_write(fd, "Hello World\n", -1)
uv.fs_close(fd)
assert(uv.fs_copyfile(path, path2))
assert(uv.fs_unlink(path))
assert(uv.fs_unlink(path2))
end, "1.14.0")
test("fs.{open,read,close}dir object sync #1", function(print, p, expect, uv)
local dir = assert(uv.fs_opendir('.'))
repeat
local dirent = dir:readdir()
if dirent then
assert(#dirent==1)
p(dirent)
end
until not dirent
assert(dir:closedir()==true)
end, "1.28.0")
test("fs.{open,read,close}dir object sync #2", function(print, p, expect, uv)
local dir = assert(uv.fs_opendir('.'))
repeat
local dirent = dir:readdir()
if dirent then
assert(#dirent==1)
p(dirent)
end
until not dirent
dir:closedir(function(err, state)
assert(err==nil)
assert(state==true)
assert(tostring(dir):match("^uv_dir_t"))
print(dir, 'closed')
end)
end, "1.28.0")
test("fs.{open,read,close}dir sync one entry", function(print, p, expect, uv)
local dir = assert(uv.fs_opendir('.'))
repeat
local dirent = uv.fs_readdir(dir)
if dirent then
assert(#dirent==1)
p(dirent)
end
until not dirent
assert(uv.fs_closedir(dir)==true)
end, "1.28.0")
test("fs.{open,read,close}dir sync more entry", function(print, p, expect, uv)
local dir = assert(uv.fs_opendir('.', nil, 50))
repeat
local dirent = uv.fs_readdir(dir)
if dirent then p(dirent) end
until not dirent
assert(uv.fs_closedir(dir)==true)
end, "1.28.0")
test("fs.{open,read,close}dir with more entry", function(print, p, expect, uv)
local function opendir_cb(err, dir)
assert(not err)
local function readdir_cb(err, dirs)
assert(not err)
if dirs then
p(dirs)
uv.fs_readdir(dir, readdir_cb)
else
assert(uv.fs_closedir(dir)==true)
end
end
uv.fs_readdir(dir, readdir_cb)
end
assert(uv.fs_opendir('.', opendir_cb, 50))
end, "1.28.0")
test("fs.opendir and fs.closedir in a loop", function(print, p, expect, uv)
-- Previously, this triggered a GC/closedir race condition
-- see https://github.com/luvit/luv/issues/597
for _ = 1,1000 do
local dir, err = uv.fs_opendir('.', nil, 64)
if not err then
uv.fs_closedir(dir)
end
end
end, "1.28.0")
test("fs.{open,read,close}dir ref check", function(print, p, expect, uv)
local dir = assert(uv.fs_opendir('.', nil, 50))
local function readdir_cb(err, dirs)
assert(not err)
if dirs then
p(dirs)
end
end
uv.fs_readdir(dir, readdir_cb)
dir = nil
collectgarbage()
collectgarbage()
collectgarbage()
end, "1.28.0")
test("fs.statfs sync", function (print, p, expect, uv)
local stat = assert(uv.fs_statfs("."))
p(stat)
assert(stat.bavail>0)
end, "1.31.0")
test("fs.statfs async", function (print, p, expect, uv)
assert(uv.fs_statfs(".", expect(function (err, stat)
assert(not err, err)
p(stat)
assert(stat.bavail>0)
end)))
end, "1.31.0")
test("fs.statfs sync error", function (print, p, expect, uv)
local stat, err, code = uv.fs_statfs("BAD_FILE!")
p{err=err,code=code,stat=stat}
assert(not stat)
assert(err)
assert(code == "ENOENT")
end, "1.31.0")
test("fs.statfs async error", function (print, p, expect, uv)
assert(uv.fs_statfs("BAD_FILE@", expect(function (err, stat)
p{err=err,stat=stat}
assert(err)
assert(not stat)
end)))
end, "1.31.0")
test("fs.mkdtemp async", function(print, p, expect, uv)
local tp = "luvXXXXXX"
uv.fs_mkdtemp(tp, function(err, path)
assert(not err)
assert(path:match("^luv......"))
assert(uv.fs_rmdir(path))
end)
end)
test("fs.mkdtemp sync", function(print, p, expect, uv)
local tp = "luvXXXXXX"
local path, err, code = uv.fs_mkdtemp(tp)
assert(path:match("^luv......"))
assert(uv.fs_rmdir(path))
end)
test("fs.mkdtemp async error", function(print, p, expect, uv)
local tp = "luvXXXXXZ"
uv.fs_mkdtemp(tp, function(err, path)
-- Will success on MacOS
if not err then
assert(path:match("^luv......"))
assert(uv.fs_rmdir(path))
else
assert(err:match("^EINVAL:"))
assert(path==nil)
end
end)
end)
test("fs.mkdtemp sync error", function(print, p, expect, uv)
local tp = "luvXXXXXZ"
local path, err, code = uv.fs_mkdtemp(tp)
-- Will success on MacOS
if not err then
assert(path:match("^luv......"))
assert(uv.fs_rmdir(path))
else
assert(path==nil)
assert(err:match("^EINVAL:"))
assert(code=='EINVAL')
end
end)
test("fs.mkstemp async", function(print, p, expect, uv)
local tp = "luvXXXXXX"
uv.fs_mkstemp(tp, function(err, fd, path)
assert(not err)
assert(type(fd)=='number')
assert(path:match("^luv......"))
assert(uv.fs_close(fd))
assert(uv.fs_unlink(path))
end)
end, "1.34.0")
test("fs.mkstemp sync", function(print, p, expect, uv)
local tp = "luvXXXXXX"
local content = "hello world!"
local fd, path = uv.fs_mkstemp(tp)
assert(type(fd)=='number')
assert(path:match("^luv......"))
uv.fs_write(fd, content, -1)
assert(uv.fs_close(fd))
fd = assert(uv.fs_open(path, "r", 438))
local stat = assert(uv.fs_fstat(fd))
local chunk = assert(uv.fs_read(fd, stat.size, 0))
assert(#chunk == stat.size)
assert(chunk==content)
assert(uv.fs_close(fd))
assert(uv.fs_unlink(path))
end, "1.34.0")
test("fs.mkstemp async error", function(print, p, expect, uv)
local tp = "luvXXXXXZ"
uv.fs_mkstemp(tp, function(err, path, fd)
assert(err:match("^EINVAL:"))
assert(path==nil)
assert(fd==nil)
end)
end, "1.34.0")
test("fs.mkstemp sync error", function(print, p, expect, uv)
local tp = "luvXXXXXZ"
local path, err, code = uv.fs_mkstemp(tp)
assert(path==nil)
assert(err:match("^EINVAL:"))
assert(code=='EINVAL')
end, "1.34.0")
test("errors with dest paths", function (print, p, expect, uv)
-- this combination will cause all of the functions below to fail
local path1, path2 = "_test_", "_testdir_"
local fd1 = assert(uv.fs_open(path1, "w", 438))
assert(uv.fs_close(fd1))
assert(uv.fs_mkdir(path2, tonumber('777', 8)))
local fns = {"fs_rename", "fs_link", "fs_symlink", "fs_copyfile"}
for _, fn_name in ipairs(fns) do
if uv[fn_name] then
local fn = uv[fn_name]
local ok, err, code = fn(path1, path2)
p(fn_name, ok, err, code)
assert(not ok)
end
end
assert(uv.fs_unlink(path1))
assert(uv.fs_rmdir(path2))
end)
end)