Skip to content

Commit 78e3f21

Browse files
committed
tests/lapi: add tests for io functions
The patch adds tests for `io.flush()`, `io.read()`, `io.seek()`, `io.setvbuf()`, `io.write()`. Other functions are not covered.
1 parent 42ef050 commit 78e3f21

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

tests/lapi/io_torture_test.lua

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
5.7 – Input and Output Facilities
6+
https://www.lua.org/manual/5.1/manual.html#5.7
7+
https://www.lua.org/pil/21.3.html
8+
9+
Synopsis: io.read(...)
10+
]]
11+
12+
local luzer = require("luzer")
13+
local MAX_N = 1e3
14+
15+
local function io_seek(self)
16+
local SEEK_MODE = { "set", "cur", "end" }
17+
local mode = self.fdp:oneof(SEEK_MODE)
18+
local offset = self.fdp:consume_integer(0, self.MAX_N)
19+
self.fh:seek(mode, offset)
20+
end
21+
22+
local function io_flush(self)
23+
self.fh:flush()
24+
end
25+
26+
local function io_setvbuf(self)
27+
local VBUF_MODE = { "no", "full", "line" }
28+
local mode = self.fdp:oneof(VBUF_MODE)
29+
local size = self.fdp:consume_integer(0, self.MAX_N)
30+
self.fh:setvbuf(mode, size)
31+
end
32+
33+
local function io_write(self)
34+
local str = self.fdp:consume_string(self.MAX_N)
35+
self.fh:write(str)
36+
end
37+
38+
local function io_read(self)
39+
local READ_MODE = { "*n", "*a", "*l" }
40+
local mode = self.fdp:oneof(READ_MODE)
41+
local _ = self.fh:read(mode)
42+
end
43+
44+
local function io_close(self)
45+
self.fh:close()
46+
end
47+
48+
local io_methods = {
49+
io_flush,
50+
io_read,
51+
io_seek,
52+
io_setvbuf,
53+
io_write,
54+
}
55+
56+
local function io_random_op(self)
57+
local io_method = self.fdp:oneof(io_methods)
58+
io_method(self)
59+
end
60+
61+
local function io_new(fdp)
62+
local fh = io.tmpfile()
63+
return {
64+
close = io_close,
65+
fdp = fdp,
66+
fh = fh,
67+
random_operation = io_random_op,
68+
MAX_N = MAX_N,
69+
}
70+
end
71+
72+
local function TestOneInput(buf)
73+
local fdp = luzer.FuzzedDataProvider(buf)
74+
local nops = fdp:consume_integer(1, MAX_N)
75+
local fh = io_new(fdp)
76+
for _ = 1, nops do
77+
fh:random_operation()
78+
end
79+
fh:close()
80+
end
81+
82+
local args = {
83+
artifact_prefix = "io_torture_",
84+
}
85+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)