2
0

ZipAlignTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. */
  40. public function testZipAlignSourceZip()
  41. {
  42. $zipFile = new ZipFile();
  43. for ($i = 0; $i < 100; $i++) {
  44. $zipFile->addFromString(
  45. 'entry' . $i . '.txt',
  46. random_bytes(mt_rand(100, 4096)),
  47. ZipFile::METHOD_STORED
  48. );
  49. }
  50. $zipFile->saveAsFile($this->outputFilename);
  51. $zipFile->close();
  52. static::assertCorrectZipArchive($this->outputFilename);
  53. $result = static::assertVerifyZipAlign($this->outputFilename);
  54. if ($result === null) {
  55. return;
  56. } // zip align not installed
  57. // check not zip align
  58. static::assertFalse($result);
  59. $zipFile->openFile($this->outputFilename);
  60. $zipFile->setZipAlign(4);
  61. $zipFile->saveAsFile($this->outputFilename);
  62. $zipFile->close();
  63. static::assertCorrectZipArchive($this->outputFilename);
  64. $result = static::assertVerifyZipAlign($this->outputFilename, true);
  65. static::assertNotNull($result);
  66. // check zip align
  67. static::assertTrue($result);
  68. }
  69. /**
  70. * @throws ZipException
  71. * @throws \Exception
  72. */
  73. public function testZipAlignNewFiles()
  74. {
  75. $zipFile = new ZipFile();
  76. for ($i = 0; $i < 100; $i++) {
  77. $zipFile->addFromString(
  78. 'entry' . $i . '.txt',
  79. random_bytes(mt_rand(100, 4096)),
  80. ZipFile::METHOD_STORED
  81. );
  82. }
  83. $zipFile->setZipAlign(4);
  84. $zipFile->saveAsFile($this->outputFilename);
  85. $zipFile->close();
  86. static::assertCorrectZipArchive($this->outputFilename);
  87. $result = static::assertVerifyZipAlign($this->outputFilename);
  88. if ($result === null) {
  89. return;
  90. } // zip align not installed
  91. // check not zip align
  92. static::assertTrue($result);
  93. }
  94. /**
  95. * @throws ZipException
  96. * @throws \Exception
  97. */
  98. public function testZipAlignFromModifiedZipArchive()
  99. {
  100. $zipFile = new ZipFile();
  101. for ($i = 0; $i < 100; $i++) {
  102. $zipFile->addFromString(
  103. 'entry' . $i . '.txt',
  104. random_bytes(mt_rand(100, 4096)),
  105. ZipFile::METHOD_STORED
  106. );
  107. }
  108. $zipFile->saveAsFile($this->outputFilename);
  109. $zipFile->close();
  110. static::assertCorrectZipArchive($this->outputFilename);
  111. $result = static::assertVerifyZipAlign($this->outputFilename);
  112. if ($result === null) {
  113. return;
  114. } // zip align not installed
  115. // check not zip align
  116. static::assertFalse($result);
  117. $zipFile->openFile($this->outputFilename);
  118. $zipFile->deleteFromRegex('~entry2[\\d]+\\.txt$~s');
  119. for ($i = 0; $i < 100; $i++) {
  120. $isStored = (bool) mt_rand(0, 1);
  121. $zipFile->addFromString(
  122. 'entry_new_' . ($isStored ? 'stored' : 'deflated') . '_' . $i . '.txt',
  123. random_bytes(mt_rand(100, 4096)),
  124. $isStored ?
  125. ZipFile::METHOD_STORED :
  126. ZipFile::METHOD_DEFLATED
  127. );
  128. }
  129. $zipFile->setZipAlign(4);
  130. $zipFile->saveAsFile($this->outputFilename);
  131. $zipFile->close();
  132. static::assertCorrectZipArchive($this->outputFilename);
  133. $result = static::assertVerifyZipAlign($this->outputFilename, true);
  134. static::assertNotNull($result);
  135. // check zip align
  136. static::assertTrue($result);
  137. }
  138. }