-
Notifications
You must be signed in to change notification settings - Fork 54
/
bsd.lua
441 lines (421 loc) · 16.1 KB
/
bsd.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
-- General BSD tests
local function init(S)
local helpers = require "test.helpers"
local types = S.types
local c = S.c
local abi = S.abi
local bit = require "syscall.bit"
local ffi = require "ffi"
local t, pt, s = types.t, types.pt, types.s
local assert = helpers.assert
local function fork_assert(cond, err, ...) -- if we have forked we need to fail in main thread not fork
if not cond then
print(tostring(err))
print(debug.traceback())
S.exit("failure")
end
if cond == true then return ... end
return cond, ...
end
local function assert_equal(...)
collectgarbage("collect") -- force gc, to test for bugs
return assert_equals(...)
end
local teststring = "this is a test string"
local size = 512
local buf = t.buffer(size)
local tmpfile = "XXXXYYYYZZZ4521" .. S.getpid()
local tmpfile2 = "./666666DDDDDFFFF" .. S.getpid()
local tmpfile3 = "MMMMMTTTTGGG" .. S.getpid()
local longfile = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" .. S.getpid()
local efile = "./tmpexXXYYY" .. S.getpid() .. ".sh"
local largeval = math.pow(2, 33) -- larger than 2^32 for testing
local mqname = "ljsyscallXXYYZZ" .. S.getpid()
local clean = function()
S.rmdir(tmpfile)
S.unlink(tmpfile)
S.unlink(tmpfile2)
S.unlink(tmpfile3)
S.unlink(longfile)
S.unlink(efile)
end
local test = {}
test.bsd_misc = {
test_sysctl_all = function()
local all, err = S.sysctl()
assert(all and type(all) == "table", "expect a table from all sysctls got " .. type(all))
end,
}
test.bsd_ids = {
test_issetugid = function()
if not S.issetugid then error "skipped" end
local res = assert(S.issetugid())
assert(res == 0 or res == 1) -- some tests call setuid so might be tainted
end,
}
test.filesystem_bsd = {
test_revoke = function()
local fd = assert(S.posix_openpt("rdwr, noctty"))
assert(fd:grantpt())
assert(fd:unlockpt())
local pts_name = assert(fd:ptsname())
local pts = assert(S.open(pts_name, "rdwr, noctty"))
assert(S.revoke(pts_name))
local n, err = pts:read()
if n then -- correct behaviour according to man page
assert_equal(#n, 0) -- read returns EOF after revoke
else -- FreeBSD is NXIO Filed http://www.freebsd.org/cgi/query-pr.cgi?pr=188952
-- OSX is EIO
assert(not n and (err.IO or err.NXIO))
end
local n, err = pts:write("test") -- write fails after revoke
assert(not n and (err.IO or err.NXIO), "access should be revoked")
assert(pts:close()) -- close succeeds after revoke
assert(fd:close())
end,
test_chflags = function()
local fd = assert(S.creat(tmpfile, "RWXU"))
assert(fd:write("append"))
local ok, err = S.chflags(tmpfile, "uf_append")
if not ok and err.OPNOTSUPP then error "skipped" end
assert(ok, err)
assert(fd:write("append"))
assert(fd:seek(0, "set"))
local n, err = fd:write("not append")
if not (S.__rump or abi.xen) then assert(err and err.PERM, "non append write should fail") end -- TODO I think this is due to tmpfs mount??
assert(S.chflags(tmpfile)) -- clear flags
assert(S.unlink(tmpfile))
assert(fd:close())
end,
test_lchflags = function()
if not S.lchflags then error "skipped" end
local fd = assert(S.creat(tmpfile, "RWXU"))
assert(fd:write("append"))
local ok, err = S.lchflags(tmpfile, "uf_append")
if not ok and err.OPNOTSUPP then error "skipped" end
assert(ok, err)
assert(fd:write("append"))
assert(fd:seek(0, "set"))
local n, err = fd:write("not append")
if not (S.__rump or abi.xen) then assert(err and err.PERM, "non append write should fail") end -- TODO I think this is due to tmpfs mount??
assert(S.lchflags(tmpfile)) -- clear flags
assert(S.unlink(tmpfile))
assert(fd:close())
end,
test_fchflags = function()
local fd = assert(S.creat(tmpfile, "RWXU"))
assert(fd:write("append"))
local ok, err = fd:chflags("uf_append")
if not ok and err.OPNOTSUPP then error "skipped" end
assert(ok, err)
assert(fd:write("append"))
assert(fd:seek(0, "set"))
local n, err = fd:write("not append")
if not (S.__rump or abi.xen) then assert(err and err.PERM, "non append write should fail") end -- TODO I think this is due to tmpfs mount??
assert(fd:chflags()) -- clear flags
assert(S.unlink(tmpfile))
assert(fd:close())
end,
test_chflagsat = function()
if not S.chflagsat then error "skipped" end
local fd = assert(S.creat(tmpfile, "RWXU"))
assert(fd:write("append"))
local ok, err = S.chflagsat("fdcwd", tmpfile, "uf_append", "symlink_nofollow")
if not ok and err.OPNOTSUPP then error "skipped" end
assert(ok, err)
assert(fd:write("append"))
assert(fd:seek(0, "set"))
local n, err = fd:write("not append")
assert(err and err.PERM, "non append write should fail")
assert(S.chflagsat("fdcwd", tmpfile)) -- clear flags
assert(S.unlink(tmpfile))
assert(fd:close())
end,
test_lchmod = function()
if not S.lchmod then error "skipped" end
local fd = assert(S.creat(tmpfile, "RWXU"))
assert(S.lchmod(tmpfile, "RUSR, WUSR"))
assert(S.access(tmpfile, "rw"))
assert(S.unlink(tmpfile))
assert(fd:close())
end,
test_utimensat = function()
-- BSD utimensat as same specification as Linux, but some functionality missing, so test simpler
if not S.utimensat then error "skipped" end
local fd = assert(S.creat(tmpfile, "RWXU"))
local dfd = assert(S.open("."))
assert(S.utimensat(nil, tmpfile))
local st1 = fd:stat()
assert(S.utimensat("fdcwd", tmpfile, {"omit", "omit"}))
local st2 = fd:stat()
assert(st1.mtime == st2.mtime, "mtime unchanged") -- cannot test atime as stat touches it
assert(S.unlink(tmpfile))
assert(fd:close())
assert(dfd:close())
end,
}
test.kqueue = {
test_kqueue_vnode = function()
local kfd = assert(S.kqueue())
local fd = assert(S.creat(tmpfile, "rwxu"))
local kevs = t.kevents{{fd = fd, filter = "vnode",
flags = "add, enable, clear", fflags = "delete, write, extend, attrib, link, rename, revoke"}}
assert(kfd:kevent(kevs, nil))
local _, _, n = assert(kfd:kevent(nil, kevs, 0))
assert_equal(n, 0) -- no events yet
assert(S.unlink(tmpfile))
local count = 0
for k, v in assert(kfd:kevent(nil, kevs, 1)) do
assert(v.DELETE, "expect delete event")
count = count + 1
end
assert_equal(count, 1)
assert(fd:write("something"))
local count = 0
for k, v in assert(kfd:kevent(nil, kevs, 1)) do
assert(v.WRITE, "expect write event")
assert(v.EXTEND, "expect extend event")
count = count + 1
end
assert_equal(count, 1)
assert(fd:close())
assert(kfd:close())
end,
test_kqueue_read = function()
local kfd = assert(S.kqueue())
local p1, p2 = assert(S.pipe())
local kevs = t.kevents{{fd = p1, filter = "read", flags = "add"}}
assert(kfd:kevent(kevs, nil))
local a, b, n = assert(kfd:kevent(nil, kevs, 0))
assert_equal(n, 0) -- no events yet
local str = "test"
p2:write(str)
local count = 0
for k, v in assert(kfd:kevent(nil, kevs, 0)) do
assert_equal(v.size, #str) -- size will be amount available to read
count = count + 1
end
assert_equal(count, 1) -- 1 event readable now
local r, err = p1:read()
local _, _, n = assert(kfd:kevent(nil, kevs, 0))
assert_equal(n, 0) -- no events any more
assert(p2:close())
local count = 0
for k, v in assert(kfd:kevent(nil, kevs, 0)) do
assert(v.EOF, "expect EOF event")
count = count + 1
end
assert_equal(count, 1)
assert(p1:close())
assert(kfd:close())
end,
test_kqueue_write = function()
local kfd = assert(S.kqueue())
local p1, p2 = assert(S.pipe())
local kevs = t.kevents{{fd = p2, filter = "write", flags = "add"}}
assert(kfd:kevent(kevs, nil))
local count = 0
for k, v in assert(kfd:kevent(nil, kevs, 0)) do
assert(v.size > 0) -- size will be amount free in buffer
count = count + 1
end
assert_equal(count, 1) -- one event
assert(p1:close()) -- close read end
count = 0
for k, v in assert(kfd:kevent(nil, kevs, 0)) do
assert(v.EOF, "expect EOF event")
count = count + 1
end
assert_equal(count, 1)
assert(p2:close())
assert(kfd:close())
end,
test_kqueue_timer = function()
local kfd = assert(S.kqueue())
local kevs = t.kevents{{ident = 0, filter = "timer", flags = "add, oneshot", data = 10}}
assert(kfd:kevent(kevs, nil))
local count = 0
for k, v in assert(kfd:kevent(nil, kevs)) do
assert_equal(v.size, 1) -- count of expiries is 1 as oneshot
count = count + 1
end
assert_equal(count, 1) -- will have expired by now
assert(kfd:close())
end,
}
test.bsd_extattr = {
teardown = clean,
test_extattr_empty_fd = function()
if not S.extattr_get_fd then error "skipped" end
local fd = assert(S.creat(tmpfile, "rwxu"))
assert(S.unlink(tmpfile))
local n, err = fd:extattr_get("user", "myattr", false) -- false does raw call with no buffer to return length
if not n and err.OPNOTSUPP then error "skipped" end -- fs does not support extattr
assert(not n, "expected to fail")
assert(err.NOATTR, err)
assert(fd:close())
end,
test_extattr_getsetdel_fd = function()
if not S.extattr_get_fd then error "skipped" end
local fd = assert(S.creat(tmpfile, "rwxu"))
assert(S.unlink(tmpfile))
local n, err = fd:extattr_get("user", "myattr", false) -- false does raw call with no buffer to return length
if not n and err.OPNOTSUPP then error "skipped" end -- fs does not support extattr
assert(not n, "expected to fail")
assert(err.NOATTR, err)
local n, err = fd:extattr_set("user", "myattr", "myvalue")
if not n and err.OPNOTSUPP then error "skipped" end -- fs does not support setting extattr
assert(n, err)
assert_equal(n, #"myvalue")
local str = assert(fd:extattr_get("user", "myattr"))
assert_equal(str, "myvalue")
local ok = assert(fd:extattr_delete("user", "myattr"))
local str, err = fd:extattr_get("user", "myattr")
assert(not str and err.NOATTR)
assert(fd:close())
end,
test_extattr_getsetdel_file = function()
if not S.extattr_get_fd then error "skipped" end
local fd = assert(S.creat(tmpfile, "rwxu"))
assert(fd:close())
local n, err = S.extattr_get_file(tmpfile, "user", "myattr", false) -- false does raw call with no buffer to return length
if not n and err.OPNOTSUPP then error "skipped" end -- fs does not support extattr
assert(not n and err.NOATTR)
local n, err = S.extattr_set_file(tmpfile, "user", "myattr", "myvalue")
if not n and err.OPNOTSUPP then error "skipped" end -- fs does not support setting extattr
assert(n, err)
assert_equal(n, #"myvalue")
local str = assert(S.extattr_get_file(tmpfile, "user", "myattr"))
assert_equal(str, "myvalue")
local ok = assert(S.extattr_delete_file(tmpfile, "user", "myattr"))
local str, err = S.extattr_get_file(tmpfile, "user", "myattr")
assert(not str and err.NOATTR)
assert(S.unlink(tmpfile))
end,
test_extattr_getsetdel_link = function()
if not S.extattr_get_fd then error "skipped" end
assert(S.symlink(tmpfile2, tmpfile))
local n, err = S.extattr_get_link(tmpfile, "user", "myattr", false) -- false does raw call with no buffer to return length
if not n and err.OPNOTSUPP then error "skipped" end -- fs does not support extattr
assert(not n and err.NOATTR)
local n, err = S.extattr_set_link(tmpfile, "user", "myattr", "myvalue")
if not n and err.OPNOTSUPP then error "skipped" end -- fs does not support setting extattr
assert(n, err)
assert_equal(n, #"myvalue")
local str = assert(S.extattr_get_link(tmpfile, "user", "myattr"))
assert_equal(str, "myvalue")
local ok = assert(S.extattr_delete_link(tmpfile, "user", "myattr"))
local str, err = S.extattr_get_link(tmpfile, "user", "myattr")
assert(not str and err.NOATTR)
assert(S.unlink(tmpfile))
end,
test_extattr_list_fd = function()
if not S.extattr_list_fd then error "skipped" end
local fd = assert(S.creat(tmpfile, "rwxu"))
assert(S.unlink(tmpfile))
local attrs, err = fd:extattr_list("user")
if not attrs and err.OPNOTSUPP then error "skipped" end -- fs does not support extattr
assert(attrs, err)
assert_equal(#attrs, 0)
assert(fd:extattr_set("user", "myattr", "myvalue"))
local attrs = assert(fd:extattr_list("user"))
assert_equal(#attrs, 1)
assert_equal(attrs[1], "myattr")
assert(fd:extattr_set("user", "newattr", "newvalue"))
local attrs = assert(fd:extattr_list("user"))
assert_equal(#attrs, 2)
assert((attrs[1] == "myattr" and attrs[2] == "newattr") or (attrs[2] == "myattr" and attrs[1] == "newattr"))
assert(fd:close())
end,
test_extattr_list_file = function()
if not S.extattr_list_file then error "skipped" end
local fd = assert(S.creat(tmpfile, "rwxu"))
local attrs, err = S.extattr_list_file(tmpfile, "user")
if not attrs and err.OPNOTSUPP then error "skipped" end -- fs does not support extattr
assert(attrs, err)
assert_equal(#attrs, 0)
assert(S.extattr_set_file(tmpfile, "user", "myattr", "myvalue"))
local attrs = assert(S.extattr_list_file(tmpfile, "user"))
assert_equal(#attrs, 1)
assert_equal(attrs[1], "myattr")
assert(S.extattr_set_file(tmpfile, "user", "newattr", "newvalue"))
local attrs = assert(S.extattr_list_file(tmpfile, "user"))
assert_equal(#attrs, 2)
assert((attrs[1] == "myattr" and attrs[2] == "newattr") or (attrs[2] == "myattr" and attrs[1] == "newattr"))
assert(S.unlink(tmpfile))
end,
test_extattr_list_link = function()
if not S.extattr_list_file then error "skipped" end
assert(S.symlink(tmpfile2, tmpfile))
local attrs, err = S.extattr_list_link(tmpfile, "user")
if not attrs and err.OPNOTSUPP then error "skipped" end -- fs does not support extattr
assert(attrs, err)
assert_equal(#attrs, 0)
assert(S.extattr_set_link(tmpfile, "user", "myattr", "myvalue"))
local attrs = assert(S.extattr_list_link(tmpfile, "user"))
assert_equal(#attrs, 1)
assert_equal(attrs[1], "myattr")
assert(S.extattr_set_link(tmpfile, "user", "newattr", "newvalue"))
local attrs = assert(S.extattr_list_link(tmpfile, "user"))
assert_equal(#attrs, 2)
assert((attrs[1] == "myattr" and attrs[2] == "newattr") or (attrs[2] == "myattr" and attrs[1] == "newattr"))
assert(S.unlink(tmpfile))
end,
test_extattr_list_long = function()
if not S.extattr_list_fd then error "skipped" end
local fd = assert(S.creat(tmpfile, "rwxu"))
assert(S.unlink(tmpfile))
local attrs, err = fd:extattr_list("user")
if not attrs and err.OPNOTSUPP then error "skipped" end -- fs does not support extattr
assert(attrs, err)
assert_equal(#attrs, 0)
local count = 100
for i = 1, count do
assert(fd:extattr_set("user", "myattr" .. i, "myvalue"))
end
local attrs = assert(fd:extattr_list("user"))
assert_equal(#attrs, count)
assert(fd:close())
end,
}
-- skip as no processes in rump
if not S.__rump then
test.kqueue.test_kqueue_proc = function()
local pid = assert(S.fork())
if pid == 0 then -- child
S.pause()
S.exit()
else -- parent
local kfd = assert(S.kqueue())
local kevs = t.kevents{{ident = pid, filter = "proc", flags = "add", fflags = "exit, fork, exec"}}
assert(kfd:kevent(kevs, nil))
assert(S.kill(pid, "term"))
local count = 0
for k, v in assert(kfd:kevent(nil, kevs, 1)) do
assert(v.EXIT)
count = count + 1
end
assert_equal(count, 1)
assert(kfd:close())
assert(S.waitpid(pid))
end
end
test.kqueue.test_kqueue_signal = function()
assert(S.signal("alrm", "ign"))
local kfd = assert(S.kqueue())
local kevs = t.kevents{{signal = "alrm", filter = "signal", flags = "add"}}
assert(kfd:kevent(kevs, nil))
assert(S.kill(0, "alrm"))
assert(S.kill(0, "alrm"))
local count = 0
for k, v in assert(kfd:kevent(nil, kevs, 1)) do
assert_equal(v.data, 2) -- event happened twice
count = count + 1
end
assert_equal(count, 1)
assert(S.signal("alrm", "dfl"))
end
end
return test
end
return {init = init}