1
0
mirror of https://github.com/Ne-Lexa/php-zip.git synced 2025-07-30 20:20:11 +02:00

PHP-doc updated (@throws added everywhere) and minor refactoring done.

This commit is contained in:
wapplay
2018-10-09 10:06:04 +03:00
parent 59773d62a8
commit f9e6a73587
38 changed files with 942 additions and 684 deletions

View File

@@ -2,7 +2,7 @@
namespace PhpZip;
use PhpZip\Exception\ZipAuthenticationException;
use PhpZip\Exception\ZipException;
/**
* Some tests from the official extension of php-zip.
@@ -12,6 +12,7 @@ class PhpZipExtResourceTest extends ZipTestCase
/**
* 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()
{
@@ -21,16 +22,17 @@ class PhpZipExtResourceTest extends ZipTestCase
$zipFile->openFile($filename);
foreach ($zipFile as $name => $contents) {
$info = $zipFile->getEntryInfo($name);
self::assertEquals(strlen($contents), $info->getSize());
$this->assertEquals(strlen($contents), $info->getSize());
}
$zipFile->close();
self::assertCorrectZipArchive($filename);
$this->assertCorrectZipArchive($filename);
}
/**
* 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()
{
@@ -42,13 +44,13 @@ class PhpZipExtResourceTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertCount(2, $zipFile);
self::assertTrue(isset($zipFile['1.txt']));
self::assertTrue(isset($zipFile['2.txt']));
self::assertEquals($zipFile['2.txt'], $zipFile['1.txt']);
$this->assertCount(2, $zipFile);
$this->assertTrue(isset($zipFile['1.txt']));
$this->assertTrue(isset($zipFile['2.txt']));
$this->assertEquals($zipFile['2.txt'], $zipFile['1.txt']);
$zipFile->close();
}
@@ -58,17 +60,18 @@ class PhpZipExtResourceTest extends ZipTestCase
* @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)
{
self::assertTrue(mkdir($this->outputDirname, 0755, true));
$this->assertTrue(mkdir($this->outputDirname, 0755, true));
$zipFile = new ZipFile();
$zipFile->openFile($filename);
$zipFile->extractTo($this->outputDirname);
$zipFile->close();
self::assertTrue(is_dir($this->outputDirname . '/test/empty'));
$this->assertTrue(is_dir($this->outputDirname . '/test/empty'));
}
public function provideBug40228()
@@ -83,6 +86,7 @@ class PhpZipExtResourceTest extends ZipTestCase
* @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()
{
@@ -98,21 +102,22 @@ class PhpZipExtResourceTest extends ZipTestCase
* @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug70752.phpt
* @expectedException \PhpZip\Exception\ZipAuthenticationException
* @expectedExceptionMessage Bad password for entry bug70752.txt
* @throws ZipException
*/
public function testBug70752()
{
$filename = __DIR__ . '/php-zip-ext-test-resources/bug70752.zip';
self::assertTrue(mkdir($this->outputDirname, 0755, true));
$this->assertTrue(mkdir($this->outputDirname, 0755, true));
$zipFile = new ZipFile();
try {
$zipFile->openFile($filename);
$zipFile->setReadPassword('bar');
$zipFile->extractTo($this->outputDirname);
self::markTestIncomplete('failed test');
} catch (ZipAuthenticationException $exception) {
self::assertFalse(file_exists($this->outputDirname . '/bug70752.txt'));
$this->markTestIncomplete('failed test');
} catch (ZipException $exception) {
$this->assertFalse(file_exists($this->outputDirname . '/bug70752.txt'));
$zipFile->close();
throw $exception;
}
@@ -121,6 +126,7 @@ class PhpZipExtResourceTest extends ZipTestCase
/**
* 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()
{
@@ -132,10 +138,10 @@ class PhpZipExtResourceTest extends ZipTestCase
$zipFile->openFile($filename);
$info = $zipFile->getEntryInfo($entryName);
self::assertTrue($info->getSize() > 0);
$this->assertTrue($info->getSize() > 0);
$contents = $zipFile[$entryName];
self::assertEquals(strlen($contents), $info->getSize());
$this->assertEquals(strlen($contents), $info->getSize());
$zipFile->close();
}

View File

@@ -2,6 +2,7 @@
namespace PhpZip;
use PhpZip\Exception\ZipException;
use PhpZip\Util\CryptoUtil;
/**
@@ -9,14 +10,17 @@ use PhpZip\Util\CryptoUtil;
*/
class ZipAlignTest extends ZipTestCase
{
/**
* @throws ZipException
*/
public function testApkAlignedAndSetZipAlignAndReSave()
{
$filename = __DIR__ . '/resources/test.apk';
self::assertCorrectZipArchive($filename);
$result = self::doZipAlignVerify($filename);
$this->assertCorrectZipArchive($filename);
$result = $this->assertVerifyZipAlign($filename);
if (null !== $result) {
self::assertTrue($result);
$this->assertTrue($result);
}
$zipFile = new ZipFile();
@@ -25,15 +29,16 @@ class ZipAlignTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$result = self::doZipAlignVerify($this->outputFilename, true);
$this->assertCorrectZipArchive($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename, true);
if (null !== $result) {
self::assertTrue($result);
$this->assertTrue($result);
}
}
/**
* Test zip alignment.
* @throws ZipException
*/
public function testZipAlignSourceZip()
{
@@ -48,30 +53,33 @@ class ZipAlignTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$result = self::doZipAlignVerify($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename);
if ($result === null) {
return;
} // zip align not installed
// check not zip align
self::assertFalse($result);
$this->assertFalse($result);
$zipFile->openFile($this->outputFilename);
$zipFile->setZipAlign(4);
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$result = self::doZipAlignVerify($this->outputFilename, true);
self::assertNotNull($result);
$result = $this->assertVerifyZipAlign($this->outputFilename, true);
$this->assertNotNull($result);
// check zip align
self::assertTrue($result);
$this->assertTrue($result);
}
/**
* @throws ZipException
*/
public function testZipAlignNewFiles()
{
$zipFile = new ZipFile();
@@ -86,16 +94,19 @@ class ZipAlignTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$result = self::doZipAlignVerify($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename);
if ($result === null) {
return;
} // zip align not installed
// check not zip align
self::assertTrue($result);
$this->assertTrue($result);
}
/**
* @throws ZipException
*/
public function testZipAlignFromModifiedZipArchive()
{
$zipFile = new ZipFile();
@@ -109,15 +120,15 @@ class ZipAlignTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$result = self::doZipAlignVerify($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename);
if ($result === null) {
return;
} // zip align not installed
// check not zip align
self::assertFalse($result);
$this->assertFalse($result);
$zipFile->openFile($this->outputFilename);
$zipFile->deleteFromRegex("~entry2[\d]+\.txt$~s");
@@ -136,12 +147,12 @@ class ZipAlignTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$result = self::doZipAlignVerify($this->outputFilename, true);
self::assertNotNull($result);
$result = $this->assertVerifyZipAlign($this->outputFilename, true);
$this->assertNotNull($result);
// check zip align
self::assertTrue($result);
$this->assertTrue($result);
}
}

View File

@@ -2,6 +2,8 @@
namespace PhpZip;
use PhpZip\Exception\ZipException;
class ZipFileExtended extends ZipFile
{
protected function onBeforeSave()
@@ -14,29 +16,32 @@ class ZipFileExtended extends ZipFile
class ZipEventTest extends ZipTestCase
{
/**
* @throws ZipException
*/
public function testBeforeSave()
{
$zipFile = new ZipFileExtended();
$zipFile->openFile(__DIR__ . '/resources/test.apk');
self::assertTrue(isset($zipFile['META-INF/MANIFEST.MF']));
self::assertTrue(isset($zipFile['META-INF/CERT.SF']));
self::assertTrue(isset($zipFile['META-INF/CERT.RSA']));
$this->assertTrue(isset($zipFile['META-INF/MANIFEST.MF']));
$this->assertTrue(isset($zipFile['META-INF/CERT.SF']));
$this->assertTrue(isset($zipFile['META-INF/CERT.RSA']));
$zipFile->saveAsFile($this->outputFilename);
self::assertFalse(isset($zipFile['META-INF/MANIFEST.MF']));
self::assertFalse(isset($zipFile['META-INF/CERT.SF']));
self::assertFalse(isset($zipFile['META-INF/CERT.RSA']));
$this->assertFalse(isset($zipFile['META-INF/MANIFEST.MF']));
$this->assertFalse(isset($zipFile['META-INF/CERT.SF']));
$this->assertFalse(isset($zipFile['META-INF/CERT.RSA']));
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$result = self::doZipAlignVerify($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$result = $this->assertVerifyZipAlign($this->outputFilename);
if (null !== $result) {
self::assertTrue($result);
$this->assertTrue($result);
}
$zipFile->openFile($this->outputFilename);
self::assertFalse(isset($zipFile['META-INF/MANIFEST.MF']));
self::assertFalse(isset($zipFile['META-INF/CERT.SF']));
self::assertFalse(isset($zipFile['META-INF/CERT.RSA']));
$this->assertFalse(isset($zipFile['META-INF/MANIFEST.MF']));
$this->assertFalse(isset($zipFile['META-INF/CERT.SF']));
$this->assertFalse(isset($zipFile['META-INF/CERT.RSA']));
$zipFile->close();
}
}

View File

@@ -2,6 +2,7 @@
namespace PhpZip;
use PhpZip\Exception\ZipException;
use PhpZip\Util\Iterator\IgnoreFilesFilterIterator;
use PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator;
@@ -72,6 +73,9 @@ class ZipFileAddDirTest extends ZipTestCase
self::assertEmpty($actualResultFiles);
}
/**
* @throws ZipException
*/
public function testAddDirWithLocalPath()
{
$localPath = 'to/path';
@@ -81,10 +85,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, [
$this->assertFilesResult($zipFile, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
@@ -93,6 +97,9 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->close();
}
/**
* @throws ZipException
*/
public function testAddDirWithoutLocalPath()
{
$zipFile = new ZipFile();
@@ -100,10 +107,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, [
$this->assertFilesResult($zipFile, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
@@ -112,6 +119,9 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->close();
}
/**
* @throws ZipException
*/
public function testAddFilesFromIterator()
{
$localPath = 'to/project';
@@ -123,10 +133,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, [
$this->assertFilesResult($zipFile, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
@@ -135,6 +145,9 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->close();
}
/**
* @throws ZipException
*/
public function testAddFilesFromIteratorEmptyLocalPath()
{
$localPath = '';
@@ -146,10 +159,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, [
$this->assertFilesResult($zipFile, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
@@ -158,6 +171,9 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->close();
}
/**
* @throws ZipException
*/
public function testAddFilesFromRecursiveIterator()
{
$localPath = 'to/project';
@@ -169,13 +185,16 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
$this->assertFilesResult($zipFile, array_keys(self::$files), $localPath);
$zipFile->close();
}
/**
* @throws ZipException
*/
public function testAddRecursiveDirWithLocalPath()
{
$localPath = 'to/path';
@@ -185,13 +204,16 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
$this->assertFilesResult($zipFile, array_keys(self::$files), $localPath);
$zipFile->close();
}
/**
* @throws ZipException
*/
public function testAddRecursiveDirWithoutLocalPath()
{
$zipFile = new ZipFile();
@@ -199,13 +221,16 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, array_keys(self::$files));
$this->assertFilesResult($zipFile, array_keys(self::$files));
$zipFile->close();
}
/**
* @throws ZipException
*/
public function testAddFilesFromIteratorWithIgnoreFiles()
{
$localPath = 'to/project';
@@ -222,16 +247,19 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, [
$this->assertFilesResult($zipFile, [
'.hidden',
'text file.txt',
], $localPath);
$zipFile->close();
}
/**
* @throws ZipException
*/
public function testAddFilesFromRecursiveIteratorWithIgnoreFiles()
{
$localPath = 'to/project';
@@ -250,10 +278,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, [
$this->assertFilesResult($zipFile, [
'text file.txt',
'Текстовый документ.txt',
'empty dir/',
@@ -268,6 +296,7 @@ class ZipFileAddDirTest extends ZipTestCase
/**
* Create archive and add files from glob pattern
* @throws ZipException
*/
public function testAddFilesFromGlob()
{
@@ -278,10 +307,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, [
$this->assertFilesResult($zipFile, [
'text file.txt',
'Текстовый документ.txt',
], $localPath);
@@ -290,6 +319,7 @@ class ZipFileAddDirTest extends ZipTestCase
/**
* Create archive and add recursively files from glob pattern
* @throws ZipException
*/
public function testAddFilesFromGlobRecursive()
{
@@ -300,10 +330,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, [
$this->assertFilesResult($zipFile, [
'text file.txt',
'Текстовый документ.txt',
'category/list.txt',
@@ -317,6 +347,7 @@ class ZipFileAddDirTest extends ZipTestCase
/**
* Create archive and add files from regex pattern
* @throws ZipException
*/
public function testAddFilesFromRegex()
{
@@ -327,10 +358,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, [
$this->assertFilesResult($zipFile, [
'text file.txt',
'Текстовый документ.txt',
], $localPath);
@@ -339,6 +370,7 @@ class ZipFileAddDirTest extends ZipTestCase
/**
* Create archive and add files recursively from regex pattern
* @throws ZipException
*/
public function testAddFilesFromRegexRecursive()
{
@@ -349,10 +381,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, [
$this->assertFilesResult($zipFile, [
'text file.txt',
'Текстовый документ.txt',
'category/list.txt',
@@ -364,6 +396,9 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->close();
}
/**
* @throws ZipException
*/
public function testArrayAccessAddDir()
{
$localPath = 'path/to';
@@ -374,10 +409,10 @@ class ZipFileAddDirTest extends ZipTestCase
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
$zipFile->openFile($this->outputFilename);
self::assertFilesResult($zipFile, array_keys(self::$files), $localPath);
$this->assertFilesResult($zipFile, array_keys(self::$files), $localPath);
$zipFile->close();
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -16,7 +16,7 @@ class ZipMatcherTest extends \PHPUnit_Framework_TestCase
}
$matcher = $zipFile->matcher();
self::assertInstanceOf(ZipEntryMatcher::class, $matcher);
$this->assertInstanceOf(ZipEntryMatcher::class, $matcher);
$this->assertTrue(is_array($matcher->getMatches()));
$this->assertCount(0, $matcher);
@@ -40,7 +40,7 @@ class ZipMatcherTest extends \PHPUnit_Framework_TestCase
$matcher->setPassword('qwerty');
$info = $zipFile->getAllInfo();
array_walk($info, function (ZipInfo $zipInfo) use ($actualMatches) {
self::assertEquals($zipInfo->isEncrypted(), in_array($zipInfo->getName(), $actualMatches));
$this->assertEquals($zipInfo->isEncrypted(), in_array($zipInfo->getName(), $actualMatches));
});
$matcher->all();
@@ -86,12 +86,12 @@ class ZipMatcherTest extends \PHPUnit_Framework_TestCase
];
foreach ($renameEntriesArray as $name) {
self::assertTrue(isset($zipFile[$name]));
$this->assertTrue(isset($zipFile[$name]));
}
$matcher = $zipFile->matcher();
$matcher->match('~^file_(1|5)\d+~');
self::assertEquals($matcher->getMatches(), $renameEntriesArray);
$this->assertEquals($matcher->getMatches(), $renameEntriesArray);
$matcher->invoke(function ($entryName) use ($zipFile) {
$newName = preg_replace('~\.(jpe?g)$~i', '.no_optimize.$1', $entryName);
@@ -99,11 +99,11 @@ class ZipMatcherTest extends \PHPUnit_Framework_TestCase
});
foreach ($renameEntriesArray as $name) {
self::assertFalse(isset($zipFile[$name]));
$this->assertFalse(isset($zipFile[$name]));
$pathInfo = pathinfo($name);
$newName = $pathInfo['filename'].'.no_optimize.'.$pathInfo['extension'];
self::assertTrue(isset($zipFile[$newName]));
$this->assertTrue(isset($zipFile[$newName]));
}
$zipFile->close();

View File

@@ -3,6 +3,8 @@
namespace PhpZip;
use PhpZip\Exception\ZipAuthenticationException;
use PhpZip\Exception\ZipEntryNotFoundException;
use PhpZip\Exception\ZipException;
use PhpZip\Model\ZipInfo;
use PhpZip\Util\CryptoUtil;
@@ -13,6 +15,7 @@ class ZipPasswordTest extends ZipFileAddDirTest
{
/**
* Test archive password.
* @throws ZipException
*/
public function testSetPassword()
{
@@ -30,7 +33,7 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename, $password);
$this->assertCorrectZipArchive($this->outputFilename, $password);
// check bad password for ZipCrypto
$zipFile->openFile($this->outputFilename);
@@ -38,20 +41,20 @@ class ZipPasswordTest extends ZipFileAddDirTest
foreach ($zipFile->getListFiles() as $entryName) {
try {
$zipFile[$entryName];
self::fail("Expected Exception has not been raised.");
$this->fail("Expected Exception has not been raised.");
} catch (ZipAuthenticationException $ae) {
self::assertNotNull($ae);
$this->assertNotNull($ae);
}
}
// check correct password for ZipCrypto
$zipFile->setReadPassword($password);
foreach ($zipFile->getAllInfo() as $info) {
self::assertTrue($info->isEncrypted());
self::assertContains('ZipCrypto', $info->getMethodName());
$this->assertTrue($info->isEncrypted());
$this->assertContains('ZipCrypto', $info->getMethodName());
$decryptContent = $zipFile[$info->getName()];
self::assertNotEmpty($decryptContent);
self::assertContains('<?php', $decryptContent);
$this->assertNotEmpty($decryptContent);
$this->assertContains('<?php', $decryptContent);
}
// change encryption method to WinZip Aes and update file
@@ -59,7 +62,7 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename, $password);
$this->assertCorrectZipArchive($this->outputFilename, $password);
// check from WinZip AES encryption
$zipFile->openFile($this->outputFilename);
@@ -68,20 +71,20 @@ class ZipPasswordTest extends ZipFileAddDirTest
foreach ($zipFile->getListFiles() as $entryName) {
try {
$zipFile[$entryName];
self::fail("Expected Exception has not been raised.");
$this->fail("Expected Exception has not been raised.");
} catch (ZipAuthenticationException $ae) {
self::assertNotNull($ae);
$this->assertNotNull($ae);
}
}
// set correct password WinZip AES
$zipFile->setReadPassword($password);
foreach ($zipFile->getAllInfo() as $info) {
self::assertTrue($info->isEncrypted());
self::assertContains('WinZip', $info->getMethodName());
$this->assertTrue($info->isEncrypted());
$this->assertContains('WinZip', $info->getMethodName());
$decryptContent = $zipFile[$info->getName()];
self::assertNotEmpty($decryptContent);
self::assertContains('<?php', $decryptContent);
$this->assertNotEmpty($decryptContent);
$this->assertContains('<?php', $decryptContent);
}
// clear password
@@ -91,16 +94,19 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename);
$this->assertCorrectZipArchive($this->outputFilename);
// check remove password
$zipFile->openFile($this->outputFilename);
foreach ($zipFile->getAllInfo() as $info) {
self::assertFalse($info->isEncrypted());
$this->assertFalse($info->isEncrypted());
}
$zipFile->close();
}
/**
* @throws ZipException
*/
public function testTraditionalEncryption()
{
if (PHP_INT_SIZE === 4) {
@@ -115,15 +121,15 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zip->saveAsFile($this->outputFilename);
$zip->close();
self::assertCorrectZipArchive($this->outputFilename, $password);
$this->assertCorrectZipArchive($this->outputFilename, $password);
$zip->openFile($this->outputFilename);
$zip->setReadPassword($password);
self::assertFilesResult($zip, array_keys(self::$files));
$this->assertFilesResult($zip, array_keys(self::$files));
foreach ($zip->getAllInfo() as $info) {
if (!$info->isFolder()) {
self::assertTrue($info->isEncrypted());
self::assertContains('ZipCrypto', $info->getMethodName());
$this->assertTrue($info->isEncrypted());
$this->assertContains('ZipCrypto', $info->getMethodName());
}
}
$zip->close();
@@ -133,6 +139,7 @@ class ZipPasswordTest extends ZipFileAddDirTest
* @dataProvider winZipKeyStrengthProvider
* @param int $encryptionMethod
* @param int $bitSize
* @throws ZipException
*/
public function testWinZipAesEncryption($encryptionMethod, $bitSize)
{
@@ -144,16 +151,16 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zip->saveAsFile($this->outputFilename);
$zip->close();
self::assertCorrectZipArchive($this->outputFilename, $password);
$this->assertCorrectZipArchive($this->outputFilename, $password);
$zip->openFile($this->outputFilename);
$zip->setReadPassword($password);
self::assertFilesResult($zip, array_keys(self::$files));
$this->assertFilesResult($zip, array_keys(self::$files));
foreach ($zip->getAllInfo() as $info) {
if (!$info->isFolder()) {
self::assertTrue($info->isEncrypted());
self::assertEquals($info->getEncryptionMethod(), $encryptionMethod);
self::assertContains('WinZip AES-' . $bitSize, $info->getMethodName());
$this->assertTrue($info->isEncrypted());
$this->assertEquals($info->getEncryptionMethod(), $encryptionMethod);
$this->assertContains('WinZip AES-' . $bitSize, $info->getMethodName());
}
}
$zip->close();
@@ -172,6 +179,10 @@ class ZipPasswordTest extends ZipFileAddDirTest
];
}
/**
* @throws Exception\ZipEntryNotFoundException
* @throws ZipException
*/
public function testEncryptionEntries()
{
if (PHP_INT_SIZE === 4) {
@@ -191,7 +202,7 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zip->openFile($this->outputFilename);
$zip->setReadPasswordEntry('.hidden', $password1);
$zip->setReadPasswordEntry('text file.txt', $password2);
self::assertFilesResult($zip, [
$this->assertFilesResult($zip, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
@@ -199,19 +210,23 @@ class ZipPasswordTest extends ZipFileAddDirTest
]);
$info = $zip->getEntryInfo('.hidden');
self::assertTrue($info->isEncrypted());
self::assertContains('ZipCrypto', $info->getMethodName());
$this->assertTrue($info->isEncrypted());
$this->assertContains('ZipCrypto', $info->getMethodName());
$info = $zip->getEntryInfo('text file.txt');
self::assertTrue($info->isEncrypted());
self::assertContains('WinZip AES', $info->getMethodName());
$this->assertTrue($info->isEncrypted());
$this->assertContains('WinZip AES', $info->getMethodName());
self::assertFalse($zip->getEntryInfo('Текстовый документ.txt')->isEncrypted());
self::assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
$this->assertFalse($zip->getEntryInfo('Текстовый документ.txt')->isEncrypted());
$this->assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
$zip->close();
}
/**
* @throws Exception\ZipEntryNotFoundException
* @throws ZipException
*/
public function testEncryptionEntriesWithDefaultPassword()
{
if (PHP_INT_SIZE === 4) {
@@ -234,7 +249,7 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zip->setReadPassword($defaultPassword);
$zip->setReadPasswordEntry('.hidden', $password1);
$zip->setReadPasswordEntry('text file.txt', $password2);
self::assertFilesResult($zip, [
$this->assertFilesResult($zip, [
'.hidden',
'text file.txt',
'Текстовый документ.txt',
@@ -242,18 +257,18 @@ class ZipPasswordTest extends ZipFileAddDirTest
]);
$info = $zip->getEntryInfo('.hidden');
self::assertTrue($info->isEncrypted());
self::assertContains('ZipCrypto', $info->getMethodName());
$this->assertTrue($info->isEncrypted());
$this->assertContains('ZipCrypto', $info->getMethodName());
$info = $zip->getEntryInfo('text file.txt');
self::assertTrue($info->isEncrypted());
self::assertContains('WinZip AES', $info->getMethodName());
$this->assertTrue($info->isEncrypted());
$this->assertContains('WinZip AES', $info->getMethodName());
$info = $zip->getEntryInfo('Текстовый документ.txt');
self::assertTrue($info->isEncrypted());
self::assertContains('WinZip AES', $info->getMethodName());
$this->assertTrue($info->isEncrypted());
$this->assertContains('WinZip AES', $info->getMethodName());
self::assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
$this->assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
$zip->close();
}
@@ -271,28 +286,32 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile->outputAsString();
}
/**
* @throws Exception\ZipEntryNotFoundException
* @throws ZipException
*/
public function testEntryPassword()
{
$zipFile = new ZipFile();
$zipFile->setPassword('pass');
$zipFile['file'] = 'content';
self::assertFalse($zipFile->getEntryInfo('file')->isEncrypted());
$this->assertFalse($zipFile->getEntryInfo('file')->isEncrypted());
for ($i = 1; $i <= 10; $i++) {
$zipFile['file' . $i] = 'content';
if ($i < 6) {
$zipFile->setPasswordEntry('file' . $i, 'pass');
self::assertTrue($zipFile->getEntryInfo('file' . $i)->isEncrypted());
$this->assertTrue($zipFile->getEntryInfo('file' . $i)->isEncrypted());
} else {
self::assertFalse($zipFile->getEntryInfo('file' . $i)->isEncrypted());
$this->assertFalse($zipFile->getEntryInfo('file' . $i)->isEncrypted());
}
}
$zipFile->disableEncryptionEntry('file3');
self::assertFalse($zipFile->getEntryInfo('file3')->isEncrypted());
self::asserttrue($zipFile->getEntryInfo('file2')->isEncrypted());
$this->assertFalse($zipFile->getEntryInfo('file3')->isEncrypted());
$this->asserttrue($zipFile->getEntryInfo('file2')->isEncrypted());
$zipFile->disableEncryption();
$infoList = $zipFile->getAllInfo();
array_walk($infoList, function (ZipInfo $zipInfo) {
self::assertFalse($zipInfo->isEncrypted());
$this->assertFalse($zipInfo->isEncrypted());
});
$zipFile->close();
}
@@ -308,6 +327,10 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile->setPasswordEntry('file', 'pass', 99);
}
/**
* @throws ZipEntryNotFoundException
* @throws ZipException
*/
public function testArchivePasswordUpdateWithoutSetReadPassword()
{
$zipFile = new ZipFile();
@@ -318,37 +341,38 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile->saveAsFile($this->outputFilename);
$zipFile->close();
self::assertCorrectZipArchive($this->outputFilename, 'password');
$this->assertCorrectZipArchive($this->outputFilename, 'password');
$zipFile->openFile($this->outputFilename);
self::assertCount(3, $zipFile);
$this->assertCount(3, $zipFile);
foreach ($zipFile->getAllInfo() as $info) {
self::assertTrue($info->isEncrypted());
$this->assertTrue($info->isEncrypted());
}
unset($zipFile['file3']);
$zipFile['file4'] = 'content';
$zipFile->rewrite();
self::assertCorrectZipArchive($this->outputFilename, 'password');
$this->assertCorrectZipArchive($this->outputFilename, 'password');
self::assertCount(3, $zipFile);
self::assertFalse(isset($zipFile['file3']));
self::assertTrue(isset($zipFile['file4']));
self::assertTrue($zipFile->getEntryInfo('file1')->isEncrypted());
self::assertTrue($zipFile->getEntryInfo('file2')->isEncrypted());
self::assertFalse($zipFile->getEntryInfo('file4')->isEncrypted());
self::assertEquals($zipFile['file4'], 'content');
$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');
$zipFile->extractTo($this->outputDirname, ['file4']);
self::assertTrue(file_exists($this->outputDirname . DIRECTORY_SEPARATOR . 'file4'));
self::assertEquals(file_get_contents($this->outputDirname . DIRECTORY_SEPARATOR . 'file4'), $zipFile['file4']);
$this->assertTrue(file_exists($this->outputDirname . DIRECTORY_SEPARATOR . 'file4'));
$this->assertEquals(file_get_contents($this->outputDirname . DIRECTORY_SEPARATOR . 'file4'), $zipFile['file4']);
$zipFile->close();
}
/**
* @see https://github.com/Ne-Lexa/php-zip/issues/9
* @throws ZipException
*/
public function testIssues9()
{
@@ -371,6 +395,10 @@ class ZipPasswordTest extends ZipFileAddDirTest
$zipFile->close();
}
/**
* @throws ZipEntryNotFoundException
* @throws ZipException
*/
public function testReadAesEncryptedAndRewriteArchive()
{
$file = __DIR__ . '/resources/aes_password_archive.zip';

View File

@@ -79,7 +79,9 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
$command = "7z t -p" . escapeshellarg($password) . " " . escapeshellarg($filename);
exec($command, $output, $returnCode);
/**
* @var array $output
*/
$output = implode(PHP_EOL, $output);
self::assertEquals($returnCode, 0);
@@ -121,7 +123,7 @@ class ZipTestCase extends \PHPUnit_Framework_TestCase
* @param bool $showErrors
* @return bool|null If null - can not install zipalign
*/
public static function doZipAlignVerify($filename, $showErrors = false)
public static function assertVerifyZipAlign($filename, $showErrors = false)
{
if (DIRECTORY_SEPARATOR !== '\\' && `which zipalign`) {
exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode);