WinZipAesExtraFieldTest.php 9.2 KB

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