2013-01-12 01:34:20 +00:00
|
|
|
<?php
|
2022-02-17 02:25:59 +00:00
|
|
|
|
2013-01-12 01:34:20 +00:00
|
|
|
/**
|
2014-02-15 18:57:49 +00:00
|
|
|
* @author Andreas Fischer <bantu@phpbb.com>
|
2014-12-09 23:02:44 +00:00
|
|
|
* @copyright 2013 Andreas Fischer
|
2014-02-15 18:57:49 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2013-01-12 01:34:20 +00:00
|
|
|
*/
|
|
|
|
|
2022-06-04 15:31:21 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-02-23 02:48:51 +00:00
|
|
|
namespace phpseclib3\Tests;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
abstract class PhpseclibTestCase extends TestCase
|
2013-01-12 01:34:20 +00:00
|
|
|
{
|
2017-11-27 08:30:14 +00:00
|
|
|
protected $tempFilesToUnlinkOnTearDown = [];
|
2014-12-05 22:24:52 +00:00
|
|
|
|
2022-06-04 15:31:21 +00:00
|
|
|
public function tearDown(): void
|
2014-12-05 22:24:52 +00:00
|
|
|
{
|
|
|
|
foreach ($this->tempFilesToUnlinkOnTearDown as $filename) {
|
|
|
|
if (!file_exists($filename) || unlink($filename)) {
|
|
|
|
unset($this->tempFilesToUnlinkOnTearDown[$filename]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 16:07:17 +00:00
|
|
|
* Creates a temporary file on the local filesystem and returns its path.
|
|
|
|
* The $number_of_writes and $bytes_per_write parameters can be used to
|
|
|
|
* write $number_of_writes * $bytes_per_write times the character 'a' to the
|
|
|
|
* temporary file. All files created using this method will be deleted from
|
|
|
|
* the filesystem on tearDown(), i.e. after each test method was run.
|
|
|
|
*/
|
2022-06-04 15:31:21 +00:00
|
|
|
protected function createTempFile(int $number_of_writes = 0, int $bytes_per_write = 0): string
|
2014-12-05 22:24:52 +00:00
|
|
|
{
|
|
|
|
$filename = tempnam(sys_get_temp_dir(), 'phpseclib-test-');
|
2014-08-21 12:58:57 +00:00
|
|
|
$this->assertTrue(file_exists($filename));
|
2014-12-05 22:24:52 +00:00
|
|
|
$this->tempFilesToUnlinkOnTearDown[] = $filename;
|
2014-08-21 12:58:57 +00:00
|
|
|
if ($number_of_writes > 0 && $bytes_per_write > 0) {
|
|
|
|
$fp = fopen($filename, 'wb');
|
|
|
|
for ($i = 0; $i < $number_of_writes; ++$i) {
|
|
|
|
fwrite($fp, str_repeat('a', $bytes_per_write));
|
|
|
|
}
|
|
|
|
fclose($fp);
|
|
|
|
$this->assertSame($number_of_writes * $bytes_per_write, filesize($filename));
|
|
|
|
}
|
2014-12-05 22:24:52 +00:00
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
2014-02-15 18:57:49 +00:00
|
|
|
/**
|
2015-03-29 16:07:17 +00:00
|
|
|
* @return null
|
|
|
|
*/
|
2022-06-04 15:31:21 +00:00
|
|
|
protected static function ensureConstant(string $constant, $expected)
|
2014-02-15 18:57:49 +00:00
|
|
|
{
|
|
|
|
if (defined($constant)) {
|
|
|
|
$value = constant($constant);
|
2013-01-12 01:34:20 +00:00
|
|
|
|
2014-02-15 18:57:49 +00:00
|
|
|
if ($value !== $expected) {
|
2016-04-10 16:30:59 +00:00
|
|
|
if (extension_loaded('runkit')) {
|
2014-02-15 18:57:49 +00:00
|
|
|
if (!runkit_constant_redefine($constant, $expected)) {
|
|
|
|
self::markTestSkipped(sprintf(
|
|
|
|
"Failed to redefine constant %s to %s",
|
|
|
|
$constant,
|
|
|
|
$expected
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self::markTestSkipped(sprintf(
|
|
|
|
"Skipping test because constant %s is %s instead of %s",
|
|
|
|
$constant,
|
|
|
|
$value,
|
|
|
|
$expected
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
define($constant, $expected);
|
|
|
|
}
|
|
|
|
}
|
2013-01-12 20:22:01 +00:00
|
|
|
|
2017-01-04 21:33:10 +00:00
|
|
|
protected static function getVar($obj, $var)
|
|
|
|
{
|
2022-08-11 13:25:16 +00:00
|
|
|
$reflection = new \ReflectionClass($obj::class);
|
2024-06-28 13:39:06 +00:00
|
|
|
// private variables are not inherited, climb hierarchy until located
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
$prop = $reflection->getProperty($var);
|
|
|
|
break;
|
|
|
|
} catch (\ReflectionException $e) {
|
|
|
|
$reflection = $reflection->getParentClass();
|
|
|
|
if (!$reflection) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-04 21:33:10 +00:00
|
|
|
$prop->setAccessible(true);
|
|
|
|
return $prop->getValue($obj);
|
|
|
|
}
|
2017-01-08 01:51:56 +00:00
|
|
|
|
2024-07-25 05:27:03 +00:00
|
|
|
protected static function setVar($obj, $var, $value): void
|
2024-06-28 13:39:06 +00:00
|
|
|
{
|
2024-07-25 05:27:03 +00:00
|
|
|
$reflection = new \ReflectionClass($obj::class);
|
2024-06-28 13:39:06 +00:00
|
|
|
// private variables are not inherited, climb hierarchy until located
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
$prop = $reflection->getProperty($var);
|
|
|
|
break;
|
|
|
|
} catch (\ReflectionException $e) {
|
|
|
|
$reflection = $reflection->getParentClass();
|
|
|
|
if (!$reflection) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$prop->setAccessible(true);
|
|
|
|
$prop->setValue($obj, $value);
|
|
|
|
}
|
|
|
|
|
2017-11-27 08:30:14 +00:00
|
|
|
public static function callFunc($obj, $func, $params = [])
|
2017-01-08 01:51:56 +00:00
|
|
|
{
|
2022-08-11 13:25:16 +00:00
|
|
|
$reflection = new \ReflectionClass($obj::class);
|
2017-01-08 01:51:56 +00:00
|
|
|
$method = $reflection->getMethod($func);
|
|
|
|
$method->setAccessible(true);
|
|
|
|
return $method->invokeArgs($obj, $params);
|
|
|
|
}
|
2013-01-12 01:34:20 +00:00
|
|
|
}
|