PhpZipExtResourceTest.php 4.9 KB

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