Skip to content

tests/lapi: os tests #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion tests/lapi/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,22 @@ local function random_locale(fdp)
return fdp:oneof(locales)
end

local function err_handler(ignored_msgs)
return function(error_msg)
for _, ignored_msg in ipairs(ignored_msgs) do
local x, _ = string.find(error_msg, ignored_msg)
if x then break end
end
end
end

return {
approx_equal = approx_equal,
bitwise_op = bitwise_op,
lua_version = lua_version,
err_handler = err_handler,
lua_current_version_ge_than = lua_current_version_ge_than,
lua_current_version_lt_than = lua_current_version_lt_than,
lua_version = lua_version,
math_pow = math_pow,
MAX_INT64 = MAX_INT64,
MIN_INT64 = MIN_INT64,
Expand Down
45 changes: 45 additions & 0 deletions tests/lapi/os_date_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
--[=[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

6.9 – Operating System Facilities
https://www.lua.org/manual/5.3/manual.html#6.9

LuaJIT 2.1 VM out of memory for `os.date`,
https://github.com/LuaJIT/LuaJIT/issues/463

Checking a format for os.date may read pass the format string,
https://www.lua.org/bugs.html#5.3.3-2

os.date throws an error when result is the empty string,
https://www.lua.org/bugs.html#5.1.1-4

Synopsis: os.date([format [, time]])
]]=]

local luzer = require("luzer")
local test_lib = require("lib")

local ignored_msgs = {
"invalid conversion specifier",
}

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
os.setlocale(test_lib.random_locale(fdp), "all")
local format = fdp:consume_string(test_lib.MAX_STR_LEN)
local time = fdp:consume_integer(test_lib.MIN_INT, test_lib.MAX_INT)
local err_handler = test_lib.err_handler(ignored_msgs)
local ok, res = xpcall(os.date, err_handler, format, time)
if not ok then return end
assert(type(res) == "string" or
type(res) == "table" or
-- Undocumented.
type(res) == "number" or
res == nil)
end

local args = {
artifact_prefix = "os_date_",
}
luzer.Fuzz(TestOneInput, nil, args)
33 changes: 33 additions & 0 deletions tests/lapi/os_difftime_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

6.9 – Operating System Facilities
https://www.lua.org/manual/5.3/manual.html#6.9

Synopsis: os.difftime(t2, t1)
]]

local luzer = require("luzer")
local test_lib = require("lib")
local MIN_INT = test_lib.MIN_INT
local MAX_INT = test_lib.MAX_INT

local ignored_msgs = {
"number has no integer representation",
}

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local t1 = fdp:consume_number(MIN_INT, MAX_INT)
local t2 = fdp:consume_number(MIN_INT, MAX_INT)
local err_handler = test_lib.err_handler(ignored_msgs)
local ok, res = xpcall(os.difftime, err_handler, t1, t2)
if not ok then return end
assert(type(res) == "number")
end

local args = {
artifact_prefix = "os_difftime_",
}
luzer.Fuzz(TestOneInput, nil, args)
21 changes: 21 additions & 0 deletions tests/lapi/os_getenv_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

6.9 – Operating System Facilities
https://www.lua.org/manual/5.3/manual.html#6.9

Synopsis: os.getenv(varname)
]]

local luzer = require("luzer")

local function TestOneInput(buf)
local v = os.getenv(buf)
assert(type(v) == "string" or v == nil)
end

local args = {
artifact_prefix = "os_getenv_",
}
luzer.Fuzz(TestOneInput, nil, args)
28 changes: 28 additions & 0 deletions tests/lapi/os_setlocale_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

6.9 – Operating System Facilities
https://www.lua.org/manual/5.3/manual.html#6.9

Synopsis: os.setlocale(locale [, category])
]]

local luzer = require("luzer")
local test_lib = require("lib")

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local locale = fdp:consume_string(test_lib.MAX_STR_LEN)
local category = fdp:oneof({
"all", "collate", "ctype", "monetary", "numeric", "time",
})
local locale_string = os.setlocale(locale, category)
assert(type(locale_string) == "string" or
locale_string == nil)
end

local args = {
artifact_prefix = "os_setlocale_",
}
luzer.Fuzz(TestOneInput, nil, args)
41 changes: 41 additions & 0 deletions tests/lapi/os_time_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

6.9 – Operating System Facilities
https://www.lua.org/manual/5.3/manual.html#6.9

Synopsis: os.time([table])
]]

local luzer = require("luzer")
local test_lib = require("lib")
local MAX_INT64 = test_lib.MAX_INT64
local MIN_INT64 = test_lib.MIN_INT64

local ignored_msgs = {
"field 'year' is out-of-bound",
}

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
os.setlocale(test_lib.random_locale(fdp), "all")
local time = {
day = fdp:consume_number(MIN_INT64, MAX_INT64),
hour = fdp:consume_number(MIN_INT64, MAX_INT64),
isdst = fdp:consume_boolean(),
min = fdp:consume_number(MIN_INT64, MAX_INT64),
month = fdp:consume_number(MIN_INT64, MAX_INT64),
sec = fdp:consume_number(MIN_INT64, MAX_INT64),
year = fdp:consume_number(MIN_INT64, MAX_INT64),
}
local err_handler = test_lib.err_handler(ignored_msgs)
local ok, res = xpcall(os.time, err_handler, time)
if not ok then return end
assert(type(res) == "number" or type(res) == "table")
end

local args = {
artifact_prefix = "os_time_",
}
luzer.Fuzz(TestOneInput, nil, args)