Skip to content

Commit 33e1d6e

Browse files
committed
tests/lapi: 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 bfac9d7 commit 33e1d6e

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

tests/lapi/os_date_test.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 function TestOneInput(buf)
24+
local fdp = luzer.FuzzedDataProvider(buf)
25+
os.setlocale(test_lib.random_locale(fdp), "all")
26+
local format = fdp:consume_string(test_lib.MAX_STR_LEN)
27+
local time = fdp:consume_number(test_lib.MIN_INT, test_lib.MAX_INT)
28+
os.date(format, time)
29+
end
30+
31+
local args = {
32+
artifact_prefix = "os_date_",
33+
}
34+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/os_difftime_test.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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_INT64
14+
local MAX_INT = test_lib.MAX_INT64
15+
16+
local function TestOneInput(buf)
17+
local fdp = luzer.FuzzedDataProvider(buf)
18+
local t1 = fdp:consume_number(MIN_INT, MAX_INT)
19+
local t2 = fdp:consume_number(MIN_INT, MAX_INT)
20+
local res = os.difftime(t1, t2)
21+
assert(type(res) == "number")
22+
end
23+
24+
local args = {
25+
artifact_prefix = "os_difftime_",
26+
}
27+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/os_getenv_test.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
12+
local luzer = require("luzer")
13+
14+
local function TestOneInput(buf)
15+
local v = os.getenv(buf)
16+
assert(type(v) == "string" or v == nil)
17+
end
18+
19+
local args = {
20+
artifact_prefix = "os_getenv_",
21+
}
22+
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 function TestOneInput(buf)
17+
local fdp = luzer.FuzzedDataProvider(buf)
18+
os.setlocale(test_lib.random_locale(fdp), "all")
19+
local time = {
20+
day = fdp:consume_number(MIN_INT64, MAX_INT64),
21+
hour = fdp:consume_number(MIN_INT64, MAX_INT64),
22+
isdst = fdp:consume_boolean(),
23+
min = fdp:consume_number(MIN_INT64, MAX_INT64),
24+
month = fdp:consume_number(MIN_INT64, MAX_INT64),
25+
sec = fdp:consume_number(MIN_INT64, MAX_INT64),
26+
year = fdp:consume_number(MIN_INT64, MAX_INT64),
27+
}
28+
os.time(time)
29+
end
30+
31+
local args = {
32+
artifact_prefix = "os_time_",
33+
}
34+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)