Skip to content

tests/lapi: package tests #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2025
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
8 changes: 8 additions & 0 deletions tests/lapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
54 changes: 54 additions & 0 deletions tests/lapi/package_require_test.lua
Original file line number Diff line number Diff line change
@@ -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)