PhpZipExtResourceTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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::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(
  115. RuntimeException::class
  116. );
  117. $this->expectExceptionMessage(
  118. 'Traditional PKWARE Encryption is not supported in 32-bit PHP.'
  119. );
  120. } else { // php 64 bit
  121. $this->expectException(
  122. ZipAuthenticationException::class
  123. );
  124. $this->expectExceptionMessage(
  125. 'Invalid password'
  126. );
  127. }
  128. $filename = __DIR__ . '/resources/pecl/bug70752.zip';
  129. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  130. $zipFile = new ZipFile();
  131. $zipFile->openFile($filename);
  132. $zipFile->setReadPassword('bar');
  133. try {
  134. $zipFile->extractTo($this->outputDirname);
  135. static::markTestIncomplete('failed test');
  136. } catch (ZipException $exception) {
  137. static::assertFileDoesNotExist($this->outputDirname . '/bug70752.txt');
  138. throw $exception;
  139. }
  140. }
  141. /**
  142. * Bug #12414 ( extracting files from damaged archives).
  143. *
  144. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/pecl12414.phpt
  145. *
  146. * @throws ZipException
  147. */
  148. public function testPecl12414()
  149. {
  150. $this->expectException(ZipException::class);
  151. $this->expectExceptionMessage('Corrupt zip file. Cannot read zip entry.');
  152. $filename = __DIR__ . '/resources/pecl/pecl12414.zip';
  153. $zipFile = new ZipFile();
  154. $zipFile->openFile($filename);
  155. }
  156. }