Skip to content

Commit 8606640

Browse files
committed
tests/lapi: add os tests
The patch adds tests for the functions in os library except the following functions: `os.tmpname`, `os.rename`, `os.remove`, `os.execute`, `os.clock`.
1 parent 8ef72ef commit 8606640

File tree

6 files changed

+179
-1
lines changed

6 files changed

+179
-1
lines changed

tests/lapi/lib.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,22 @@ local function random_locale(fdp)
9696
return fdp:oneof(locales)
9797
end
9898

99+
local function err_handler(ignored_msgs)
100+
return function(error_msg)
101+
for _, ignored_msg in ipairs(ignored_msgs) do
102+
local x, _ = string.find(error_msg, ignored_msg)
103+
if x then break end
104+
end
105+
end
106+
end
107+
99108
return {
100109
approx_equal = approx_equal,
101110
bitwise_op = bitwise_op,
102-
lua_version = lua_version,
111+
err_handler = err_handler,
103112
lua_current_version_ge_than = lua_current_version_ge_than,
104113
lua_current_version_lt_than = lua_current_version_lt_than,
114+
lua_version = lua_version,
105115
math_pow = math_pow,
106116
MAX_INT64 = MAX_INT64,
107117
MIN_INT64 = MIN_INT64,

tests/lapi/os_date_test.lua

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--[=[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.9 – Operating System Facilities
6+
https://www.lua.org/manual/5.3/manual.html#6.9
7+
8+
LuaJIT 2.1 VM out of memory for `os.date`,
9+
https://github.com/LuaJIT/LuaJIT/issues/463
10+
11+
Checking a format for os.date may read pass the format string,
12+
https://www.lua.org/bugs.html#5.3.3-2
13+
14+
os.date throws an error when result is the empty string,
15+
https://www.lua.org/bugs.html#5.1.1-4
16+
17+
Synopsis: os.date([format [, time]])
18+
]]=]
19+
20+
local luzer = require("luzer")
21+
local test_lib = require("lib")
22+
23+
local ignored_msgs = {
24+
"invalid conversion specifier",
25+
}
26+
27+
local function TestOneInput(buf)
28+
local fdp = luzer.FuzzedDataProvider(buf)
29+
os.setlocale(test_lib.random_locale(fdp), "all")
30+
local format = fdp:consume_string(test_lib.MAX_STR_LEN)
31+
local time = fdp:consume_integer(test_lib.MIN_INT, test_lib.MAX_INT)
32+
local err_handler = test_lib.err_handler(ignored_msgs)
33+
local ok, res = xpcall(os.date, err_handler, format, time)
34+
if not ok then return end
35+
assert(type(res) == "string" or
36+
type(res) == "table" or
37+
-- Undocumented.
38+
type(res) == "number" or
39+
res == nil)
40+
end
41+
42+
local args = {
43+
artifact_prefix = "os_date_",
44+
}
45+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/os_difftime_test.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.9 – Operating System Facilities
6+
https://www.lua.org/manual/5.3/manual.html#6.9
7+
8+
Synopsis: os.difftime(t2, t1)
9+
]]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
local MIN_INT = test_lib.MIN_INT
14+
local MAX_INT = test_lib.MAX_INT
15+
16+
local ignored_msgs = {
17+
"number has no integer representation",
18+
}
19+
20+
local function TestOneInput(buf)
21+
local fdp = luzer.FuzzedDataProvider(buf)
22+
local t1 = fdp:consume_number(MIN_INT, MAX_INT)
23+
local t2 = fdp:consume_number(MIN_INT, MAX_INT)
24+
local err_handler = test_lib.err_handler(ignored_msgs)
25+
local ok, res = xpcall(os.difftime, err_handler, t1, t2)
26+
if not ok then return end
27+
assert(type(res) == "number")
28+
end
29+
30+
local args = {
31+
artifact_prefix = "os_difftime_",
32+
}
33+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/os_getenv_test.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.9 – Operating System Facilities
6+
https://www.lua.org/manual/5.3/manual.html#6.9
7+
8+
Synopsis: os.getenv(varname)
9+
]]
10+
11+
local luzer = require("luzer")
12+
13+
local function TestOneInput(buf)
14+
local v = os.getenv(buf)
15+
assert(type(v) == "string" or v == nil)
16+
end
17+
18+
local args = {
19+
artifact_prefix = "os_getenv_",
20+
}
21+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/os_setlocale_test.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.9 – Operating System Facilities
6+
https://www.lua.org/manual/5.3/manual.html#6.9
7+
8+
Synopsis: os.setlocale(locale [, category])
9+
]]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
14+
local function TestOneInput(buf)
15+
local fdp = luzer.FuzzedDataProvider(buf)
16+
local locale = fdp:consume_string(test_lib.MAX_STR_LEN)
17+
local category = fdp:oneof({
18+
"all", "collate", "ctype", "monetary", "numeric", "time",
19+
})
20+
local locale_string = os.setlocale(locale, category)
21+
assert(type(locale_string) == "string" or
22+
locale_string == nil)
23+
end
24+
25+
local args = {
26+
artifact_prefix = "os_setlocale_",
27+
}
28+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/os_time_test.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.9 – Operating System Facilities
6+
https://www.lua.org/manual/5.3/manual.html#6.9
7+
8+
Synopsis: os.time([table])
9+
]]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
local MAX_INT64 = test_lib.MAX_INT64
14+
local MIN_INT64 = test_lib.MIN_INT64
15+
16+
local ignored_msgs = {
17+
"field 'year' is out-of-bound",
18+
}
19+
20+
local function TestOneInput(buf)
21+
local fdp = luzer.FuzzedDataProvider(buf)
22+
os.setlocale(test_lib.random_locale(fdp), "all")
23+
local time = {
24+
day = fdp:consume_number(MIN_INT64, MAX_INT64),
25+
hour = fdp:consume_number(MIN_INT64, MAX_INT64),
26+
isdst = fdp:consume_boolean(),
27+
min = fdp:consume_number(MIN_INT64, MAX_INT64),
28+
month = fdp:consume_number(MIN_INT64, MAX_INT64),
29+
sec = fdp:consume_number(MIN_INT64, MAX_INT64),
30+
year = fdp:consume_number(MIN_INT64, MAX_INT64),
31+
}
32+
local err_handler = test_lib.err_handler(ignored_msgs)
33+
local ok, res = xpcall(os.time, err_handler, time)
34+
if not ok then return end
35+
assert(type(res) == "number" or type(res) == "table")
36+
end
37+
38+
local args = {
39+
artifact_prefix = "os_time_",
40+
}
41+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)