2
0

ZipTestCase.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace PhpZip;
  3. use PhpZip\Model\EndOfCentralDirectory;
  4. /**
  5. * PHPUnit test case and helper methods.
  6. */
  7. class ZipTestCase extends \PHPUnit_Framework_TestCase
  8. {
  9. /**
  10. * Assert correct zip archive.
  11. *
  12. * @param string $filename
  13. * @param string|null $password
  14. */
  15. public static function assertCorrectZipArchive($filename, $password = null)
  16. {
  17. if (DIRECTORY_SEPARATOR !== '\\' && `which unzip`) {
  18. $command = "unzip";
  19. if ($password !== null) {
  20. $command .= " -P " . escapeshellarg($password);
  21. }
  22. $command .= " -t " . escapeshellarg($filename);
  23. exec($command, $output, $returnCode);
  24. $output = implode(PHP_EOL, $output);
  25. if ($password !== null && $returnCode === 81) {
  26. if (`which 7z`) {
  27. // WinZip 99-character limit
  28. // @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
  29. $password = substr($password, 0, 99);
  30. $command = "7z t -p" . escapeshellarg($password) . " " . escapeshellarg($filename);
  31. exec($command, $output, $returnCode);
  32. $output = implode(PHP_EOL, $output);
  33. self::assertEquals($returnCode, 0);
  34. self::assertNotContains(' Errors', $output);
  35. self::assertContains(' Ok', $output);
  36. } else {
  37. fwrite(STDERR, 'Program unzip cannot support this function.' . PHP_EOL);
  38. fwrite(STDERR, 'Please install 7z. For Ubuntu-like: sudo apt-get install p7zip-full' . PHP_EOL);
  39. }
  40. } else {
  41. self::assertEquals($returnCode, 0, $output);
  42. self::assertNotContains('incorrect password', $output);
  43. self::assertContains(' OK', $output);
  44. self::assertContains('No errors', $output);
  45. }
  46. }
  47. }
  48. /**
  49. * Assert correct empty zip archive.
  50. *
  51. * @param $filename
  52. */
  53. public static function assertCorrectEmptyZip($filename)
  54. {
  55. if (DIRECTORY_SEPARATOR !== '\\' && `which zipinfo`) {
  56. exec("zipinfo " . escapeshellarg($filename), $output, $returnCode);
  57. $output = implode(PHP_EOL, $output);
  58. self::assertContains('Empty zipfile', $output);
  59. }
  60. $actualEmptyZipData = pack('VVVVVv', EndOfCentralDirectory::END_OF_CENTRAL_DIRECTORY_RECORD_SIG, 0, 0, 0, 0, 0);
  61. self::assertEquals(file_get_contents($filename), $actualEmptyZipData);
  62. }
  63. /**
  64. * @param string $filename
  65. * @param bool $showErrors
  66. * @return bool|null If null - can not install zipalign
  67. */
  68. public static function doZipAlignVerify($filename, $showErrors = false)
  69. {
  70. if (DIRECTORY_SEPARATOR !== '\\' && `which zipalign`) {
  71. exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode);
  72. if ($showErrors && $returnCode !== 0) fwrite(STDERR, implode(PHP_EOL, $output));
  73. return $returnCode === 0;
  74. } else {
  75. fwrite(STDERR, 'Can not find program "zipalign" for test');
  76. return null;
  77. }
  78. }
  79. }