PhpZipExtResourceTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. final 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. self::assertSame(\strlen($contents), $info->getSize());
  32. }
  33. $zipFile->close();
  34. self::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. self::assertCorrectZipArchive($this->outputFilename);
  52. $zipFile->openFile($this->outputFilename);
  53. self::assertCount(2, $zipFile);
  54. self::assertTrue(isset($zipFile['1.txt']));
  55. self::assertTrue(isset($zipFile['2.txt']));
  56. self::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. self::assertTrue(mkdir($this->outputDirname, 0755, true));
  73. $zipFile = new ZipFile();
  74. $zipFile->openFile($filename);
  75. $zipFile->extractTo($this->outputDirname);
  76. $zipFile->close();
  77. self::assertDirectoryExists($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->expectException(Crc32Exception::class);
  98. $this->expectExceptionMessage('file1');
  99. $filename = __DIR__ . '/resources/pecl/bug49072.zip';
  100. $zipFile = new ZipFile();
  101. $zipFile->openFile($filename);
  102. $zipFile->getEntryContents('file1');
  103. }
  104. /**
  105. * Bug #70752 (Depacking with wrong password leaves 0 length files).
  106. *
  107. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/bug70752.phpt
  108. *
  109. * @throws ZipException
  110. */
  111. public function testBug70752()
  112. {
  113. if (\PHP_INT_SIZE === 4) { // php 32 bit
  114. $this->expectException(RuntimeException::class);
  115. $this->expectExceptionMessage('Traditional PKWARE Encryption is not supported in 32-bit PHP.');
  116. } else { // php 64 bit
  117. $this->expectException(ZipAuthenticationException::class);
  118. $this->expectExceptionMessage('Invalid password');
  119. }
  120. $filename = __DIR__ . '/resources/pecl/bug70752.zip';
  121. self::assertTrue(mkdir($this->outputDirname, 0755, true));
  122. $zipFile = new ZipFile();
  123. $zipFile->openFile($filename);
  124. $zipFile->setReadPassword('bar');
  125. try {
  126. $zipFile->extractTo($this->outputDirname);
  127. self::markTestIncomplete('failed test');
  128. } catch (ZipException $exception) {
  129. self::assertFileDoesNotExist($this->outputDirname . '/bug70752.txt');
  130. throw $exception;
  131. }
  132. }
  133. /**
  134. * Bug #12414 ( extracting files from damaged archives).
  135. *
  136. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/pecl12414.phpt
  137. *
  138. * @throws ZipException
  139. */
  140. public function testPecl12414()
  141. {
  142. $this->expectException(ZipException::class);
  143. $this->expectExceptionMessage('Corrupt zip file. Cannot read zip entry.');
  144. $filename = __DIR__ . '/resources/pecl/pecl12414.zip';
  145. $zipFile = new ZipFile();
  146. $zipFile->openFile($filename);
  147. }
  148. }