diff --git a/tests/lapi/lib.lua b/tests/lapi/lib.lua index 9c95a0f..b0f1a7f 100644 --- a/tests/lapi/lib.lua +++ b/tests/lapi/lib.lua @@ -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, diff --git a/tests/lapi/os_date_test.lua b/tests/lapi/os_date_test.lua new file mode 100644 index 0000000..ac63eac --- /dev/null +++ b/tests/lapi/os_date_test.lua @@ -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) diff --git a/tests/lapi/os_difftime_test.lua b/tests/lapi/os_difftime_test.lua new file mode 100644 index 0000000..08d9187 --- /dev/null +++ b/tests/lapi/os_difftime_test.lua @@ -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) diff --git a/tests/lapi/os_getenv_test.lua b/tests/lapi/os_getenv_test.lua new file mode 100644 index 0000000..8ee9191 --- /dev/null +++ b/tests/lapi/os_getenv_test.lua @@ -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) diff --git a/tests/lapi/os_setlocale_test.lua b/tests/lapi/os_setlocale_test.lua new file mode 100644 index 0000000..3a8c4cd --- /dev/null +++ b/tests/lapi/os_setlocale_test.lua @@ -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) diff --git a/tests/lapi/os_time_test.lua b/tests/lapi/os_time_test.lua new file mode 100644 index 0000000..cc6e742 --- /dev/null +++ b/tests/lapi/os_time_test.lua @@ -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)