PhpZipExtResourceTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace PhpZip;
  3. use PhpZip\Exception\Crc32Exception;
  4. use PhpZip\Exception\ZipAuthenticationException;
  5. use PhpZip\Exception\ZipException;
  6. /**
  7. * Some tests from the official extension of php-zip.
  8. *
  9. * @internal
  10. *
  11. * @small
  12. * @covers
  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. $this->setExpectedException(ZipAuthenticationException::class, 'nvalid password for zip entry "bug70752.txt"');
  112. $filename = __DIR__ . '/php-zip-ext-test-resources/bug70752.zip';
  113. static::assertTrue(mkdir($this->outputDirname, 0755, true));
  114. $zipFile = new ZipFile();
  115. try {
  116. $zipFile->openFile($filename);
  117. $zipFile->setReadPassword('bar');
  118. $zipFile->extractTo($this->outputDirname);
  119. static::markTestIncomplete('failed test');
  120. } catch (ZipException $exception) {
  121. static::assertFileNotExists($this->outputDirname . '/bug70752.txt');
  122. $zipFile->close();
  123. throw $exception;
  124. }
  125. }
  126. /**
  127. * Bug #12414 ( extracting files from damaged archives).
  128. *
  129. * @see https://github.com/php/php-src/blob/master/ext/zip/tests/pecl12414.phpt
  130. *
  131. * @throws ZipException
  132. */
  133. public function testPecl12414()
  134. {
  135. $filename = __DIR__ . '/php-zip-ext-test-resources/pecl12414.zip';
  136. $entryName = 'MYLOGOV2.GFX';
  137. $zipFile = new ZipFile();
  138. $zipFile->openFile($filename);
  139. $info = $zipFile->getEntryInfo($entryName);
  140. static::assertTrue($info->getSize() > 0);
  141. $contents = $zipFile[$entryName];
  142. static::assertSame(\strlen($contents), $info->getSize());
  143. $zipFile->close();
  144. }
  145. }