diff --git a/tests/lapi/CMakeLists.txt b/tests/lapi/CMakeLists.txt index e5f35a7..8f2836d 100644 --- a/tests/lapi/CMakeLists.txt +++ b/tests/lapi/CMakeLists.txt @@ -55,3 +55,11 @@ file(GLOB tests LIST_DIRECTORIES false ${CMAKE_CURRENT_SOURCE_DIR}/*_test.lua) foreach(filename ${tests}) create_test(FILENAME ${filename}) endforeach() + +if (NOT OSS_FUZZ) + # The test `package_require_test` is dangerous, because it + # modifies paths used by `require` to search for loaders. + set_tests_properties(package_require_test PROPERTIES + DISABLED TRUE + ) +endif() diff --git a/tests/lapi/package_require_test.lua b/tests/lapi/package_require_test.lua new file mode 100644 index 0000000..536ece4 --- /dev/null +++ b/tests/lapi/package_require_test.lua @@ -0,0 +1,54 @@ +--[[ +SPDX-License-Identifier: ISC +Copyright (c) 2023-2025, Sergey Bronnikov. + +5.3 – Modules +https://www.lua.org/manual/5.1/manual.html#5.3 + +PIL: 15 – Packages +https://www.lua.org/pil/15.html + +Synopsis: require(modname) +]] + +local luzer = require("luzer") +local test_lib = require("lib") + +local MAX_STR_LEN = test_lib.MAX_STR_LEN +local MAX_PATH_NUM = 10 + +local function build_path(fdp) + local count = fdp:consume_integer(0, MAX_PATH_NUM) + local paths = fdp:consume_strings(MAX_STR_LEN, count) + local path_str = table.concat(paths, ";") + local enable_def_path = fdp:consume_boolean() + return enable_def_path and path_str or path_str .. ";;" +end + +local function TestOneInput(buf) + -- Save paths used by `require` to search for a Lua loader. + local old_path = package.path + local old_cpath = package.cpath + + local fdp = luzer.FuzzedDataProvider(buf) + local path = build_path(fdp) + package.path = path + local cpath = build_path(fdp) + package.cpath = cpath + + local module_name = fdp:consume_string(MAX_STR_LEN) + -- If there is any error loading or running the module, or if + -- it cannot find any loader for the module, then require + -- signals an error, + -- https://www.lua.org/manual/5.1/manual.html#pdf-require. + pcall(require, module_name) + + -- Teardown. + package.path = old_path + package.cpath = old_cpath +end + +local args = { + artifact_prefix = "package_require_", +} +luzer.Fuzz(TestOneInput, nil, args)