2
0

PhpZipExtResourceTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. *
  72. * @dataProvider provideBug40228
  73. *
  74. * @throws ZipException
  75. */
  76. public function testBug40228(string $filename): void
  77. {
  78. self::assertTrue(mkdir($this->outputDirname, 0755, true));
  79. $zipFile = new ZipFile();
  80. $zipFile->openFile($filename);
  81. $zipFile->extractTo($this->outputDirname);
  82. $zipFile->close();
  83. self::assertDirectoryExists($this->outputDirname . '/test/empty');
  84. }
  85. public function provideBug40228(): array
  86. {
  87. return [
  88. [__DIR__ . '/resources/pecl/bug40228.zip'],
  89. ];
  90. }
  91. /**
  92. * Bug #49072 (feof never returns true for damaged file in zip).
  93. *
  94. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug49072.phpt
  95. *
  96. * @throws ZipException
  97. */
  98. public function testBug49072(): void
  99. {
  100. $this->expectException(Crc32Exception::class);
  101. $this->expectExceptionMessage('file1');
  102. $filename = __DIR__ . '/resources/pecl/bug49072.zip';
  103. $zipFile = new ZipFile();
  104. $zipFile->openFile($filename);
  105. $zipFile->getEntryContents('file1');
  106. }
  107. /**
  108. * Bug #70752 (Depacking with wrong password leaves 0 length files).
  109. *
  110. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug70752.phpt
  111. *
  112. * @throws ZipException
  113. */
  114. public function testBug70752(): void
  115. {
  116. if (\PHP_INT_SIZE === 4) { // php 32 bit
  117. $this->expectException(RuntimeException::class);
  118. $this->expectExceptionMessage('Traditional PKWARE Encryption is not supported in 32-bit PHP.');
  119. } else { // php 64 bit
  120. $this->expectException(ZipAuthenticationException::class);
  121. $this->expectExceptionMessage('Invalid password');
  122. }
  123. $filename = __DIR__ . '/resources/pecl/bug70752.zip';
  124. self::assertTrue(mkdir($this->outputDirname, 0755, true));
  125. $zipFile = new ZipFile();
  126. $zipFile->openFile($filename);
  127. $zipFile->setReadPassword('bar');
  128. try {
  129. $zipFile->extractTo($this->outputDirname);
  130. self::markTestIncomplete('failed test');
  131. } catch (ZipException $exception) {
  132. self::assertFileDoesNotExist($this->outputDirname . '/bug70752.txt');
  133. throw $exception;
  134. }
  135. }
  136. /**
  137. * Bug #12414 (extracting files from damaged archives).
  138. *
  139. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/pecl12414.phpt
  140. *
  141. * @throws ZipException
  142. */
  143. public function testPecl12414(): void
  144. {
  145. $this->expectException(ZipException::class);
  146. $this->expectExceptionMessage('Corrupt zip file. Cannot read zip entry.');
  147. $filename = __DIR__ . '/resources/pecl/pecl12414.zip';
  148. $zipFile = new ZipFile();
  149. $zipFile->openFile($filename);
  150. }
  151. }