2
0

AsiExtraFieldTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Exception\Crc32Exception;
  12. use PhpZip\Exception\ZipException;
  13. use PhpZip\Model\Extra\Fields\AsiExtraField;
  14. /**
  15. * @internal
  16. *
  17. * @small
  18. */
  19. final class AsiExtraFieldTest extends TestCase
  20. {
  21. /**
  22. * @dataProvider provideExtraField
  23. *
  24. * @throws ZipException
  25. */
  26. public function testExtraField(int $mode, int $uid, int $gid, string $link, string $binaryData): void
  27. {
  28. $asiExtraField = new AsiExtraField($mode, $uid, $gid, $link);
  29. self::assertSame($asiExtraField->getHeaderId(), AsiExtraField::HEADER_ID);
  30. self::assertSame($asiExtraField->getMode(), $mode);
  31. self::assertSame($asiExtraField->getUserId(), $uid);
  32. self::assertSame($asiExtraField->getGroupId(), $uid);
  33. self::assertSame($asiExtraField->getLink(), $link);
  34. self::assertSame($asiExtraField->packLocalFileData(), $binaryData);
  35. self::assertSame($asiExtraField->packCentralDirData(), $binaryData);
  36. self::assertEquals(AsiExtraField::unpackLocalFileData($binaryData), $asiExtraField);
  37. self::assertEquals(AsiExtraField::unpackCentralDirData($binaryData), $asiExtraField);
  38. }
  39. public function provideExtraField(): array
  40. {
  41. return [
  42. [
  43. 040755,
  44. AsiExtraField::USER_GID_PID,
  45. AsiExtraField::USER_GID_PID,
  46. '',
  47. "#\x06\\\xF6\xEDA\x00\x00\x00\x00\xE8\x03\xE8\x03",
  48. ],
  49. [
  50. 0100644,
  51. 0,
  52. 0,
  53. 'sites-enabled/example.conf',
  54. "_\xB8\xC7b\xA4\x81\x1A\x00\x00\x00\x00\x00\x00\x00sites-enabled/example.conf",
  55. ],
  56. ];
  57. }
  58. public function testSetter(): void
  59. {
  60. $extraField = new AsiExtraField(0777);
  61. $extraField->setMode(0100666);
  62. self::assertSame(0100666, $extraField->getMode());
  63. $extraField->setUserId(700);
  64. self::assertSame(700, $extraField->getUserId());
  65. $extraField->setGroupId(500);
  66. self::assertSame(500, $extraField->getGroupId());
  67. $extraField->setLink('link.txt');
  68. self::assertSame($extraField->getLink(), 'link.txt');
  69. self::assertSame(0120666, $extraField->getMode());
  70. // dir mode
  71. $extraField->setMode(0755);
  72. self::assertSame(0120755, $extraField->getMode());
  73. $extraField->setLink('');
  74. self::assertSame($extraField->getLink(), '');
  75. self::assertSame(0100755, $extraField->getMode());
  76. }
  77. /**
  78. * @throws Crc32Exception
  79. */
  80. public function testInvalidParse(): void
  81. {
  82. $this->expectException(Crc32Exception::class);
  83. $this->expectExceptionMessage('Asi Unix Extra Filed Data (expected CRC32 value');
  84. AsiExtraField::unpackLocalFileData("\x01\x06\\\xF6\xEDA\x00\x00\x00\x00\xE8\x03\xE8\x03");
  85. }
  86. }