Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaCommon/CommonTests.lua

raw symbols SCIP
1 local testframework = require 'Module:TestFramework'
2
3 local test = {}
4
5 function test.clone1()
6 local x = 1
7 local y = mw.clone( x )
8 return ( x == y )
9 end
10
11 function test.clone2()
12 local x = { 'a' }
13 local y = mw.clone( x )
14 assert( x ~= y )
15 return testframework.deepEquals( x, y )
16 end
17
18 function test.clone2b()
19 local x = { 'a' }
20 local y = mw.clone( x )
21 assert( x ~= y )
22 y[2] = 'b'
23 return testframework.deepEquals( x, y )
24 end
25
26 function test.clone3()
27 local mt = { __add = function() end }
28 local x = {}
29 setmetatable( x, mt )
30 local y = mw.clone( x )
31 assert( getmetatable( x ) ~= getmetatable( y ) )
32 return testframework.deepEquals( getmetatable( x ), getmetatable( y ) )
33 end
34
35 function test.clone4()
36 local x = {}
37 x.x = x
38 local y = mw.clone( x )
39 assert( x ~= y )
40 return y == y.x
41 end
42
43 function test.setfenv1()
44 setfenv( 0, {} )
45 end
46
47 function test.setfenv2()
48 setfenv( 1000, {} )
49 end
50
51 function test.setfenv3()
52 local function jailbreak()
53 setfenv( 2, {} )
54 end
55 local new_setfenv, new_getfenv = mw.makeProtectedEnvFuncsForTest( { [_G] = true }, {} )
56 setfenv( jailbreak, {setfenv = new_setfenv} )
57 jailbreak()
58 end
59
60 function test.setfenv4()
61 -- Set an unprotected environment at a higher stack level than a protected
62 -- environment. It's assumed that any higher-level environment will protect
63 -- itself with its own setfenv wrapper, so this succeeds.
64 local function level3()
65 local protected = {setfenv = setfenv, getfenv = getfenv, mw = mw}
66 local function level2()
67 local function level1()
68 setfenv( 3, {} )
69 end
70
71 local env = {}
72 env.setfenv, env.getfenv = mw.makeProtectedEnvFuncsForTest(
73 {[protected] = true}, {} )
74 setfenv( level1, env )()
75 end
76 setfenv( level2, protected )()
77 end
78 local unprotected = {setfenv = setfenv, getfenv = getfenv, mw = mw}
79 setfenv( level3, unprotected )()
80 assert( getfenv( level3 ) ~= unprotected )
81 return 'ok'
82 end
83
84 function test.setfenv5()
85 local function allowed()
86 (function() setfenv( 2, {} ) end )()
87 end
88 local new_setfenv, new_getfenv = mw.makeProtectedEnvFuncsForTest( { [_G] = true }, {} )
89 setfenv( allowed, {setfenv = new_setfenv} )()
90 return 'ok'
91 end
92
93 function test.setfenv6()
94 local function target() end
95 local function jailbreak()
96 setfenv( target, {} )
97 end
98 local new_setfenv, new_getfenv = mw.makeProtectedEnvFuncsForTest( {}, { [target] = true } )
99 setfenv( jailbreak, {setfenv = new_setfenv} )()
100 end
101
102 function test.setfenv7()
103 setfenv( {}, {} )
104 end
105
106 function test.getfenv1()
107 assert( getfenv( 1 ) == _G )
108 return 'ok'
109 end
110
111 function test.getfenv2()
112 getfenv( 0 )
113 end
114
115 function test.getfenv3()
116 local function foo()
117 return getfenv( 2 )
118 end
119
120 local function bar()
121 return foo()
122 end
123
124 -- The "at level #" bit varies between environments, so
125 -- catch the error and strip that part out
126 local ok, err = pcall( bar )
127 if not ok then
128 err = string.gsub( err, '^%S+:%d+: ', '' )
129 err = string.gsub( err, ' at level %d$', '' )
130 error( err )
131 end
132 end
133
134 function test.executeExpensiveCalls( n )
135 for i = 1, n do
136 mw.incrementExpensiveFunctionCount()
137 end
138 return 'Did not error out'
139 end
140
141 function test.stringMetatableHidden1()
142 return getmetatable( "" )
143 end
144
145 function test.stringMetatableHidden2()
146 string.foo = 42
147 return ("").foo
148 end
149
150 local pairs_test_table = {}
151 setmetatable( pairs_test_table, {
152 __pairs = function () return 1, 2, 3, 'ignore' end,
153 __ipairs = function () return 4, 5, 6, 'ignore' end,
154 } )
155
156 function test.unpackCount( t, i, j )
157 return select( '#', unpack( t, i, j ) )
158 end
159
160 function test.noLeaksViaPackageLoaded()
161 assert( package.loaded.debug == debug, "package.loaded.debug ~= debug" )
162 assert( package.loaded.string == string, "package.loaded.string ~= string" )
163 assert( package.loaded.math == math, "package.loaded.math ~= math" )
164 assert( package.loaded.io == io, "package.loaded.io ~= io" )
165 assert( package.loaded.os == os, "package.loaded.os ~= os" )
166 assert( package.loaded.table == table, "package.loaded.table ~= table" )
167 assert( package.loaded._G == _G , "package.loaded._G ~= _G " )
168 assert( package.loaded.coroutine == coroutine, "package.loaded.coroutine ~= coroutine" )
169 assert( package.loaded.package == package, "package.loaded.package ~= package" )
170 return 'ok'
171 end
172
173 function test.strictGood()
174 require( 'strict' )
175 local foo = "bar"
176 return foo
177 end
178
179 function test.strictBad1()
180 require( 'strict' )
181 return bar
182 end
183
184 function test.strictBad2()
185 require( 'strict' )
186 bar = "foo"
187 end
188
189 test.loadData = {}
190
191 function test.loadData.get( ... )
192 local d = mw.loadData( 'Module:CommonTests-data' )
193 for i = 1, select( '#', ... ) do
194 local k = select( i, ... )
195 d = d[k]
196 end
197 return d
198 end
199
200 function test.loadData.set( v, ... )
201 local d = mw.loadData( 'Module:CommonTests-data' )
202 local n = select( '#', ... )
203 for i = 1, n - 1 do
204 local k = select( i, ... )
205 d = d[k]
206 end
207 d[select( n, ... )] = v
208 return d[select( n, ... )]
209 end
210
211 function test.loadData.recursion()
212 local d = mw.loadData( 'Module:CommonTests-data' )
213 return d == d.t, d.t == d.t.t, d.table2 == d.table
214 end
215
216 function test.loadData.iterate( func )
217 local d = mw.loadData( 'Module:CommonTests-data' )
218 local ret = {}
219 for k, v in func( d.table ) do
220 ret[k] = v
221 end
222 return ret
223 end
224
225 function test.loadData.setmetatable()
226 local d = mw.loadData( 'Module:CommonTests-data' )
227 setmetatable( d, {} )
228 return 'setmetatable succeeded'
229 end
230
231 function test.loadData.rawset()
232 -- We can't easily prevent rawset (and it's not worth trying to redefine
233 -- it), but we can make sure it doesn't affect other instances of the data
234 local d1 = mw.loadData( 'Module:CommonTests-data' )
235 local d2 = mw.loadData( 'Module:CommonTests-data' )
236 rawset( d1, 'str', 'ugh' )
237 local d3 = mw.loadData( 'Module:CommonTests-data' )
238 return d1.str, d2.str, d3.str
239 end
240
241 test.loadJsonData = {}
242
243 function test.loadJsonData.get( ... )
244 local d = mw.loadJsonData( 'Module:CommonTests-data.json' )
245 for i = 1, select( '#', ... ) do
246 local k = select( i, ... )
247 d = d[k]
248 end
249 return d
250 end
251
252 function test.loadJsonData.set( v, ... )
253 local d = mw.loadJsonData( 'Module:CommonTests-data.json' )
254 local n = select( '#', ... )
255 for i = 1, n - 1 do
256 local k = select( i, ... )
257 d = d[k]
258 end
259 d[select( n, ... )] = v
260 return d[select( n, ... )]
261 end
262
263 function test.loadJsonData.iterate( func )
264 local d = mw.loadJsonData( 'Module:CommonTests-data.json' )
265 local ret = {}
266 for k, v in func( d.table ) do
267 ret[k] = v
268 end
269 return ret
270 end
271
272 function test.loadJsonData.setmetatable()
273 local d = mw.loadJsonData( 'Module:CommonTests-data.json' )
274 setmetatable( d, {} )
275 return 'setmetatable succeeded'
276 end
277
278 function test.loadJsonData.rawset()
279 -- We can't easily prevent rawset (and it's not worth trying to redefine
280 -- it), but we can make sure it doesn't affect other instances of the data
281 local d1 = mw.loadJsonData( 'Module:CommonTests-data.json' )
282 local d2 = mw.loadJsonData( 'Module:CommonTests-data.json' )
283 rawset( d1, 'str', 'ugh' )
284 local d3 = mw.loadJsonData( 'Module:CommonTests-data.json' )
285 return d1.str, d2.str, d3.str
286 end
287
288 function test.loadJsonData.error( name )
289 mw.loadJsonData( name )
290 end
291
292 return testframework.getTestProvider( {
293 { name = 'clone', func = test.clone1,
294 expect = { true },
295 },
296 { name = 'clone table', func = test.clone2,
297 expect = { true },
298 },
299 { name = 'clone table then modify', func = test.clone2b,
300 expect = { false, { 2 }, nil, 'b' },
301 },
302 { name = 'clone table with metatable', func = test.clone3,
303 expect = { true },
304 },
305 { name = 'clone recursive table', func = test.clone4,
306 expect = { true },
307 },
308
309 { name = 'setfenv global', func = test.setfenv1,
310 expect = "'setfenv' cannot set the global environment, it is protected",
311 },
312 { name = 'setfenv invalid level', func = test.setfenv2,
313 expect = "'setfenv' cannot set an environment at a level greater than 10",
314 },
315 { name = 'setfenv invalid environment', func = test.setfenv3,
316 expect = "'setfenv' cannot set the requested environment, it is protected",
317 },
318 { name = 'setfenv on unprotected past protected', func = test.setfenv4,
319 expect = { 'ok' },
320 },
321 { name = 'setfenv from inside protected', func = test.setfenv5,
322 expect = { 'ok' },
323 },
324 { name = 'setfenv protected function', func = test.setfenv6,
325 expect = "'setfenv' cannot be called on a protected function",
326 },
327 { name = 'setfenv on a non-function', func = test.setfenv7,
328 expect = "'setfenv' can only be called with a function or integer as the first argument",
329 },
330
331 { name = 'getfenv(1)', func = test.getfenv1,
332 expect = { 'ok' },
333 },
334 { name = 'getfenv(0)', func = test.getfenv2,
335 expect = "'getfenv' cannot get the global environment",
336 },
337 { name = 'getfenv with tail call', func = test.getfenv3,
338 expect = "no function environment for tail call",
339 },
340
341 { name = 'Not quite too many expensive function calls',
342 func = test.executeExpensiveCalls, args = { 10 },
343 expect = { 'Did not error out' }
344 },
345
346 { name = 'Too many expensive function calls',
347 func = test.executeExpensiveCalls, args = { 11 },
348 expect = 'too many expensive function calls'
349 },
350
351 { name = 'string metatable is hidden', func = test.stringMetatableHidden1,
352 expect = { nil }
353 },
354
355 { name = 'string is not string metatable', func = test.stringMetatableHidden2,
356 expect = { nil }
357 },
358
359 { name = 'pairs with __pairs',
360 func = pairs, args = { pairs_test_table },
361 expect = { 1, 2, 3 },
362 },
363
364 { name = 'ipairs with __ipairs',
365 func = ipairs, args = { pairs_test_table },
366 expect = { 4, 5, 6 },
367 },
368
369 { name = 'unpack empty table',
370 func = unpack, args = { {} },
371 expect = {}
372 },
373
374 { name = 'unpack one value',
375 func = unpack, args = { { 1 } },
376 expect = { 1 }
377 },
378
379 { name = 'unpack extra right',
380 func = test.unpackCount, args = { {}, 1, 3 },
381 expect = { 3 }
382 },
383
384 { name = 'unpack extra left',
385 func = test.unpackCount, args = { {}, -2, 0 },
386 expect = { 3 }
387 },
388
389 { name = 'unpack too many positive',
390 func = test.unpackCount, args = { {}, 1, 8000 },
391 expect = "too many results to unpack"
392 },
393
394 { name = 'unpack too many negative',
395 func = test.unpackCount, args = { {}, -8000, 0 },
396 expect = "too many results to unpack"
397 },
398
399 { name = 'unpack INT_MAX',
400 func = test.unpackCount, args = { {}, 0, 2^31 - 1 },
401 expect = "too many results to unpack"
402 },
403
404 { name = 'unpack large float',
405 func = test.unpackCount, args = { {}, 0, 2^43 },
406 expect = "too many results to unpack"
407 },
408
409 { name = 'unpack non-table',
410 func = test.unpackCount, args = { 0 },
411 expect = "bad argument #1 to 'old_unpack' (table expected, got number)"
412 },
413
414 { name = 'package.loaded does not leak references to out-of-environment objects',
415 func = test.noLeaksViaPackageLoaded,
416 expect = { 'ok' },
417 },
418
419 { name = 'strict on good code raises no errors',
420 func = test.strictGood,
421 expect = { 'bar' },
422 },
423 { name = 'strict on code reading from a global errors',
424 func = test.strictBad1,
425 expect = "variable 'bar' is not declared",
426 },
427 { name = 'strict on code setting from a global errors',
428 func = test.strictBad2,
429 expect = "assign to undeclared variable 'bar'",
430 },
431
432 { name = 'mw.loadData, returning non-table',
433 func = mw.loadData, args = { 'Module:CommonTests-data-fail1' },
434 expect = "Module:CommonTests-data-fail1 returned string, table expected",
435 },
436 { name = 'mw.loadData, containing function',
437 func = mw.loadData, args = { 'Module:CommonTests-data-fail2' },
438 expect = "data for mw.loadData contains unsupported data type 'function'",
439 },
440 { name = 'mw.loadData, containing table-with-metatable',
441 func = mw.loadData, args = { 'Module:CommonTests-data-fail3' },
442 expect = "data for mw.loadData contains a table with a metatable",
443 },
444 { name = 'mw.loadData, containing function as key',
445 func = mw.loadData, args = { 'Module:CommonTests-data-fail4' },
446 expect = "data for mw.loadData contains unsupported data type 'function'",
447 },
448 { name = 'mw.loadData, containing table-with-metatable as key',
449 func = mw.loadData, args = { 'Module:CommonTests-data-fail5' },
450 expect = "data for mw.loadData contains a table as a key",
451 },
452 { name = 'mw.loadData, getter (true)',
453 func = test.loadData.get, args = { 'true' },
454 expect = { true }
455 },
456 { name = 'mw.loadData, getter (false)',
457 func = test.loadData.get, args = { 'false' },
458 expect = { false }
459 },
460 { name = 'mw.loadData, getter (NaN)',
461 func = test.loadData.get, args = { 'NaN' },
462 expect = { 0/0 }
463 },
464 { name = 'mw.loadData, getter (inf)',
465 func = test.loadData.get, args = { 'inf' },
466 expect = { 1/0 }
467 },
468 { name = 'mw.loadData, getter (num)',
469 func = test.loadData.get, args = { 'num' },
470 expect = { 12.5 }
471 },
472 { name = 'mw.loadData, getter (str)',
473 func = test.loadData.get, args = { 'str' },
474 expect = { 'foo bar' }
475 },
476 { name = 'mw.loadData, getter (table.2)',
477 func = test.loadData.get, args = { 'table', 2 },
478 expect = { 'two' }
479 },
480 { name = 'mw.loadData, getter (t.t.t.t.str)',
481 func = test.loadData.get, args = { 't', 't', 't', 't', 'str' },
482 expect = { 'foo bar' }
483 },
484 { name = 'mw.loadData, getter recursion',
485 func = test.loadData.recursion,
486 expect = { true, true, true },
487 },
488 { name = 'mw.loadData, pairs',
489 func = test.loadData.iterate, args = { pairs },
490 expect = { { 'one', 'two', 'three', foo = 'bar' } },
491 },
492 { name = 'mw.loadData, ipairs',
493 func = test.loadData.iterate, args = { ipairs },
494 expect = { { 'one', 'two', 'three' } },
495 },
496 { name = 'mw.loadData, setmetatable',
497 func = test.loadData.setmetatable,
498 expect = "cannot change a protected metatable"
499 },
500 { name = 'mw.loadData, setter (1)',
501 func = test.loadData.set, args = { 'ugh', 'str' },
502 expect = "table from mw.loadData is read-only",
503 },
504 { name = 'mw.loadData, setter (2)',
505 func = test.loadData.set, args = { 'ugh', 'table', 2 },
506 expect = "table from mw.loadData is read-only",
507 },
508 { name = 'mw.loadData, setter (3)',
509 func = test.loadData.set, args = { 'ugh', 't' },
510 expect = "table from mw.loadData is read-only",
511 },
512 { name = 'mw.loadData, rawset',
513 func = test.loadData.rawset,
514 expect = { 'ugh', 'foo bar', 'foo bar' },
515 },
516
517 { name = 'mw.loadJsonData, getter (true)',
518 func = test.loadJsonData.get, args = { 'true' },
519 expect = { true }
520 },
521 { name = 'mw.loadJsonData, getter (false)',
522 func = test.loadJsonData.get, args = { 'false' },
523 expect = { false }
524 },
525 { name = 'mw.loadJsonData, getter (num)',
526 func = test.loadJsonData.get, args = { 'num' },
527 expect = { 12.5 }
528 },
529 { name = 'mw.loadJsonData, getter (str)',
530 func = test.loadJsonData.get, args = { 'str' },
531 expect = { 'foo bar' }
532 },
533 { name = 'mw.loadJsonData, getter (table.2)',
534 func = test.loadJsonData.get, args = { 'table', 2 },
535 expect = { 'two' }
536 },
537 { name = 'mw.loadJsonData, pairs',
538 func = test.loadJsonData.iterate, args = { pairs },
539 expect = { { 'one', 'two', 'three' } },
540 },
541 { name = 'mw.loadJsonData, ipairs',
542 func = test.loadJsonData.iterate, args = { ipairs },
543 expect = { { 'one', 'two', 'three' } },
544 },
545 { name = 'mw.loadJsonData, setmetatable',
546 func = test.loadJsonData.setmetatable,
547 expect = "cannot change a protected metatable"
548 },
549 { name = 'mw.loadJsonData, setter (1)',
550 func = test.loadJsonData.set, args = { 'ugh', 'str' },
551 expect = "table from mw.loadJsonData is read-only",
552 },
553 { name = 'mw.loadJsonData, setter (2)',
554 func = test.loadJsonData.set, args = { 'ugh', 'table', 2 },
555 expect = "table from mw.loadJsonData is read-only",
556 },
557 { name = 'mw.loadJsonData, setter (3)',
558 func = test.loadJsonData.set, args = { 'ugh', 't' },
559 expect = "table from mw.loadJsonData is read-only",
560 },
561 { name = 'mw.loadJsonData, rawset',
562 func = test.loadJsonData.rawset,
563 expect = { 'ugh', 'foo bar', 'foo bar' },
564 },
565 { name = 'mw.loadJsonData, bad title (1)',
566 func = test.loadJsonData.error, args = { 0 },
567 expect = "bad argument #1 to 'mw.loadJsonData' (string expected, got number)",
568 },
569 { name = 'mw.loadJsonData, bad title (2)',
570 func = test.loadJsonData.error, args = { "<invalid title>" },
571 expect = "bad argument #1 to 'mw.loadJsonData' ('<invalid title>' is not a valid JSON page)",
572 },
573 { name = 'mw.loadJsonData, bad title (3)',
574 func = test.loadJsonData.error, args = { "Help:Foo" },
575 expect = "bad argument #1 to 'mw.loadJsonData' ('Help:Foo' is not a valid JSON page)",
576 },
577 { name = 'mw.loadJsonData, bad title (4)',
578 func = test.loadJsonData.error, args = { "Help:Does not exist" },
579 expect = "bad argument #1 to 'mw.loadJsonData' ('Help:Does not exist' is not a valid JSON page)",
580 },
581
582 { name = 'mw.addWarning',
583 func = mw.addWarning, args = { 'warn' },
584 expect = {},
585 },
586 { name = 'mw.addWarning, bad type',
587 func = mw.addWarning, args = { true },
588 expect = "bad argument #1 to 'addWarning' (string expected)",
589 },
590 } )