2013-01-12 02:34:20 +01:00
|
|
|
<?php
|
2022-02-17 03:25:59 +01:00
|
|
|
|
2013-01-12 02:34:20 +01:00
|
|
|
/**
|
2014-02-15 19:57:49 +01:00
|
|
|
* @author Andreas Fischer <bantu@phpbb.com>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2013 Andreas Fischer
|
2014-02-15 19:57:49 +01:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2013-01-12 02:34:20 +01:00
|
|
|
*/
|
|
|
|
|
2022-06-04 17:31:21 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-02-23 03:48:51 +01:00
|
|
|
namespace phpseclib3\Tests;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
abstract class PhpseclibTestCase extends TestCase
|
2013-01-12 02:34:20 +01:00
|
|
|
{
|
2017-11-27 09:30:14 +01:00
|
|
|
protected $tempFilesToUnlinkOnTearDown = [];
|
2014-12-05 23:24:52 +01:00
|
|
|
|
2022-06-04 17:31:21 +02:00
|
|
|
public function tearDown(): void
|
2014-12-05 23:24:52 +01:00
|
|
|
{
|
|
|
|
foreach ($this->tempFilesToUnlinkOnTearDown as $filename) {
|
|
|
|
if (!file_exists($filename) || unlink($filename)) {
|
|
|
|
unset($this->tempFilesToUnlinkOnTearDown[$filename]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-29 18:07:17 +02: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 17:31:21 +02:00
|
|
|
protected function createTempFile(int $number_of_writes = 0, int $bytes_per_write = 0): string
|
2014-12-05 23:24:52 +01:00
|
|
|
{
|
|
|
|
$filename = tempnam(sys_get_temp_dir(), 'phpseclib-test-');
|
2014-08-21 14:58:57 +02:00
|
|
|
$this->assertTrue(file_exists($filename));
|
2014-12-05 23:24:52 +01:00
|
|
|
$this->tempFilesToUnlinkOnTearDown[] = $filename;
|
2014-08-21 14:58:57 +02: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 23:24:52 +01:00
|
|
|
return $filename;
|
|
|
|
}
|
|
|
|
|
2014-02-15 19:57:49 +01:00
|
|
|
/**
|
2015-03-29 18:07:17 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
2022-06-04 17:31:21 +02:00
|
|
|
protected static function ensureConstant(string $constant, $expected)
|
2014-02-15 19:57:49 +01:00
|
|
|
{
|
|
|
|
if (defined($constant)) {
|
|
|
|
$value = constant($constant);
|
2013-01-12 02:34:20 +01:00
|
|
|
|
2014-02-15 19:57:49 +01:00
|
|
|
if ($value !== $expected) {
|
2016-04-10 18:30:59 +02:00
|
|
|
if (extension_loaded('runkit')) {
|
2014-02-15 19:57:49 +01: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 21:22:01 +01:00
|
|
|
|
2017-01-04 22:33:10 +01:00
|
|
|
protected static function getVar($obj, $var)
|
|
|
|
{
|
2022-02-23 03:48:51 +01:00
|
|
|
$reflection = new \ReflectionClass(get_class($obj));
|
2017-01-04 22:33:10 +01:00
|
|
|
$prop = $reflection->getProperty($var);
|
|
|
|
$prop->setAccessible(true);
|
|
|
|
return $prop->getValue($obj);
|
|
|
|
}
|
2017-01-08 02:51:56 +01:00
|
|
|
|
2017-11-27 09:30:14 +01:00
|
|
|
public static function callFunc($obj, $func, $params = [])
|
2017-01-08 02:51:56 +01:00
|
|
|
{
|
2022-02-23 03:48:51 +01:00
|
|
|
$reflection = new \ReflectionClass(get_class($obj));
|
2017-01-08 02:51:56 +01:00
|
|
|
$method = $reflection->getMethod($func);
|
|
|
|
$method->setAccessible(true);
|
|
|
|
return $method->invokeArgs($obj, $params);
|
|
|
|
}
|
2020-12-13 02:22:36 +01:00
|
|
|
|
2020-12-12 22:11:04 +01:00
|
|
|
// assertIsArray was not introduced until PHPUnit 8
|
2022-06-04 17:31:21 +02:00
|
|
|
public static function assertIsArray($actual, string $message = ''): void
|
2020-12-12 22:11:04 +01:00
|
|
|
{
|
2022-03-09 01:59:30 +01:00
|
|
|
if (method_exists(parent::class, 'assertIsArray')) {
|
2020-12-12 22:11:04 +01:00
|
|
|
parent::assertIsArray($actual, $message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::assertInternalType('array', $actual, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertIsString was not introduced until PHPUnit 8
|
2022-06-04 17:31:21 +02:00
|
|
|
public static function assertIsString($actual, string $message = ''): void
|
2020-12-12 22:11:04 +01:00
|
|
|
{
|
2022-03-09 01:59:30 +01:00
|
|
|
if (method_exists(parent::class, 'assertIsString')) {
|
2020-12-12 22:11:04 +01:00
|
|
|
parent::assertIsString($actual, $message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::assertInternalType('string', $actual, $message);
|
|
|
|
}
|
|
|
|
|
2020-12-13 02:22:36 +01:00
|
|
|
// assertIsResource was not introduced until PHPUnit 8
|
2022-06-04 17:31:21 +02:00
|
|
|
public static function assertIsResource($actual, string $message = ''): void
|
2020-12-13 02:22:36 +01:00
|
|
|
{
|
2022-03-09 01:59:30 +01:00
|
|
|
if (method_exists(parent::class, 'assertIsResource')) {
|
2020-12-13 02:22:36 +01:00
|
|
|
parent::assertIsResource($actual, $message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::assertInternalType('resource', $actual, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertIsObject was not introduced until PHPUnit 8
|
2022-06-04 17:31:21 +02:00
|
|
|
public static function assertIsObject($actual, string $message = ''): void
|
2020-12-13 02:22:36 +01:00
|
|
|
{
|
2022-03-09 01:59:30 +01:00
|
|
|
if (method_exists(parent::class, 'assertIsObject')) {
|
2020-12-13 02:22:36 +01:00
|
|
|
parent::assertIsObject($actual, $message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::assertInternalType('object', $actual, $message);
|
|
|
|
}
|
|
|
|
|
2020-12-12 22:11:04 +01:00
|
|
|
// assertContains is deprecated for strings in PHPUnit 8
|
2022-06-04 17:31:21 +02:00
|
|
|
public static function assertStringContainsString(string $needle, string $haystack, string $message = ''): void
|
2020-12-12 22:11:04 +01:00
|
|
|
{
|
2022-03-09 01:59:30 +01:00
|
|
|
if (method_exists(parent::class, 'assertStringContainsString')) {
|
2020-12-12 22:11:04 +01:00
|
|
|
parent::assertStringContainsString($needle, $haystack, $message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::assertContains($needle, $haystack, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertNotContains is deprecated for strings in PHPUnit 8
|
2022-06-04 17:31:21 +02:00
|
|
|
public static function assertStringNotContainsString(string $needle, string $haystack, string $message = ''): void
|
2020-12-12 22:11:04 +01:00
|
|
|
{
|
2022-03-09 01:59:30 +01:00
|
|
|
if (method_exists(parent::class, 'assertStringContainsString')) {
|
2020-12-12 22:11:04 +01:00
|
|
|
parent::assertStringNotContainsString($needle, $haystack, $message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::assertNotContains($needle, $haystack, $message);
|
|
|
|
}
|
2022-03-09 01:59:30 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* assertRegExp() was deprecated in favor of assertMatchesRegularExpression().
|
|
|
|
*/
|
2022-06-04 17:31:21 +02:00
|
|
|
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
|
2022-03-09 01:59:30 +01:00
|
|
|
{
|
|
|
|
if (method_exists(parent::class, 'assertMatchesRegularExpression')) {
|
|
|
|
parent::assertMatchesRegularExpression($pattern, $string, $message);
|
|
|
|
} else {
|
|
|
|
parent::assertRegExp($pattern, $string, $message);
|
|
|
|
}
|
|
|
|
}
|
2013-01-12 02:34:20 +01:00
|
|
|
}
|