Zip64ExtraFieldTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Tests\Extra\Fields;
  10. use PHPUnit\Framework\TestCase;
  11. use PhpZip\Constants\ZipConstants;
  12. use PhpZip\Exception\ZipException;
  13. use PhpZip\Model\Extra\Fields\Zip64ExtraField;
  14. use PhpZip\Model\ZipEntry;
  15. /**
  16. * @internal
  17. *
  18. * @small
  19. */
  20. final class Zip64ExtraFieldTest extends TestCase
  21. {
  22. protected function setUp(): void
  23. {
  24. if (\PHP_INT_SIZE === 4) {
  25. self::markTestSkipped('only 64 bit test');
  26. }
  27. }
  28. /**
  29. * @dataProvider provideExtraField
  30. *
  31. * @noinspection PhpTooManyParametersInspection
  32. *
  33. * @param ?int $uncompressedSize
  34. * @param ?int $compressedSize
  35. * @param ?int $localHeaderOffset
  36. * @param ?int $diskStart
  37. * @param ?string $localBinaryData
  38. * @param ?string $cdBinaryData
  39. *
  40. * @throws ZipException
  41. */
  42. public function testExtraField(
  43. ?int $uncompressedSize,
  44. ?int $compressedSize,
  45. ?int $localHeaderOffset,
  46. ?int $diskStart,
  47. ?string $localBinaryData,
  48. ?string $cdBinaryData
  49. ): void {
  50. $extraField = new Zip64ExtraField(
  51. $uncompressedSize,
  52. $compressedSize,
  53. $localHeaderOffset,
  54. $diskStart
  55. );
  56. self::assertSame($extraField->getHeaderId(), Zip64ExtraField::HEADER_ID);
  57. self::assertSame($extraField->getUncompressedSize(), $uncompressedSize);
  58. self::assertSame($extraField->getCompressedSize(), $compressedSize);
  59. self::assertSame($extraField->getLocalHeaderOffset(), $localHeaderOffset);
  60. self::assertSame($extraField->getDiskStart(), $diskStart);
  61. $zipEntry = new ZipEntry('entry');
  62. $zipEntry->setUncompressedSize($uncompressedSize !== null ? ZipConstants::ZIP64_MAGIC : 0xFFFFF);
  63. $zipEntry->setCompressedSize($compressedSize !== null ? ZipConstants::ZIP64_MAGIC : 0xFFFF);
  64. $zipEntry->setLocalHeaderOffset($localHeaderOffset !== null ? ZipConstants::ZIP64_MAGIC : 0xFFF);
  65. if ($localBinaryData !== null) {
  66. self::assertSame($localBinaryData, $extraField->packLocalFileData());
  67. self::assertEquals(Zip64ExtraField::unpackLocalFileData($localBinaryData, $zipEntry), $extraField);
  68. }
  69. if ($cdBinaryData !== null) {
  70. self::assertSame($cdBinaryData, $extraField->packCentralDirData());
  71. self::assertEquals(Zip64ExtraField::unpackCentralDirData($cdBinaryData, $zipEntry), $extraField);
  72. }
  73. }
  74. public function provideExtraField(): array
  75. {
  76. return [
  77. [
  78. 0,
  79. 2,
  80. null,
  81. null,
  82. "\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00",
  83. null,
  84. ],
  85. [
  86. 5368709120,
  87. 5369580144,
  88. null,
  89. null,
  90. null,
  91. "\x00\x00\x00@\x01\x00\x00\x00pJ\x0D@\x01\x00\x00\x00",
  92. ],
  93. [
  94. null,
  95. null,
  96. 4945378839,
  97. null,
  98. null,
  99. "\x17~\xC4&\x01\x00\x00\x00",
  100. ],
  101. ];
  102. }
  103. public function testSetter(): void
  104. {
  105. $extraField = new Zip64ExtraField();
  106. self::assertNull($extraField->getUncompressedSize());
  107. self::assertNull($extraField->getCompressedSize());
  108. self::assertNull($extraField->getLocalHeaderOffset());
  109. self::assertNull($extraField->getDiskStart());
  110. $uncompressedSize = 12222;
  111. $extraField->setUncompressedSize($uncompressedSize);
  112. self::assertSame($extraField->getUncompressedSize(), $uncompressedSize);
  113. $compressedSize = 12222;
  114. $extraField->setCompressedSize($uncompressedSize);
  115. self::assertSame($extraField->getCompressedSize(), $compressedSize);
  116. $localHeaderOffset = 12222;
  117. $extraField->setLocalHeaderOffset($localHeaderOffset);
  118. self::assertSame($extraField->getLocalHeaderOffset(), $localHeaderOffset);
  119. $diskStart = 2;
  120. $extraField->setDiskStart($diskStart);
  121. self::assertSame($extraField->getDiskStart(), $diskStart);
  122. }
  123. }