PhpZipExtResourceTest.php 5.0 KB

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