PhpZipExtResourceTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Tests;
  10. use PhpZip\Exception\Crc32Exception;
  11. use PhpZip\Exception\RuntimeException;
  12. use PhpZip\Exception\ZipAuthenticationException;
  13. use PhpZip\Exception\ZipException;
  14. use PhpZip\ZipFile;
  15. /**
  16. * Some tests from the official extension of php-zip.
  17. *
  18. * @internal
  19. *
  20. * @small
  21. */
  22. final class PhpZipExtResourceTest extends ZipTestCase
  23. {
  24. /**
  25. * Bug #7214 (zip_entry_read() binary safe).
  26. *
  27. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug7214.phpt
  28. *
  29. * @throws ZipException
  30. */
  31. public function testBinaryNull(): void
  32. {
  33. $filename = __DIR__ . '/resources/pecl/binarynull.zip';
  34. $zipFile = new ZipFile();
  35. $zipFile->openFile($filename);
  36. foreach ($zipFile as $name => $contents) {
  37. $entry = $zipFile->getEntry($name);
  38. self::assertSame(\strlen($contents), $entry->getUncompressedSize());
  39. }
  40. $zipFile->close();
  41. self::assertCorrectZipArchive($filename);
  42. }
  43. /**
  44. * Bug #8009 (cannot add again same entry to an archive).
  45. *
  46. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug8009.phpt
  47. *
  48. * @throws ZipException
  49. */
  50. public function testBug8009(): void
  51. {
  52. $filename = __DIR__ . '/resources/pecl/bug8009.zip';
  53. $zipFile = new ZipFile();
  54. $zipFile->openFile($filename);
  55. $zipFile->addFromString('2.txt', '=)');
  56. $zipFile->saveAsFile($this->outputFilename);
  57. $zipFile->close();
  58. self::assertCorrectZipArchive($this->outputFilename);
  59. $zipFile->openFile($this->outputFilename);
  60. self::assertCount(2, $zipFile);
  61. self::assertTrue(isset($zipFile['1.txt']));
  62. self::assertTrue(isset($zipFile['2.txt']));
  63. self::assertSame($zipFile['2.txt'], $zipFile['1.txt']);
  64. $zipFile->close();
  65. }
  66. /**
  67. * Bug #40228 (extractTo does not create recursive empty path).
  68. *
  69. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug40228.phpt
  70. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug40228-mb.phpt
  71. * @dataProvider provideBug40228
  72. *
  73. * @throws ZipException
  74. */
  75. public function testBug40228(string $filename): void
  76. {
  77. self::assertTrue(mkdir($this->outputDirname, 0755, true));
  78. $zipFile = new ZipFile();
  79. $zipFile->openFile($filename);
  80. $zipFile->extractTo($this->outputDirname);
  81. $zipFile->close();
  82. self::assertDirectoryExists($this->outputDirname . '/test/empty');
  83. }
  84. public function provideBug40228(): array
  85. {
  86. return [
  87. [__DIR__ . '/resources/pecl/bug40228.zip'],
  88. ];
  89. }
  90. /**
  91. * Bug #49072 (feof never returns true for damaged file in zip).
  92. *
  93. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug49072.phpt
  94. *
  95. * @throws ZipException
  96. */
  97. public function testBug49072(): void
  98. {
  99. $this->expectException(Crc32Exception::class);
  100. $this->expectExceptionMessage('file1');
  101. $filename = __DIR__ . '/resources/pecl/bug49072.zip';
  102. $zipFile = new ZipFile();
  103. $zipFile->openFile($filename);
  104. $zipFile->getEntryContents('file1');
  105. }
  106. /**
  107. * Bug #70752 (Depacking with wrong password leaves 0 length files).
  108. *
  109. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug70752.phpt
  110. *
  111. * @throws ZipException
  112. */
  113. public function testBug70752(): void
  114. {
  115. if (\PHP_INT_SIZE === 4) { // php 32 bit
  116. $this->expectException(RuntimeException::class);
  117. $this->expectExceptionMessage('Traditional PKWARE Encryption is not supported in 32-bit PHP.');
  118. } else { // php 64 bit
  119. $this->expectException(ZipAuthenticationException::class);
  120. $this->expectExceptionMessage('Invalid password');
  121. }
  122. $filename = __DIR__ . '/resources/pecl/bug70752.zip';
  123. self::assertTrue(mkdir($this->outputDirname, 0755, true));
  124. $zipFile = new ZipFile();
  125. $zipFile->openFile($filename);
  126. $zipFile->setReadPassword('bar');
  127. try {
  128. $zipFile->extractTo($this->outputDirname);
  129. self::markTestIncomplete('failed test');
  130. } catch (ZipException $exception) {
  131. self::assertFileDoesNotExist($this->outputDirname . '/bug70752.txt');
  132. throw $exception;
  133. }
  134. }
  135. /**
  136. * Bug #12414 (extracting files from damaged archives).
  137. *
  138. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/pecl12414.phpt
  139. *
  140. * @throws ZipException
  141. */
  142. public function testPecl12414(): void
  143. {
  144. $this->expectException(ZipException::class);
  145. $this->expectExceptionMessage('Corrupt zip file. Cannot read zip entry.');
  146. $filename = __DIR__ . '/resources/pecl/pecl12414.zip';
  147. $zipFile = new ZipFile();
  148. $zipFile->openFile($filename);
  149. }
  150. }