3 * @todo Could use a test of extended XMP segments. Hard to find programs that
4 * create example files, and creating my own in vim probably wouldn't
5 * serve as a very good "test". (Adobe photoshop probably creates such files
6 * but it costs money). The implementation of it currently in MediaWiki is based
7 * solely on reading the standard, without any real world test files.
10 * @covers \JpegMetadataExtractor
12 class JpegMetadataExtractorTest
extends MediaWikiIntegrationTestCase
{
13 private const FILE_PATH
= __DIR__
. '/../../data/media/';
16 * We also use this test to test padding bytes don't
19 * @param string $file Filename
21 * @dataProvider provideUtf8Comment
23 public function testUtf8Comment( $file ) {
24 $res = JpegMetadataExtractor
::segmentSplitter( self
::FILE_PATH
. $file );
25 $this->assertEquals( [ 'UTF-8 JPEG Comment — ¼' ], $res['COM'] );
28 public static function provideUtf8Comment() {
30 [ 'jpeg-comment-utf.jpg' ],
31 [ 'jpeg-padding-even.jpg' ],
32 [ 'jpeg-padding-odd.jpg' ],
36 /** The file is iso-8859-1, but it should get auto converted */
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'] );
42 /** Comment values that are non-textual (random binary junk) should not be shown.
43 * The example test file has a comment with a 0x5 byte in it which is a control character
44 * and considered binary junk for our purposes.
46 public function testBinaryCommentStripped() {
47 $res = JpegMetadataExtractor
::segmentSplitter( self
::FILE_PATH
. 'jpeg-comment-binary.jpg' );
48 $this->assertSame( [], $res['COM'] );
51 /* Very rarely a file can have multiple comments.
52 * Order of comments is based on order inside the file.
54 public function testMultipleComment() {
55 $res = JpegMetadataExtractor
::segmentSplitter( self
::FILE_PATH
. 'jpeg-comment-multiple.jpg' );
56 $this->assertEquals( [ 'foo', 'bar' ], $res['COM'] );
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'] );
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] ) );
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'] );
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'] );
84 public function testIPTCHashComparisionNoHash() {
85 $segments = JpegMetadataExtractor
::segmentSplitter( self
::FILE_PATH
. 'jpeg-xmp-psir.jpg' );
86 $res = JpegMetadataExtractor
::doPSIR( $segments['PSIR'][0] );
88 $this->assertEquals( 'iptc-no-hash', $res );
91 public function testIPTCHashComparisionBadHash() {
92 $segments = JpegMetadataExtractor
::segmentSplitter( self
::FILE_PATH
. 'jpeg-iptc-bad-hash.jpg' );
93 $res = JpegMetadataExtractor
::doPSIR( $segments['PSIR'][0] );
95 $this->assertEquals( 'iptc-bad-hash', $res );
98 public function testIPTCHashComparisionGoodHash() {
99 $segments = JpegMetadataExtractor
::segmentSplitter( self
::FILE_PATH
. 'jpeg-iptc-good-hash.jpg' );
100 $res = JpegMetadataExtractor
::doPSIR( $segments['PSIR'][0] );
102 $this->assertEquals( 'iptc-good-hash', $res );
105 public function testExifByteOrder() {
106 $res = JpegMetadataExtractor
::segmentSplitter( self
::FILE_PATH
. 'exif-user-comment.jpg' );
108 $this->assertEquals( $expected, $res['byteOrder'] );
111 public function testInfiniteRead() {
112 // test file truncated right after a segment, which previously
113 // caused an infinite loop looking for the next segment byte.
114 // Should get past infinite loop and throw in StringUtils::unpack()
115 $this->expectException( InvalidJpegException
::class );
116 $res = JpegMetadataExtractor
::segmentSplitter( self
::FILE_PATH
. 'jpeg-segment-loop1.jpg' );
119 public function testInfiniteRead2() {
120 // test file truncated after a segment's marker and size, which
121 // would cause a seek past end of file. Seek past end of file
122 // doesn't actually fail, but prevents further reading and was
123 // devolving into the previous case (testInfiniteRead).
124 $this->expectException( InvalidJpegException
::class );
125 $res = JpegMetadataExtractor
::segmentSplitter( self
::FILE_PATH
. 'jpeg-segment-loop2.jpg' );