Skip to content

Commit 0679d2f

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 3864203 commit 0679d2f

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

tests/lapi/io_torture_test.lua

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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:
10+
11+
io.close([file])
12+
io.flush()
13+
io.open(filename [, mode])
14+
io.read(...)
15+
io.tmpfile()
16+
io.write(...)
17+
file:seek([whence] [, offset])
18+
file:setvbuf(mode [, size])
19+
]]
20+
21+
local luzer = require("luzer")
22+
-- The maximum file size is 1Mb (1000 * 1000).
23+
local MAX_N = 1e3
24+
25+
local function io_seek(self)
26+
local SEEK_MODE = { "set", "cur", "end" }
27+
local mode = self.fdp:oneof(SEEK_MODE)
28+
local offset = self.fdp:consume_integer(0, self.MAX_N)
29+
self.fh:seek(mode, offset)
30+
end
31+
32+
local function io_flush(self)
33+
self.fh:flush()
34+
end
35+
36+
local function io_setvbuf(self)
37+
local VBUF_MODE = { "no", "full", "line" }
38+
local mode = self.fdp:oneof(VBUF_MODE)
39+
local size = self.fdp:consume_integer(0, self.MAX_N)
40+
self.fh:setvbuf(mode, size)
41+
end
42+
43+
local function io_write(self)
44+
local str = self.fdp:consume_string(self.MAX_N)
45+
self.fh:write(str)
46+
end
47+
48+
local function io_read(self)
49+
-- As a special case, `io.read(0)` works as a test for end of
50+
-- file: It returns an empty string if there is more to be
51+
-- read or `nil` otherwise.
52+
local size = self.fdp:consume_integer(0, self.MAX_N)
53+
local READ_MODE = { "*number", "*all", "*line", size }
54+
local mode = self.fdp:oneof(READ_MODE)
55+
local _ = self.fh:read(mode)
56+
end
57+
58+
local function io_close(self)
59+
self.fh:close()
60+
end
61+
62+
local io_methods = {
63+
io_flush,
64+
io_read,
65+
io_seek,
66+
io_setvbuf,
67+
io_write,
68+
}
69+
70+
local function io_random_op(self)
71+
local io_method = self.fdp:oneof(io_methods)
72+
io_method(self)
73+
end
74+
75+
local function io_new(fdp)
76+
local fh = io.tmpfile()
77+
return {
78+
close = io_close,
79+
fdp = fdp,
80+
fh = fh,
81+
random_operation = io_random_op,
82+
MAX_N = MAX_N,
83+
}
84+
end
85+
86+
local function TestOneInput(buf)
87+
local fdp = luzer.FuzzedDataProvider(buf)
88+
local nops = fdp:consume_integer(1, MAX_N)
89+
local fh = io_new(fdp)
90+
for _ = 1, nops do
91+
fh:random_operation()
92+
end
93+
fh:close()
94+
end
95+
96+
local args = {
97+
artifact_prefix = "io_torture_",
98+
}
99+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)