| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?php
- namespace PhpZip;
- use PhpZip\Exception\ZipAuthenticationException;
- use PhpZip\Exception\ZipEntryNotFoundException;
- use PhpZip\Exception\ZipException;
- use PhpZip\Model\ZipInfo;
- use PhpZip\Util\CryptoUtil;
- /**
- * Tests with zip password.
- */
- 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.');
- }
- $password = base64_encode(CryptoUtil::randomBytes(100));
- $badPassword = "bad password";
- // create encryption password with ZipCrypto
- $zipFile = new ZipFile();
- $zipFile->addDir(__DIR__);
- $zipFile->setPassword($password, ZipFileInterface::ENCRYPTION_METHOD_TRADITIONAL);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $this->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.");
- } catch (ZipAuthenticationException $ae) {
- $this->assertNotNull($ae);
- }
- }
- // check correct password for ZipCrypto
- $zipFile->setReadPassword($password);
- foreach ($zipFile->getAllInfo() as $info) {
- $this->assertTrue($info->isEncrypted());
- $this->assertContains('ZipCrypto', $info->getMethodName());
- $decryptContent = $zipFile[$info->getName()];
- $this->assertNotEmpty($decryptContent);
- $this->assertContains('<?php', $decryptContent);
- }
- // change encryption method to WinZip Aes and update file
- $zipFile->setPassword($password, ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES);
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $this->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.");
- } catch (ZipAuthenticationException $ae) {
- $this->assertNotNull($ae);
- }
- }
- // set correct password WinZip AES
- $zipFile->setReadPassword($password);
- foreach ($zipFile->getAllInfo() as $info) {
- $this->assertTrue($info->isEncrypted());
- $this->assertContains('WinZip', $info->getMethodName());
- $decryptContent = $zipFile[$info->getName()];
- $this->assertNotEmpty($decryptContent);
- $this->assertContains('<?php', $decryptContent);
- }
- // clear password
- $zipFile->addFromString('file1', '');
- $zipFile->disableEncryption();
- $zipFile->addFromString('file2', '');
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $this->assertCorrectZipArchive($this->outputFilename);
- // check remove password
- $zipFile->openFile($this->outputFilename);
- foreach ($zipFile->getAllInfo() as $info) {
- $this->assertFalse($info->isEncrypted());
- }
- $zipFile->close();
- }
- /**
- * @throws ZipException
- */
- public function testTraditionalEncryption()
- {
- if (PHP_INT_SIZE === 4) {
- $this->markTestSkipped('Skip test for 32-bit system. Not support Traditional PKWARE Encryption.');
- }
- $password = base64_encode(CryptoUtil::randomBytes(50));
- $zip = new ZipFile();
- $zip->addDirRecursive($this->outputDirname);
- $zip->setPassword($password, ZipFileInterface::ENCRYPTION_METHOD_TRADITIONAL);
- $zip->saveAsFile($this->outputFilename);
- $zip->close();
- $this->assertCorrectZipArchive($this->outputFilename, $password);
- $zip->openFile($this->outputFilename);
- $zip->setReadPassword($password);
- $this->assertFilesResult($zip, array_keys(self::$files));
- foreach ($zip->getAllInfo() as $info) {
- if (!$info->isFolder()) {
- $this->assertTrue($info->isEncrypted());
- $this->assertContains('ZipCrypto', $info->getMethodName());
- }
- }
- $zip->close();
- }
- /**
- * @dataProvider winZipKeyStrengthProvider
- * @param int $encryptionMethod
- * @param int $bitSize
- * @throws ZipException
- */
- public function testWinZipAesEncryption($encryptionMethod, $bitSize)
- {
- $password = base64_encode(CryptoUtil::randomBytes(50));
- $zip = new ZipFile();
- $zip->addDirRecursive($this->outputDirname);
- $zip->setPassword($password, $encryptionMethod);
- $zip->saveAsFile($this->outputFilename);
- $zip->close();
- $this->assertCorrectZipArchive($this->outputFilename, $password);
- $zip->openFile($this->outputFilename);
- $zip->setReadPassword($password);
- $this->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());
- }
- }
- $zip->close();
- }
- /**
- * @return array
- */
- public function winZipKeyStrengthProvider()
- {
- return [
- [ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_128, 128],
- [ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_192, 192],
- [ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES, 256],
- [ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES_256, 256],
- ];
- }
- /**
- * @throws Exception\ZipEntryNotFoundException
- * @throws ZipException
- */
- public function testEncryptionEntries()
- {
- if (PHP_INT_SIZE === 4) {
- $this->markTestSkipped('Skip test for 32-bit system. Not support Traditional PKWARE Encryption.');
- }
- $password1 = '353442434235424234';
- $password2 = 'adgerhvrwjhqqehtqhkbqrgewg';
- $zip = new ZipFile();
- $zip->addDir($this->outputDirname);
- $zip->setPasswordEntry('.hidden', $password1, ZipFileInterface::ENCRYPTION_METHOD_TRADITIONAL);
- $zip->setPasswordEntry('text file.txt', $password2, ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES);
- $zip->saveAsFile($this->outputFilename);
- $zip->close();
- $zip->openFile($this->outputFilename);
- $zip->setReadPasswordEntry('.hidden', $password1);
- $zip->setReadPasswordEntry('text file.txt', $password2);
- $this->assertFilesResult($zip, [
- '.hidden',
- 'text file.txt',
- 'Текстовый документ.txt',
- 'empty dir/',
- ]);
- $info = $zip->getEntryInfo('.hidden');
- $this->assertTrue($info->isEncrypted());
- $this->assertContains('ZipCrypto', $info->getMethodName());
- $info = $zip->getEntryInfo('text file.txt');
- $this->assertTrue($info->isEncrypted());
- $this->assertContains('WinZip AES', $info->getMethodName());
- $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) {
- $this->markTestSkipped('Skip test for 32-bit system. Not support Traditional PKWARE Encryption.');
- }
- $password1 = '353442434235424234';
- $password2 = 'adgerhvrwjhqqehtqhkbqrgewg';
- $defaultPassword = ' f f f f f ffff f5 ';
- $zip = new ZipFile();
- $zip->addDir($this->outputDirname);
- $zip->setPassword($defaultPassword);
- $zip->setPasswordEntry('.hidden', $password1, ZipFileInterface::ENCRYPTION_METHOD_TRADITIONAL);
- $zip->setPasswordEntry('text file.txt', $password2, ZipFileInterface::ENCRYPTION_METHOD_WINZIP_AES);
- $zip->saveAsFile($this->outputFilename);
- $zip->close();
- $zip->openFile($this->outputFilename);
- $zip->setReadPassword($defaultPassword);
- $zip->setReadPasswordEntry('.hidden', $password1);
- $zip->setReadPasswordEntry('text file.txt', $password2);
- $this->assertFilesResult($zip, [
- '.hidden',
- 'text file.txt',
- 'Текстовый документ.txt',
- 'empty dir/',
- ]);
- $info = $zip->getEntryInfo('.hidden');
- $this->assertTrue($info->isEncrypted());
- $this->assertContains('ZipCrypto', $info->getMethodName());
- $info = $zip->getEntryInfo('text file.txt');
- $this->assertTrue($info->isEncrypted());
- $this->assertContains('WinZip AES', $info->getMethodName());
- $info = $zip->getEntryInfo('Текстовый документ.txt');
- $this->assertTrue($info->isEncrypted());
- $this->assertContains('WinZip AES', $info->getMethodName());
- $this->assertFalse($zip->getEntryInfo('empty dir/')->isEncrypted());
- $zip->close();
- }
- /**
- * @expectedException \PhpZip\Exception\ZipException
- * @expectedExceptionMessage Invalid encryption method
- */
- public function testSetEncryptionMethodInvalid()
- {
- $zipFile = new ZipFile();
- $encryptionMethod = 9999;
- $zipFile->setPassword('pass', $encryptionMethod);
- $zipFile['entry'] = 'content';
- $zipFile->outputAsString();
- }
- /**
- * @throws Exception\ZipEntryNotFoundException
- * @throws ZipException
- */
- public function testEntryPassword()
- {
- $zipFile = new ZipFile();
- $zipFile->setPassword('pass');
- $zipFile['file'] = 'content';
- $this->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());
- } else {
- $this->assertFalse($zipFile->getEntryInfo('file' . $i)->isEncrypted());
- }
- }
- $zipFile->disableEncryptionEntry('file3');
- $this->assertFalse($zipFile->getEntryInfo('file3')->isEncrypted());
- $this->asserttrue($zipFile->getEntryInfo('file2')->isEncrypted());
- $zipFile->disableEncryption();
- $infoList = $zipFile->getAllInfo();
- array_walk($infoList, function (ZipInfo $zipInfo) {
- $this->assertFalse($zipInfo->isEncrypted());
- });
- $zipFile->close();
- }
- /**
- * @expectedException \PhpZip\Exception\ZipException
- * @expectedExceptionMessage Invalid encryption method
- */
- public function testInvalidEncryptionMethodEntry()
- {
- $zipFile = new ZipFile();
- $zipFile->addFromString('file', 'content', ZipFileInterface::METHOD_STORED);
- $zipFile->setPasswordEntry('file', 'pass', 99);
- }
- /**
- * @throws ZipEntryNotFoundException
- * @throws ZipException
- */
- public function testArchivePasswordUpdateWithoutSetReadPassword()
- {
- $zipFile = new ZipFile();
- $zipFile['file1'] = 'content';
- $zipFile['file2'] = 'content';
- $zipFile['file3'] = 'content';
- $zipFile->setPassword('password');
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile->close();
- $this->assertCorrectZipArchive($this->outputFilename, 'password');
- $zipFile->openFile($this->outputFilename);
- $this->assertCount(3, $zipFile);
- foreach ($zipFile->getAllInfo() as $info) {
- $this->assertTrue($info->isEncrypted());
- }
- unset($zipFile['file3']);
- $zipFile['file4'] = 'content';
- $zipFile->rewrite();
- $this->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');
- $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']);
- $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);
- $password = base64_encode(CryptoUtil::randomBytes(20));
- $encryptMethod = ZipFile::ENCRYPTION_METHOD_WINZIP_AES_256;
- $zipFile = new ZipFile();
- $zipFile
- ->addFromString('codes.csv', $contents)
- ->setPassword($password, $encryptMethod)
- ->saveAsFile($this->outputFilename)
- ->close();
- $this->assertCorrectZipArchive($this->outputFilename, $password);
- $zipFile->openFile($this->outputFilename);
- $zipFile->setReadPassword($password);
- $this->assertEquals($zipFile['codes.csv'], $contents);
- $zipFile->close();
- }
- /**
- * @throws ZipEntryNotFoundException
- * @throws ZipException
- */
- public function testReadAesEncryptedAndRewriteArchive()
- {
- $file = __DIR__ . '/resources/aes_password_archive.zip';
- $password = '1234567890';
- $zipFile = new ZipFile();
- $zipFile->openFile($file);
- $zipFile->setReadPassword($password);
- $zipFile->setEntryComment('contents.txt', 'comment'); // change entry, but not changed contents
- $zipFile->saveAsFile($this->outputFilename);
- $zipFile2 = new ZipFile();
- $zipFile2->openFile($this->outputFilename);
- $zipFile2->setReadPassword($password);
- $this->assertEquals($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);
- }
- $zipFile2->close();
- $zipFile->close();
- }
- }
|