diff --git a/tests/lapi/utf8_char_test.lua b/tests/lapi/utf8_char_test.lua new file mode 100644 index 0000000..a6195b3 --- /dev/null +++ b/tests/lapi/utf8_char_test.lua @@ -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) diff --git a/tests/lapi/utf8_codepoint_test.lua b/tests/lapi/utf8_codepoint_test.lua new file mode 100644 index 0000000..cf508c4 --- /dev/null +++ b/tests/lapi/utf8_codepoint_test.lua @@ -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) diff --git a/tests/lapi/utf8_codes_test.lua b/tests/lapi/utf8_codes_test.lua new file mode 100644 index 0000000..e68b633 --- /dev/null +++ b/tests/lapi/utf8_codes_test.lua @@ -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) diff --git a/tests/lapi/utf8_len_test.lua b/tests/lapi/utf8_len_test.lua new file mode 100644 index 0000000..d3bfb5d --- /dev/null +++ b/tests/lapi/utf8_len_test.lua @@ -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) diff --git a/tests/lapi/utf8_offset_test.lua b/tests/lapi/utf8_offset_test.lua new file mode 100644 index 0000000..79c6623 --- /dev/null +++ b/tests/lapi/utf8_offset_test.lua @@ -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)