first commit

This commit is contained in:
2024-11-10 21:08:49 +01:00
commit 0d932ce5ee
14455 changed files with 2567501 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
<?php
namespace WebPConvert\Tests\Helpers;
use WebPConvert\Helpers\Sanitize;
use PHPUnit\Framework\TestCase;
/**
* @coversDefaultClass WebPConvert\Helpers\Sanitize
* @covers WebPConvert\Helpers\Sanitize
*/
class SanitizeTest extends TestCase
{
/**
* @covers ::removeNUL
*/
public function testRemoveNUL()
{
$this->assertEquals(
'a',
Sanitize::removeNUL("a\0")
);
}
/**
* @covers ::removeStreamWrappers
*/
public function testRemoveStreamWrappers()
{
$this->assertEquals(
'dytdyt',
Sanitize::removeStreamWrappers("phar://dytdyt")
);
}
}

View File

@@ -0,0 +1,276 @@
<?php
namespace WebPConvert\Tests\Helpers;
use WebPConvert\Helpers\SanityCheck;
use WebPConvert\Exceptions\SanityException;
use PHPUnit\Framework\TestCase;
/**
* @coversDefaultClass WebPConvert\Helpers\SanityCheck
* @covers WebPConvert\Helpers\SanityCheck
*/
class SanityCheckTest extends TestCase
{
/**
* @covers ::noNUL
*/
public function testNoNUL()
{
$this->expectException(SanityException::class);
SanityCheck::noNUL("here it comes: \0");
}
/**
* @covers ::noNUL
*/
public function testNoNUL2()
{
$this->expectException(SanityException::class);
SanityCheck::noNUL("here it comes: " . chr(0));
}
/**
* @covers ::noNUL
*/
public function testNoNUL3()
{
SanityCheck::noNUL("here it does not come");
}
/**
* @covers ::noControlChars
*/
public function testNoControlChars()
{
$this->expectException(SanityException::class);
$sanitized = SanityCheck::noControlChars("..\1..");
}
/**
* @covers ::noControlChars
*/
public function testNoControlChars2()
{
$this->expectException(SanityException::class);
$sanitized = SanityCheck::noControlChars("..\n..");
}
/**
* @covers ::noControlChars
*/
public function testNoControlChars3()
{
//$this->expectException(SanityException::class);
$unsanitized = urldecode("%EF%B8%8F");
//echo 'look:' . $unsanitized;
$sanitized = SanityCheck::noControlChars($unsanitized);
//echo $sanitized;
}
/**
* @covers ::noControlChars
*/
public function testNoControlChars4()
{
//$this->expectException(SanityException::class);
//SanityCheck::noControlChars("Skærmbillede");
//SanityCheck::noControlChars("Skrm");
//SanityCheck::noControlChars("Skærmbillede-2018-10-12-kl.-11.26.38-e1539336533920.png.webp");
SanityCheck::noControlChars("Skærmbillede-2018-10-12-kl.-11.26.38-e1539336533920.png.webp");
$sanitized = SanityCheck::noControlChars("space is ok.");
echo $sanitized;
}
/**
* @covers ::notEmpty
*/
public function testNotEmpty()
{
$this->expectException(SanityException::class);
SanityCheck::notEmpty(null);
}
/**
* @covers ::notEmpty
*/
/*
public function testNotEmpty2()
{
$arr = [];
$this->expectException(SanityException::class);
SanityCheck::notEmpty($arr['not-exist']);
}
*/
/**
* @covers ::notEmpty
*/
public function testNotEmpty2()
{
SanityCheck::notEmpty('..');
}
/**
* @covers ::noDirectoryTraversal
*/
public function testNoDirectoryTraversal()
{
$this->expectException(SanityException::class);
SanityCheck::noDirectoryTraversal('hello/../hi');
}
/**
* @covers ::noStreamWrappers
*/
public function testNoStreamWrappers()
{
$this->expectException(SanityException::class);
SanityCheck::noStreamWrappers('phar://aoeu');
}
/**
* @covers ::noStreamWrappers
*/
public function testNoStreamWrappers2()
{
$this->expectException(SanityException::class);
SanityCheck::noStreamWrappers("phar:\0//aoeu");
}
/**
* @covers ::mustBeString
*/
public function testMustBeString()
{
$this->expectException(SanityException::class);
SanityCheck::mustBeString(0);
}
/**
* @covers ::mustBeString
*/
public function testMustBeString2()
{
SanityCheck::mustBeString('');
SanityCheck::mustBeString('hello');
}
/**
* @covers ::isJSONArray
*/
public function testIsJSONArray()
{
$this->expectException(SanityException::class);
SanityCheck::isJSONArray('');
}
/**
* @covers ::isJSONArray
*/
public function testIsJSONArray2()
{
SanityCheck::isJSONArray('[]');
SanityCheck::isJSONArray('["hello", "hi"]');
}
/**
* @covers ::pregMatch
*/
public function testPregMatch()
{
$this->expectException(SanityException::class);
SanityCheck::pregMatch('#\d#', 'a');
}
/**
* @covers ::pregMatch
*/
public function testPregMatch2()
{
SanityCheck::pregMatch('#\d#', '0');
SanityCheck::pregMatch('#^[a-z]+$#', 'gd');
}
public function testPathBeginsWith()
{
SanityCheck::pathBeginsWith('/var/www/my-site/hello.php', '/var/www/');
}
public function testPathBeginsWith2()
{
$this->expectException(SanityException::class);
SanityCheck::pathBeginsWith('/var/bin/exec', '/var/www/');
}
public function testFindClosestExistingFolderSymLinksExpanded()
{
/*
$this->assertEquals(
'/var/www/webp-express-tests/we0/plugins-moved/webp-express',
SanityCheck::findClosestExistingFolderSymLinksExpanded(
'/var/www/webp-express-tests/we0/plugins-moved/webp-express/i/do/not/exist/test-pattern-tv.jpg'
)
);*/
//echo dirname()
/*
echo 'dir:' . SanityCheck::findClosestExistingFolderSymLinksExpanded(
'/var/www/webp-express-tests/we19/wp-content/webp-express/webp-images/doc-root/wp-content/plugins/webp-express/test/test-pattern-tv.jpg.webp'
);*/
}
/*
public function testPathBeginsWithSymLinksExpanded()
{
$this->expectException(SanityException::class);
SanityCheck::pathBeginsWithSymLinksExpanded(
'/aoeu/var/www/webp-express-tests/we19/wp-content/webp-express/webp-images/doc-root/wp-content/plugins/webp-express/test/test-pattern-tv.jpg.webp',
'/var/www/webp-express-tests/'
);
}
public function testPathBeginsWithSymLinksExpanded2()
{
SanityCheck::pathBeginsWithSymLinksExpanded(
'/var/www/webp-express-tests/we19/wp-content/webp-express/webp-images/doc-root/wp-content/plugins/webp-express/test/test-pattern-tv.jpg.webp',
'/var/www/webp-express-tests/'
);
}
public function testPathBeginsWithSymLinksExpanded3()
{
$this->expectException(SanityException::class);
SanityCheck::pathBeginsWithSymLinksExpanded(
'aoeu/var/www/webp-express-tests/we19/wp-content/webp-express/webp-images/doc-root/wp-content/plugins/webp-express/test/test-pattern-tv.jpg.webp',
'/var/www/webp-express-tests/'
);
}*/
public function testAbsPathMicrosoftStyle()
{
SanityCheck::absPathMicrosoftStyle("C:\\");
SanityCheck::absPathMicrosoftStyle("C:/");
}
public function testAbsPathMicrosoftStyle2()
{
$this->expectException(SanityException::class);
SanityCheck::absPathMicrosoftStyle("C:1");
}
public function testAbsPath()
{
SanityCheck::absPath('/var/bin/exec');
SanityCheck::absPath('var/bin/exec');
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace WebPConvert\Tests\Helpers;
use WebPConvert\Helpers\WarningsIntoExceptions;
use WebPConvert\Exceptions\WarningException;
use PHPUnit\Framework\TestCase;
class WarningsIntoExceptionsTest extends TestCase
{
public function testNothing()
{
$this->assertTrue(true);
}
/*
private static $imgDir = __DIR__ . '/../../images';
public function testUserWarning()
{
WarningsIntoExceptions::activate();
$this->expectException(WarningException::class);
// trigger user warning
trigger_error('warning test', E_USER_WARNING);
WarningsIntoExceptions::deactivate();
}
public function testWarning()
{
WarningsIntoExceptions::activate();
$this->expectException(WarningException::class);
// trigger build-in warning (chmod expects exactly two parameters)
chmod('hth');
WarningsIntoExceptions::deactivate();
}*/
/*
To suppress and capture output from exec calls, you need to redirect the stderr to stdout.
Otherwise it is "echoed to screen"
https://stackoverflow.com/questions/1606943/suppressing-output-from-exec-calls-in-php
public function testWarning2()
{
WarningsIntoExceptions::activate();
//$this->expectException(WarningException);
//ob_start();
exec('hahotehua 2>&1', $output, $returnCode);
//ob_end_clean();
WarningsIntoExceptions::deactivate();
}*/
}