ZipTestCase.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace PhpZip\Tests;
  3. use PhpZip\Constants\ZipConstants;
  4. use PhpZip\Tests\Polyfill\LegacyTestCase;
  5. use PhpZip\Util\FilesUtil;
  6. /**
  7. * PHPUnit test case and helper methods.
  8. */
  9. abstract class ZipTestCase extends LegacyTestCase
  10. {
  11. /** @var string */
  12. protected $outputFilename;
  13. /** @var string */
  14. protected $outputDirname;
  15. /**
  16. * Before test.
  17. *
  18. * @noinspection PhpMissingParentCallCommonInspection
  19. */
  20. protected function setUp(): void
  21. {
  22. $id = uniqid('phpzip', false);
  23. $tempDir = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . 'phpunit-phpzip';
  24. if (!is_dir($tempDir) && !mkdir($tempDir, 0755, true) && !is_dir($tempDir)) {
  25. throw new \RuntimeException(sprintf('Directory "%s" was not created', $tempDir));
  26. }
  27. $this->outputFilename = $tempDir . \DIRECTORY_SEPARATOR . $id . '.zip';
  28. $this->outputDirname = $tempDir . \DIRECTORY_SEPARATOR . $id;
  29. }
  30. /**
  31. * After test.
  32. */
  33. protected function tearDown(): void
  34. {
  35. if ($this->outputFilename !== null && file_exists($this->outputFilename)) {
  36. unlink($this->outputFilename);
  37. }
  38. if ($this->outputDirname !== null && is_dir($this->outputDirname)) {
  39. FilesUtil::removeDir($this->outputDirname);
  40. }
  41. }
  42. /**
  43. * Assert correct zip archive.
  44. *
  45. * @param string $filename
  46. * @param string|null $password
  47. */
  48. public static function assertCorrectZipArchive($filename, $password = null)
  49. {
  50. if (self::existsProgram('7z')) {
  51. self::assertCorrectZipArchiveFrom7z($filename, $password);
  52. } elseif (self::existsProgram('unzip')) {
  53. self::assertCorrectZipArchiveFromUnzip($filename, $password);
  54. } else {
  55. fwrite(\STDERR, 'Skipped testing the zip archive for errors using third-party utilities.' . \PHP_EOL);
  56. fwrite(\STDERR, 'To fix this, install 7-zip or unzip.' . \PHP_EOL);
  57. fwrite(\STDERR, \PHP_EOL);
  58. fwrite(\STDERR, 'Install on Ubuntu: sudo apt-get install p7zip-full unzip' . \PHP_EOL);
  59. fwrite(\STDERR, \PHP_EOL);
  60. fwrite(\STDERR, 'Install on Windows:' . \PHP_EOL);
  61. fwrite(\STDERR, ' * 7-zip - https://www.7-zip.org/download.html' . \PHP_EOL);
  62. fwrite(\STDERR, ' * unzip - http://gnuwin32.sourceforge.net/packages/unzip.htm' . \PHP_EOL);
  63. fwrite(\STDERR, \PHP_EOL);
  64. }
  65. }
  66. /**
  67. * @param string $filename
  68. * @param string|null $password
  69. */
  70. private static function assertCorrectZipArchiveFrom7z($filename, $password = null)
  71. {
  72. $command = '7z t';
  73. if ($password !== null) {
  74. $command .= ' -p' . escapeshellarg($password);
  75. }
  76. $command .= ' ' . escapeshellarg($filename) . ' 2>&1';
  77. exec($command, $output, $returnCode);
  78. $output = implode(\PHP_EOL, $output);
  79. static::assertSame($returnCode, 0);
  80. static::assertStringNotContainsString(' Errors', $output);
  81. static::assertStringContainsString(' Ok', $output);
  82. }
  83. /**
  84. * @param string $filename
  85. * @param string|null $password
  86. */
  87. private static function assertCorrectZipArchiveFromUnzip($filename, $password = null)
  88. {
  89. $command = 'unzip';
  90. if ($password !== null) {
  91. $command .= ' -P ' . escapeshellarg($password);
  92. }
  93. $command .= ' -t ' . escapeshellarg($filename) . ' 2>&1';
  94. exec($command, $output, $returnCode);
  95. $output = implode(\PHP_EOL, $output);
  96. if ($password !== null && $returnCode === 81) {
  97. fwrite(\STDERR, 'Program unzip cannot support this function.' . \PHP_EOL);
  98. fwrite(\STDERR, 'You have to install 7-zip to complete this test.' . \PHP_EOL);
  99. fwrite(\STDERR, 'Install 7-Zip on Ubuntu: sudo apt-get install p7zip-full' . \PHP_EOL);
  100. fwrite(\STDERR, 'Install 7-Zip on Windows: https://www.7-zip.org/download.html' . \PHP_EOL);
  101. return;
  102. }
  103. static::assertSame($returnCode, 0, $output);
  104. static::assertNotContains('incorrect password', $output);
  105. static::assertStringContainsString(' OK', $output);
  106. static::assertStringContainsString('No errors', $output);
  107. }
  108. /**
  109. * @param string $program
  110. * @param array $successCodes
  111. *
  112. * @return bool
  113. */
  114. protected static function existsProgram($program, array $successCodes = [0])
  115. {
  116. $command = \DIRECTORY_SEPARATOR === '\\'
  117. ? escapeshellarg($program)
  118. : 'which ' . escapeshellarg($program);
  119. $command .= ' 2>&1';
  120. exec($command, $output, $returnCode);
  121. return \in_array($returnCode, $successCodes, true);
  122. }
  123. /**
  124. * Assert correct empty zip archive.
  125. *
  126. * @param $filename
  127. */
  128. public static function assertCorrectEmptyZip($filename)
  129. {
  130. if (self::existsProgram('zipinfo')) {
  131. exec('zipinfo ' . escapeshellarg($filename), $output, $returnCode);
  132. $output = implode(\PHP_EOL, $output);
  133. static::assertStringContainsString('Empty zipfile', $output);
  134. }
  135. $actualEmptyZipData = pack('VVVVVv', ZipConstants::END_CD, 0, 0, 0, 0, 0);
  136. static::assertStringEqualsFile($filename, $actualEmptyZipData);
  137. }
  138. /**
  139. * @param string $filename
  140. * @param bool $showErrors
  141. *
  142. * @return bool|null If null returned, then the zipalign program is not installed
  143. */
  144. public static function assertVerifyZipAlign($filename, $showErrors = false)
  145. {
  146. if (self::existsProgram('zipalign', [0, 2])) {
  147. exec('zipalign -c -v 4 ' . escapeshellarg($filename), $output, $returnCode);
  148. if ($showErrors && $returnCode !== 0) {
  149. fwrite(\STDERR, implode(\PHP_EOL, $output));
  150. }
  151. return $returnCode === 0;
  152. }
  153. fwrite(\STDERR, "Cannot find the program 'zipalign' for the test" . \PHP_EOL);
  154. fwrite(\STDERR, 'To fix this, install zipalign.' . \PHP_EOL);
  155. fwrite(\STDERR, \PHP_EOL);
  156. fwrite(\STDERR, 'Install on Ubuntu: sudo apt-get install zipalign' . \PHP_EOL);
  157. fwrite(\STDERR, \PHP_EOL);
  158. fwrite(\STDERR, 'Install on Windows:' . \PHP_EOL);
  159. fwrite(\STDERR, ' 1. Install Android Studio' . \PHP_EOL);
  160. fwrite(\STDERR, ' 2. Install Android Sdk' . \PHP_EOL);
  161. fwrite(\STDERR, ' 3. Add zipalign path to $Path' . \PHP_EOL);
  162. return null;
  163. }
  164. public static function skipTestForRootUser()
  165. {
  166. if (\extension_loaded('posix') && posix_getuid() === 0) {
  167. static::markTestSkipped('Skip the test for a user with root privileges');
  168. }
  169. }
  170. public static function skipTestForWindows()
  171. {
  172. if (\DIRECTORY_SEPARATOR === '\\') {
  173. static::markTestSkipped('Skip on Windows');
  174. }
  175. }
  176. }