mediawiki-extensions-Scribunto/tests/engines/LuaSandbox/LuaSandboxInterpreterTest.php
tstarling cebe775ee8 Added more Lua environment features
Package library:

* Added a simulation of the Lua 5.1 package library.
* Removed mw.import(), replaced it with a package loader. Packages can be
  retrieved from the wiki, using require('Module:Foo'), or from files
  distributed with Scribunto, using require('foo'). The "Module:" prefix allows
  for source compatibility with existing Lua code.
* Added a couple of libraries from LuaForge: luabit and stringtools.
* Made fetchModuleFromParser() return null on error instead of throwing an
  exception, to more easily support the desired behaviour of the package loader,
  which needs to return null on error.
* Renamed mw.setupEnvironment() to mw.setup() since it is setting up things
  other than the environment now.
* In MWServer:handleRegisterLibrary(), remove the feature which interprets dots
  in library names, since LuaSandbox doesn't support this.

Improved module isolation and related refactoring:

* Expose restricted versions of getfenv() and setfenv() to user Lua code.
  Requires luasandbox r114952.
* Don't cache the export list returned by module execution for later function
  calls. This breaks isolation of #invoke calls, since the local variables are
  persistent.
* Removed ScribuntoFunctionBase and its children, since it doesn't really have
  a purpose if it can't cache anything. Instead, invoke functions using a module
  method called invoke().
* Removed Module::initialize(), replaced it with a validate() function. This is
  a more elegant interface and works better with the new module caching scheme.
* Use a Status object for the return value of Engine::validate() instead of an
  array. Use the formatting facilities of the Status class.

Other:

* Removed "too many returns" error, doesn't fit in with Lua conventions.
* Use the standalone engine by default, so that the extension will work without
  configuration for more people.
* Added an accessor for $engine->interpreter
* Fix mw.clone() to correctly clone metatables
* If the standalone interpreter exits due to an error, there are some contexts
  where the initial error will be caught and ignored, and the user will see the
  error from checkValid() instead. In this case, rethrow the original error for
  a more informative message.
* Load mw.lua into the initial standalone environment, to reduce code
  duplication between mw.lua and MWServer.lua.
* Fixed a bug in Scribunto_LuaStandaloneInterpreter::handleCall() for functions
  that return no results.
* Fixed a bug in encodeLuaVar() for strings with "\r". Added test case.
* In MWServer.lua, don't call error() for internal errors, instead just print
  the error and exit. This avoids a protocol violation when an error is
  encountered from within handleCall().
* Added lots of documentation. Lua doc comments are in LuaDoc format.

Change-Id: Ie2fd572c362bedf02f45d3fa5352a5280e034740
2012-04-18 13:46:18 +10:00

142 lines
3.8 KiB
PHP

<?php
class Scribunto_LuaSandboxInterpreterTest extends MediaWikiTestCase {
var $stdOpts = array(
'errorFile' => null,
'luaPath' => null,
'memoryLimit' => 50000000,
'cpuLimit' => 30,
);
function setUp() {
try {
$interpreter = $this->newInterpreter();
} catch ( MWException $e ) {
if ( preg_match( '/extension is not present/', $e->getMessage() ) ) {
$this->markTestSkipped( "LuaSandbox not available" );
return;
}
}
}
function getBusyLoop( $interpreter ) {
$chunk = $interpreter->loadString( '
local args = {...}
local x, i
local s = string.rep("x", 1000000)
local n = args[1]
for i = 1, n do
x = x or string.find(s, "y", 1, true)
end',
'busy' );
return $chunk;
}
function getPassthru( $interpreter ) {
return $interpreter->loadString( 'return ...', 'passthru' );
}
function newInterpreter( $opts = array() ) {
$opts = $opts + $this->stdOpts;
$engine = new Scribunto_LuaSandboxEngine( $this->stdOpts );
return new Scribunto_LuaSandboxInterpreter( $engine, $opts );
}
/** @dataProvider provideRoundtrip */
function testRoundtrip( /*...*/ ) {
$args = func_get_args();
$args = $this->normalizeOrder( $args );
$interpreter = $this->newInterpreter();
$passthru = $interpreter->loadString( 'return ...', 'passthru' );
$finalArgs = $args;
array_unshift( $finalArgs, $passthru );
$ret = call_user_func_array( array( $interpreter, 'callFunction' ), $finalArgs );
$ret = $this->normalizeOrder( $ret );
$this->assertSame( $args, $ret );
}
/** @dataProvider provideRoundtrip */
function testDoubleRoundtrip( /* ... */ ) {
$args = func_get_args();
$args = $this->normalizeOrder( $args );
$interpreter = $this->newInterpreter();
$interpreter->registerLibrary( 'test',
array( 'passthru' => array( $this, 'passthru' ) ) );
$doublePassthru = $interpreter->loadString(
'return test.passthru(...)', 'doublePassthru' );
$finalArgs = $args;
array_unshift( $finalArgs, $doublePassthru );
$ret = call_user_func_array( array( $interpreter, 'callFunction' ), $finalArgs );
$ret = $this->normalizeOrder( $ret );
$this->assertSame( $args, $ret );
}
function normalizeOrder( $a ) {
ksort( $a );
foreach ( $a as &$value ) {
if ( is_array( $value ) ) {
$value = $this->normalizeOrder( $value );
}
}
return $a;
}
function passthru( /* ... */ ) {
$args = func_get_args();
return $args;
}
function provideRoundtrip() {
return array(
array( 1 ),
array( true ),
array( false ),
array( 'hello' ),
array( implode( '', array_map( 'chr', range( 0, 255 ) ) ) ),
array( 1, 2, 3 ),
array( array() ),
array( array( 0 => 'foo', 1 => 'bar' ) ),
array( array( 1 => 'foo', 2 => 'bar' ) ),
array( array( 'x' => 'foo', 'y' => 'bar', 'z' => array() ) )
);
}
function testGetMemoryUsage() {
$interpreter = $this->newInterpreter();
$chunk = $interpreter->loadString( 's = string.rep("x", 1000000)', 'mem' );
$interpreter->callFunction( $chunk );
$mem = $interpreter->getMemoryUsage();
$this->assertGreaterThan( 1000000, $mem, 'memory usage' );
$this->assertLessThan( 10000000, $mem, 'memory usage' );
}
/**
* @expectedException ScribuntoException
* @expectedExceptionMessage The time allocated for running scripts has expired.
*/
function testTimeLimit() {
$interpreter = $this->newInterpreter( array( 'cpuLimit' => 1 ) );
$chunk = $this->getBusyLoop( $interpreter );
$interpreter->callFunction( $chunk, 1e9 );
}
/**
* @expectedException ScribuntoException
* @expectedExceptionMessage Lua error: not enough memory
*/
function testTestMemoryLimit() {
$interpreter = $this->newInterpreter( array( 'memoryLimit' => 20 * 1e6 ) );
$chunk = $interpreter->loadString( '
t = {}
for i = 1, 10 do
t[#t + 1] = string.rep("x" .. i, 1000000)
end
',
'memoryLimit' );
$interpreter->callFunction( $chunk );
}
}