Skip to content

Commit fb7a2e9

Browse files
committed
tests/lapi: add utf8 tests
1 parent 8606640 commit fb7a2e9

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed

tests/lapi/utf8_char_test.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.5 – UTF-8 Support
6+
https://www.lua.org/manual/5.3/manual.html#6.5
7+
8+
Synopsis: utf8.char(...)
9+
]]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
local MAX_INT = test_lib.MAX_INT
14+
local MIN_INT = test_lib.MIN_INT
15+
16+
-- The function is introduced in Lua 5.3.
17+
if test_lib.lua_current_version_lt_than(5, 3) then
18+
print("Unsupported version.")
19+
os.exit()
20+
end
21+
22+
local unpack = unpack or table.unpack
23+
24+
local ignored_msgs = {
25+
"value out of range",
26+
}
27+
28+
local function TestOneInput(buf)
29+
local fdp = luzer.FuzzedDataProvider(buf)
30+
-- Limit count to prevent error "too many results to unpack".
31+
local MAX_N = 1000
32+
local count = fdp:consume_integer(1, MAX_N)
33+
local chars = fdp:consume_integers(MIN_INT, MAX_INT, count)
34+
os.setlocale(test_lib.random_locale(fdp), "all")
35+
local err_handler = test_lib.err_handler(ignored_msgs)
36+
local ok, _ = xpcall(utf8.char, err_handler, unpack(chars))
37+
if not ok then return end
38+
end
39+
40+
local args = {
41+
artifact_prefix = "utf8_char_",
42+
}
43+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/utf8_codepoint_test.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--[=[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.5 – UTF-8 Support
6+
https://www.lua.org/manual/5.3/manual.html#6.5
7+
8+
Synopsis: utf8.codepoint(s [, i [, j [, lax]]])
9+
]]=]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
local MAX_INT = test_lib.MAX_INT
14+
15+
-- The function is introduced in Lua 5.3.
16+
if test_lib.lua_current_version_lt_than(5, 3) then
17+
print("Unsupported version.")
18+
os.exit()
19+
end
20+
21+
local function TestOneInput(buf)
22+
local fdp = luzer.FuzzedDataProvider(buf)
23+
local max_len = fdp:consume_integer(1, MAX_INT)
24+
local s = fdp:consume_string(max_len)
25+
local i = fdp:consume_integer(0, MAX_INT)
26+
local j = fdp:consume_integer(0, MAX_INT)
27+
local lax = fdp:consume_boolean()
28+
os.setlocale(test_lib.random_locale(fdp), "all")
29+
pcall(utf8.codepoint, s, i, j, lax)
30+
end
31+
32+
local args = {
33+
artifact_prefix = "utf8_codepoint_",
34+
}
35+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/utf8_codes_test.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.5 – UTF-8 Support
6+
https://www.lua.org/manual/5.3/manual.html#6.5
7+
8+
'utf8.codes' does not raise an error on spurious continuation bytes,
9+
https://github.com/lua/lua/commit/a1089b415a3f5c753aa1b40758ffdaf28d5701b0
10+
11+
Synopsis: utf8.codes(s [, lax])
12+
]]
13+
14+
local luzer = require("luzer")
15+
local test_lib = require("lib")
16+
local MAX_INT = test_lib.MAX_INT
17+
18+
-- The function is introduced in Lua 5.3.
19+
if test_lib.lua_current_version_lt_than(5, 3) then
20+
print("Unsupported version.")
21+
os.exit()
22+
end
23+
24+
local ignored_msgs = {
25+
"invalid UTF-8 code",
26+
}
27+
28+
local function TestOneInput(buf)
29+
local fdp = luzer.FuzzedDataProvider(buf)
30+
local max_len = fdp:consume_integer(1, MAX_INT)
31+
local s = fdp:consume_string(max_len)
32+
local lax = fdp:consume_boolean()
33+
os.setlocale(test_lib.random_locale(fdp), "all")
34+
local err_handler = test_lib.err_handler(ignored_msgs)
35+
local ok, _ = xpcall(utf8.codes, err_handler, s, lax)
36+
if not ok then return end
37+
end
38+
39+
local args = {
40+
artifact_prefix = "utf8_codes_",
41+
}
42+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/utf8_len_test.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--[=[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.5 – UTF-8 Support
6+
https://www.lua.org/manual/5.3/manual.html#6.5
7+
8+
Synopsis: utf8.len(s [, i [, j [, lax]]])
9+
]]=]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
local MAX_INT = test_lib.MAX_INT
14+
15+
-- The function is introduced in Lua 5.3.
16+
if test_lib.lua_current_version_lt_than(5, 3) then
17+
print("Unsupported version.")
18+
os.exit()
19+
end
20+
21+
local function TestOneInput(buf)
22+
local fdp = luzer.FuzzedDataProvider(buf)
23+
local max_len = fdp:consume_integer(1, MAX_INT)
24+
local s = fdp:consume_string(max_len)
25+
local i = fdp:consume_integer(0, MAX_INT)
26+
local j = fdp:consume_integer(0, MAX_INT)
27+
local lax = fdp:consume_boolean()
28+
os.setlocale(test_lib.random_locale(fdp), "all")
29+
pcall(utf8.len, s, i, j, lax)
30+
end
31+
32+
local args = {
33+
artifact_prefix = "utf8_len_",
34+
}
35+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/utf8_offset_test.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2023-2025, Sergey Bronnikov.
4+
5+
6.5 – UTF-8 Support
6+
https://www.lua.org/manual/5.3/manual.html#6.5
7+
8+
Synopsis: utf8.offset(s, n [, i])
9+
]]
10+
11+
local luzer = require("luzer")
12+
local test_lib = require("lib")
13+
local MAX_INT = test_lib.MAX_INT
14+
local MIN_INT = test_lib.MIN_INT
15+
16+
-- The function is introduced in Lua 5.3.
17+
if test_lib.lua_current_version_lt_than(5, 3) then
18+
print("Unsupported version.")
19+
os.exit()
20+
end
21+
22+
local function TestOneInput(buf)
23+
local fdp = luzer.FuzzedDataProvider(buf)
24+
local max_len = fdp:consume_integer(0, MAX_INT)
25+
local s = fdp:consume_string(max_len)
26+
local n = fdp:consume_integer(MIN_INT, MAX_INT)
27+
local i = fdp:consume_integer(1, MAX_INT)
28+
os.setlocale(test_lib.random_locale(fdp), "all")
29+
utf8.offset(s, n, i)
30+
end
31+
32+
local args = {
33+
artifact_prefix = "utf8_offset_",
34+
}
35+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)