-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path_arbitraries.js
More file actions
296 lines (281 loc) · 8.74 KB
/
_arbitraries.js
File metadata and controls
296 lines (281 loc) · 8.74 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* @overview Provides custom fast-check arbitraries for tests.
* @license MIT
*/
import * as fc from "fast-check";
import * as constants from "./_constants.js";
/**
* The env arbitrary generates objects modeled after `process.env`.
*
* For a description of `process.env`, see:
* https://nodejs.org/api/process.html#processenv.
*
* @param {object} [args] Configuration for the arbitrary.
* @param {string[]} [args.keys] Keys that should appear in the environment.
* @returns {object} Arbitrary `process.env`s.
*/
export const env = ({ keys } = {}) =>
fc.dictionary(
fc.oneof(fc.string(), ...(keys || []).map((key) => fc.constant(key))),
fc.string(),
);
/**
* The osType arbitrary generates known OS types.
*
* @returns {string | undefined} Arbitrary OS types.
*/
export const osType = () => fc.constantFrom(undefined, ...constants.osTypes);
/**
* The platform arbitrary generates known platforms.
*
* For a list of platforms, see: https://nodejs.org/api/os.html#osplatform.
*
* @returns {string} Arbitrary OS platforms.
*/
export const platform = () => fc.constantFrom(...constants.platforms);
/**
* The process arbitrary generates objects modeled after `process`. The
* generated object may not represent `process` fully.
*
* For a description of `process`, see: https://nodejs.org/api/process.html.
*
* @returns {object} Arbitrary `process` objects.
*/
export const process = () =>
fc
.record({
abort: fc.func(fc.constant(undefined)),
allowedNodeEnvironmentFlags: fc
.array(fc.string())
.map((flags) => new Set(flags)),
arch: fc.constantFrom(
"arm",
"arm64",
"ia32",
"mips",
"mispel",
"ppc",
"ppc64",
"s390",
"s390x",
"x64",
),
argv: fc.array(fc.string()),
chdir: fc.func(fc.constant(undefined)),
config: fc.object(),
connected: fc.boolean(),
cpuUsage: fc.func(fc.record({ user: fc.nat(), system: fc.nat() })),
cwd: fc.func(fc.string()),
debugPort: fc.nat(),
disconnect: fc.func(fc.constant(undefined)),
dlopen: fc.func(fc.constant(undefined)),
emitWarning: fc.func(fc.constant(undefined)),
env: env(),
execArgv: fc.array(fc.string()),
execPath: fc.string(),
exit: fc.func(fc.nat()),
exitCode: fc.nat(),
getActiveResourcesInfo: fc.array(fc.string()),
getegid: fc.option(fc.func(fc.nat())),
geteuid: fc.option(fc.func(fc.nat())),
getgid: fc.option(fc.func(fc.nat())),
getgroups: fc.option(fc.func(fc.nat())),
getuid: fc.option(fc.func(fc.nat())),
hasUncaughtExceptionCaptureCallback: fc.func(fc.boolean()),
initgroups: fc.option(fc.func(fc.constant(undefined))),
kill: fc.func(fc.constant(undefined)),
memoryUsage: fc.func(
fc.record({
rss: fc.nat(),
heapTotal: fc.nat(),
heapUsed: fc.nat(),
external: fc.nat(),
arrayBuffers: fc.nat(),
}),
),
nextTick: fc.func(fc.constant(undefined)),
noDeprecation: fc.boolean(),
pid: fc.nat(),
platform: fc.func(platform()),
ppid: fc.nat(),
release: fc.record({
name: fc.constant("node"),
sourceUrl: fc.webUrl(),
headersUrl: fc.webUrl(),
libUrl: fc.webUrl(),
lts: fc.constantFrom(undefined, "Dubnium", "Erbium"),
}),
report: fc.record({
compact: fc.boolean(),
directory: fc.string(),
filename: fc.string(),
getReport: fc.func(fc.object()),
reportOnFatalError: fc.boolean(),
reportOnSignal: fc.boolean(),
reportOnUncaughtException: fc.boolean(),
signal: fc.string(),
writeReport: fc.func(fc.string()),
}),
resourceUsage: fc.func(
fc.record({
userCPUTime: fc.nat(),
systemCPUTime: fc.nat(),
maxRSS: fc.nat(),
sharedMemorySize: fc.nat(),
unsharedDataSize: fc.nat(),
unsharedStackSize: fc.nat(),
minorPageFault: fc.nat(),
majorPageFault: fc.nat(),
swappedOut: fc.nat(),
fsRead: fc.nat(),
fsWrite: fc.nat(),
ipcSent: fc.nat(),
ipcReceived: fc.nat(),
signalsCount: fc.nat(),
voluntaryContextSwitches: fc.nat(),
involuntaryContextSwitches: fc.nat(),
}),
),
send: fc.func(fc.boolean()),
setegid: fc.option(fc.func(fc.nat())),
seteuid: fc.option(fc.func(fc.nat())),
setgid: fc.option(fc.func(fc.nat())),
setgroups: fc.option(fc.func(fc.nat())),
setuid: fc.option(fc.func(fc.nat())),
setSourceMapsEnabled: fc.option(fc.func(fc.constant(undefined))),
setUncaughtExceptionCaptureCallback: fc.func(fc.constant(undefined)),
throwDeprecation: fc.boolean(),
title: fc.string(),
traceDeprecation: fc.boolean(),
versions: fc.record({
node: semver(),
v8: fc.string(),
uv: fc.string(),
zlib: fc.string(),
brotli: fc.string(),
ares: fc.string(),
modules: fc.string(),
nghttp2: fc.string(),
napi: fc.string(),
llhttp: fc.string(),
openssl: fc.string(),
cldr: fc.string(),
icu: fc.string(),
tz: fc.string(),
unicode: fc.string(),
}),
})
.map((process) => {
process.argv0 = process.argv[0];
process.version = `v${process.versions.node}`;
return process;
});
/**
* The semver arbitrary generates semver version strings.
*
* @param {object} [args] Configuration for the arbitrary.
* @param {number} [args.maxMajor] The maximum major version.
* @param {number} [args.minMajor] The minimum major version.
* @returns {string} Arbitrary semver version strings.
*/
export const semver = ({ maxMajor, minMajor } = {}) => {
minMajor ||= 0;
return fc
.tuple(
fc.oneof(
fc.constantFrom(
...[minMajor, maxMajor].filter((value) => value !== undefined),
),
fc.integer({ min: minMajor, max: maxMajor }),
),
fc.integer({ min: 0 }),
fc.integer({ min: 0 }),
)
.map(([major, minor, patch]) => `${major}.${minor}.${patch}`);
};
/**
* The shescapeArg arbitrary generates strings that could be inputs to the
* Shescape API for escaping.
*
* @returns {string | number | boolean} Arbitrary valid Shescape arguments.
*/
export const shescapeArg = () =>
fc.oneof(fc.string(), fc.integer(), fc.float(), fc.double(), fc.boolean());
/**
* The shescapeOptions arbitrary generates valid `options` arguments for the
* Shescape API.
*
* @returns {object | undefined} Arbitrary valid Shescape options.
*/
export const shescapeOptions = () =>
fc.option(
fc.record(
{
flagProtection: fc.boolean(),
shell: fc.oneof(
fc.boolean(),
fc.constantFrom(null, undefined),
constants.isWindows ? windowsShell() : unixShell(),
fc.constant("not-actually-a-shell-that-exists"),
fc.constant("node"),
),
},
{ withDeletedKeys: true },
),
{ nil: undefined },
);
/**
* The unixPath arbitrary generates absolute Unix file/folder paths.
*
* @returns {string} Arbitrary Unix file/folder paths.
*/
export const unixPath = () => fc.string().map((path) => `/${path}`);
/**
* The unixShells arbitrary generates Unix shells supported by Shescape.
*
* @returns {string} Arbitrary Unix shells supported by Shescape.
*/
export const unixShell = () => fc.constantFrom(...constants.shellsUnix);
/**
* The unsupportedUnixShell arbitrary generates strings that are not Unix shells
* supported by Shescape.
*
* @returns {string} Arbitrary non-Unix shell strings.
*/
export const unsupportedUnixShell = () =>
fc.oneof(
windowsShell(),
fc
.stringMatching(/^[0-9A-Za-z]+$/u)
.filter((shell) => !constants.shellsUnix.includes(shell)),
);
/**
* The unsupportedWindowsShell arbitrary generates strings that are not Windows
* shells supported by Shescape.
*
* @returns {string} Arbitrary non-Windows shell strings.
*/
export const unsupportedWindowsShell = () =>
fc.oneof(
unixShell(),
fc
.stringMatching(/^[0-9A-Za-z]+\.exe$/u)
.filter(
(shell) => !constants.shellsWindows.includes(shell.toLowerCase()),
),
);
/**
* The windowsPath arbitrary generates absolute Windows file/folder paths.
*
* @returns {string} Arbitrary Windows file/folder paths.
*/
export const windowsPath = () =>
fc
.tuple(fc.stringMatching(/^[A-Z]$/u), fc.string())
.map(([driveLetter, path]) => `${driveLetter}:\\${path}`);
/**
* The windowsShell arbitrary generates Windows shells supported by Shescape.
*
* @returns {string} Arbitrary Windows shells supported by Shescape.
*/
export const windowsShell = () => fc.constantFrom(...constants.shellsWindows);