| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class JpegMetadataExtractorTest extends MediaWikiIntegrationTestCase { |
| 13 |
private const FILE_PATH = __DIR__ . '/../../data/media/'; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public function testUtf8Comment( $file ) { |
| 24 |
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . $file ); |
| 25 |
$this->assertEquals( [ 'UTF-8 JPEG Comment — ¼' ], $res['COM'] ); |
| 26 |
} |
| 27 |
|
| 28 |
public static function provideUtf8Comment() { |
| 29 |
return [ |
| 30 |
[ 'jpeg-comment-utf.jpg' ], |
| 31 |
[ 'jpeg-padding-even.jpg' ], |
| 32 |
[ 'jpeg-padding-odd.jpg' ], |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
public function testIso88591Comment() { |
| 38 |
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-comment-iso8859-1.jpg' ); |
| 39 |
$this->assertEquals( [ 'ISO-8859-1 JPEG Comment - ¼' ], $res['COM'] ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function testBinaryCommentStripped() { |
| 47 |
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-comment-binary.jpg' ); |
| 48 |
$this->assertSame( [], $res['COM'] ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function testMultipleComment() { |
| 55 |
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-comment-multiple.jpg' ); |
| 56 |
$this->assertEquals( [ 'foo', 'bar' ], $res['COM'] ); |
| 57 |
} |
| 58 |
|
| 59 |
public function testXMPExtraction() { |
| 60 |
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-xmp-psir.jpg' ); |
| 61 |
$expected = file_get_contents( self::FILE_PATH . 'jpeg-xmp-psir.xmp' ); |
| 62 |
$this->assertEquals( $expected, $res['XMP'] ); |
| 63 |
} |
| 64 |
|
| 65 |
public function testPSIRExtraction() { |
| 66 |
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-xmp-psir.jpg' ); |
| 67 |
$expected = '50686f746f73686f7020332e30003842494d04040000000' |
| 68 |
. '000181c02190004746573741c02190003666f6f1c020000020004'; |
| 69 |
$this->assertEquals( $expected, bin2hex( $res['PSIR'][0] ) ); |
| 70 |
} |
| 71 |
|
| 72 |
public function testXMPExtractionNullChar() { |
| 73 |
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-xmp-nullchar.jpg' ); |
| 74 |
$expected = file_get_contents( self::FILE_PATH . 'jpeg-xmp-psir.xmp' ); |
| 75 |
$this->assertEquals( $expected, $res['XMP'] ); |
| 76 |
} |
| 77 |
|
| 78 |
public function testXMPExtractionAltAppId() { |
| 79 |
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-xmp-alt.jpg' ); |
| 80 |
$expected = file_get_contents( self::FILE_PATH . 'jpeg-xmp-psir.xmp' ); |
| 81 |
$this->assertEquals( $expected, $res['XMP'] ); |
| 82 |
} |
| 83 |
|
| 84 |
public function testIPTCHashComparisionNoHash() { |
| 85 |
$segments = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-xmp-psir.jpg' ); |
| 86 |
$res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] ); |
| 87 |
|
| 88 |
$this->assertEquals( 'iptc-no-hash', $res ); |
| 89 |
} |
| 90 |
|
| 91 |
public function testIPTCHashComparisionBadHash() { |
| 92 |
$segments = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-iptc-bad-hash.jpg' ); |
| 93 |
$res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] ); |
| 94 |
|
| 95 |
$this->assertEquals( 'iptc-bad-hash', $res ); |
| 96 |
} |
| 97 |
|
| 98 |
public function testIPTCHashComparisionGoodHash() { |
| 99 |
$segments = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-iptc-good-hash.jpg' ); |
| 100 |
$res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] ); |
| 101 |
|
| 102 |
$this->assertEquals( 'iptc-good-hash', $res ); |
| 103 |
} |
| 104 |
|
| 105 |
public function testExifByteOrder() { |
| 106 |
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'exif-user-comment.jpg' ); |
| 107 |
$expected = 'BE'; |
| 108 |
$this->assertEquals( $expected, $res['byteOrder'] ); |
| 109 |
} |
| 110 |
|
| 111 |
public function testInfiniteRead() { |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
$this->expectException( InvalidJpegException::class ); |
| 116 |
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-segment-loop1.jpg' ); |
| 117 |
} |
| 118 |
|
| 119 |
public function testInfiniteRead2() { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
$this->expectException( InvalidJpegException::class ); |
| 125 |
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-segment-loop2.jpg' ); |
| 126 |
} |
| 127 |
} |