ZipEntryTest.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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;
  10. use PHPUnit\Framework\TestCase;
  11. use PhpZip\Constants\DosAttrs;
  12. use PhpZip\Constants\DosCodePage;
  13. use PhpZip\Constants\GeneralPurposeBitFlag;
  14. use PhpZip\Constants\ZipCompressionLevel;
  15. use PhpZip\Constants\ZipCompressionMethod;
  16. use PhpZip\Constants\ZipConstants;
  17. use PhpZip\Constants\ZipEncryptionMethod;
  18. use PhpZip\Constants\ZipPlatform;
  19. use PhpZip\Constants\ZipVersion;
  20. use PhpZip\Exception\InvalidArgumentException;
  21. use PhpZip\Exception\ZipException;
  22. use PhpZip\Exception\ZipUnsupportMethodException;
  23. use PhpZip\Model\Data\ZipFileData;
  24. use PhpZip\Model\Data\ZipNewData;
  25. use PhpZip\Model\Extra\ExtraFieldsCollection;
  26. use PhpZip\Model\Extra\Fields\AsiExtraField;
  27. use PhpZip\Model\Extra\Fields\ExtendedTimestampExtraField;
  28. use PhpZip\Model\Extra\Fields\JarMarkerExtraField;
  29. use PhpZip\Model\Extra\Fields\NewUnixExtraField;
  30. use PhpZip\Model\Extra\Fields\NtfsExtraField;
  31. use PhpZip\Model\Extra\Fields\OldUnixExtraField;
  32. use PhpZip\Model\Extra\Fields\UnicodePathExtraField;
  33. use PhpZip\Model\ZipEntry;
  34. /**
  35. * Class ZipEntryTest.
  36. *
  37. * @internal
  38. *
  39. * @small
  40. */
  41. class ZipEntryTest extends TestCase
  42. {
  43. public function testEntry(): void
  44. {
  45. $zipEntry = new ZipEntry('entry');
  46. static::assertSame($zipEntry->getName(), 'entry');
  47. static::assertFalse($zipEntry->isDirectory());
  48. static::assertNull($zipEntry->getData());
  49. static::assertSame($zipEntry->getCompressionMethod(), ZipEntry::UNKNOWN);
  50. static::assertSame($zipEntry->getCreatedOS(), ZipEntry::UNKNOWN);
  51. static::assertSame($zipEntry->getExtractedOS(), ZipEntry::UNKNOWN);
  52. static::assertSame($zipEntry->getSoftwareVersion(), ZipVersion::v10_DEFAULT_MIN);
  53. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v10_DEFAULT_MIN);
  54. static::assertSame($zipEntry->getGeneralPurposeBitFlags(), 0);
  55. static::assertSame($zipEntry->getDosTime(), ZipEntry::UNKNOWN);
  56. static::assertSame($zipEntry->getTime(), ZipEntry::UNKNOWN);
  57. static::assertSame($zipEntry->getCrc(), ZipEntry::UNKNOWN);
  58. static::assertSame($zipEntry->getCompressedSize(), ZipEntry::UNKNOWN);
  59. static::assertSame($zipEntry->getUncompressedSize(), ZipEntry::UNKNOWN);
  60. static::assertSame($zipEntry->getInternalAttributes(), 0);
  61. static::assertSame($zipEntry->getExternalAttributes(), DosAttrs::DOS_ARCHIVE);
  62. static::assertSame($zipEntry->getLocalHeaderOffset(), 0);
  63. static::assertCount(0, $zipEntry->getCdExtraFields());
  64. static::assertCount(0, $zipEntry->getLocalExtraFields());
  65. static::assertSame($zipEntry->getComment(), '');
  66. static::assertNull($zipEntry->getPassword());
  67. static::assertSame($zipEntry->getEncryptionMethod(), ZipEncryptionMethod::NONE);
  68. static::assertSame($zipEntry->getCompressionLevel(), ZipCompressionLevel::NORMAL);
  69. static::assertNull($zipEntry->getCharset());
  70. static::assertNull($zipEntry->getATime());
  71. static::assertNull($zipEntry->getCTime());
  72. static::assertSame($zipEntry->getUnixMode(), 0100644);
  73. $zipDirEntry = $zipEntry->rename('directory/');
  74. static::assertNotSame($zipEntry, $zipDirEntry);
  75. static::assertSame($zipDirEntry->getName(), 'directory/');
  76. static::assertTrue($zipDirEntry->isDirectory());
  77. static::assertSame($zipDirEntry->getExternalAttributes(), DosAttrs::DOS_DIRECTORY);
  78. static::assertSame($zipDirEntry->getUnixMode(), 040755);
  79. static::assertNotSame($zipDirEntry->getName(), $zipEntry->getName());
  80. static::assertNotSame($zipDirEntry->isDirectory(), $zipEntry->isDirectory());
  81. static::assertNotSame($zipDirEntry->getExternalAttributes(), $zipEntry->getExternalAttributes());
  82. static::assertNotSame($zipDirEntry->getUnixMode(), $zipEntry->getUnixMode());
  83. }
  84. /**
  85. * @dataProvider provideEmptyName
  86. */
  87. public function testEmptyName(string $entryName, string $exceptionMessage): void
  88. {
  89. $this->expectException(InvalidArgumentException::class);
  90. $this->expectExceptionMessage($exceptionMessage);
  91. new ZipEntry($entryName);
  92. }
  93. public function provideEmptyName(): array
  94. {
  95. return [
  96. ['', 'Empty zip entry name'],
  97. ['/', 'Empty zip entry name'],
  98. ];
  99. }
  100. /**
  101. * @dataProvider provideEntryName
  102. */
  103. public function testEntryName(string $entryName, string $actualEntryName, bool $directory): void
  104. {
  105. $entry = new ZipEntry($entryName);
  106. static::assertSame($entry->getName(), $actualEntryName);
  107. static::assertSame($entry->isDirectory(), $directory);
  108. }
  109. public function provideEntryName(): array
  110. {
  111. return [
  112. ['0', '0', false],
  113. ['directory/', 'directory/', true],
  114. ];
  115. }
  116. /**
  117. * @dataProvider provideCompressionMethod
  118. *
  119. * @throws ZipUnsupportMethodException
  120. */
  121. public function testCompressionMethod(int $compressionMethod): void
  122. {
  123. $entry = new ZipEntry('entry');
  124. static::assertSame($entry->getCompressionMethod(), ZipEntry::UNKNOWN);
  125. $entry->setCompressionMethod($compressionMethod);
  126. static::assertSame($entry->getCompressionMethod(), $compressionMethod);
  127. }
  128. public function provideCompressionMethod(): array
  129. {
  130. $provides = [
  131. [ZipCompressionMethod::STORED],
  132. [ZipCompressionMethod::DEFLATED],
  133. ];
  134. if (\extension_loaded('bz2')) {
  135. $provides[] = [ZipCompressionMethod::BZIP2];
  136. }
  137. return $provides;
  138. }
  139. /**
  140. * @dataProvider provideOutOfRangeCompressionMethod
  141. *
  142. * @throws ZipUnsupportMethodException
  143. */
  144. public function testOutOfRangeCompressionMethod(int $compressionMethod): void
  145. {
  146. $this->expectException(InvalidArgumentException::class);
  147. $this->expectExceptionMessage('method out of range: ' . $compressionMethod);
  148. $zipEntry = new ZipEntry('entry');
  149. $zipEntry->setCompressionMethod($compressionMethod);
  150. }
  151. public function provideOutOfRangeCompressionMethod(): array
  152. {
  153. return [
  154. [-1],
  155. [0x44444],
  156. ];
  157. }
  158. /**
  159. * @dataProvider provideUnsupportCompressionMethod
  160. *
  161. * @throws ZipUnsupportMethodException
  162. */
  163. public function testUnsupportCompressionMethod(int $compressionMethod, string $exceptionMessage): void
  164. {
  165. $this->expectException(ZipUnsupportMethodException::class);
  166. $this->expectExceptionMessage($exceptionMessage);
  167. $zipEntry = new ZipEntry('entry');
  168. $zipEntry->setCompressionMethod($compressionMethod);
  169. }
  170. public function provideUnsupportCompressionMethod(): array
  171. {
  172. return [
  173. [1, 'Compression method 1 (Shrunk) is not supported.'],
  174. [2, 'Compression method 2 (Reduced compression factor 1) is not supported.'],
  175. [3, 'Compression method 3 (Reduced compression factor 2) is not supported.'],
  176. [4, 'Compression method 4 (Reduced compression factor 3) is not supported.'],
  177. [5, 'Compression method 5 (Reduced compression factor 4) is not supported.'],
  178. [6, 'Compression method 6 (Imploded) is not supported.'],
  179. [7, 'Compression method 7 (Reserved for Tokenizing compression algorithm) is not supported.'],
  180. [9, 'Compression method 9 (Enhanced Deflating using Deflate64(tm)) is not supported.'],
  181. [10, 'Compression method 10 (PKWARE Data Compression Library Imploding) is not supported.'],
  182. [11, 'Compression method 11 (Reserved by PKWARE) is not supported.'],
  183. [13, 'Compression method 13 (Reserved by PKWARE) is not supported.'],
  184. [14, 'Compression method 14 (LZMA) is not supported.'],
  185. [15, 'Compression method 15 (Reserved by PKWARE) is not supported.'],
  186. [16, 'Compression method 16 (Reserved by PKWARE) is not supported.'],
  187. [17, 'Compression method 17 (Reserved by PKWARE) is not supported.'],
  188. [18, 'Compression method 18 (File is compressed using IBM TERSE (new)) is not supported.'],
  189. [19, 'Compression method 19 (IBM LZ77 z Architecture (PFS)) is not supported.'],
  190. [96, 'Compression method 96 (WinZip JPEG Compression) is not supported.'],
  191. [97, 'Compression method 97 (WavPack compressed data) is not supported.'],
  192. [98, 'Compression method 98 (PPMd version I, Rev 1) is not supported.'],
  193. [
  194. ZipCompressionMethod::WINZIP_AES,
  195. 'Compression method ' . ZipCompressionMethod::WINZIP_AES . ' (AES Encryption) is not supported.',
  196. ],
  197. [100, 'Compression method 100 (Unknown Method) is not supported.'],
  198. ];
  199. }
  200. public function testCharset(): void
  201. {
  202. $zipEntry = new ZipEntry('entry');
  203. $zipEntry->setCharset(DosCodePage::CP_CYRILLIC_RUSSIAN);
  204. static::assertSame($zipEntry->getCharset(), DosCodePage::CP_CYRILLIC_RUSSIAN);
  205. $zipEntry->setCharset(/* null */);
  206. static::assertNull($zipEntry->getCharset());
  207. }
  208. public function testEmptyCharset(): void
  209. {
  210. $this->expectException(InvalidArgumentException::class);
  211. $this->expectExceptionMessage('Empty charset');
  212. $zipEntry = new ZipEntry('entry');
  213. $zipEntry->setCharset('');
  214. }
  215. public function testRenameAndDeleteUnicodePath(): void
  216. {
  217. $entryName = 'файл.txt';
  218. $charset = DosCodePage::CP_CYRILLIC_RUSSIAN;
  219. $dosEntryName = DosCodePage::fromUTF8($entryName, $charset);
  220. static::assertSame(DosCodePage::toUTF8($dosEntryName, $charset), $entryName);
  221. $unicodePathExtraField = new UnicodePathExtraField(crc32($dosEntryName), $entryName);
  222. $zipEntry = new ZipEntry($dosEntryName, $charset);
  223. static::assertSame($zipEntry->getName(), $dosEntryName);
  224. static::assertSame($zipEntry->getCharset(), $charset);
  225. static::assertFalse($zipEntry->isUtf8Flag());
  226. $zipEntry->addExtraField($unicodePathExtraField);
  227. static::assertSame(
  228. $zipEntry->getLocalExtraField(UnicodePathExtraField::HEADER_ID),
  229. $unicodePathExtraField
  230. );
  231. static::assertSame(
  232. $zipEntry->getCdExtraField(UnicodePathExtraField::HEADER_ID),
  233. $unicodePathExtraField
  234. );
  235. $utf8EntryName = $zipEntry->rename($entryName);
  236. static::assertSame($utf8EntryName->getName(), $entryName);
  237. static::assertTrue($utf8EntryName->isUtf8Flag());
  238. static::assertNull($utf8EntryName->getCharset());
  239. static::assertNull($utf8EntryName->getLocalExtraField(UnicodePathExtraField::HEADER_ID));
  240. static::assertNull($utf8EntryName->getCdExtraField(UnicodePathExtraField::HEADER_ID));
  241. }
  242. public function testData(): void
  243. {
  244. $zipEntry = new ZipEntry('entry');
  245. static::assertNull($zipEntry->getData());
  246. $zipData = new ZipNewData($zipEntry, 'Text contents');
  247. $zipEntry->setData($zipData);
  248. static::assertSame($zipEntry->getData(), $zipData);
  249. $zipEntry->setData(null);
  250. static::assertNull($zipEntry->getData());
  251. }
  252. /**
  253. * @throws \Exception
  254. */
  255. public function testZipNewDataGuardClone(): void
  256. {
  257. $resource = fopen('php://temp', 'r+b');
  258. static::assertNotFalse($resource);
  259. fwrite($resource, random_bytes(1024));
  260. rewind($resource);
  261. $zipEntry = new ZipEntry('entry');
  262. $zipEntry2 = new ZipEntry('entry2');
  263. $zipData = new ZipNewData($zipEntry, $resource);
  264. $zipData2 = new ZipNewData($zipEntry2, $resource);
  265. $cloneData = clone $zipData;
  266. $cloneData2 = clone $cloneData;
  267. static::assertSame($zipData->getDataAsStream(), $resource);
  268. static::assertSame($zipData2->getDataAsStream(), $resource);
  269. static::assertSame($cloneData->getDataAsStream(), $resource);
  270. static::assertSame($cloneData2->getDataAsStream(), $resource);
  271. $validResource = \is_resource($resource);
  272. static::assertTrue($validResource);
  273. unset($cloneData);
  274. $validResource = \is_resource($resource);
  275. static::assertTrue($validResource);
  276. unset($zipData);
  277. $validResource = \is_resource($resource);
  278. static::assertTrue($validResource);
  279. unset($zipData2);
  280. $validResource = \is_resource($resource);
  281. static::assertTrue($validResource);
  282. $reflectionClass = new \ReflectionClass($cloneData2);
  283. static::assertSame(
  284. $reflectionClass->getStaticProperties()['guardClonedStream'][(int) $resource],
  285. 0
  286. );
  287. unset($cloneData2);
  288. $validResource = \is_resource($resource);
  289. static::assertFalse($validResource);
  290. }
  291. /**
  292. * @dataProvider providePlatform
  293. */
  294. public function testCreatedOS(int $zipOS): void
  295. {
  296. $zipEntry = new ZipEntry('entry');
  297. static::assertSame($zipEntry->getCreatedOS(), ZipEntry::UNKNOWN);
  298. $zipEntry->setCreatedOS($zipOS);
  299. static::assertSame($zipEntry->getCreatedOS(), $zipOS);
  300. }
  301. public function providePlatform(): array
  302. {
  303. return [
  304. [ZipPlatform::OS_DOS],
  305. [ZipPlatform::OS_UNIX],
  306. [ZipPlatform::OS_MAC_OSX],
  307. ];
  308. }
  309. /**
  310. * @dataProvider providePlatform
  311. */
  312. public function testExtractedOS(int $zipOS): void
  313. {
  314. $zipEntry = new ZipEntry('entry');
  315. static::assertSame($zipEntry->getExtractedOS(), ZipEntry::UNKNOWN);
  316. $zipEntry->setExtractedOS($zipOS);
  317. static::assertSame($zipEntry->getExtractedOS(), $zipOS);
  318. }
  319. /**
  320. * @dataProvider provideInvalidPlatform
  321. */
  322. public function testInvalidCreatedOs(int $zipOS): void
  323. {
  324. $this->expectException(InvalidArgumentException::class);
  325. $this->expectExceptionMessage('Platform out of range');
  326. $zipEntry = new ZipEntry('entry');
  327. $zipEntry->setCreatedOS($zipOS);
  328. }
  329. public function provideInvalidPlatform(): array
  330. {
  331. return [
  332. [-1],
  333. [0xFF + 1],
  334. ];
  335. }
  336. /**
  337. * @dataProvider provideInvalidPlatform
  338. */
  339. public function testInvalidExtractedOs(int $zipOS): void
  340. {
  341. $this->expectException(InvalidArgumentException::class);
  342. $this->expectExceptionMessage('Platform out of range');
  343. $zipEntry = new ZipEntry('entry');
  344. $zipEntry->setExtractedOS($zipOS);
  345. }
  346. /**
  347. * @throws ZipException
  348. */
  349. public function testAutoExtractVersion(): void
  350. {
  351. $zipEntry = new ZipEntry('entry');
  352. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v10_DEFAULT_MIN);
  353. $zipEntry->setCompressionMethod(ZipCompressionMethod::DEFLATED);
  354. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v20_DEFLATED_FOLDER_ZIPCRYPTO);
  355. static::assertSame(
  356. (new ZipEntry('directory/'))->getExtractVersion(),
  357. ZipVersion::v20_DEFLATED_FOLDER_ZIPCRYPTO
  358. );
  359. if (\extension_loaded('bz2')) {
  360. $zipEntry->setCompressionMethod(ZipCompressionMethod::BZIP2);
  361. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v46_BZIP2);
  362. }
  363. $zipEntry->setCompressionMethod(ZipCompressionMethod::STORED);
  364. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v10_DEFAULT_MIN);
  365. $zipEntry->setPassword('12345', ZipEncryptionMethod::PKWARE);
  366. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v20_DEFLATED_FOLDER_ZIPCRYPTO);
  367. $zipEntry->setEncryptionMethod(ZipEncryptionMethod::WINZIP_AES_256);
  368. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v51_ENCR_AES_RC2_CORRECT);
  369. }
  370. /**
  371. * @throws ZipException
  372. */
  373. public function testExtractVersion(): void
  374. {
  375. $zipEntry = new ZipEntry('entry');
  376. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v10_DEFAULT_MIN);
  377. $zipEntry->setExtractVersion(ZipVersion::v63_LZMA_PPMD_BLOWFISH_TWOFISH);
  378. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v63_LZMA_PPMD_BLOWFISH_TWOFISH);
  379. $renameEntry = $zipEntry->rename('new_entry');
  380. static::assertSame($renameEntry->getExtractVersion(), ZipVersion::v63_LZMA_PPMD_BLOWFISH_TWOFISH);
  381. $renameDirEntry = $zipEntry->rename('new_directory/');
  382. static::assertSame($renameDirEntry->getExtractVersion(), ZipVersion::v63_LZMA_PPMD_BLOWFISH_TWOFISH);
  383. $zipEntry->setExtractVersion(ZipVersion::v10_DEFAULT_MIN);
  384. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v10_DEFAULT_MIN);
  385. $renameDirEntry = $zipEntry->rename('new_directory/');
  386. static::assertSame($renameDirEntry->getExtractVersion(), ZipVersion::v20_DEFLATED_FOLDER_ZIPCRYPTO);
  387. $zipEntry->setCompressionMethod(ZipCompressionMethod::DEFLATED);
  388. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v20_DEFLATED_FOLDER_ZIPCRYPTO);
  389. if (\extension_loaded('bz2')) {
  390. $zipEntry->setExtractVersion(ZipVersion::v10_DEFAULT_MIN);
  391. $zipEntry->setCompressionMethod(ZipCompressionMethod::BZIP2);
  392. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v46_BZIP2);
  393. }
  394. $zipEntry->setExtractVersion(ZipVersion::v63_LZMA_PPMD_BLOWFISH_TWOFISH);
  395. $zipEntry->setCompressionMethod(ZipCompressionMethod::STORED);
  396. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v10_DEFAULT_MIN);
  397. $zipEntry->setExtractVersion(ZipVersion::v10_DEFAULT_MIN);
  398. $zipEntry->setPassword('12345', ZipEncryptionMethod::PKWARE);
  399. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v20_DEFLATED_FOLDER_ZIPCRYPTO);
  400. $zipEntry->setExtractVersion(ZipVersion::v10_DEFAULT_MIN);
  401. $zipEntry->setEncryptionMethod(ZipEncryptionMethod::WINZIP_AES_256);
  402. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v51_ENCR_AES_RC2_CORRECT);
  403. }
  404. public function testSoftwareVersion(): void
  405. {
  406. $zipEntry = new ZipEntry('entry');
  407. static::assertSame($zipEntry->getSoftwareVersion(), $zipEntry->getExtractVersion());
  408. $zipEntry->setExtractVersion(ZipVersion::v45_ZIP64_EXT);
  409. static::assertSame($zipEntry->getSoftwareVersion(), $zipEntry->getExtractVersion());
  410. $softwareVersion = 35;
  411. $zipEntry->setSoftwareVersion($softwareVersion);
  412. static::assertSame($softwareVersion, $zipEntry->getSoftwareVersion());
  413. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v45_ZIP64_EXT);
  414. $zipEntry->setExtractVersion(ZipVersion::v63_LZMA_PPMD_BLOWFISH_TWOFISH);
  415. static::assertNotSame($zipEntry->getSoftwareVersion(), $zipEntry->getExtractVersion());
  416. static::assertSame($softwareVersion, $zipEntry->getSoftwareVersion());
  417. static::assertSame($zipEntry->getExtractVersion(), ZipVersion::v63_LZMA_PPMD_BLOWFISH_TWOFISH);
  418. }
  419. public function testSize(): void
  420. {
  421. $zipEntry = new ZipEntry('entry');
  422. static::assertSame($zipEntry->getCompressedSize(), ZipEntry::UNKNOWN);
  423. static::assertSame($zipEntry->getUncompressedSize(), ZipEntry::UNKNOWN);
  424. $compressedSize = 100000;
  425. $uncompressedSize = 400000;
  426. $zipEntry->setCompressedSize($compressedSize);
  427. $zipEntry->setUncompressedSize($uncompressedSize);
  428. static::assertSame($zipEntry->getCompressedSize(), $compressedSize);
  429. static::assertSame($zipEntry->getUncompressedSize(), $uncompressedSize);
  430. $zipEntry->setCompressedSize(ZipEntry::UNKNOWN);
  431. $zipEntry->setUncompressedSize(ZipEntry::UNKNOWN);
  432. static::assertSame($zipEntry->getCompressedSize(), ZipEntry::UNKNOWN);
  433. static::assertSame($zipEntry->getUncompressedSize(), ZipEntry::UNKNOWN);
  434. }
  435. public function testInvalidCompressedSize(): void
  436. {
  437. $this->expectException(InvalidArgumentException::class);
  438. $this->expectExceptionMessage('Compressed size < -1');
  439. $zipEntry = new ZipEntry('entry');
  440. $zipEntry->setCompressedSize(-2);
  441. }
  442. public function testInvalidUncompressedSize(): void
  443. {
  444. $this->expectException(InvalidArgumentException::class);
  445. $this->expectExceptionMessage('Uncompressed size < -1');
  446. $zipEntry = new ZipEntry('entry');
  447. $zipEntry->setUncompressedSize(-2);
  448. }
  449. public function testLocalHeaderOffset(): void
  450. {
  451. $zipEntry = new ZipEntry('entry');
  452. static::assertSame($zipEntry->getLocalHeaderOffset(), 0);
  453. $localHeaderOffset = 10000;
  454. $zipEntry->setLocalHeaderOffset($localHeaderOffset);
  455. static::assertSame($zipEntry->getLocalHeaderOffset(), $localHeaderOffset);
  456. $this->expectException(InvalidArgumentException::class);
  457. $this->expectExceptionMessage('Negative $localHeaderOffset');
  458. $zipEntry->setLocalHeaderOffset(-1);
  459. }
  460. public function testGeneralPurposeBitFlags(): void
  461. {
  462. $zipEntry = new ZipEntry('entry');
  463. static::assertSame($zipEntry->getGeneralPurposeBitFlags(), 0);
  464. static::assertFalse($zipEntry->isUtf8Flag());
  465. static::assertFalse($zipEntry->isEncrypted());
  466. static::assertFalse($zipEntry->isStrongEncryption());
  467. static::assertFalse($zipEntry->isDataDescriptorEnabled());
  468. $gpbf = GeneralPurposeBitFlag::DATA_DESCRIPTOR | GeneralPurposeBitFlag::UTF8;
  469. $zipEntry->setGeneralPurposeBitFlags($gpbf);
  470. static::assertSame($zipEntry->getGeneralPurposeBitFlags(), $gpbf);
  471. static::assertTrue($zipEntry->isDataDescriptorEnabled());
  472. static::assertTrue($zipEntry->isUtf8Flag());
  473. $zipEntry->setGeneralPurposeBitFlags(0);
  474. static::assertSame($zipEntry->getGeneralPurposeBitFlags(), 0);
  475. static::assertFalse($zipEntry->isUtf8Flag());
  476. static::assertFalse($zipEntry->isDataDescriptorEnabled());
  477. $zipEntry->enableUtf8Name(true);
  478. static::assertTrue($zipEntry->isUtf8Flag());
  479. static::assertSame(
  480. ($zipEntry->getGeneralPurposeBitFlags() & GeneralPurposeBitFlag::UTF8),
  481. GeneralPurposeBitFlag::UTF8
  482. );
  483. $zipEntry->enableUtf8Name(false);
  484. static::assertFalse($zipEntry->isUtf8Flag());
  485. static::assertSame(
  486. ($zipEntry->getGeneralPurposeBitFlags() & GeneralPurposeBitFlag::UTF8),
  487. 0
  488. );
  489. $zipEntry->enableDataDescriptor(true);
  490. static::assertTrue($zipEntry->isDataDescriptorEnabled());
  491. static::assertSame(
  492. ($zipEntry->getGeneralPurposeBitFlags() & GeneralPurposeBitFlag::DATA_DESCRIPTOR),
  493. GeneralPurposeBitFlag::DATA_DESCRIPTOR
  494. );
  495. $zipEntry->enableDataDescriptor(false);
  496. static::assertFalse($zipEntry->isDataDescriptorEnabled());
  497. static::assertSame(
  498. ($zipEntry->getGeneralPurposeBitFlags() & GeneralPurposeBitFlag::DATA_DESCRIPTOR),
  499. 0
  500. );
  501. }
  502. public function testEncryptionGPBF(): void
  503. {
  504. $zipEntry = new ZipEntry('entry');
  505. static::assertFalse($zipEntry->isEncrypted());
  506. $zipEntry->setGeneralPurposeBitFlags(GeneralPurposeBitFlag::ENCRYPTION);
  507. static::assertSame(
  508. ($zipEntry->getGeneralPurposeBitFlags() & GeneralPurposeBitFlag::ENCRYPTION),
  509. GeneralPurposeBitFlag::ENCRYPTION
  510. );
  511. static::assertTrue($zipEntry->isEncrypted());
  512. $zipEntry->disableEncryption();
  513. static::assertSame(
  514. ($zipEntry->getGeneralPurposeBitFlags() & GeneralPurposeBitFlag::ENCRYPTION),
  515. 0
  516. );
  517. static::assertFalse($zipEntry->isEncrypted());
  518. // SIC! Strong encryption is not supported in ZipReader and ZipWriter
  519. static::assertFalse($zipEntry->isStrongEncryption());
  520. $zipEntry->setGeneralPurposeBitFlags(GeneralPurposeBitFlag::STRONG_ENCRYPTION);
  521. static::assertTrue($zipEntry->isStrongEncryption());
  522. }
  523. /**
  524. * @dataProvider provideInvalidGPBF
  525. */
  526. public function testInvalidGPBF(int $gpbf): void
  527. {
  528. $this->expectException(InvalidArgumentException::class);
  529. $this->expectExceptionMessage('general purpose bit flags out of range');
  530. $zipEntry = new ZipEntry('entry');
  531. $zipEntry->setGeneralPurposeBitFlags($gpbf);
  532. }
  533. public function provideInvalidGPBF(): array
  534. {
  535. return [
  536. [-1],
  537. [0x10000],
  538. ];
  539. }
  540. /**
  541. * @dataProvider provideCompressionLevelGPBF
  542. *
  543. * @throws ZipUnsupportMethodException
  544. */
  545. public function testSetCompressionFlags(int $compressionLevel, bool $bit1, bool $bit2): void
  546. {
  547. $zipEntry = new ZipEntry('entry');
  548. $zipEntry->setCompressionMethod(ZipCompressionMethod::DEFLATED);
  549. $gpbf = ($bit1 ? GeneralPurposeBitFlag::COMPRESSION_FLAG1 : 0)
  550. | ($bit2 ? GeneralPurposeBitFlag::COMPRESSION_FLAG2 : 0);
  551. $zipEntry->setGeneralPurposeBitFlags($gpbf);
  552. static::assertSame($zipEntry->getCompressionLevel(), $compressionLevel);
  553. static::assertSame(
  554. (
  555. $zipEntry->getGeneralPurposeBitFlags() & GeneralPurposeBitFlag::COMPRESSION_FLAG1
  556. ) === GeneralPurposeBitFlag::COMPRESSION_FLAG1,
  557. $bit1,
  558. 'Compression flag1 is not same'
  559. );
  560. static::assertSame(
  561. (
  562. $zipEntry->getGeneralPurposeBitFlags() & GeneralPurposeBitFlag::COMPRESSION_FLAG2
  563. ) === GeneralPurposeBitFlag::COMPRESSION_FLAG2,
  564. $bit2,
  565. 'Compression flag2 is not same'
  566. );
  567. }
  568. public function provideCompressionLevelGPBF(): array
  569. {
  570. return [
  571. [ZipCompressionLevel::SUPER_FAST, true, true],
  572. [ZipCompressionLevel::FAST, false, true],
  573. [ZipCompressionLevel::NORMAL, false, false],
  574. [ZipCompressionLevel::MAXIMUM, true, false],
  575. ];
  576. }
  577. /**
  578. * @dataProvider provideCompressionLevels
  579. *
  580. * @throws ZipUnsupportMethodException
  581. */
  582. public function testSetCompressionLevel(int $compressionLevel, bool $bit1, bool $bit2): void
  583. {
  584. $zipEntry = new ZipEntry('entry');
  585. $zipEntry->setCompressionMethod(ZipCompressionMethod::DEFLATED);
  586. $zipEntry->setCompressionLevel($compressionLevel);
  587. static::assertSame($zipEntry->getCompressionLevel(), $compressionLevel);
  588. static::assertSame(
  589. (
  590. $zipEntry->getGeneralPurposeBitFlags() & GeneralPurposeBitFlag::COMPRESSION_FLAG1
  591. ) === GeneralPurposeBitFlag::COMPRESSION_FLAG1,
  592. $bit1,
  593. 'Compression flag1 is not same'
  594. );
  595. static::assertSame(
  596. (
  597. $zipEntry->getGeneralPurposeBitFlags() & GeneralPurposeBitFlag::COMPRESSION_FLAG2
  598. ) === GeneralPurposeBitFlag::COMPRESSION_FLAG2,
  599. $bit2,
  600. 'Compression flag2 is not same'
  601. );
  602. }
  603. public function provideCompressionLevels(): array
  604. {
  605. return [
  606. [ZipCompressionLevel::SUPER_FAST, true, true],
  607. [ZipCompressionLevel::FAST, false, true],
  608. [3, false, false],
  609. [4, false, false],
  610. [ZipCompressionLevel::NORMAL, false, false],
  611. [6, false, false],
  612. [7, false, false],
  613. [8, false, false],
  614. [ZipCompressionLevel::MAXIMUM, true, false],
  615. ];
  616. }
  617. /**
  618. * @throws ZipException
  619. */
  620. public function testLegacyDefaultCompressionLevel(): void
  621. {
  622. $zipEntry = new ZipEntry('entry');
  623. $zipEntry->setCompressionMethod(ZipCompressionMethod::DEFLATED);
  624. $zipEntry->setCompressionLevel(ZipCompressionLevel::MAXIMUM);
  625. static::assertSame($zipEntry->getCompressionLevel(), ZipCompressionLevel::MAXIMUM);
  626. $zipEntry->setCompressionLevel(ZipEntry::UNKNOWN);
  627. static::assertSame($zipEntry->getCompressionLevel(), ZipCompressionLevel::NORMAL);
  628. }
  629. /**
  630. * @dataProvider provideInvalidCompressionLevel
  631. *
  632. * @throws ZipException
  633. */
  634. public function testInvalidCompressionLevel(int $compressionLevel): void
  635. {
  636. $this->expectException(InvalidArgumentException::class);
  637. $this->expectExceptionMessage(
  638. sprintf(
  639. 'Invalid compression level. Minimum level %s. Maximum level %s',
  640. ZipCompressionLevel::LEVEL_MIN,
  641. ZipCompressionLevel::LEVEL_MAX
  642. )
  643. );
  644. $zipEntry = new ZipEntry('entry');
  645. $zipEntry->setCompressionMethod(ZipCompressionMethod::DEFLATED);
  646. $zipEntry->setCompressionLevel($compressionLevel);
  647. }
  648. public function provideInvalidCompressionLevel(): array
  649. {
  650. return [
  651. [0],
  652. [-2],
  653. [10],
  654. [100],
  655. ];
  656. }
  657. /**
  658. * @dataProvider provideDosTime
  659. */
  660. public function testDosTime(int $dosTime): void
  661. {
  662. $zipEntry = new ZipEntry('entry');
  663. static::assertSame($zipEntry->getDosTime(), ZipEntry::UNKNOWN);
  664. $zipEntry->setDosTime($dosTime);
  665. static::assertSame($zipEntry->getDosTime(), $dosTime);
  666. }
  667. public function provideDosTime(): array
  668. {
  669. return [
  670. [0],
  671. [1043487716],
  672. [1177556759],
  673. [1282576076],
  674. ];
  675. }
  676. /**
  677. * @dataProvider provideInvalidDosTime
  678. */
  679. public function testInvalidDosTime(int $dosTime): void
  680. {
  681. if (\PHP_INT_SIZE === 4) {
  682. static::markTestSkipped('only 64 bit test');
  683. }
  684. $this->expectException(InvalidArgumentException::class);
  685. $this->expectExceptionMessage('DosTime out of range');
  686. $zipEntry = new ZipEntry('entry');
  687. $zipEntry->setDosTime($dosTime);
  688. }
  689. public function provideInvalidDosTime(): array
  690. {
  691. return [
  692. [-1],
  693. [0xFFFFFFFF + 1],
  694. ];
  695. }
  696. public function testSetTime(): void
  697. {
  698. $zipEntry = new ZipEntry('entry');
  699. static::assertSame($zipEntry->getDosTime(), ZipEntry::UNKNOWN);
  700. $zipEntry->setTime(ZipEntry::UNKNOWN);
  701. static::assertSame($zipEntry->getDosTime(), 0);
  702. $zipEntry->setTime(0);
  703. static::assertSame($zipEntry->getDosTime(), 0);
  704. }
  705. /**
  706. * @dataProvider provideExternalAttributes
  707. *
  708. * @noinspection PhpTooManyParametersInspection
  709. *
  710. * @param ?int $externalAttr
  711. */
  712. public function testExternalAttributes(
  713. string $entryName,
  714. int $expectedExternalAttr,
  715. int $createdOS,
  716. int $extractedOS,
  717. ?int $externalAttr,
  718. int $unixMode
  719. ): void {
  720. $zipEntry = new ZipEntry($entryName);
  721. static::assertSame($zipEntry->getExternalAttributes(), $expectedExternalAttr);
  722. $zipEntry
  723. ->setCreatedOS($createdOS)
  724. ->setExtractedOS($extractedOS)
  725. ;
  726. if ($externalAttr !== null) {
  727. $zipEntry->setExternalAttributes($externalAttr);
  728. static::assertSame($zipEntry->getExternalAttributes(), $externalAttr);
  729. }
  730. static::assertSame($zipEntry->getUnixMode(), $unixMode);
  731. }
  732. public function provideExternalAttributes(): array
  733. {
  734. return [
  735. [
  736. 'entry.txt',
  737. DosAttrs::DOS_ARCHIVE,
  738. ZipPlatform::OS_UNIX,
  739. ZipPlatform::OS_UNIX,
  740. (010644 << 16) | DosAttrs::DOS_ARCHIVE,
  741. 010644,
  742. ],
  743. [
  744. 'dir/',
  745. DosAttrs::DOS_DIRECTORY,
  746. ZipPlatform::OS_UNIX,
  747. ZipPlatform::OS_UNIX,
  748. (040755 << 16) | DosAttrs::DOS_DIRECTORY,
  749. 040755,
  750. ],
  751. [
  752. 'entry.txt',
  753. DosAttrs::DOS_ARCHIVE,
  754. ZipPlatform::OS_DOS,
  755. ZipPlatform::OS_DOS,
  756. null,
  757. 0100644,
  758. ],
  759. [
  760. 'entry.txt',
  761. DosAttrs::DOS_ARCHIVE,
  762. ZipPlatform::OS_DOS,
  763. ZipPlatform::OS_UNIX,
  764. null,
  765. 0100644,
  766. ],
  767. [
  768. 'entry.txt',
  769. DosAttrs::DOS_ARCHIVE,
  770. ZipPlatform::OS_UNIX,
  771. ZipPlatform::OS_DOS,
  772. null,
  773. 0100644,
  774. ],
  775. [
  776. 'dir/',
  777. DosAttrs::DOS_DIRECTORY,
  778. ZipPlatform::OS_DOS,
  779. ZipPlatform::OS_DOS,
  780. null,
  781. 040755,
  782. ],
  783. [
  784. 'dir/',
  785. DosAttrs::DOS_DIRECTORY,
  786. ZipPlatform::OS_DOS,
  787. ZipPlatform::OS_UNIX,
  788. null,
  789. 040755,
  790. ],
  791. [
  792. 'dir/',
  793. DosAttrs::DOS_DIRECTORY,
  794. ZipPlatform::OS_UNIX,
  795. ZipPlatform::OS_DOS,
  796. null,
  797. 040755,
  798. ],
  799. [
  800. 'entry.txt',
  801. DosAttrs::DOS_ARCHIVE,
  802. ZipPlatform::OS_UNIX,
  803. ZipPlatform::OS_UNIX,
  804. 0777 << 16,
  805. 0777,
  806. ],
  807. ];
  808. }
  809. /**
  810. * @dataProvider provideInvalidExternalAttributes
  811. */
  812. public function testInvalidExternalAttributes(int $externalAttributes): void
  813. {
  814. if (\PHP_INT_SIZE === 4) {
  815. static::markTestSkipped('only 64 bit test');
  816. }
  817. $this->expectException(InvalidArgumentException::class);
  818. $this->expectExceptionMessage('external attributes out of range');
  819. $zipEntry = new ZipEntry('entry');
  820. $zipEntry->setExternalAttributes($externalAttributes);
  821. }
  822. public function provideInvalidExternalAttributes(): array
  823. {
  824. return [
  825. [-1],
  826. [0xFFFFFFFF + 1],
  827. ];
  828. }
  829. public function testInternalAttributes(): void
  830. {
  831. $zipEntry = new ZipEntry('entry');
  832. static::assertSame($zipEntry->getInternalAttributes(), 0);
  833. $zipEntry->setInternalAttributes(1);
  834. static::assertSame($zipEntry->getInternalAttributes(), 1);
  835. }
  836. /**
  837. * @dataProvider provideInvalidInternalAttributes
  838. */
  839. public function testInvalidInternalAttributes(int $internalAttributes): void
  840. {
  841. $this->expectException(InvalidArgumentException::class);
  842. $this->expectExceptionMessage('internal attributes out of range');
  843. $zipEntry = new ZipEntry('entry');
  844. $zipEntry->setInternalAttributes($internalAttributes);
  845. }
  846. public function provideInvalidInternalAttributes(): array
  847. {
  848. return [
  849. [-1],
  850. [0xFFFF + 1],
  851. ];
  852. }
  853. public function testExtraFields(): void
  854. {
  855. $zipEntry = new ZipEntry('entry');
  856. $extraCdFields = $zipEntry->getCdExtraFields();
  857. $extraLocalFields = $zipEntry->getLocalExtraFields();
  858. static::assertCount(0, $extraCdFields);
  859. static::assertCount(0, $extraLocalFields);
  860. $extraNtfs = new NtfsExtraField(time(), time() - 10000, time() - 100000);
  861. $extraAsi = new AsiExtraField(010644);
  862. $extraJar = new JarMarkerExtraField();
  863. $extraLocalFields->add($extraNtfs);
  864. $extraCdFields->add($extraNtfs);
  865. static::assertCount(1, $extraCdFields);
  866. static::assertCount(1, $extraLocalFields);
  867. $zipEntry->addExtraField($extraAsi);
  868. static::assertCount(2, $extraCdFields);
  869. static::assertCount(2, $extraLocalFields);
  870. $zipEntry->addCdExtraField($extraJar);
  871. static::assertCount(3, $extraCdFields);
  872. static::assertCount(2, $extraLocalFields);
  873. static::assertSame($zipEntry->getCdExtraField(JarMarkerExtraField::HEADER_ID), $extraJar);
  874. static::assertNull($zipEntry->getLocalExtraField(JarMarkerExtraField::HEADER_ID));
  875. static::assertSame($zipEntry->getLocalExtraField(AsiExtraField::HEADER_ID), $extraAsi);
  876. static::assertSame(
  877. [$extraNtfs, $extraAsi, $extraJar],
  878. array_values($extraCdFields->getAll())
  879. );
  880. static::assertSame(
  881. [$extraNtfs, $extraAsi],
  882. array_values($extraLocalFields->getAll())
  883. );
  884. $zipEntry->removeExtraField(AsiExtraField::HEADER_ID);
  885. static::assertNull($zipEntry->getCdExtraField(AsiExtraField::HEADER_ID));
  886. static::assertNull($zipEntry->getLocalExtraField(AsiExtraField::HEADER_ID));
  887. static::assertCount(2, $extraCdFields);
  888. static::assertCount(1, $extraLocalFields);
  889. static::assertSame(
  890. [$extraNtfs, $extraJar],
  891. array_values($extraCdFields->getAll())
  892. );
  893. static::assertSame(
  894. [$extraNtfs],
  895. array_values($extraLocalFields->getAll())
  896. );
  897. static::assertTrue($zipEntry->hasExtraField(NtfsExtraField::HEADER_ID));
  898. static::assertTrue($zipEntry->hasExtraField(JarMarkerExtraField::HEADER_ID));
  899. static::assertFalse($zipEntry->hasExtraField(AsiExtraField::HEADER_ID));
  900. }
  901. public function testComment(): void
  902. {
  903. $zipEntry = new ZipEntry('entry');
  904. static::assertSame($zipEntry->getComment(), '');
  905. $zipEntry->setComment('comment');
  906. static::assertSame($zipEntry->getComment(), 'comment');
  907. $zipEntry->setComment(null);
  908. static::assertSame($zipEntry->getComment(), '');
  909. static::assertFalse($zipEntry->isUtf8Flag());
  910. $zipEntry->setComment('комментарий');
  911. static::assertTrue($zipEntry->isUtf8Flag());
  912. static::assertSame($zipEntry->getComment(), 'комментарий');
  913. }
  914. /**
  915. * @throws \Exception
  916. */
  917. public function testLongComment(): void
  918. {
  919. $this->expectException(InvalidArgumentException::class);
  920. $this->expectExceptionMessage('Comment too long');
  921. $longComment = random_bytes(0xFFFF + 1);
  922. $zipEntry = new ZipEntry('entry');
  923. $zipEntry->setComment($longComment);
  924. }
  925. /**
  926. * @dataProvider provideDataDescriptorRequired
  927. */
  928. public function testDataDescriptorRequired(int $crc, int $compressedSize, int $uncompressedSize, bool $required): void
  929. {
  930. $zipEntry = new ZipEntry('entry');
  931. $zipEntry->setCrc($crc);
  932. $zipEntry->setCompressedSize($compressedSize);
  933. $zipEntry->setUncompressedSize($uncompressedSize);
  934. static::assertSame($zipEntry->isDataDescriptorRequired(), $required);
  935. static::assertSame($zipEntry->getCrc(), $crc);
  936. static::assertSame($zipEntry->getCompressedSize(), $compressedSize);
  937. static::assertSame($zipEntry->getUncompressedSize(), $uncompressedSize);
  938. }
  939. public function provideDataDescriptorRequired(): array
  940. {
  941. return [
  942. [ZipEntry::UNKNOWN, ZipEntry::UNKNOWN, ZipEntry::UNKNOWN, true],
  943. [0xF33F33, ZipEntry::UNKNOWN, ZipEntry::UNKNOWN, true],
  944. [0xF33F33, 11111111, ZipEntry::UNKNOWN, true],
  945. [0xF33F33, ZipEntry::UNKNOWN, 22333333, true],
  946. [ZipEntry::UNKNOWN, 11111111, ZipEntry::UNKNOWN, true],
  947. [ZipEntry::UNKNOWN, 11111111, 22333333, true],
  948. [ZipEntry::UNKNOWN, ZipEntry::UNKNOWN, 22333333, true],
  949. [0xF33F33, 11111111, 22333333, false],
  950. ];
  951. }
  952. /**
  953. * @dataProvider provideEncryption
  954. *
  955. * @param ?string $password
  956. * @param ?int $encryptionMethod
  957. */
  958. public function testEncryption(?string $password, ?int $encryptionMethod, bool $encrypted, int $expectedEncryptionMethod): void
  959. {
  960. $zipEntry = new ZipEntry('entry');
  961. $zipEntry->setPassword($password, $encryptionMethod);
  962. static::assertSame($zipEntry->isEncrypted(), $encrypted);
  963. static::assertSame($zipEntry->getPassword(), $password);
  964. static::assertSame($zipEntry->getEncryptionMethod(), $expectedEncryptionMethod);
  965. $zipEntry->setPassword($password);
  966. static::assertSame($zipEntry->getEncryptionMethod(), $expectedEncryptionMethod);
  967. }
  968. public function provideEncryption(): array
  969. {
  970. return [
  971. [null, null, false, ZipEncryptionMethod::NONE],
  972. [null, ZipEncryptionMethod::WINZIP_AES_256, false, ZipEncryptionMethod::NONE],
  973. ['12345', null, true, ZipEncryptionMethod::WINZIP_AES_256],
  974. ['12345', ZipEncryptionMethod::PKWARE, true, ZipEncryptionMethod::PKWARE],
  975. ['12345', ZipEncryptionMethod::WINZIP_AES_256, true, ZipEncryptionMethod::WINZIP_AES_256],
  976. ['12345', ZipEncryptionMethod::WINZIP_AES_128, true, ZipEncryptionMethod::WINZIP_AES_128],
  977. ['12345', ZipEncryptionMethod::WINZIP_AES_192, true, ZipEncryptionMethod::WINZIP_AES_192],
  978. ];
  979. }
  980. public function testDirectoryEncryption(): void
  981. {
  982. $zipEntry = new ZipEntry('directory/');
  983. $zipEntry->setPassword('12345', ZipEncryptionMethod::WINZIP_AES_256);
  984. static::assertTrue($zipEntry->isDirectory());
  985. static::assertNull($zipEntry->getPassword());
  986. static::assertFalse($zipEntry->isEncrypted());
  987. static::assertSame($zipEntry->getEncryptionMethod(), ZipEncryptionMethod::NONE);
  988. }
  989. /**
  990. * @dataProvider provideEncryptionMethod
  991. *
  992. * @param ?int $encryptionMethod
  993. */
  994. public function testEncryptionMethod(
  995. ?int $encryptionMethod,
  996. int $expectedEncryptionMethod,
  997. bool $encrypted,
  998. int $extractVersion
  999. ): void {
  1000. $zipEntry = new ZipEntry('entry');
  1001. $zipEntry->setEncryptionMethod($encryptionMethod);
  1002. static::assertSame($zipEntry->isEncrypted(), $encrypted);
  1003. static::assertSame($zipEntry->getEncryptionMethod(), $expectedEncryptionMethod);
  1004. static::assertSame($zipEntry->getExtractVersion(), $extractVersion);
  1005. }
  1006. public function provideEncryptionMethod(): array
  1007. {
  1008. return [
  1009. [
  1010. null,
  1011. ZipEncryptionMethod::NONE,
  1012. false,
  1013. ZipVersion::v10_DEFAULT_MIN,
  1014. ],
  1015. [
  1016. ZipEncryptionMethod::NONE,
  1017. ZipEncryptionMethod::NONE,
  1018. false,
  1019. ZipVersion::v10_DEFAULT_MIN,
  1020. ],
  1021. [
  1022. ZipEncryptionMethod::PKWARE,
  1023. ZipEncryptionMethod::PKWARE,
  1024. true,
  1025. ZipVersion::v20_DEFLATED_FOLDER_ZIPCRYPTO,
  1026. ],
  1027. [
  1028. ZipEncryptionMethod::WINZIP_AES_256,
  1029. ZipEncryptionMethod::WINZIP_AES_256,
  1030. true,
  1031. ZipVersion::v51_ENCR_AES_RC2_CORRECT,
  1032. ],
  1033. [
  1034. ZipEncryptionMethod::WINZIP_AES_192,
  1035. ZipEncryptionMethod::WINZIP_AES_192,
  1036. true,
  1037. ZipVersion::v51_ENCR_AES_RC2_CORRECT,
  1038. ],
  1039. [
  1040. ZipEncryptionMethod::WINZIP_AES_128,
  1041. ZipEncryptionMethod::WINZIP_AES_128,
  1042. true,
  1043. ZipVersion::v51_ENCR_AES_RC2_CORRECT,
  1044. ],
  1045. ];
  1046. }
  1047. /**
  1048. * @dataProvider provideInvalidEncryptionMethod
  1049. */
  1050. public function testInvalidEncryptionMethod(int $encryptionMethod): void
  1051. {
  1052. $this->expectException(InvalidArgumentException::class);
  1053. $this->expectExceptionMessage(sprintf('Encryption method %d is not supported.', $encryptionMethod));
  1054. $zipEntry = new ZipEntry('entry');
  1055. $zipEntry->setEncryptionMethod($encryptionMethod);
  1056. }
  1057. public function provideInvalidEncryptionMethod(): array
  1058. {
  1059. return [
  1060. [-2],
  1061. [4],
  1062. [5],
  1063. ];
  1064. }
  1065. /**
  1066. * @dataProvider provideUnixMode
  1067. */
  1068. public function testUnixMode(string $entryName, int $unixMode): void
  1069. {
  1070. $zipEntry = new ZipEntry($entryName);
  1071. $zipEntry->setUnixMode($unixMode);
  1072. static::assertSame($zipEntry->getUnixMode(), $unixMode);
  1073. static::assertSame($zipEntry->getCreatedOS(), ZipPlatform::OS_UNIX);
  1074. }
  1075. public function provideUnixMode(): array
  1076. {
  1077. return [
  1078. ['entry.txt', 0700], // read, write, & execute only for owner
  1079. ['entry.txt', 0770], // read, write, & execute for owner and group
  1080. ['entry.txt', 0777], // read, write, & execute for owner, group and others
  1081. ['entry.txt', 0111], // execute
  1082. ['entry.txt', 0222], // write
  1083. ['entry.txt', 0333], // write & execute
  1084. ['entry.txt', 0444], // read
  1085. ['entry.txt', 0555], // read & execute
  1086. ['entry.txt', 0666], // read & write
  1087. ['entry.txt', 0740], // owner can read, write, & execute; group can only read; others have no permissions
  1088. ['entry.txt', 0777], // owner can read, write, & execute
  1089. ['directory/', 040700], // directory, read, write, & execute only for owner
  1090. ['directory/', 040770], // directory, read, write, & execute for owner and group
  1091. ['directory/', 040777], // directory, read, write, & execute
  1092. ];
  1093. }
  1094. /**
  1095. * @dataProvider provideUnixMode
  1096. * @dataProvider provideSymlink
  1097. */
  1098. public function testSymlink(string $entryName, int $unixMode, bool $symlink = false): void
  1099. {
  1100. $zipEntry = new ZipEntry($entryName);
  1101. $zipEntry->setUnixMode($unixMode);
  1102. static::assertSame($zipEntry->isUnixSymlink(), $symlink);
  1103. }
  1104. public function testAsiUnixMode(): void
  1105. {
  1106. $unixMode = 0100666;
  1107. $asiUnixMode = 0100600;
  1108. $asiExtraField = new AsiExtraField($asiUnixMode);
  1109. $zipEntry = new ZipEntry('entry');
  1110. $zipEntry->setCreatedOS(ZipPlatform::OS_DOS);
  1111. $zipEntry->setExtractedOS(ZipPlatform::OS_DOS);
  1112. $zipEntry->setExternalAttributes(DosAttrs::DOS_ARCHIVE);
  1113. $zipEntry->addExtraField($asiExtraField);
  1114. static::assertSame($zipEntry->getUnixMode(), $asiUnixMode);
  1115. $zipEntry->setUnixMode($unixMode);
  1116. static::assertSame($zipEntry->getCreatedOS(), ZipPlatform::OS_UNIX);
  1117. static::assertSame($zipEntry->getUnixMode(), $unixMode);
  1118. }
  1119. public function provideSymlink(): array
  1120. {
  1121. return [
  1122. ['entry', 0120644, true],
  1123. ['dir/', 0120755, true],
  1124. ];
  1125. }
  1126. /**
  1127. * @dataProvider provideIsZip64ExtensionsRequired
  1128. */
  1129. public function testIsZip64ExtensionsRequired(int $compressionSize, int $uncompressionSize, bool $required): void
  1130. {
  1131. if (\PHP_INT_SIZE === 4) {
  1132. static::markTestSkipped('only php 64-bit');
  1133. }
  1134. $zipEntry = new ZipEntry('entry');
  1135. $zipEntry->setCompressedSize($compressionSize);
  1136. $zipEntry->setUncompressedSize($uncompressionSize);
  1137. static::assertSame($zipEntry->isZip64ExtensionsRequired(), $required);
  1138. }
  1139. public function provideIsZip64ExtensionsRequired(): array
  1140. {
  1141. return [
  1142. [11111111, 22222222, false],
  1143. [ZipEntry::UNKNOWN, ZipEntry::UNKNOWN, false],
  1144. [ZipEntry::UNKNOWN, ZipConstants::ZIP64_MAGIC + 1, true],
  1145. [ZipConstants::ZIP64_MAGIC + 1, ZipEntry::UNKNOWN, true],
  1146. [ZipConstants::ZIP64_MAGIC + 1, ZipConstants::ZIP64_MAGIC + 1, true],
  1147. [ZipConstants::ZIP64_MAGIC, ZipConstants::ZIP64_MAGIC, false],
  1148. [ZipConstants::ZIP64_MAGIC, ZipEntry::UNKNOWN, false],
  1149. [ZipEntry::UNKNOWN, ZipConstants::ZIP64_MAGIC, false],
  1150. ];
  1151. }
  1152. /**
  1153. * @dataProvider provideExtraTime
  1154. *
  1155. * @param ?\DateTimeInterface $atime
  1156. * @param ?\DateTimeInterface $ctime
  1157. */
  1158. public function testMTimeATimeCTime(ExtraFieldsCollection $extraFieldsCollection, \DateTimeInterface $mtime, ?\DateTimeInterface $atime, ?\DateTimeInterface $ctime): void
  1159. {
  1160. $unixTimestamp = time();
  1161. $zipEntry = new ZipEntry('entry');
  1162. $zipEntry->setTime($unixTimestamp);
  1163. // converting from unixtime to dos may occur with a small margin of error
  1164. static::assertLessThanOrEqual($zipEntry->getMTime()->getTimestamp() + 1, $unixTimestamp);
  1165. static::assertGreaterThanOrEqual($zipEntry->getMTime()->getTimestamp() - 1, $unixTimestamp);
  1166. static::assertNull($zipEntry->getATime());
  1167. static::assertNull($zipEntry->getCTime());
  1168. $zipEntry->getCdExtraFields()->addCollection($extraFieldsCollection);
  1169. $zipEntry->getLocalExtraFields()->addCollection($extraFieldsCollection);
  1170. static::assertNotNull($zipEntry->getMTime());
  1171. static::assertSame($zipEntry->getMTime()->getTimestamp(), $mtime->getTimestamp());
  1172. if ($atime !== null) {
  1173. static::assertSame($zipEntry->getATime()->getTimestamp(), $atime->getTimestamp());
  1174. } else {
  1175. static::assertNull($zipEntry->getATime());
  1176. }
  1177. if ($ctime !== null) {
  1178. static::assertSame($zipEntry->getCTime()->getTimestamp(), $ctime->getTimestamp());
  1179. } else {
  1180. static::assertNull($zipEntry->getCTime());
  1181. }
  1182. }
  1183. /**
  1184. * @throws \Exception
  1185. */
  1186. public function provideExtraTime(): array
  1187. {
  1188. $ntfsExtra = NtfsExtraField::create(
  1189. new \DateTimeImmutable('-1 week'),
  1190. new \DateTimeImmutable('-1 month'),
  1191. new \DateTimeImmutable('-1 year')
  1192. );
  1193. $extendedTimestampExtraField = ExtendedTimestampExtraField::create(
  1194. strtotime('-2 weeks'),
  1195. strtotime('-2 months'),
  1196. strtotime('-2 years')
  1197. );
  1198. $oldUnixExtraField = new OldUnixExtraField(
  1199. strtotime('-3 weeks'),
  1200. strtotime('-3 months'),
  1201. 1000,
  1202. 1000
  1203. );
  1204. $ntfsTimeCollection = new ExtraFieldsCollection();
  1205. $ntfsTimeCollection->add($ntfsExtra);
  1206. $extendedTimestampCollection = new ExtraFieldsCollection();
  1207. $extendedTimestampCollection->add($extendedTimestampExtraField);
  1208. $oldUnixExtraFieldCollection = new ExtraFieldsCollection();
  1209. $oldUnixExtraFieldCollection->add($oldUnixExtraField);
  1210. $oldExtendedCollection = clone $oldUnixExtraFieldCollection;
  1211. $oldExtendedCollection->add($extendedTimestampExtraField);
  1212. $fullCollection = clone $oldExtendedCollection;
  1213. $fullCollection->add($ntfsExtra);
  1214. return [
  1215. [
  1216. $ntfsTimeCollection,
  1217. $ntfsExtra->getModifyDateTime(),
  1218. $ntfsExtra->getAccessDateTime(),
  1219. $ntfsExtra->getCreateDateTime(),
  1220. ],
  1221. [
  1222. $extendedTimestampCollection,
  1223. $extendedTimestampExtraField->getModifyDateTime(),
  1224. $extendedTimestampExtraField->getAccessDateTime(),
  1225. $extendedTimestampExtraField->getCreateDateTime(),
  1226. ],
  1227. [
  1228. $oldUnixExtraFieldCollection,
  1229. $oldUnixExtraField->getModifyDateTime(),
  1230. $oldUnixExtraField->getAccessDateTime(),
  1231. null,
  1232. ],
  1233. [
  1234. $oldExtendedCollection,
  1235. $extendedTimestampExtraField->getModifyDateTime(),
  1236. $extendedTimestampExtraField->getAccessDateTime(),
  1237. $extendedTimestampExtraField->getCreateDateTime(),
  1238. ],
  1239. [
  1240. $fullCollection,
  1241. $ntfsExtra->getModifyDateTime(),
  1242. $ntfsExtra->getAccessDateTime(),
  1243. $ntfsExtra->getCreateDateTime(),
  1244. ],
  1245. ];
  1246. }
  1247. /**
  1248. * @throws ZipException
  1249. */
  1250. public function testClone(): void
  1251. {
  1252. $newUnixExtra = new NewUnixExtraField();
  1253. $zipEntry = new ZipEntry('entry');
  1254. $zipData = new ZipFileData($zipEntry, new \SplFileInfo(__FILE__));
  1255. $zipEntry->addExtraField($newUnixExtra);
  1256. $zipEntry->setData($zipData);
  1257. $cloneEntry = clone $zipEntry;
  1258. static::assertNotSame($cloneEntry, $zipEntry);
  1259. static::assertNotSame($cloneEntry->getCdExtraFields(), $zipEntry->getCdExtraFields());
  1260. static::assertNotSame($cloneEntry->getLocalExtraFields(), $zipEntry->getLocalExtraFields());
  1261. static::assertNotSame($cloneEntry->getCdExtraField(NewUnixExtraField::HEADER_ID), $newUnixExtra);
  1262. static::assertNotSame($cloneEntry->getLocalExtraField(NewUnixExtraField::HEADER_ID), $newUnixExtra);
  1263. static::assertNotSame($cloneEntry->getData(), $zipData);
  1264. }
  1265. public function testExtraCollection(): void
  1266. {
  1267. $zipEntry = new ZipEntry('entry');
  1268. $cdCollection = $zipEntry->getCdExtraFields();
  1269. $localCollection = $zipEntry->getLocalExtraFields();
  1270. static::assertNotSame($cdCollection, $localCollection);
  1271. $anotherCollection = new ExtraFieldsCollection();
  1272. $anotherCollection->add(new JarMarkerExtraField());
  1273. $anotherCollection->add(new AsiExtraField(0100777, 1000, 1000));
  1274. $zipEntry->setCdExtraFields($anotherCollection);
  1275. static::assertSame($anotherCollection, $zipEntry->getCdExtraFields());
  1276. static::assertSame($localCollection, $zipEntry->getLocalExtraFields());
  1277. $zipEntry->setLocalExtraFields($anotherCollection);
  1278. static::assertSame($anotherCollection, $zipEntry->getLocalExtraFields());
  1279. static::assertSame($zipEntry->getCdExtraFields(), $zipEntry->getLocalExtraFields());
  1280. $newUnixExtraField = new NewUnixExtraField(1, 1000, 1000);
  1281. $zipEntry->getCdExtraFields()->add($newUnixExtraField);
  1282. static::assertSame($zipEntry->getCdExtraField(NewUnixExtraField::HEADER_ID), $newUnixExtraField);
  1283. static::assertSame($zipEntry->getLocalExtraField(NewUnixExtraField::HEADER_ID), $newUnixExtraField);
  1284. }
  1285. }