-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpackage_require_test.lua
More file actions
54 lines (43 loc) · 1.44 KB
/
package_require_test.lua
File metadata and controls
54 lines (43 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)