2011-08-18 05:45:04 +00:00
|
|
|
<?php
|
2011-10-11 18:30:50 +00:00
|
|
|
/**
|
2011-08-21 18:05:34 +00:00
|
|
|
* @todo Could use a test of extended XMP segments. Hard to find programs that
|
2019-09-11 22:12:22 +00:00
|
|
|
* create example files, and creating my own in vim probably wouldn't
|
2011-08-21 18:05:34 +00:00
|
|
|
* serve as a very good "test". (Adobe photoshop probably creates such files
|
|
|
|
|
* but it costs money). The implementation of it currently in MediaWiki is based
|
|
|
|
|
* solely on reading the standard, without any real world test files.
|
2013-10-24 18:04:38 +00:00
|
|
|
*
|
2014-07-15 17:31:07 +00:00
|
|
|
* @group Media
|
2013-10-24 18:04:38 +00:00
|
|
|
* @covers JpegMetadataExtractor
|
2011-08-21 18:05:34 +00:00
|
|
|
*/
|
phpunit: Repair GLOBALS reset in MediaWikiUnitTestCase
This code didn't work because the $GLOBALS array is exposed by reference.
Once this reference was broken by unset(), the rest just manipulated a
local array that happens to be called "GLOBALS". It must not be unset or
re-assigned. It can only be changed in-place.
Before this, the execution of a MediaWikiUnitTestCase test stored a
copy of GLOBALS in unitGlobals, then lost the GLOBALS pointer and
created a new variable called "GLOBALS". As such, the tearDown() function
didn't do what it meant to do, either – which then results in odd
failures like T230023
Rewrite it as follows:
* In setup, store the current GLOBALS keys and values, then reduce
GLOBALS to only the whitelisted keys and values.
* In teardown, restore the original state.
* As optimisation, do this from setUpBeforeClass as well, so that
there are relatively few globals to reset between tests.
(Thanks @Simetrical!)
The following tests were previously passing by accident under
MediaWikiUnitTestCase but actually did depend on global config.
* MainSlotRoleHandlerTest (…, ContentHandler, $wgContentHandlers)
* SlotRecordTest (…, ContentHandler, $wgContentHandlers)
* WikiReferenceTest (wfParseUrl, $wgUrlProtocols)
* DifferenceEngineSlotDiffRendererTest (DifferenceEngine, wfDebug, …)
* SlotDiffRendererTest (…, ContentHandler, $wgContentHandlers)
* FileBackendDBRepoWrapperTest (wfWikiID, "Backend domain ID not provided")
* JpegMetadataExtractorTest (…, wfDebug, …, LoggerFactory, …)
* ParserFactoryTest (…, wfDebug, …, LoggerFactory, InvalidArgumentException)
* MediaWikiPageNameNormalizerTest (…, wfDebug, …, LoggerFactory, …)
* SiteExporterTest (SiteImporter, wfLogWarning, …)
* SiteImporterTest (Site::newForType, $wgSiteTypes)
* ZipDirectoryReaderTest (…, wfDebug, …, LoggerFactory, …)
Bug: T230023
Change-Id: Ic22075bb5e81b7c2c4c1b8647547aa55306a10a7
2019-08-07 13:40:55 +00:00
|
|
|
class JpegMetadataExtractorTest extends MediaWikiIntegrationTestCase {
|
2011-08-18 05:45:04 +00:00
|
|
|
|
2013-10-23 22:12:39 +00:00
|
|
|
protected $filePath;
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
phpunit: Repair GLOBALS reset in MediaWikiUnitTestCase
This code didn't work because the $GLOBALS array is exposed by reference.
Once this reference was broken by unset(), the rest just manipulated a
local array that happens to be called "GLOBALS". It must not be unset or
re-assigned. It can only be changed in-place.
Before this, the execution of a MediaWikiUnitTestCase test stored a
copy of GLOBALS in unitGlobals, then lost the GLOBALS pointer and
created a new variable called "GLOBALS". As such, the tearDown() function
didn't do what it meant to do, either – which then results in odd
failures like T230023
Rewrite it as follows:
* In setup, store the current GLOBALS keys and values, then reduce
GLOBALS to only the whitelisted keys and values.
* In teardown, restore the original state.
* As optimisation, do this from setUpBeforeClass as well, so that
there are relatively few globals to reset between tests.
(Thanks @Simetrical!)
The following tests were previously passing by accident under
MediaWikiUnitTestCase but actually did depend on global config.
* MainSlotRoleHandlerTest (…, ContentHandler, $wgContentHandlers)
* SlotRecordTest (…, ContentHandler, $wgContentHandlers)
* WikiReferenceTest (wfParseUrl, $wgUrlProtocols)
* DifferenceEngineSlotDiffRendererTest (DifferenceEngine, wfDebug, …)
* SlotDiffRendererTest (…, ContentHandler, $wgContentHandlers)
* FileBackendDBRepoWrapperTest (wfWikiID, "Backend domain ID not provided")
* JpegMetadataExtractorTest (…, wfDebug, …, LoggerFactory, …)
* ParserFactoryTest (…, wfDebug, …, LoggerFactory, InvalidArgumentException)
* MediaWikiPageNameNormalizerTest (…, wfDebug, …, LoggerFactory, …)
* SiteExporterTest (SiteImporter, wfLogWarning, …)
* SiteImporterTest (Site::newForType, $wgSiteTypes)
* ZipDirectoryReaderTest (…, wfDebug, …, LoggerFactory, …)
Bug: T230023
Change-Id: Ic22075bb5e81b7c2c4c1b8647547aa55306a10a7
2019-08-07 13:40:55 +00:00
|
|
|
$this->filePath = __DIR__ . '/../../data/media/';
|
2011-08-18 05:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-24 02:47:22 +00:00
|
|
|
/**
|
|
|
|
|
* We also use this test to test padding bytes don't
|
|
|
|
|
* screw stuff up
|
|
|
|
|
*
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param string $file Filename
|
2011-10-24 02:47:22 +00:00
|
|
|
*
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider provideUtf8Comment
|
2011-10-24 02:47:22 +00:00
|
|
|
*/
|
|
|
|
|
public function testUtf8Comment( $file ) {
|
|
|
|
|
$res = JpegMetadataExtractor::segmentSplitter( $this->filePath . $file );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [ 'UTF-8 JPEG Comment — ¼' ], $res['COM'] );
|
2011-08-18 05:45:04 +00:00
|
|
|
}
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideUtf8Comment() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 'jpeg-comment-utf.jpg' ],
|
|
|
|
|
[ 'jpeg-padding-even.jpg' ],
|
|
|
|
|
[ 'jpeg-padding-odd.jpg' ],
|
|
|
|
|
];
|
2011-10-24 02:47:22 +00:00
|
|
|
}
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2011-08-18 05:45:04 +00:00
|
|
|
/** The file is iso-8859-1, but it should get auto converted */
|
|
|
|
|
public function testIso88591Comment() {
|
|
|
|
|
$res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-iso8859-1.jpg' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [ 'ISO-8859-1 JPEG Comment - ¼' ], $res['COM'] );
|
2011-08-18 05:45:04 +00:00
|
|
|
}
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2011-08-18 05:45:04 +00:00
|
|
|
/** Comment values that are non-textual (random binary junk) should not be shown.
|
|
|
|
|
* The example test file has a comment with a 0x5 byte in it which is a control character
|
|
|
|
|
* and considered binary junk for our purposes.
|
|
|
|
|
*/
|
|
|
|
|
public function testBinaryCommentStripped() {
|
|
|
|
|
$res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-binary.jpg' );
|
|
|
|
|
$this->assertEmpty( $res['COM'] );
|
|
|
|
|
}
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2011-08-18 05:45:04 +00:00
|
|
|
/* Very rarely a file can have multiple comments.
|
|
|
|
|
* Order of comments is based on order inside the file.
|
|
|
|
|
*/
|
|
|
|
|
public function testMultipleComment() {
|
|
|
|
|
$res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-multiple.jpg' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [ 'foo', 'bar' ], $res['COM'] );
|
2011-08-18 05:45:04 +00:00
|
|
|
}
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2011-08-21 18:05:34 +00:00
|
|
|
public function testXMPExtraction() {
|
|
|
|
|
$res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
|
|
|
|
|
$expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' );
|
|
|
|
|
$this->assertEquals( $expected, $res['XMP'] );
|
|
|
|
|
}
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2011-08-21 18:05:34 +00:00
|
|
|
public function testPSIRExtraction() {
|
|
|
|
|
$res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
|
2014-04-24 17:51:33 +00:00
|
|
|
$expected = '50686f746f73686f7020332e30003842494d04040000000'
|
|
|
|
|
. '000181c02190004746573741c02190003666f6f1c020000020004';
|
2012-01-05 23:25:39 +00:00
|
|
|
$this->assertEquals( $expected, bin2hex( $res['PSIR'][0] ) );
|
2011-08-21 18:05:34 +00:00
|
|
|
}
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2019-09-11 22:12:22 +00:00
|
|
|
public function testXMPExtractionNullChar() {
|
|
|
|
|
$res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-nullchar.jpg' );
|
|
|
|
|
$expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' );
|
|
|
|
|
$this->assertEquals( $expected, $res['XMP'] );
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-21 18:05:34 +00:00
|
|
|
public function testXMPExtractionAltAppId() {
|
|
|
|
|
$res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-alt.jpg' );
|
|
|
|
|
$expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' );
|
|
|
|
|
$this->assertEquals( $expected, $res['XMP'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIPTCHashComparisionNoHash() {
|
|
|
|
|
$segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' );
|
2012-01-05 23:25:39 +00:00
|
|
|
$res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
|
2011-08-21 18:05:34 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( 'iptc-no-hash', $res );
|
|
|
|
|
}
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2011-08-21 18:05:34 +00:00
|
|
|
public function testIPTCHashComparisionBadHash() {
|
|
|
|
|
$segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-bad-hash.jpg' );
|
2012-01-05 23:25:39 +00:00
|
|
|
$res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
|
2011-08-21 18:05:34 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( 'iptc-bad-hash', $res );
|
|
|
|
|
}
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2011-08-21 18:05:34 +00:00
|
|
|
public function testIPTCHashComparisionGoodHash() {
|
|
|
|
|
$segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-good-hash.jpg' );
|
2012-01-05 23:25:39 +00:00
|
|
|
$res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
|
2011-08-21 18:05:34 +00:00
|
|
|
|
|
|
|
|
$this->assertEquals( 'iptc-good-hash', $res );
|
|
|
|
|
}
|
2013-02-15 10:17:52 +00:00
|
|
|
|
2011-08-21 18:05:34 +00:00
|
|
|
public function testExifByteOrder() {
|
|
|
|
|
$res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'exif-user-comment.jpg' );
|
|
|
|
|
$expected = 'BE';
|
|
|
|
|
$this->assertEquals( $expected, $res['byteOrder'] );
|
|
|
|
|
}
|
2018-02-21 22:45:35 +00:00
|
|
|
|
|
|
|
|
public function testInfiniteRead() {
|
2018-02-23 09:42:10 +00:00
|
|
|
// test file truncated right after a segment, which previously
|
|
|
|
|
// caused an infinite loop looking for the next segment byte.
|
2018-02-21 22:45:35 +00:00
|
|
|
// Should get past infinite loop and throw in wfUnpack()
|
2019-10-06 09:52:39 +00:00
|
|
|
$this->expectException( MWException::class );
|
2018-02-23 09:42:10 +00:00
|
|
|
$res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-segment-loop1.jpg' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInfiniteRead2() {
|
|
|
|
|
// test file truncated after a segment's marker and size, which
|
|
|
|
|
// would cause a seek past end of file. Seek past end of file
|
|
|
|
|
// doesn't actually fail, but prevents further reading and was
|
|
|
|
|
// devolving into the previous case (testInfiniteRead).
|
2019-10-06 09:52:39 +00:00
|
|
|
$this->expectException( MWException::class );
|
2018-02-23 09:42:10 +00:00
|
|
|
$res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-segment-loop2.jpg' );
|
2018-02-21 22:45:35 +00:00
|
|
|
}
|
2011-08-18 05:45:04 +00:00
|
|
|
}
|