ZipAlignTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace PhpZip;
  3. use PhpZip\Exception\ZipException;
  4. /**
  5. * Test ZipAlign.
  6. *
  7. * @internal
  8. *
  9. * @small
  10. */
  11. class ZipAlignTest extends ZipTestCase
  12. {
  13. /**
  14. * @throws ZipException
  15. */
  16. public function testApkAlignedAndSetZipAlignAndReSave()
  17. {
  18. $filename = __DIR__ . '/resources/apk.zip';
  19. static::assertCorrectZipArchive($filename);
  20. $result = static::assertVerifyZipAlign($filename);
  21. if ($result !== null) {
  22. static::assertTrue($result);
  23. }
  24. $zipFile = new ZipFile();
  25. $zipFile->openFile($filename);
  26. $zipFile->setZipAlign(4);
  27. $zipFile->saveAsFile($this->outputFilename);
  28. $zipFile->close();
  29. static::assertCorrectZipArchive($this->outputFilename);
  30. $result = static::assertVerifyZipAlign($this->outputFilename, true);
  31. if ($result !== null) {
  32. static::assertTrue($result);
  33. }
  34. }
  35. /**
  36. * Test zip alignment.
  37. *
  38. * @throws ZipException
  39. * @throws \Exception
  40. */
  41. public function testZipAlignSourceZip()
  42. {
  43. $zipFile = new ZipFile();
  44. for ($i = 0; $i < 100; $i++) {
  45. $zipFile->addFromString(
  46. 'entry' . $i . '.txt',
  47. random_bytes(mt_rand(100, 4096)),
  48. ZipFile::METHOD_STORED
  49. );
  50. }
  51. $zipFile->saveAsFile($this->outputFilename);
  52. $zipFile->close();
  53. static::assertCorrectZipArchive($this->outputFilename);
  54. $result = static::assertVerifyZipAlign($this->outputFilename);
  55. if ($result === null) {
  56. return;
  57. } // zip align not installed
  58. // check not zip align
  59. static::assertFalse($result);
  60. $zipFile->openFile($this->outputFilename);
  61. $zipFile->setZipAlign(4);
  62. $zipFile->saveAsFile($this->outputFilename);
  63. $zipFile->close();
  64. static::assertCorrectZipArchive($this->outputFilename);
  65. $result = static::assertVerifyZipAlign($this->outputFilename, true);
  66. static::assertNotNull($result);
  67. // check zip align
  68. static::assertTrue($result);
  69. }
  70. /**
  71. * @throws ZipException
  72. * @throws \Exception
  73. */
  74. public function testZipAlignNewFiles()
  75. {
  76. $zipFile = new ZipFile();
  77. for ($i = 0; $i < 100; $i++) {
  78. $zipFile->addFromString(
  79. 'entry' . $i . '.txt',
  80. random_bytes(mt_rand(100, 4096)),
  81. ZipFile::METHOD_STORED
  82. );
  83. }
  84. $zipFile->setZipAlign(4);
  85. $zipFile->saveAsFile($this->outputFilename);
  86. $zipFile->close();
  87. static::assertCorrectZipArchive($this->outputFilename);
  88. $result = static::assertVerifyZipAlign($this->outputFilename);
  89. if ($result === null) {
  90. return;
  91. } // zip align not installed
  92. // check not zip align
  93. static::assertTrue($result);
  94. }
  95. /**
  96. * @throws ZipException
  97. * @throws \Exception
  98. */
  99. public function testZipAlignFromModifiedZipArchive()
  100. {
  101. $zipFile = new ZipFile();
  102. for ($i = 0; $i < 100; $i++) {
  103. $zipFile->addFromString(
  104. 'entry' . $i . '.txt',
  105. random_bytes(mt_rand(100, 4096)),
  106. ZipFile::METHOD_STORED
  107. );
  108. }
  109. $zipFile->saveAsFile($this->outputFilename);
  110. $zipFile->close();
  111. static::assertCorrectZipArchive($this->outputFilename);
  112. $result = static::assertVerifyZipAlign($this->outputFilename);
  113. if ($result === null) {
  114. return;
  115. } // zip align not installed
  116. // check not zip align
  117. static::assertFalse($result);
  118. $zipFile->openFile($this->outputFilename);
  119. $zipFile->deleteFromRegex('~entry2[\\d]+\\.txt$~s');
  120. for ($i = 0; $i < 100; $i++) {
  121. $isStored = (bool) mt_rand(0, 1);
  122. $zipFile->addFromString(
  123. 'entry_new_' . ($isStored ? 'stored' : 'deflated') . '_' . $i . '.txt',
  124. random_bytes(mt_rand(100, 4096)),
  125. $isStored ?
  126. ZipFile::METHOD_STORED :
  127. ZipFile::METHOD_DEFLATED
  128. );
  129. }
  130. $zipFile->setZipAlign(4);
  131. $zipFile->saveAsFile($this->outputFilename);
  132. $zipFile->close();
  133. static::assertCorrectZipArchive($this->outputFilename);
  134. $result = static::assertVerifyZipAlign($this->outputFilename, true);
  135. static::assertNotNull($result);
  136. // check zip align
  137. static::assertTrue($result);
  138. }
  139. }