Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
<?php
|
|
|
|
|
2022-07-30 22:43:58 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaStandalone;
|
|
|
|
|
2018-01-10 14:16:00 +00:00
|
|
|
if ( !wfIsCLI() ) {
|
2016-05-17 14:52:05 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2022-07-30 22:43:58 +00:00
|
|
|
use Exception;
|
2022-08-03 12:31:15 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaError;
|
2022-07-30 18:52:37 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\Engines\LuaStandalone\LuaStandaloneEngine;
|
|
|
|
use MediaWiki\Extension\Scribunto\Engines\LuaStandalone\LuaStandaloneInterpreter;
|
|
|
|
use MediaWiki\Extension\Scribunto\Engines\LuaStandalone\LuaStandaloneInterpreterFunction;
|
2022-04-07 23:12:32 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\ScribuntoException;
|
2023-11-21 22:30:54 +00:00
|
|
|
use MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon\LuaInterpreterTestBase;
|
2018-11-09 19:31:08 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
|
|
|
|
2013-11-01 15:53:56 +00:00
|
|
|
/**
|
|
|
|
* @group Lua
|
|
|
|
* @group LuaStandalone
|
2020-04-08 21:27:28 +00:00
|
|
|
* @group Standalone
|
2022-07-30 18:52:37 +00:00
|
|
|
* @covers \MediaWiki\Extension\Scribunto\Engines\LuaStandalone\LuaStandaloneInterpreter
|
2013-11-01 15:53:56 +00:00
|
|
|
*/
|
2023-11-21 22:30:54 +00:00
|
|
|
class StandaloneInterpreterTest extends LuaInterpreterTestBase {
|
2021-01-23 01:33:17 +00:00
|
|
|
/** @var array */
|
2017-06-15 17:19:00 +00:00
|
|
|
public $stdOpts = [
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
'errorFile' => null,
|
|
|
|
'luaPath' => null,
|
|
|
|
'memoryLimit' => 50000000,
|
|
|
|
'cpuLimit' => 30,
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
private function getVsize( $pid ) {
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
$size = wfShellExec( wfEscapeShellArg( 'ps', '-p', $pid, '-o', 'vsz', '--no-headers' ) );
|
2018-02-02 05:08:32 +00:00
|
|
|
return trim( $size ) * 1024;
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
protected function newInterpreter( $opts = [] ) {
|
2022-04-16 21:09:10 +00:00
|
|
|
$opts += $this->stdOpts;
|
2022-07-30 18:52:37 +00:00
|
|
|
$engine = new LuaStandaloneEngine( $this->stdOpts );
|
|
|
|
return new LuaStandaloneInterpreter( $engine, $opts );
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-06 19:55:24 +00:00
|
|
|
public function testIOErrorExit() {
|
|
|
|
$interpreter = $this->newInterpreter();
|
|
|
|
try {
|
|
|
|
$interpreter->testquit();
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
} catch ( ScribuntoException $ex ) {
|
|
|
|
$this->assertSame( 'scribunto-luastandalone-exited', $ex->getMessageName() );
|
|
|
|
$this->assertSame( [ '[UNKNOWN]', 42 ], $ex->messageArgs );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIOErrorSignal() {
|
|
|
|
$interpreter = $this->newInterpreter();
|
|
|
|
try {
|
|
|
|
proc_terminate( $interpreter->proc, 15 );
|
|
|
|
// Some dummy protocol interaction to make it see the interpreter went away
|
|
|
|
$interpreter->loadString( 'return ...', 'test' );
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
} catch ( ScribuntoException $ex ) {
|
|
|
|
$this->assertSame( 'scribunto-luastandalone-signal', $ex->getMessageName() );
|
|
|
|
$this->assertSame( [ '[UNKNOWN]', 15 ], $ex->messageArgs );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testGetStatus() {
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
$startTime = microtime( true );
|
|
|
|
if ( php_uname( 's' ) !== 'Linux' ) {
|
|
|
|
$this->markTestSkipped( "getStatus() not supported on platforms other than Linux" );
|
|
|
|
}
|
|
|
|
$interpreter = $this->newInterpreter();
|
2018-11-09 19:31:08 +00:00
|
|
|
$engine = TestingAccessWrapper::newFromObject( $interpreter->engine );
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
$status = $interpreter->getStatus();
|
|
|
|
$pid = $status['pid'];
|
2020-01-14 11:52:14 +00:00
|
|
|
$this->assertIsInt( $status['pid'] );
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
$initialVsize = $this->getVsize( $pid );
|
|
|
|
$this->assertGreaterThan( 0, $initialVsize, 'Initial vsize' );
|
|
|
|
|
|
|
|
$chunk = $this->getBusyLoop( $interpreter );
|
|
|
|
|
|
|
|
while ( microtime( true ) - $startTime < 1 ) {
|
|
|
|
$interpreter->callFunction( $chunk, 100 );
|
|
|
|
}
|
|
|
|
$status = $interpreter->getStatus();
|
|
|
|
$vsize = $this->getVsize( $pid );
|
2018-11-09 19:31:08 +00:00
|
|
|
$time = $status['time'] / $engine->getClockTick();
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
$this->assertGreaterThan( 0.1, $time, 'getStatus() time usage' );
|
|
|
|
$this->assertLessThan( 1.5, $time, 'getStatus() time usage' );
|
2020-01-23 16:53:33 +00:00
|
|
|
$this->assertEqualsWithDelta( $vsize, $status['vsize'], $vsize * 0.1, 'vsize' );
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
}
|
2013-11-01 00:04:12 +00:00
|
|
|
|
2018-02-06 21:03:11 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider providePhpToLuaArrayKeyConversion
|
|
|
|
*/
|
|
|
|
public function testPhpToLuaArrayKeyConversion( $array, $expect ) {
|
|
|
|
$interpreter = $this->newInterpreter();
|
|
|
|
|
|
|
|
$ret = $interpreter->callFunction(
|
|
|
|
$interpreter->loadString(
|
|
|
|
'local t, r = ..., {}; for k, v in pairs( t ) do r[v] = type(k) end return r', 'test'
|
|
|
|
),
|
|
|
|
$array
|
|
|
|
);
|
|
|
|
ksort( $ret[0], SORT_STRING );
|
|
|
|
$this->assertSame( $expect, $ret[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function providePhpToLuaArrayKeyConversion() {
|
|
|
|
if ( PHP_INT_MAX > 9007199254740992 ) {
|
|
|
|
$a = [
|
|
|
|
'9007199254740992' => 'max', '9007199254740993' => 'max+1',
|
|
|
|
'-9007199254740992' => 'min', '-9007199254740993' => 'min-1',
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$a = [
|
|
|
|
'2147483647' => 'max', '2147483648' => 'max+1',
|
|
|
|
'-2147483648' => 'min', '-2147483649' => 'min-1',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'simple integers' => [
|
|
|
|
[ -10 => 'minus ten', 0 => 'zero', 10 => 'ten' ],
|
|
|
|
[ 'minus ten' => 'number', 'ten' => 'number', 'zero' => 'number' ],
|
|
|
|
],
|
|
|
|
'maximal values' => [
|
|
|
|
$a,
|
|
|
|
[ 'max' => 'number', 'max+1' => 'string', 'min' => 'number', 'min-1' => 'string' ],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideLuaToPhpArrayKeyConversion
|
|
|
|
*/
|
|
|
|
public function testLuaToPhpArrayKeyConversion( $lua, $expect ) {
|
|
|
|
if ( $expect instanceof Exception ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
$this->expectException( LuaError::class );
|
2019-10-05 19:31:04 +00:00
|
|
|
$this->expectExceptionMessage( $expect->getMessage() );
|
2018-02-06 21:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$interpreter = $this->newInterpreter();
|
|
|
|
$ret = $interpreter->callFunction(
|
|
|
|
$interpreter->loadString( "return { $lua }", 'test' )
|
|
|
|
);
|
|
|
|
if ( $expect instanceof Exception ) {
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
}
|
|
|
|
ksort( $ret[0], SORT_STRING );
|
|
|
|
$this->assertSame( $expect, $ret[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideLuaToPhpArrayKeyConversion() {
|
|
|
|
if ( PHP_INT_MAX > 9007199254740992 ) {
|
|
|
|
$max = '9223372036854774784';
|
|
|
|
$max2 = '9223372036854775808';
|
|
|
|
$min = '-9223372036854775808';
|
|
|
|
$min2 = '-9223372036854775809';
|
|
|
|
} else {
|
|
|
|
$max = '2147483647';
|
|
|
|
$max2 = '2147483648';
|
|
|
|
$min = '-2147483648';
|
|
|
|
$min2 = '-2147483649';
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'simple integers' => [
|
|
|
|
'[-10] = "minus ten", [0] = "zero", [10] = "ten"',
|
|
|
|
[ -10 => 'minus ten', 0 => 'zero', 10 => 'ten' ],
|
|
|
|
],
|
|
|
|
'stringified integers' => [
|
|
|
|
'["-10"] = "minus ten", ["0"] = "zero", ["10"] = "ten"',
|
|
|
|
[ -10 => 'minus ten', 0 => 'zero', 10 => 'ten' ],
|
|
|
|
],
|
|
|
|
'maximal integers' => [
|
|
|
|
"['$max'] = 'near max', ['$max2'] = 'max+1', ['$min'] = 'min', ['$min2'] = 'min-1'",
|
|
|
|
[ $min => 'min', $min2 => 'min-1', $max => 'near max', $max2 => 'max+1' ],
|
|
|
|
],
|
|
|
|
'collision (0)' => [
|
|
|
|
'[0] = "number zero", ["0"] = "string zero"',
|
|
|
|
new Exception( 'Collision for array key 0 when passing data from Lua to PHP.' ),
|
|
|
|
],
|
|
|
|
'collision (float)' => [
|
|
|
|
'[1.5] = "number 1.5", ["1.5"] = "string 1.5"',
|
|
|
|
new Exception( 'Collision for array key 1.5 when passing data from Lua to PHP.' ),
|
|
|
|
],
|
|
|
|
'collision (inf)' => [
|
|
|
|
'[1/0] = "number inf", ["inf"] = "string inf"',
|
|
|
|
new Exception( 'Collision for array key inf when passing data from Lua to PHP.' ),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testFreeFunctions() {
|
2013-11-01 00:04:12 +00:00
|
|
|
$interpreter = $this->newInterpreter();
|
|
|
|
|
|
|
|
// Test #1: Make sure freeing actually works
|
|
|
|
$ret = $interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'return function() return "testFreeFunction #1" end', 'test' )
|
|
|
|
);
|
|
|
|
$id = $ret[0]->id;
|
|
|
|
$interpreter->cleanupLuaChunks();
|
|
|
|
$this->assertEquals(
|
2017-06-15 17:19:00 +00:00
|
|
|
[ 'testFreeFunction #1' ], $interpreter->callFunction( $ret[0] ),
|
2013-11-01 00:04:12 +00:00
|
|
|
'Test that function #1 was not freed while a reference exists'
|
|
|
|
);
|
|
|
|
$ret = null;
|
|
|
|
$interpreter->cleanupLuaChunks();
|
2022-07-30 18:52:37 +00:00
|
|
|
$testfunc = new LuaStandaloneInterpreterFunction( $interpreter->id, $id );
|
2013-11-01 00:04:12 +00:00
|
|
|
try {
|
|
|
|
$interpreter->callFunction( $testfunc );
|
|
|
|
$this->fail( "Expected exception because function #1 should have been freed" );
|
2022-08-03 12:31:15 +00:00
|
|
|
} catch ( LuaError $e ) {
|
2013-11-01 00:04:12 +00:00
|
|
|
$this->assertEquals(
|
2015-02-19 22:12:49 +00:00
|
|
|
"function id $id does not exist", $e->messageArgs[1],
|
2013-11-01 00:04:12 +00:00
|
|
|
'Testing for expected error when calling a freed function #1'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test #2: Make sure constructing a new copy of the function works
|
|
|
|
$ret = $interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'return function() return "testFreeFunction #2" end', 'test' )
|
|
|
|
);
|
|
|
|
$id = $ret[0]->id;
|
2022-07-30 18:52:37 +00:00
|
|
|
$func = new LuaStandaloneInterpreterFunction( $interpreter->id, $id );
|
2013-11-01 00:04:12 +00:00
|
|
|
$ret = null;
|
|
|
|
$interpreter->cleanupLuaChunks();
|
|
|
|
$this->assertEquals(
|
2017-06-15 17:19:00 +00:00
|
|
|
[ 'testFreeFunction #2' ], $interpreter->callFunction( $func ),
|
2013-11-01 00:04:12 +00:00
|
|
|
'Test that function #2 was not freed while a reference exists'
|
|
|
|
);
|
|
|
|
$func = null;
|
|
|
|
$interpreter->cleanupLuaChunks();
|
2022-07-30 18:52:37 +00:00
|
|
|
$testfunc = new LuaStandaloneInterpreterFunction( $interpreter->id, $id );
|
2013-11-01 00:04:12 +00:00
|
|
|
try {
|
|
|
|
$interpreter->callFunction( $testfunc );
|
|
|
|
$this->fail( "Expected exception because function #2 should have been freed" );
|
2022-08-03 12:31:15 +00:00
|
|
|
} catch ( LuaError $e ) {
|
2013-11-01 00:04:12 +00:00
|
|
|
$this->assertEquals(
|
2015-02-19 22:12:49 +00:00
|
|
|
"function id $id does not exist", $e->messageArgs[1],
|
2013-11-01 00:04:12 +00:00
|
|
|
'Testing for expected error when calling a freed function #2'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test #3: Make sure cloning works
|
|
|
|
$ret = $interpreter->callFunction(
|
|
|
|
$interpreter->loadString( 'return function() return "testFreeFunction #3" end', 'test' )
|
|
|
|
);
|
|
|
|
$id = $ret[0]->id;
|
|
|
|
$func = clone $ret[0];
|
|
|
|
$ret = null;
|
|
|
|
$interpreter->cleanupLuaChunks();
|
|
|
|
$this->assertEquals(
|
2017-06-15 17:19:00 +00:00
|
|
|
[ 'testFreeFunction #3' ], $interpreter->callFunction( $func ),
|
2013-11-01 00:04:12 +00:00
|
|
|
'Test that function #3 was not freed while a reference exists'
|
|
|
|
);
|
|
|
|
$func = null;
|
|
|
|
$interpreter->cleanupLuaChunks();
|
2022-07-30 18:52:37 +00:00
|
|
|
$testfunc = new LuaStandaloneInterpreterFunction( $interpreter->id, $id );
|
2013-11-01 00:04:12 +00:00
|
|
|
try {
|
|
|
|
$interpreter->callFunction( $testfunc );
|
|
|
|
$this->fail( "Expected exception because function #3 should have been freed" );
|
2022-08-03 12:31:15 +00:00
|
|
|
} catch ( LuaError $e ) {
|
2013-11-01 00:04:12 +00:00
|
|
|
$this->assertEquals(
|
2015-02-19 22:12:49 +00:00
|
|
|
"function id $id does not exist", $e->messageArgs[1],
|
2013-11-01 00:04:12 +00:00
|
|
|
'Testing for expected error when calling a freed function #3'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
Added tests and fixed bugs
* Added unit tests for the two Lua interpreter classes
* Fixed a bug in checkType()
* Have Scribunto_LuaSandboxInterpreter throw an exception on construct
when the extension doesn't exist, to match the standalone behaviour.
* In Scribunto_LuaSandboxInterpreter, removed debugging statements
accidentally left in.
* Convert LuaSandboxTimeoutError to the appropriate common error
message.
* Moved the option munging from the sandbox engine to the interpreter,
so that the interpreter can be unit tested separately.
* Use /bin/sh instead of bash for lua_ulimit.sh, since dash is smaller
and still supports ulimit.
* Use exec to run the lua binary, so that the vsize of the shell doesn't
add to the memory limit.
* Added a quit function to the standalone interpreter. Unused at present.
* Don't add a comma after the last element of a table in a Lua
expression.
* Make the SIGXCPU detection work: proc_open() runs the command via a
shell, which reports signals in the child via the exit status, so
proc_get_status() will never return a valid termsig element.
* In MWServer:call(), fixed a bug causing the return values to be
wrapped in an array.
* Fixed a misunderstanding of what select() does.
* In MWServer:getStatus(), fixed indexes so that vsize will be correct.
Removed RSS, since it wasn't used anyway and turns out to be measured
in multiples of the page size, and I couldn't be bothered trying to
fetch that from getconf. Return the PID and vsize as numbers rather
than strings.
* Added a simple table dump feature to MWServer:debug().
* Fixed brackets in MWServer:tostring().
* Added missing Linux 32-bit binary.
Change-Id: Ibf5f4656b1c0a9f81287d363184c3fe9d2abdafd
2012-04-16 04:41:08 +00:00
|
|
|
}
|