Skip to content
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
43 changes: 43 additions & 0 deletions tests/lapi/utf8_char_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--[[
SPDX-License-Identifier: ISC
Copyright (c) 2023-2025, Sergey Bronnikov.

6.5 – UTF-8 Support
https://www.lua.org/manual/5.3/manual.html#6.5

Synopsis: utf8.char(...)
]]

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

-- The function is introduced in Lua 5.3.
if test_lib.lua_current_version_lt_than(5, 3) then
print("Unsupported version.")
os.exit()
end

local unpack = unpack or table.unpack

local ignored_msgs = {
"value out of range",
}

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
-- Limit count to prevent error "too many results to unpack".
local MAX_N = 1000
local count = fdp:consume_integer(1, MAX_N)
local chars = fdp:consume_integers(MIN_INT, MAX_INT, count)
os.setlocale(test_lib.random_locale(fdp), "all")
local err_handler = test_lib.err_handler(ignored_msgs)
local ok, _ = xpcall(utf8.char, err_handler, unpack(chars))
if not ok then return end
end

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

6.5 – UTF-8 Support
https://www.lua.org/manual/5.3/manual.html#6.5

Synopsis: utf8.codepoint(s [, i [, j [, lax]]])
]]=]

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

-- The function is introduced in Lua 5.3.
if test_lib.lua_current_version_lt_than(5, 3) then
print("Unsupported version.")
os.exit()
end

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local max_len = fdp:consume_integer(1, MAX_INT)
local s = fdp:consume_string(max_len)
local i = fdp:consume_integer(0, MAX_INT)
local j = fdp:consume_integer(0, MAX_INT)
local lax = fdp:consume_boolean()
os.setlocale(test_lib.random_locale(fdp), "all")
pcall(utf8.codepoint, s, i, j, lax)
end

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

6.5 – UTF-8 Support
https://www.lua.org/manual/5.3/manual.html#6.5

'utf8.codes' does not raise an error on spurious continuation bytes,
https://github.com/lua/lua/commit/a1089b415a3f5c753aa1b40758ffdaf28d5701b0

Synopsis: utf8.codes(s [, lax])
]]

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

-- The function is introduced in Lua 5.3.
if test_lib.lua_current_version_lt_than(5, 3) then
print("Unsupported version.")
os.exit()
end

local ignored_msgs = {
"invalid UTF-8 code",
}

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local max_len = fdp:consume_integer(1, MAX_INT)
local s = fdp:consume_string(max_len)
local lax = fdp:consume_boolean()
os.setlocale(test_lib.random_locale(fdp), "all")
local err_handler = test_lib.err_handler(ignored_msgs)
local ok, _ = xpcall(utf8.codes, err_handler, s, lax)
if not ok then return end
end

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

6.5 – UTF-8 Support
https://www.lua.org/manual/5.3/manual.html#6.5

Synopsis: utf8.len(s [, i [, j [, lax]]])
]]=]

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

-- The function is introduced in Lua 5.3.
if test_lib.lua_current_version_lt_than(5, 3) then
print("Unsupported version.")
os.exit()
end

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local max_len = fdp:consume_integer(1, MAX_INT)
local s = fdp:consume_string(max_len)
local i = fdp:consume_integer(0, MAX_INT)
local j = fdp:consume_integer(0, MAX_INT)
local lax = fdp:consume_boolean()
os.setlocale(test_lib.random_locale(fdp), "all")
pcall(utf8.len, s, i, j, lax)
end

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

6.5 – UTF-8 Support
https://www.lua.org/manual/5.3/manual.html#6.5

Synopsis: utf8.offset(s, n [, i])
]]

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

-- The function is introduced in Lua 5.3.
if test_lib.lua_current_version_lt_than(5, 3) then
print("Unsupported version.")
os.exit()
end

local function TestOneInput(buf)
local fdp = luzer.FuzzedDataProvider(buf)
local max_len = fdp:consume_integer(0, MAX_INT)
local s = fdp:consume_string(max_len)
local n = fdp:consume_integer(MIN_INT, MAX_INT)
local i = fdp:consume_integer(1, MAX_INT)
os.setlocale(test_lib.random_locale(fdp), "all")
utf8.offset(s, n, i)
end

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