WinZipAesExtraFieldTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\ZipCompressionMethod;
  12. use PhpZip\Constants\ZipEncryptionMethod;
  13. use PhpZip\Exception\InvalidArgumentException;
  14. use PhpZip\Exception\ZipException;
  15. use PhpZip\Exception\ZipUnsupportMethodException;
  16. use PhpZip\Model\Extra\Fields\WinZipAesExtraField;
  17. /**
  18. * @internal
  19. *
  20. * @small
  21. */
  22. final class WinZipAesExtraFieldTest extends TestCase
  23. {
  24. /**
  25. * @dataProvider provideExtraField
  26. *
  27. * @throws ZipException
  28. * @throws ZipUnsupportMethodException
  29. */
  30. public function testExtraField(
  31. int $vendorVersion,
  32. int $keyStrength,
  33. int $compressionMethod,
  34. int $saltSize,
  35. string $binaryData
  36. ): void {
  37. $extraField = new WinZipAesExtraField($vendorVersion, $keyStrength, $compressionMethod);
  38. self::assertSame($extraField->getHeaderId(), WinZipAesExtraField::HEADER_ID);
  39. self::assertSame($extraField->getVendorVersion(), $vendorVersion);
  40. self::assertSame($extraField->getKeyStrength(), $keyStrength);
  41. self::assertSame($extraField->getCompressionMethod(), $compressionMethod);
  42. self::assertSame($extraField->getVendorId(), WinZipAesExtraField::VENDOR_ID);
  43. self::assertSame($extraField->getSaltSize(), $saltSize);
  44. self::assertSame($binaryData, $extraField->packLocalFileData());
  45. self::assertSame($binaryData, $extraField->packCentralDirData());
  46. self::assertEquals(WinZipAesExtraField::unpackLocalFileData($binaryData), $extraField);
  47. self::assertEquals(WinZipAesExtraField::unpackCentralDirData($binaryData), $extraField);
  48. }
  49. public function provideExtraField(): array
  50. {
  51. return [
  52. [
  53. WinZipAesExtraField::VERSION_AE1,
  54. WinZipAesExtraField::KEY_STRENGTH_128BIT,
  55. ZipCompressionMethod::STORED,
  56. 8,
  57. "\x01\x00AE\x01\x00\x00",
  58. ],
  59. [
  60. WinZipAesExtraField::VERSION_AE1,
  61. WinZipAesExtraField::KEY_STRENGTH_192BIT,
  62. ZipCompressionMethod::DEFLATED,
  63. 12,
  64. "\x01\x00AE\x02\x08\x00",
  65. ],
  66. [
  67. WinZipAesExtraField::VERSION_AE2,
  68. WinZipAesExtraField::KEY_STRENGTH_128BIT,
  69. ZipCompressionMethod::DEFLATED,
  70. 8,
  71. "\x02\x00AE\x01\x08\x00",
  72. ],
  73. [
  74. WinZipAesExtraField::VERSION_AE2,
  75. WinZipAesExtraField::KEY_STRENGTH_256BIT,
  76. ZipCompressionMethod::STORED,
  77. 16,
  78. "\x02\x00AE\x03\x00\x00",
  79. ],
  80. [
  81. WinZipAesExtraField::VERSION_AE2,
  82. WinZipAesExtraField::KEY_STRENGTH_192BIT,
  83. ZipCompressionMethod::DEFLATED,
  84. 12,
  85. "\x02\x00AE\x02\x08\x00",
  86. ],
  87. [
  88. WinZipAesExtraField::VERSION_AE2,
  89. WinZipAesExtraField::KEY_STRENGTH_256BIT,
  90. ZipCompressionMethod::STORED,
  91. 16,
  92. "\x02\x00AE\x03\x00\x00",
  93. ],
  94. ];
  95. }
  96. /**
  97. * @throws ZipUnsupportMethodException
  98. */
  99. public function testSetter(): void
  100. {
  101. $extraField = new WinZipAesExtraField(
  102. WinZipAesExtraField::VERSION_AE1,
  103. WinZipAesExtraField::KEY_STRENGTH_256BIT,
  104. ZipCompressionMethod::DEFLATED
  105. );
  106. self::assertSame($extraField->getVendorVersion(), WinZipAesExtraField::VERSION_AE1);
  107. self::assertSame($extraField->getKeyStrength(), WinZipAesExtraField::KEY_STRENGTH_256BIT);
  108. self::assertSame($extraField->getCompressionMethod(), ZipCompressionMethod::DEFLATED);
  109. self::assertSame($extraField->getSaltSize(), 16);
  110. self::assertSame($extraField->getEncryptionStrength(), 256);
  111. self::assertSame($extraField->getEncryptionMethod(), ZipEncryptionMethod::WINZIP_AES_256);
  112. $extraField->setVendorVersion(WinZipAesExtraField::VERSION_AE2);
  113. self::assertSame($extraField->getVendorVersion(), WinZipAesExtraField::VERSION_AE2);
  114. self::assertSame($extraField->getKeyStrength(), WinZipAesExtraField::KEY_STRENGTH_256BIT);
  115. self::assertSame($extraField->getCompressionMethod(), ZipCompressionMethod::DEFLATED);
  116. self::assertSame($extraField->getSaltSize(), 16);
  117. self::assertSame($extraField->getEncryptionStrength(), 256);
  118. self::assertSame($extraField->getEncryptionMethod(), ZipEncryptionMethod::WINZIP_AES_256);
  119. $extraField->setKeyStrength(WinZipAesExtraField::KEY_STRENGTH_128BIT);
  120. self::assertSame($extraField->getVendorVersion(), WinZipAesExtraField::VERSION_AE2);
  121. self::assertSame($extraField->getKeyStrength(), WinZipAesExtraField::KEY_STRENGTH_128BIT);
  122. self::assertSame($extraField->getCompressionMethod(), ZipCompressionMethod::DEFLATED);
  123. self::assertSame($extraField->getSaltSize(), 8);
  124. self::assertSame($extraField->getEncryptionStrength(), 128);
  125. self::assertSame($extraField->getEncryptionMethod(), ZipEncryptionMethod::WINZIP_AES_128);
  126. $extraField->setKeyStrength(WinZipAesExtraField::KEY_STRENGTH_192BIT);
  127. self::assertSame($extraField->getVendorVersion(), WinZipAesExtraField::VERSION_AE2);
  128. self::assertSame($extraField->getKeyStrength(), WinZipAesExtraField::KEY_STRENGTH_192BIT);
  129. self::assertSame($extraField->getCompressionMethod(), ZipCompressionMethod::DEFLATED);
  130. self::assertSame($extraField->getSaltSize(), 12);
  131. self::assertSame($extraField->getEncryptionStrength(), 192);
  132. self::assertSame($extraField->getEncryptionMethod(), ZipEncryptionMethod::WINZIP_AES_192);
  133. $extraField->setCompressionMethod(ZipCompressionMethod::STORED);
  134. self::assertSame($extraField->getVendorVersion(), WinZipAesExtraField::VERSION_AE2);
  135. self::assertSame($extraField->getKeyStrength(), WinZipAesExtraField::KEY_STRENGTH_192BIT);
  136. self::assertSame($extraField->getCompressionMethod(), ZipCompressionMethod::STORED);
  137. self::assertSame($extraField->getSaltSize(), 12);
  138. self::assertSame($extraField->getEncryptionStrength(), 192);
  139. self::assertSame($extraField->getEncryptionMethod(), ZipEncryptionMethod::WINZIP_AES_192);
  140. }
  141. /**
  142. * @throws ZipUnsupportMethodException
  143. */
  144. public function testConstructUnsupportVendorVersion(): void
  145. {
  146. $this->expectException(InvalidArgumentException::class);
  147. $this->expectExceptionMessage('Unsupport WinZip AES vendor version: 3');
  148. new WinZipAesExtraField(
  149. 3,
  150. WinZipAesExtraField::KEY_STRENGTH_192BIT,
  151. ZipCompressionMethod::STORED
  152. );
  153. }
  154. public function testSetterUnsupportVendorVersion(): void
  155. {
  156. $this->expectException(InvalidArgumentException::class);
  157. $this->expectExceptionMessage('Unsupport WinZip AES vendor version: 3');
  158. $extraField = new WinZipAesExtraField(
  159. WinZipAesExtraField::VERSION_AE1,
  160. WinZipAesExtraField::KEY_STRENGTH_192BIT,
  161. ZipCompressionMethod::STORED
  162. );
  163. $extraField->setVendorVersion(3);
  164. }
  165. public function testConstructUnsupportCompressionMethod(): void
  166. {
  167. $this->expectException(ZipUnsupportMethodException::class);
  168. $this->expectExceptionMessage('Compression method 3 (Reduced compression factor 2) is not supported.');
  169. new WinZipAesExtraField(
  170. WinZipAesExtraField::VERSION_AE1,
  171. WinZipAesExtraField::KEY_STRENGTH_192BIT,
  172. 3
  173. );
  174. }
  175. public function testSetterUnsupportCompressionMethod(): void
  176. {
  177. $this->expectException(ZipUnsupportMethodException::class);
  178. $this->expectExceptionMessage('Compression method 3 (Reduced compression factor 2) is not supported.');
  179. $extraField = new WinZipAesExtraField(
  180. WinZipAesExtraField::VERSION_AE1,
  181. WinZipAesExtraField::KEY_STRENGTH_192BIT,
  182. ZipCompressionMethod::STORED
  183. );
  184. $extraField->setCompressionMethod(3);
  185. }
  186. /**
  187. * @throws ZipUnsupportMethodException
  188. */
  189. public function testConstructUnsupportKeyStrength(): void
  190. {
  191. $this->expectException(InvalidArgumentException::class);
  192. $this->expectExceptionMessage('Key strength 16 not support value. Allow values: 1, 2, 3');
  193. new WinZipAesExtraField(
  194. WinZipAesExtraField::VERSION_AE1,
  195. 0x10,
  196. ZipCompressionMethod::STORED
  197. );
  198. }
  199. /**
  200. * @throws ZipException
  201. */
  202. public function testSetterUnsupportKeyStrength(): void
  203. {
  204. $this->expectException(InvalidArgumentException::class);
  205. $this->expectExceptionMessage('Key strength 16 not support value. Allow values: 1, 2, 3');
  206. new WinZipAesExtraField(
  207. WinZipAesExtraField::VERSION_AE1,
  208. 0x10,
  209. ZipCompressionMethod::STORED
  210. );
  211. }
  212. }