1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-07-31 12:40:09 +02:00

php cs fix

This commit is contained in:
Ne-Lexa
2019-12-05 19:36:11 +03:00
parent ca068fa78f
commit 3bafe01ff0
62 changed files with 5315 additions and 2459 deletions

View File

@@ -0,0 +1,57 @@
<?php
namespace PhpZip\Internal;
/**
* Try to load using dummy stream.
*/
class DummyFileSystemStream
{
/** @var resource */
private $fp;
public function stream_open($path, $mode, $options, &$opened_path)
{
// echo "DummyFileSystemStream->stream_open($path, $mode, $options)" . PHP_EOL;
$parsedUrl = parse_url($path);
$path = $parsedUrl['path'];
$this->fp = fopen($path, $mode);
return true;
}
public function stream_read($count)
{
// echo "DummyFileSystemStream->stream_read($count)" . PHP_EOL;
$position = ftell($this->fp);
// echo "Loading chunk " . $position . " to " . ($position + $count - 1) . PHP_EOL;
return fread($this->fp, $count);
// echo "String length: " . strlen($ret) . PHP_EOL;
}
public function stream_tell()
{
// echo "DummyFileSystemStream->stream_tell()" . PHP_EOL;
return ftell($this->fp);
}
public function stream_eof()
{
// echo "DummyFileSystemStream->stream_eof()" . PHP_EOL;
return feof($this->fp);
}
public function stream_seek($offset, $whence)
{
// echo "DummyFileSystemStream->stream_seek($offset, $whence)" . PHP_EOL;
fseek($this->fp, $offset, $whence);
}
public function stream_stat()
{
// echo "DummyFileSystemStream->stream_stat()" . PHP_EOL;
return fstat($this->fp);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace PhpZip\Internal;
use PhpZip\ZipFile;
/**
* Class ZipFileExtended.
*/
class ZipFileExtended extends ZipFile
{
protected function onBeforeSave()
{
parent::onBeforeSave();
$this->setZipAlign(4);
$this->deleteFromRegex('~^META\-INF/~i');
}
}

View File

@@ -5,6 +5,12 @@ namespace PhpZip;
use PhpZip\Exception\ZipException;
use PhpZip\Util\CryptoUtil;
/**
* @internal
*
* @small
* @covers
*/
class Issue24Test extends ZipTestCase
{
/**
@@ -12,7 +18,7 @@ class Issue24Test extends ZipTestCase
*/
public static function setUpBeforeClass()
{
stream_wrapper_register("dummyfs", DummyFileSystemStream::class);
stream_wrapper_register('dummyfs', Internal\DummyFileSystemStream::class);
}
/**
@@ -32,73 +38,13 @@ class Issue24Test extends ZipTestCase
$zip->saveAsFile($this->outputFilename);
$zip->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$stream = fopen('dummyfs://localhost/' . $this->outputFilename, 'rb');
$this->assertNotFalse($stream);
static::assertNotFalse($stream);
$zip->openFromStream($stream);
$this->assertEquals($zip->getListFiles(), ['file.txt']);
$this->assertEquals($zip['file.txt'], $fileContents);
static::assertSame($zip->getListFiles(), ['file.txt']);
static::assertSame($zip['file.txt'], $fileContents);
$zip->close();
}
}
/**
* Try to load using dummy stream
*/
class DummyFileSystemStream
{
/**
* @var resource
*/
private $fp;
public function stream_open($path, $mode, $options, &$opened_path)
{
// echo "DummyFileSystemStream->stream_open($path, $mode, $options)" . PHP_EOL;
$parsedUrl = parse_url($path);
$path = $parsedUrl['path'];
$this->fp = fopen($path, $mode);
return true;
}
public function stream_read($count)
{
// echo "DummyFileSystemStream->stream_read($count)" . PHP_EOL;
$position = ftell($this->fp);
// echo "Loading chunk " . $position . " to " . ($position + $count - 1) . PHP_EOL;
$ret = fread($this->fp, $count);
// echo "String length: " . strlen($ret) . PHP_EOL;
return $ret;
}
public function stream_tell()
{
// echo "DummyFileSystemStream->stream_tell()" . PHP_EOL;
return ftell($this->fp);
}
public function stream_eof()
{
// echo "DummyFileSystemStream->stream_eof()" . PHP_EOL;
$isfeof = feof($this->fp);
return $isfeof;
}
public function stream_seek($offset, $whence)
{
// echo "DummyFileSystemStream->stream_seek($offset, $whence)" . PHP_EOL;
fseek($this->fp, $offset, $whence);
}
public function stream_stat()
{
// echo "DummyFileSystemStream->stream_stat()" . PHP_EOL;
return fstat($this->fp);
}
}

View File

@@ -2,16 +2,25 @@
namespace PhpZip;
use PhpZip\Exception\Crc32Exception;
use PhpZip\Exception\ZipAuthenticationException;
use PhpZip\Exception\ZipException;
/**
* Some tests from the official extension of php-zip.
*
* @internal
*
* @small
* @covers
*/
class PhpZipExtResourceTest extends ZipTestCase
{
/**
* Bug #7214 (zip_entry_read() binary safe)
* Bug #7214 (zip_entry_read() binary safe).
*
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug7214.phpt
*
* @throws ZipException
*/
public function testBinaryNull()
@@ -20,18 +29,21 @@ class PhpZipExtResourceTest extends ZipTestCase
$zipFile = new ZipFile();
$zipFile->openFile($filename);
foreach ($zipFile as $name => $contents) {
$info = $zipFile->getEntryInfo($name);
$this->assertEquals(strlen($contents), $info->getSize());
static::assertSame(\strlen($contents), $info->getSize());
}
$zipFile->close();
$this->assertCorrectZipArchive($filename);
static::assertCorrectZipArchive($filename);
}
/**
* Bug #8009 (cannot add again same entry to an archive)
* Bug #8009 (cannot add again same entry to an archive).
*
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug8009.phpt
*
* @throws ZipException
*/
public function testBug8009()
@@ -44,36 +56,42 @@ class PhpZipExtResourceTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertCount(2, $zipFile);
$this->assertTrue(isset($zipFile['1.txt']));
$this->assertTrue(isset($zipFile['2.txt']));
$this->assertEquals($zipFile['2.txt'], $zipFile['1.txt']);
static::assertCount(2, $zipFile);
static::assertTrue(isset($zipFile['1.txt']));
static::assertTrue(isset($zipFile['2.txt']));
static::assertSame($zipFile['2.txt'], $zipFile['1.txt']);
$zipFile->close();
}
/**
* Bug #40228 (extractTo does not create recursive empty path)
* Bug #40228 (extractTo does not create recursive empty path).
*
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug40228.phpt
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug40228-mb.phpt
* @dataProvider provideBug40228
*
* @param string $filename
*
* @throws ZipException
*/
public function testBug40228($filename)
{
$this->assertTrue(mkdir($this->outputDirname, 0755, true));
static::assertTrue(mkdir($this->outputDirname, 0755, true));
$zipFile = new ZipFile();
$zipFile->openFile($filename);
$zipFile->extractTo($this->outputDirname);
$zipFile->close();
$this->assertTrue(is_dir($this->outputDirname . '/test/empty'));
static::assertTrue(is_dir($this->outputDirname . '/test/empty'));
}
/**
* @return array
*/
public function provideBug40228()
{
return [
@@ -82,14 +100,16 @@ class PhpZipExtResourceTest extends ZipTestCase
}
/**
* Bug #49072 (feof never returns true for damaged file in zip)
* Bug #49072 (feof never returns true for damaged file in zip).
*
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug49072.phpt
* @expectedException \PhpZip\Exception\Crc32Exception
* @expectedExceptionMessage file1
*
* @throws ZipException
*/
public function testBug49072()
{
$this->setExpectedException(Crc32Exception::class, 'file1');
$filename = __DIR__ . '/php-zip-ext-test-resources/bug49072.zip';
$zipFile = new ZipFile();
@@ -98,34 +118,40 @@ class PhpZipExtResourceTest extends ZipTestCase
}
/**
* Bug #70752 (Depacking with wrong password leaves 0 length files)
* Bug #70752 (Depacking with wrong password leaves 0 length files).
*
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug70752.phpt
* @expectedException \PhpZip\Exception\ZipAuthenticationException
* @expectedExceptionMessage nvalid password for zip entry "bug70752.txt"
*
* @throws ZipException
*/
public function testBug70752()
{
$this->setExpectedException(ZipAuthenticationException::class, 'nvalid password for zip entry "bug70752.txt"');
$filename = __DIR__ . '/php-zip-ext-test-resources/bug70752.zip';
$this->assertTrue(mkdir($this->outputDirname, 0755, true));
static::assertTrue(mkdir($this->outputDirname, 0755, true));
$zipFile = new ZipFile();
try {
$zipFile->openFile($filename);
$zipFile->setReadPassword('bar');
$zipFile->extractTo($this->outputDirname);
$this->markTestIncomplete('failed test');
static::markTestIncomplete('failed test');
} catch (ZipException $exception) {
$this->assertFalse(file_exists($this->outputDirname . '/bug70752.txt'));
static::assertFileNotExists($this->outputDirname . '/bug70752.txt');
$zipFile->close();
throw $exception;
}
}
/**
* Bug #12414 ( extracting files from damaged archives)
* Bug #12414 ( extracting files from damaged archives).
*
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/pecl12414.phpt
*
* @throws ZipException
*/
public function testPecl12414()
@@ -138,10 +164,10 @@ class PhpZipExtResourceTest extends ZipTestCase
$zipFile->openFile($filename);
$info = $zipFile->getEntryInfo($entryName);
$this->assertTrue($info->getSize() > 0);
static::assertTrue($info->getSize() > 0);
$contents = $zipFile[$entryName];
$this->assertEquals(strlen($contents), $info->getSize());
static::assertSame(\strlen($contents), $info->getSize());
$zipFile->close();
}

View File

@@ -0,0 +1,45 @@
<?php
namespace PhpZip;
use PhpZip\Exception\ZipException;
/**
* @internal
*
* @large
* @covers
*/
class Zip64Test extends ZipTestCase
{
/**
* Test support ZIP64 ext (slow test - normal).
* Create > 65535 files in archive and open and extract to /dev/null.
*
* @throws ZipException
*/
public function testCreateAndOpenZip64Ext()
{
$countFiles = 0xffff + 1;
$zipFile = new ZipFile();
for ($i = 0; $i < $countFiles; $i++) {
$zipFile[$i . '.txt'] = (string) $i;
}
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
static::assertSame($zipFile->count(), $countFiles);
$i = 0;
foreach ($zipFile as $entry => $content) {
static::assertSame($entry, $i . '.txt');
static::assertSame($content, (string) $i);
$i++;
}
$zipFile->close();
}
}

View File

@@ -6,7 +6,12 @@ use PhpZip\Exception\ZipException;
use PhpZip\Util\CryptoUtil;
/**
* Test ZipAlign
* Test ZipAlign.
*
* @internal
*
* @small
* @covers
*/
class ZipAlignTest extends ZipTestCase
{
@@ -17,10 +22,11 @@ class ZipAlignTest extends ZipTestCase
{
$filename = __DIR__ . '/resources/apk.zip';
$this->assertCorrectZipArchive($filename);
$result = $this->assertVerifyZipAlign($filename);
if (null !== $result) {
$this->assertTrue($result);
static::assertCorrectZipArchive($filename);
$result = static::assertVerifyZipAlign($filename);
if ($result !== null) {
static::assertTrue($result);
}
$zipFile = new ZipFile();
@@ -29,15 +35,17 @@ class ZipAlignTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename, true);
if (null !== $result) {
$this->assertTrue($result);
static::assertCorrectZipArchive($this->outputFilename);
$result = static::assertVerifyZipAlign($this->outputFilename, true);
if ($result !== null) {
static::assertTrue($result);
}
}
/**
* Test zip alignment.
*
* @throws ZipException
*/
public function testZipAlignSourceZip()
@@ -53,28 +61,29 @@ class ZipAlignTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$result = static::assertVerifyZipAlign($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename);
if ($result === null) {
return;
} // zip align not installed
// check not zip align
$this->assertFalse($result);
static::assertFalse($result);
$zipFile->openFile($this->outputFilename);
$zipFile->setZipAlign(4);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename, true);
$this->assertNotNull($result);
$result = static::assertVerifyZipAlign($this->outputFilename, true);
static::assertNotNull($result);
// check zip align
$this->assertTrue($result);
static::assertTrue($result);
}
/**
@@ -94,14 +103,15 @@ class ZipAlignTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$result = static::assertVerifyZipAlign($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename);
if ($result === null) {
return;
} // zip align not installed
// check not zip align
$this->assertTrue($result);
static::assertTrue($result);
}
/**
@@ -120,20 +130,21 @@ class ZipAlignTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$result = static::assertVerifyZipAlign($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename);
if ($result === null) {
return;
} // zip align not installed
// check not zip align
$this->assertFalse($result);
static::assertFalse($result);
$zipFile->openFile($this->outputFilename);
$zipFile->deleteFromRegex("~entry2[\d]+\.txt$~s");
$zipFile->deleteFromRegex('~entry2[\\d]+\\.txt$~s');
for ($i = 0; $i < 100; $i++) {
$isStored = (bool)mt_rand(0, 1);
$isStored = (bool) mt_rand(0, 1);
$zipFile->addFromString(
'entry_new_' . ($isStored ? 'stored' : 'deflated') . '_' . $i . '.txt',
@@ -147,12 +158,12 @@ class ZipAlignTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename, true);
$this->assertNotNull($result);
$result = static::assertVerifyZipAlign($this->outputFilename, true);
static::assertNotNull($result);
// check zip align
$this->assertTrue($result);
static::assertTrue($result);
}
}

View File

@@ -4,16 +4,12 @@ namespace PhpZip;
use PhpZip\Exception\ZipException;
class ZipFileExtended extends ZipFile
{
protected function onBeforeSave()
{
parent::onBeforeSave();
$this->setZipAlign(4);
$this->deleteFromRegex('~^META\-INF/~i');
}
}
/**
* @internal
*
* @small
* @covers
*/
class ZipEventTest extends ZipTestCase
{
/**
@@ -21,27 +17,28 @@ class ZipEventTest extends ZipTestCase
*/
public function testBeforeSave()
{
$zipFile = new ZipFileExtended();
$zipFile = new Internal\ZipFileExtended();
$zipFile->openFile(__DIR__ . '/resources/apk.zip');
$this->assertTrue(isset($zipFile['META-INF/MANIFEST.MF']));
$this->assertTrue(isset($zipFile['META-INF/CERT.SF']));
$this->assertTrue(isset($zipFile['META-INF/CERT.RSA']));
static::assertTrue(isset($zipFile['META-INF/MANIFEST.MF']));
static::assertTrue(isset($zipFile['META-INF/CERT.SF']));
static::assertTrue(isset($zipFile['META-INF/CERT.RSA']));
$zipFile->saveAsFile($this->outputFilename);
$this->assertFalse(isset($zipFile['META-INF/MANIFEST.MF']));
$this->assertFalse(isset($zipFile['META-INF/CERT.SF']));
$this->assertFalse(isset($zipFile['META-INF/CERT.RSA']));
static::assertFalse(isset($zipFile['META-INF/MANIFEST.MF']));
static::assertFalse(isset($zipFile['META-INF/CERT.SF']));
static::assertFalse(isset($zipFile['META-INF/CERT.RSA']));
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename);
if (null !== $result) {
$this->assertTrue($result);
static::assertCorrectZipArchive($this->outputFilename);
$result = static::assertVerifyZipAlign($this->outputFilename);
if ($result !== null) {
static::assertTrue($result);
}
$zipFile->openFile($this->outputFilename);
$this->assertFalse(isset($zipFile['META-INF/MANIFEST.MF']));
$this->assertFalse(isset($zipFile['META-INF/CERT.SF']));
$this->assertFalse(isset($zipFile['META-INF/CERT.RSA']));
static::assertFalse(isset($zipFile['META-INF/MANIFEST.MF']));
static::assertFalse(isset($zipFile['META-INF/CERT.SF']));
static::assertFalse(isset($zipFile['META-INF/CERT.RSA']));
$zipFile->close();
}
}

View File

@@ -8,6 +8,11 @@ use PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator;
/**
* Test add directory to zip archive.
*
* @internal
*
* @small
* @covers
*/
class ZipFileAddDirTest extends ZipTestCase
{
@@ -28,7 +33,7 @@ class ZipFileAddDirTest extends ZipTestCase
];
/**
* Before test
* Before test.
*/
protected function setUp()
{
@@ -40,12 +45,14 @@ class ZipFileAddDirTest extends ZipTestCase
{
foreach (self::$files as $name => $content) {
$fullName = $this->outputDirname . '/' . $name;
if ($content === null) {
if (!is_dir($fullName)) {
mkdir($fullName, 0755, true);
}
} else {
$dirname = dirname($fullName);
$dirname = \dirname($fullName);
if (!is_dir($dirname)) {
mkdir($dirname, 0755, true);
}
@@ -54,23 +61,33 @@ class ZipFileAddDirTest extends ZipTestCase
}
}
protected static function assertFilesResult(ZipFileInterface $zipFile, array $actualResultFiles = [], $localPath = '/')
{
/**
* @param ZipFileInterface $zipFile
* @param array $actualResultFiles
* @param string $localPath
*/
protected static function assertFilesResult(
ZipFileInterface $zipFile,
array $actualResultFiles = [],
$localPath = '/'
) {
$localPath = rtrim($localPath, '/');
$localPath = empty($localPath) ? "" : $localPath . '/';
self::assertEquals(sizeof($zipFile), sizeof($actualResultFiles));
$localPath = empty($localPath) ? '' : $localPath . '/';
static::assertCount(\count($zipFile), $actualResultFiles);
$actualResultFiles = array_flip($actualResultFiles);
foreach (self::$files as $file => $content) {
$zipEntryName = $localPath . $file;
if (isset($actualResultFiles[$file])) {
self::assertTrue(isset($zipFile[$zipEntryName]));
self::assertEquals($zipFile[$zipEntryName], $content);
static::assertTrue(isset($zipFile[$zipEntryName]));
static::assertSame($zipFile[$zipEntryName], $content);
unset($actualResultFiles[$file]);
} else {
self::assertFalse(isset($zipFile[$zipEntryName]));
static::assertFalse(isset($zipFile[$zipEntryName]));
}
}
self::assertEmpty($actualResultFiles);
static::assertEmpty($actualResultFiles);
}
/**
@@ -85,15 +102,19 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
], $localPath);
static::assertFilesResult(
$zipFile,
[
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
],
$localPath
);
$zipFile->close();
}
@@ -107,15 +128,18 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
]);
static::assertFilesResult(
$zipFile,
[
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
]
);
$zipFile->close();
}
@@ -133,15 +157,19 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
], $localPath);
static::assertFilesResult(
$zipFile,
[
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
],
$localPath
);
$zipFile->close();
}
@@ -159,15 +187,18 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
]);
static::assertFilesResult(
$zipFile,
[
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
]
);
$zipFile->close();
}
@@ -185,10 +216,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, array_keys(self::$files), $localPath);
static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
$zipFile->close();
}
@@ -204,10 +235,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, array_keys(self::$files), $localPath);
static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
$zipFile->close();
}
@@ -221,10 +252,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, array_keys(self::$files));
static::assertFilesResult($zipFile, array_keys(self::$files));
$zipFile->close();
}
@@ -236,7 +267,7 @@ class ZipFileAddDirTest extends ZipTestCase
$localPath = 'to/project';
$ignoreFiles = [
'Текстовый документ.txt',
'empty dir/'
'empty dir/',
];
$directoryIterator = new \DirectoryIterator($this->outputDirname);
@@ -247,13 +278,17 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, [
'.hidden',
'text file.txt',
], $localPath);
static::assertFilesResult(
$zipFile,
[
'.hidden',
'text file.txt',
],
$localPath
);
$zipFile->close();
}
@@ -278,24 +313,29 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, [
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
'catalog/New File',
'catalog/New File 2',
'catalog/Empty Dir/',
'category/Pictures/128x160/Car/01.jpg',
'category/Pictures/128x160/Car/02.jpg',
], $localPath);
static::assertFilesResult(
$zipFile,
[
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
'catalog/New File',
'catalog/New File 2',
'catalog/Empty Dir/',
'category/Pictures/128x160/Car/01.jpg',
'category/Pictures/128x160/Car/02.jpg',
],
$localPath
);
$zipFile->close();
}
/**
* Create archive and add files from glob pattern
* Create archive and add files from glob pattern.
*
* @throws ZipException
*/
public function testAddFilesFromGlob()
@@ -307,18 +347,23 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, [
'text file.txt',
'Текстовый документ.txt',
], $localPath);
static::assertFilesResult(
$zipFile,
[
'text file.txt',
'Текстовый документ.txt',
],
$localPath
);
$zipFile->close();
}
/**
* Create archive and add recursively files from glob pattern
* Create archive and add recursively files from glob pattern.
*
* @throws ZipException
*/
public function testAddFilesFromGlobRecursive()
@@ -330,23 +375,28 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, [
'text file.txt',
'Текстовый документ.txt',
'category/list.txt',
'category/Pictures/128x160/Car/01.jpg',
'category/Pictures/128x160/Car/02.jpg',
'category/Pictures/240x320/Car/01.jpg',
'category/Pictures/240x320/Car/02.jpg',
], $localPath);
static::assertFilesResult(
$zipFile,
[
'text file.txt',
'Текстовый документ.txt',
'category/list.txt',
'category/Pictures/128x160/Car/01.jpg',
'category/Pictures/128x160/Car/02.jpg',
'category/Pictures/240x320/Car/01.jpg',
'category/Pictures/240x320/Car/02.jpg',
],
$localPath
);
$zipFile->close();
}
/**
* Create archive and add files from regex pattern
* Create archive and add files from regex pattern.
*
* @throws ZipException
*/
public function testAddFilesFromRegex()
@@ -358,18 +408,23 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, [
'text file.txt',
'Текстовый документ.txt',
], $localPath);
static::assertFilesResult(
$zipFile,
[
'text file.txt',
'Текстовый документ.txt',
],
$localPath
);
$zipFile->close();
}
/**
* Create archive and add files recursively from regex pattern
* Create archive and add files recursively from regex pattern.
*
* @throws ZipException
*/
public function testAddFilesFromRegexRecursive()
@@ -381,18 +436,22 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, [
'text file.txt',
'Текстовый документ.txt',
'category/list.txt',
'category/Pictures/128x160/Car/01.jpg',
'category/Pictures/128x160/Car/02.jpg',
'category/Pictures/240x320/Car/01.jpg',
'category/Pictures/240x320/Car/02.jpg',
], $localPath);
static::assertFilesResult(
$zipFile,
[
'text file.txt',
'Текстовый документ.txt',
'category/list.txt',
'category/Pictures/128x160/Car/01.jpg',
'category/Pictures/128x160/Car/02.jpg',
'category/Pictures/240x320/Car/01.jpg',
'category/Pictures/240x320/Car/02.jpg',
],
$localPath
);
$zipFile->close();
}
@@ -409,10 +468,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
$this->assertFilesResult($zipFile, array_keys(self::$files), $localPath);
static::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
$zipFile->close();
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -2,11 +2,18 @@
namespace PhpZip;
use PHPUnit\Framework\TestCase;
use PhpZip\Model\ZipEntryMatcher;
use PhpZip\Model\ZipInfo;
use PhpZip\Util\CryptoUtil;
class ZipMatcherTest extends \PHPUnit_Framework_TestCase
/**
* @internal
*
* @small
* @covers
*/
class ZipMatcherTest extends TestCase
{
public function testMatcher()
{
@@ -16,41 +23,45 @@ class ZipMatcherTest extends \PHPUnit_Framework_TestCase
}
$matcher = $zipFile->matcher();
$this->assertInstanceOf(ZipEntryMatcher::class, $matcher);
static::assertInstanceOf(ZipEntryMatcher::class, $matcher);
$this->assertTrue(is_array($matcher->getMatches()));
$this->assertCount(0, $matcher);
static::assertInternalType('array', $matcher->getMatches());
static::assertCount(0, $matcher);
$matcher->add(1)->add(10)->add(20);
$this->assertCount(3, $matcher);
$this->assertEquals($matcher->getMatches(), ['1', '10', '20']);
static::assertCount(3, $matcher);
static::assertEquals($matcher->getMatches(), ['1', '10', '20']);
$matcher->delete();
$this->assertCount(97, $zipFile);
$this->assertCount(0, $matcher);
static::assertCount(97, $zipFile);
static::assertCount(0, $matcher);
$matcher->match('~^[2][1-5]|[3][6-9]|40$~s');
$this->assertCount(10, $matcher);
static::assertCount(10, $matcher);
$actualMatches = [
'21', '22', '23', '24', '25',
'36', '37', '38', '39',
'40'
'36', '37', '38', '39', '40',
];
$this->assertEquals($matcher->getMatches(), $actualMatches);
static::assertSame($matcher->getMatches(), $actualMatches);
$matcher->setPassword('qwerty');
$info = $zipFile->getAllInfo();
array_walk($info, function (ZipInfo $zipInfo) use ($actualMatches) {
$this->assertEquals($zipInfo->isEncrypted(), in_array($zipInfo->getName(), $actualMatches));
});
array_walk(
$info,
function (ZipInfo $zipInfo) use ($actualMatches) {
$this->assertSame($zipInfo->isEncrypted(), \in_array($zipInfo->getName(), $actualMatches, true));
}
);
$matcher->all();
$this->assertCount(count($zipFile), $matcher);
static::assertCount(\count($zipFile), $matcher);
$expectedNames = [];
$matcher->invoke(function ($entryName) use (&$expectedNames) {
$expectedNames[] = $entryName;
});
$this->assertEquals($expectedNames, $matcher->getMatches());
$matcher->invoke(
static function ($entryName) use (&$expectedNames) {
$expectedNames[] = $entryName;
}
);
static::assertSame($expectedNames, $matcher->getMatches());
$zipFile->close();
}
@@ -59,7 +70,7 @@ class ZipMatcherTest extends \PHPUnit_Framework_TestCase
{
$zipFile = new ZipFile();
for ($i = 0; $i < 100; $i++) {
$zipFile['file_'.$i.'.jpg'] = CryptoUtil::randomBytes(100);
$zipFile['file_' . $i . '.jpg'] = CryptoUtil::randomBytes(100);
}
$renameEntriesArray = [
@@ -86,24 +97,26 @@ class ZipMatcherTest extends \PHPUnit_Framework_TestCase
];
foreach ($renameEntriesArray as $name) {
$this->assertTrue(isset($zipFile[$name]));
static::assertTrue(isset($zipFile[$name]));
}
$matcher = $zipFile->matcher();
$matcher->match('~^file_(1|5)\d+~');
$this->assertEquals($matcher->getMatches(), $renameEntriesArray);
static::assertSame($matcher->getMatches(), $renameEntriesArray);
$matcher->invoke(function ($entryName) use ($zipFile) {
$newName = preg_replace('~\.(jpe?g)$~i', '.no_optimize.$1', $entryName);
$zipFile->rename($entryName, $newName);
});
$matcher->invoke(
static function ($entryName) use ($zipFile) {
$newName = preg_replace('~\.(jpe?g)$~i', '.no_optimize.$1', $entryName);
$zipFile->rename($entryName, $newName);
}
);
foreach ($renameEntriesArray as $name) {
$this->assertFalse(isset($zipFile[$name]));
static::assertFalse(isset($zipFile[$name]));
$pathInfo = pathinfo($name);
$newName = $pathInfo['filename'].'.no_optimize.'.$pathInfo['extension'];
$this->assertTrue(isset($zipFile[$newName]));
$newName = $pathInfo['filename'] . '.no_optimize.' . $pathInfo['extension'];
static::assertTrue(isset($zipFile[$newName]));
}
$zipFile->close();

View File

@@ -10,21 +10,27 @@ use PhpZip\Util\CryptoUtil;
/**
* Tests with zip password.
*
* @internal
*
* @small
* @covers
*/
class ZipPasswordTest extends ZipFileAddDirTest
{
/**
* Test archive password.
*
* @throws ZipException
*/
public function testSetPassword()
{
if (PHP_INT_SIZE === 4) {
$this->markTestSkipped('Skip test for 32-bit system. Not support Traditional PKWARE Encryption.');
if (\PHP_INT_SIZE === 4) {
static::markTestSkipped('Skip test for 32-bit system. Not support Traditional PKWARE Encryption.');
}
$password = base64_encode(CryptoUtil::randomBytes(100));
$badPassword = "bad password";
$badPassword = 'bad password';
// create encryption password with ZipCrypto
$zipFile = new ZipFile();
@@ -33,28 +39,30 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename, $password);
static::assertCorrectZipArchive($this->outputFilename, $password);
// check bad password for ZipCrypto
$zipFile->openFile($this->outputFilename);
$zipFile->setReadPassword($badPassword);
foreach ($zipFile->getListFiles() as $entryName) {
try {
$zipFile[$entryName];
$this->fail("Expected Exception has not been raised.");
static::fail('Expected Exception has not been raised.');
} catch (ZipAuthenticationException $ae) {
$this->assertContains('Invalid password for zip entry', $ae->getMessage());
static::assertContains('Invalid password for zip entry', $ae->getMessage());
}
}
// check correct password for ZipCrypto
$zipFile->setReadPassword($password);
foreach ($zipFile->getAllInfo() as $info) {
$this->assertTrue($info->isEncrypted());
$this->assertContains('ZipCrypto', $info->getMethodName());
static::assertTrue($info->isEncrypted());
static::assertContains('ZipCrypto', $info->getMethodName());
$decryptContent = $zipFile[$info->getName()];
$this->assertNotEmpty($decryptContent);
$this->assertContains('<?php', $decryptContent);
static::assertNotEmpty($decryptContent);
static::assertContains('<?php', $decryptContent);
}
// change encryption method to WinZip Aes and update file
@@ -62,29 +70,31 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename, $password);
static::assertCorrectZipArchive($this->outputFilename, $password);
// check from WinZip AES encryption
$zipFile->openFile($this->outputFilename);
// set bad password WinZip AES
$zipFile->setReadPassword($badPassword);
foreach ($zipFile->getListFiles() as $entryName) {
try {
$zipFile[$entryName];
$this->fail("Expected Exception has not been raised.");
static::fail('Expected Exception has not been raised.');
} catch (ZipAuthenticationException $ae) {
$this->assertNotNull($ae);
static::assertNotNull($ae);
}
}
// set correct password WinZip AES
$zipFile->setReadPassword($password);
foreach ($zipFile->getAllInfo() as $info) {
$this->assertTrue($info->isEncrypted());
$this->assertContains('WinZip', $info->getMethodName());
static::assertTrue($info->isEncrypted());
static::assertContains('WinZip', $info->getMethodName());
$decryptContent = $zipFile[$info->getName()];
$this->assertNotEmpty($decryptContent);
$this->assertContains('<?php', $decryptContent);
static::assertNotEmpty($decryptContent);
static::assertContains('<?php', $decryptContent);
}
// clear password
@@ -94,12 +104,13 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename);
static::assertCorrectZipArchive($this->outputFilename);
// check remove password
$zipFile->openFile($this->outputFilename);
foreach ($zipFile->getAllInfo() as $info) {
$this->assertFalse($info->isEncrypted());
static::assertFalse($info->isEncrypted());
}
$zipFile->close();
}
@@ -109,8 +120,8 @@ class ZipPasswordTest extends ZipFileAddDirTest
*/
public function testTraditionalEncryption()
{
if (PHP_INT_SIZE === 4) {
$this->markTestSkipped('Skip test for 32-bit system. Not support Traditional PKWARE Encryption.');
if (\PHP_INT_SIZE === 4) {
static::markTestSkipped('Skip test for 32-bit system. Not support Traditional PKWARE Encryption.');
}
$password = base64_encode(CryptoUtil::randomBytes(50));
@@ -121,15 +132,16 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zip->saveAsFile($this->outputFilename);
$zip->close();
$this->assertCorrectZipArchive($this->outputFilename, $password);
static::assertCorrectZipArchive($this->outputFilename, $password);
$zip->openFile($this->outputFilename);
$zip->setReadPassword($password);
$this->assertFilesResult($zip, array_keys(self::$files));
static::assertFilesResult($zip, array_keys(self::$files));
foreach ($zip->getAllInfo() as $info) {
if (!$info->isFolder()) {
$this->assertTrue($info->isEncrypted());
$this->assertContains('ZipCrypto', $info->getMethodName());
static::assertTrue($info->isEncrypted());
static::assertContains('ZipCrypto', $info->getMethodName());
}
}
$zip->close();
@@ -137,8 +149,10 @@ class ZipPasswordTest extends ZipFileAddDirTest
/**
* @dataProvider winZipKeyStrengthProvider
*
* @param int $encryptionMethod
* @param int $bitSize
*
* @throws ZipException
*/
public function testWinZipAesEncryption($encryptionMethod, $bitSize)
@@ -151,16 +165,17 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zip->saveAsFile($this->outputFilename);
$zip->close();
$this->assertCorrectZipArchive($this->outputFilename, $password);
static::assertCorrectZipArchive($this->outputFilename, $password);
$zip->openFile($this->outputFilename);
$zip->setReadPassword($password);
$this->assertFilesResult($zip, array_keys(self::$files));
static::assertFilesResult($zip, array_keys(self::$files));
foreach ($zip->getAllInfo() as $info) {
if (!$info->isFolder()) {
$this->assertTrue($info->isEncrypted());
$this->assertEquals($info->getEncryptionMethod(), $encryptionMethod);
$this->assertContains('WinZip AES-' . $bitSize, $info->getMethodName());
static::assertTrue($info->isEncrypted());
static::assertSame($info->getEncryptionMethod(), $encryptionMethod);
static::assertContains('WinZip AES-' . $bitSize, $info->getMethodName());
}
}
$zip->close();
@@ -185,8 +200,8 @@ class ZipPasswordTest extends ZipFileAddDirTest
*/
public function testEncryptionEntries()
{
if (PHP_INT_SIZE === 4) {
$this->markTestSkipped('Skip test for 32-bit system. Not support Traditional PKWARE Encryption.');
if (\PHP_INT_SIZE === 4) {
static::markTestSkipped('Skip test for 32-bit system. Not support Traditional PKWARE Encryption.');
}
$password1 = '353442434235424234';
@@ -202,23 +217,26 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zip->openFile($this->outputFilename);
$zip->setReadPasswordEntry('.hidden', $password1);
$zip->setReadPasswordEntry('text file.txt', $password2);
$this->assertFilesResult($zip, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
]);
static::assertFilesResult(
$zip,
[
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
]
);
$info = $zip->getEntryInfo('.hidden');
$this->assertTrue($info->isEncrypted());
$this->assertContains('ZipCrypto', $info->getMethodName());
static::assertTrue($info->isEncrypted());
static::assertContains('ZipCrypto', $info->getMethodName());
$info = $zip->getEntryInfo('text file.txt');
$this->assertTrue($info->isEncrypted());
$this->assertContains('WinZip AES', $info->getMethodName());
static::assertTrue($info->isEncrypted());
static::assertContains('WinZip AES', $info->getMethodName());
$this->assertFalse($zip->getEntryInfo('Текстовый документ.txt')->isEncrypted());
$this->assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
static::assertFalse($zip->getEntryInfo('Текстовый документ.txt')->isEncrypted());
static::assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
$zip->close();
}
@@ -229,8 +247,8 @@ class ZipPasswordTest extends ZipFileAddDirTest
*/
public function testEncryptionEntriesWithDefaultPassword()
{
if (PHP_INT_SIZE === 4) {
$this->markTestSkipped('Skip test for 32-bit system. Not support Traditional PKWARE Encryption.');
if (\PHP_INT_SIZE === 4) {
static::markTestSkipped('Skip test for 32-bit system. Not support Traditional PKWARE Encryption.');
}
$password1 = '353442434235424234';
@@ -249,36 +267,40 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zip->setReadPassword($defaultPassword);
$zip->setReadPasswordEntry('.hidden', $password1);
$zip->setReadPasswordEntry('text file.txt', $password2);
$this->assertFilesResult($zip, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
]);
static::assertFilesResult(
$zip,
[
'.hidden',
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
]
);
$info = $zip->getEntryInfo('.hidden');
$this->assertTrue($info->isEncrypted());
$this->assertContains('ZipCrypto', $info->getMethodName());
static::assertTrue($info->isEncrypted());
static::assertContains('ZipCrypto', $info->getMethodName());
$info = $zip->getEntryInfo('text file.txt');
$this->assertTrue($info->isEncrypted());
$this->assertContains('WinZip AES', $info->getMethodName());
static::assertTrue($info->isEncrypted());
static::assertContains('WinZip AES', $info->getMethodName());
$info = $zip->getEntryInfo('Текстовый документ.txt');
$this->assertTrue($info->isEncrypted());
$this->assertContains('WinZip AES', $info->getMethodName());
static::assertTrue($info->isEncrypted());
static::assertContains('WinZip AES', $info->getMethodName());
$this->assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
static::assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
$zip->close();
}
/**
* @expectedException \PhpZip\Exception\ZipException
* @expectedExceptionMessage Invalid encryption method
* @throws ZipException
*/
public function testSetEncryptionMethodInvalid()
{
$this->setExpectedException(ZipException::class, 'Invalid encryption method');
$zipFile = new ZipFile();
$encryptionMethod = 9999;
$zipFile->setPassword('pass', $encryptionMethod);
@@ -295,33 +317,38 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile = new ZipFile();
$zipFile->setPassword('pass');
$zipFile['file'] = 'content';
$this->assertFalse($zipFile->getEntryInfo('file')->isEncrypted());
static::assertFalse($zipFile->getEntryInfo('file')->isEncrypted());
for ($i = 1; $i <= 10; $i++) {
$zipFile['file' . $i] = 'content';
if ($i < 6) {
$zipFile->setPasswordEntry('file' . $i, 'pass');
$this->assertTrue($zipFile->getEntryInfo('file' . $i)->isEncrypted());
static::assertTrue($zipFile->getEntryInfo('file' . $i)->isEncrypted());
} else {
$this->assertFalse($zipFile->getEntryInfo('file' . $i)->isEncrypted());
static::assertFalse($zipFile->getEntryInfo('file' . $i)->isEncrypted());
}
}
$zipFile->disableEncryptionEntry('file3');
$this->assertFalse($zipFile->getEntryInfo('file3')->isEncrypted());
$this->asserttrue($zipFile->getEntryInfo('file2')->isEncrypted());
static::assertFalse($zipFile->getEntryInfo('file3')->isEncrypted());
static::assertTrue($zipFile->getEntryInfo('file2')->isEncrypted());
$zipFile->disableEncryption();
$infoList = $zipFile->getAllInfo();
array_walk($infoList, function (ZipInfo $zipInfo) {
$this->assertFalse($zipInfo->isEncrypted());
});
array_walk(
$infoList,
function (ZipInfo $zipInfo) {
$this->assertFalse($zipInfo->isEncrypted());
}
);
$zipFile->close();
}
/**
* @expectedException \PhpZip\Exception\ZipException
* @expectedExceptionMessage Invalid encryption method
* @throws ZipException
*/
public function testInvalidEncryptionMethodEntry()
{
$this->setExpectedException(ZipException::class, 'Invalid encryption method');
$zipFile = new ZipFile();
$zipFile->addFromString('file', 'content', ZipFileInterface::METHOD_STORED);
$zipFile->setPasswordEntry('file', 'pass', 99);
@@ -341,42 +368,44 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
$this->assertCorrectZipArchive($this->outputFilename, 'password');
static::assertCorrectZipArchive($this->outputFilename, 'password');
$zipFile->openFile($this->outputFilename);
$this->assertCount(3, $zipFile);
static::assertCount(3, $zipFile);
foreach ($zipFile->getAllInfo() as $info) {
$this->assertTrue($info->isEncrypted());
static::assertTrue($info->isEncrypted());
}
unset($zipFile['file3']);
$zipFile['file4'] = 'content';
$zipFile->rewrite();
$this->assertCorrectZipArchive($this->outputFilename, 'password');
static::assertCorrectZipArchive($this->outputFilename, 'password');
$this->assertCount(3, $zipFile);
$this->assertFalse(isset($zipFile['file3']));
$this->assertTrue(isset($zipFile['file4']));
$this->assertTrue($zipFile->getEntryInfo('file1')->isEncrypted());
$this->assertTrue($zipFile->getEntryInfo('file2')->isEncrypted());
$this->assertFalse($zipFile->getEntryInfo('file4')->isEncrypted());
$this->assertEquals($zipFile['file4'], 'content');
static::assertCount(3, $zipFile);
static::assertFalse(isset($zipFile['file3']));
static::assertTrue(isset($zipFile['file4']));
static::assertTrue($zipFile->getEntryInfo('file1')->isEncrypted());
static::assertTrue($zipFile->getEntryInfo('file2')->isEncrypted());
static::assertFalse($zipFile->getEntryInfo('file4')->isEncrypted());
static::assertSame($zipFile['file4'], 'content');
$zipFile->extractTo($this->outputDirname, ['file4']);
$this->assertTrue(file_exists($this->outputDirname . DIRECTORY_SEPARATOR . 'file4'));
$this->assertEquals(file_get_contents($this->outputDirname . DIRECTORY_SEPARATOR . 'file4'), $zipFile['file4']);
static::assertFileExists($this->outputDirname . \DIRECTORY_SEPARATOR . 'file4');
static::assertStringEqualsFile($this->outputDirname . \DIRECTORY_SEPARATOR . 'file4', $zipFile['file4']);
$zipFile->close();
}
/**
* @see https://github.com/Ne-Lexa/php-zip/issues/9
*
* @throws ZipException
*/
public function testIssues9()
{
$contents = str_pad('', 1000, 'test;test2;test3' . PHP_EOL, STR_PAD_RIGHT);
$contents = str_pad('', 1000, 'test;test2;test3' . \PHP_EOL, \STR_PAD_RIGHT);
$password = base64_encode(CryptoUtil::randomBytes(20));
$encryptMethod = ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256;
@@ -385,13 +414,14 @@ class ZipPasswordTest extends ZipFileAddDirTest
->addFromString('codes.csv', $contents)
->setPassword($password, $encryptMethod)
->saveAsFile($this->outputFilename)
->close();
->close()
;
$this->assertCorrectZipArchive($this->outputFilename, $password);
static::assertCorrectZipArchive($this->outputFilename, $password);
$zipFile->openFile($this->outputFilename);
$zipFile->setReadPassword($password);
$this->assertEquals($zipFile['codes.csv'], $contents);
static::assertSame($zipFile['codes.csv'], $contents);
$zipFile->close();
}
@@ -413,12 +443,13 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile2 = new ZipFile();
$zipFile2->openFile($this->outputFilename);
$zipFile2->setReadPassword($password);
$this->assertEquals($zipFile2->getListFiles(), $zipFile->getListFiles());
static::assertSame($zipFile2->getListFiles(), $zipFile->getListFiles());
foreach ($zipFile as $name => $contents) {
$this->assertNotEmpty($name);
$this->assertNotEmpty($contents);
$this->assertContains('test contents', $contents);
$this->assertEquals($zipFile2[$name], $contents);
static::assertNotEmpty($name);
static::assertNotEmpty($contents);
static::assertContains('test contents', $contents);
static::assertSame($zipFile2[$name], $contents);
}
$zipFile2->close();

View File

@@ -3,20 +3,17 @@
namespace PhpZip;
use PhpZip\Exception\ZipException;
use PhpZip\Util\Iterator\IgnoreFilesFilterIterator;
use PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator;
/**
* Test add remote files to zip archive
* Test add remote files to zip archive.
*
* @internal
*
* @small
* @covers
*/
class ZipRemoteFileTest extends ZipTestCase
{
protected function setUp()
{
parent::setUp();
}
/**
* @throws ZipException
*/
@@ -25,16 +22,28 @@ class ZipRemoteFileTest extends ZipTestCase
$zipFile = new ZipFile();
$outputZip = $this->outputFilename;
$fileUrl = 'https://raw.githubusercontent.com/Ne-Lexa/php-zip/master/README.md';
$fp = @fopen($fileUrl, 'rb', false, stream_context_create([
'http' => [
'timeout' => 3,
]
]));
/** @noinspection PhpUsageOfSilenceOperatorInspection */
$fp = @fopen(
$fileUrl,
'rb',
false,
stream_context_create(
[
'http' => [
'timeout' => 3,
],
]
)
);
if ($fp === false) {
self::markTestSkipped(sprintf(
"Could not fetch remote file: %s",
$fileUrl
));
static::markTestSkipped(
sprintf(
'Could not fetch remote file: %s',
$fileUrl
)
);
return;
}
@@ -47,9 +56,8 @@ class ZipRemoteFileTest extends ZipTestCase
$zipFile = new ZipFile();
$zipFile->openFile($outputZip);
$files = $zipFile->getListFiles();
self::assertCount(1, $files);
self::assertSame($fileName, $files[0]);
static::assertCount(1, $files);
static::assertSame($fileName, $files[0]);
$zipFile->close();
}
}

View File

@@ -3,11 +3,15 @@
namespace PhpZip;
/**
* Class ZipSlipVulnerabilityTest
* Class ZipSlipVulnerabilityTest.
*
* @package PhpZip
* @see https://github.com/Ne-Lexa/php-zip/issues/39 Issue#31
* @see https://snyk.io/research/zip-slip-vulnerability Zip Slip Vulnerability
*
* @internal
*
* @small
* @covers
*/
class ZipSlipVulnerabilityTest extends ZipTestCase
{
@@ -19,7 +23,7 @@ class ZipSlipVulnerabilityTest extends ZipTestCase
$localFile = '../dir/./../../file.txt';
$zipFile = new ZipFile();
$zipFile->addFromString($localFile, 'contents');
self::assertContains($localFile, $zipFile->getListFiles());
static::assertContains($localFile, $zipFile->getListFiles());
$zipFile->close();
}
@@ -28,7 +32,7 @@ class ZipSlipVulnerabilityTest extends ZipTestCase
*/
public function testUnpack()
{
$this->assertTrue(mkdir($this->outputDirname, 0755, true));
static::assertTrue(mkdir($this->outputDirname, 0755, true));
$zipFile = new ZipFile();
$zipFile->addFromString('../dir/./../../file.txt', 'contents');
@@ -36,6 +40,6 @@ class ZipSlipVulnerabilityTest extends ZipTestCase
$zipFile->close();
$expectedExtractedFile = $this->outputDirname . '/dir/file.txt';
self::assertTrue(is_file($expectedExtractedFile));
static::assertTrue(is_file($expectedExtractedFile));
}
}

View File

@@ -2,32 +2,29 @@
namespace PhpZip;
use PHPUnit\Framework\TestCase;
use PhpZip\Model\EndOfCentralDirectory;
use PhpZip\Util\FilesUtil;
/**
* PHPUnit test case and helper methods.
*/
class ZipTestCase extends \PHPUnit_Framework_TestCase
abstract class ZipTestCase extends TestCase
{
/**
* @var string
*/
/** @var string */
protected $outputFilename;
/**
* @var string
*/
/** @var string */
protected $outputDirname;
/**
* Before test
* Before test.
*/
protected function setUp()
{
parent::setUp();
$id = uniqid('phpzip', true);
$tempDir = sys_get_temp_dir() . '/phpunit-phpzip';
if (!is_dir($tempDir) && !mkdir($tempDir, 0755, true) && !is_dir($tempDir)) {
throw new \RuntimeException('Dir ' . $tempDir . " can't created");
}
@@ -36,7 +33,7 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
}
/**
* After test
* After test.
*/
protected function tearDown()
{
@@ -45,6 +42,7 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
if ($this->outputFilename !== null && file_exists($this->outputFilename)) {
unlink($this->outputFilename);
}
if ($this->outputDirname !== null && is_dir($this->outputDirname)) {
FilesUtil::removeDir($this->outputDirname);
}
@@ -53,25 +51,29 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
/**
* Assert correct zip archive.
*
* @param string $filename
* @param string $filename
* @param string|null $password
*/
public static function assertCorrectZipArchive($filename, $password = null)
{
if (self::existsProgram('unzip')) {
$command = 'unzip';
if ($password !== null) {
$command .= ' -P ' . escapeshellarg($password);
}
$command .= ' -t ' . escapeshellarg($filename);
exec($command, $output, $returnCode);
$output = implode(PHP_EOL, $output);
$output = implode(\PHP_EOL, $output);
if ($password !== null && $returnCode === 81) {
if (self::existsProgram('7z')) {
// WinZip 99-character limit
// @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
/**
* WinZip 99-character limit.
*
* @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
*/
$password = substr($password, 0, 99);
$command = '7z t -p' . escapeshellarg($password) . ' ' . escapeshellarg($filename);
@@ -79,31 +81,34 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
/**
* @var array $output
*/
$output = implode(PHP_EOL, $output);
$output = implode(\PHP_EOL, $output);
self::assertEquals($returnCode, 0);
self::assertNotContains(' Errors', $output);
self::assertContains(' Ok', $output);
static::assertSame($returnCode, 0);
static::assertNotContains(' Errors', $output);
static::assertContains(' Ok', $output);
} else {
fwrite(STDERR, 'Program unzip cannot support this function.' . PHP_EOL);
fwrite(STDERR, 'Please install 7z. For Ubuntu-like: sudo apt-get install p7zip-full' . PHP_EOL);
fwrite(\STDERR, 'Program unzip cannot support this function.' . \PHP_EOL);
fwrite(\STDERR, 'Please install 7z. For Ubuntu-like: sudo apt-get install p7zip-full' . \PHP_EOL);
}
} else {
self::assertEquals($returnCode, 0, $output);
self::assertNotContains('incorrect password', $output);
self::assertContains(' OK', $output);
self::assertContains('No errors', $output);
static::assertSame($returnCode, 0, $output);
static::assertNotContains('incorrect password', $output);
static::assertContains(' OK', $output);
static::assertContains('No errors', $output);
}
}
}
/**
* @param string $program
*
* @return bool
*/
private static function existsProgram($program){
if (DIRECTORY_SEPARATOR !== '\\') {
private static function existsProgram($program)
{
if (\DIRECTORY_SEPARATOR !== '\\') {
exec('which ' . escapeshellarg($program), $output, $returnCode);
return $returnCode === 0;
}
// false for Windows
@@ -120,30 +125,34 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
if (self::existsProgram('zipinfo')) {
exec('zipinfo ' . escapeshellarg($filename), $output, $returnCode);
$output = implode(PHP_EOL, $output);
$output = implode(\PHP_EOL, $output);
self::assertContains('Empty zipfile', $output);
static::assertContains('Empty zipfile', $output);
}
$actualEmptyZipData = pack('VVVVVv', EndOfCentralDirectory::END_OF_CENTRAL_DIRECTORY_RECORD_SIG, 0, 0, 0, 0, 0);
self::assertStringEqualsFile($filename, $actualEmptyZipData);
static::assertStringEqualsFile($filename, $actualEmptyZipData);
}
/**
* @param string $filename
* @param bool $showErrors
* @return bool|null If null - can not install zipalign
* @param bool $showErrors
*
* @return bool|null If null returned, then the zipalign program is not installed
*/
public static function assertVerifyZipAlign($filename, $showErrors = false)
{
if (self::existsProgram('zipalign')) {
exec('zipalign -c -v 4 ' . escapeshellarg($filename), $output, $returnCode);
if ($showErrors && $returnCode !== 0) {
fwrite(STDERR, implode(PHP_EOL, $output));
fwrite(\STDERR, implode(\PHP_EOL, $output));
}
return $returnCode === 0;
}
fwrite(STDERR, 'Can not find program "zipalign" for test' . PHP_EOL);
fwrite(\STDERR, "Cannot find the program 'zipalign' for the test" . \PHP_EOL);
return null;
}
}