NtfsExtraFieldTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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\Model\Extra\Fields\NtfsExtraField;
  12. /**
  13. * @internal
  14. *
  15. * @small
  16. */
  17. final class NtfsExtraFieldTest extends TestCase
  18. {
  19. protected function setUp(): void
  20. {
  21. if (\PHP_INT_SIZE === 4) {
  22. self::markTestSkipped('only 64 bit test');
  23. }
  24. }
  25. /**
  26. * @dataProvider provideExtraField
  27. *
  28. * @throws \Exception
  29. *
  30. * @noinspection PhpTooManyParametersInspection
  31. */
  32. public function testExtraField(
  33. int $modifyNtfsTime,
  34. int $accessNtfsTime,
  35. int $createNtfsTime,
  36. float $modifyTimestamp,
  37. float $accessTimestamp,
  38. float $createTimestamp,
  39. string $binaryData
  40. ): void {
  41. $extraField = new NtfsExtraField($modifyNtfsTime, $accessNtfsTime, $createNtfsTime);
  42. self::assertSame($extraField->getHeaderId(), NtfsExtraField::HEADER_ID);
  43. self::assertEquals($extraField->getModifyDateTime()->getTimestamp(), (int) $modifyTimestamp);
  44. self::assertEquals($extraField->getAccessDateTime()->getTimestamp(), (int) $accessTimestamp);
  45. self::assertEquals($extraField->getCreateDateTime()->getTimestamp(), (int) $createTimestamp);
  46. self::assertEquals(NtfsExtraField::unpackLocalFileData($binaryData), $extraField);
  47. self::assertEquals(NtfsExtraField::unpackCentralDirData($binaryData), $extraField);
  48. self::assertSame($extraField->packLocalFileData(), $binaryData);
  49. self::assertSame($extraField->packCentralDirData(), $binaryData);
  50. $extraFieldFromDateTime = NtfsExtraField::create(
  51. $extraField->getModifyDateTime(),
  52. $extraField->getAccessDateTime(),
  53. $extraField->getCreateDateTime()
  54. );
  55. self::assertEqualsIntegerWithDelta(
  56. $extraFieldFromDateTime->getModifyNtfsTime(),
  57. $extraField->getModifyNtfsTime(),
  58. 100
  59. );
  60. self::assertEqualsIntegerWithDelta(
  61. $extraFieldFromDateTime->getAccessNtfsTime(),
  62. $extraField->getAccessNtfsTime(),
  63. 100
  64. );
  65. self::assertEqualsIntegerWithDelta(
  66. $extraFieldFromDateTime->getCreateNtfsTime(),
  67. $extraField->getCreateNtfsTime(),
  68. 100
  69. );
  70. }
  71. public function provideExtraField(): array
  72. {
  73. return [
  74. [
  75. 129853553114795379,
  76. 129853553114795379,
  77. 129853552641022547,
  78. 1340881711.4795379,
  79. 1340881711.4795379,
  80. 1340881664.1022547,
  81. "\x00\x00\x00\x00\x01\x00\x18\x00s\xCD:Z\x1EU\xCD\x01s\xCD:Z\x1EU\xCD\x01S\x9A\xFD=\x1EU\xCD\x01",
  82. ],
  83. [
  84. 131301570250000000,
  85. 131865940850000000,
  86. 131840940680000000,
  87. 1485683425.000000,
  88. 1542120485.000000,
  89. 1539620468.000000,
  90. "\x00\x00\x00\x00\x01\x00\x18\x00\x80\xC63\x1D\x15z\xD2\x01\x80@V\xE2_{\xD4\x01\x00\xB2\x15\x14\xA3d\xD4\x01",
  91. ],
  92. [
  93. 132181086710000000,
  94. 132181086710000000,
  95. 132181086710000000,
  96. 1573635071.000000,
  97. 1573635071.000000,
  98. 1573635071.000000,
  99. "\x00\x00\x00\x00\x01\x00\x18\x00\x80\xE9_\x7F\xFF\x99\xD5\x01\x80\xE9_\x7F\xFF\x99\xD5\x01\x80\xE9_\x7F\xFF\x99\xD5\x01",
  100. ],
  101. ];
  102. }
  103. private static function assertEqualsIntegerWithDelta(
  104. int $expected,
  105. int $actual,
  106. int $delta,
  107. string $message = ''
  108. ): void {
  109. self::assertSame(
  110. self::roundInt($expected, $delta),
  111. self::roundInt($actual, $delta),
  112. $message
  113. );
  114. }
  115. private static function roundInt(int $number, int $delta): int
  116. {
  117. return (int) (floor($number / $delta) * $delta);
  118. }
  119. /**
  120. * @dataProvider provideExtraField
  121. *
  122. * @noinspection PhpTooManyParametersInspection
  123. */
  124. public function testConverter(
  125. int $mtimeNtfs,
  126. int $atimeNtfs,
  127. int $ctimeNtfs,
  128. float $mtimeTimestamp,
  129. float $atimeTimestamp,
  130. float $ctimeTimestamp
  131. ): void {
  132. self::assertEqualsWithDelta(NtfsExtraField::ntfsTimeToTimestamp($mtimeNtfs), $mtimeTimestamp, 0.00001);
  133. self::assertEqualsWithDelta(NtfsExtraField::ntfsTimeToTimestamp($atimeNtfs), $atimeTimestamp, 0.00001);
  134. self::assertEqualsWithDelta(NtfsExtraField::ntfsTimeToTimestamp($ctimeNtfs), $ctimeTimestamp, 0.00001);
  135. self::assertEqualsIntegerWithDelta(NtfsExtraField::timestampToNtfsTime($mtimeTimestamp), $mtimeNtfs, 10);
  136. self::assertEqualsIntegerWithDelta(NtfsExtraField::timestampToNtfsTime($atimeTimestamp), $atimeNtfs, 10);
  137. self::assertEqualsIntegerWithDelta(NtfsExtraField::timestampToNtfsTime($ctimeTimestamp), $ctimeNtfs, 10);
  138. }
  139. /**
  140. * @throws \Exception
  141. */
  142. public function testSetter(): void
  143. {
  144. $timeZone = new \DateTimeZone('UTC');
  145. $initDateTime = new \DateTimeImmutable('-1 min', $timeZone);
  146. $mtimeDateTime = new \DateTimeImmutable('-1 hour', $timeZone);
  147. $atimeDateTime = new \DateTimeImmutable('-1 day', $timeZone);
  148. $ctimeDateTime = new \DateTimeImmutable('-1 year', $timeZone);
  149. $extraField = NtfsExtraField::create($initDateTime, $initDateTime, $initDateTime);
  150. self::assertEquals(
  151. $extraField->getModifyDateTime()->getTimestamp(),
  152. $initDateTime->getTimestamp()
  153. );
  154. self::assertEquals(
  155. $extraField->getAccessDateTime()->getTimestamp(),
  156. $initDateTime->getTimestamp()
  157. );
  158. self::assertEquals(
  159. $extraField->getCreateDateTime()->getTimestamp(),
  160. $initDateTime->getTimestamp()
  161. );
  162. $extraField->setModifyDateTime($mtimeDateTime);
  163. self::assertEquals(
  164. $extraField->getModifyDateTime()->getTimestamp(),
  165. $mtimeDateTime->getTimestamp()
  166. );
  167. self::assertEquals(
  168. $extraField->getAccessDateTime()->getTimestamp(),
  169. $initDateTime->getTimestamp()
  170. );
  171. self::assertEquals(
  172. $extraField->getCreateDateTime()->getTimestamp(),
  173. $initDateTime->getTimestamp()
  174. );
  175. $extraField->setAccessDateTime($atimeDateTime);
  176. self::assertEquals(
  177. $extraField->getModifyDateTime()->getTimestamp(),
  178. $mtimeDateTime->getTimestamp()
  179. );
  180. self::assertEquals(
  181. $extraField->getAccessDateTime()->getTimestamp(),
  182. $atimeDateTime->getTimestamp()
  183. );
  184. self::assertEquals(
  185. $extraField->getCreateDateTime()->getTimestamp(),
  186. $initDateTime->getTimestamp()
  187. );
  188. $extraField->setCreateDateTime($ctimeDateTime);
  189. self::assertEquals(
  190. $extraField->getModifyDateTime()->getTimestamp(),
  191. $mtimeDateTime->getTimestamp()
  192. );
  193. self::assertEquals(
  194. $extraField->getAccessDateTime()->getTimestamp(),
  195. $atimeDateTime->getTimestamp()
  196. );
  197. self::assertEquals(
  198. $extraField->getCreateDateTime()->getTimestamp(),
  199. $ctimeDateTime->getTimestamp()
  200. );
  201. $newModifyNtfsTime = $extraField->getCreateNtfsTime();
  202. $newAccessNtfsTime = $extraField->getModifyNtfsTime();
  203. $newCreateNtfsTime = $extraField->getAccessNtfsTime();
  204. $extraField->setModifyNtfsTime($newModifyNtfsTime);
  205. $extraField->setAccessNtfsTime($newAccessNtfsTime);
  206. $extraField->setCreateNtfsTime($newCreateNtfsTime);
  207. self::assertSame($extraField->getModifyNtfsTime(), $newModifyNtfsTime);
  208. self::assertSame($extraField->getAccessNtfsTime(), $newAccessNtfsTime);
  209. self::assertSame($extraField->getCreateNtfsTime(), $newCreateNtfsTime);
  210. }
  211. }