wiki.techinc.nl/tests/phpunit/includes/media/PNGMetadataExtractorTest.php
Tim Starling 2e507003ca Ignore invalid chunks in PNG files, instead of aborting metadata extraction
* Factor out file read errors and unexpected EOF errors.
* For errors relating to chunk content, instead of throwing an
  exception which is silently discarded, just log an error and continue
  to the next chunk. This allows the dimensions to be extracted even if
  other metadata is mangled.
* As an additional sanity check, verify the CRC of each chunk.

Bug: T286273
Change-Id: I11d0186496324e0bb1bb0a143f438e0368a8e902
2021-07-13 11:11:03 +10:00

147 lines
4.4 KiB
PHP

<?php
/**
* @group Media
* @covers PNGMetadataExtractor
*/
class PNGMetadataExtractorTest extends MediaWikiIntegrationTestCase {
protected function setUp() : void {
parent::setUp();
$this->filePath = __DIR__ . '/../../data/media/';
}
/**
* Tests zTXt tag (compressed textual metadata)
*/
public function testPngNativetZtxt() {
$this->checkPHPExtension( 'zlib' );
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'Png-native-test.png' );
$expected = "foo bar baz foo foo foo foof foo foo foo foo";
$this->assertArrayHasKey( 'text', $meta );
$meta = $meta['text'];
$this->assertArrayHasKey( 'Make', $meta );
$this->assertArrayHasKey( 'x-default', $meta['Make'] );
$this->assertEquals( $expected, $meta['Make']['x-default'] );
}
/**
* Test tEXt tag (Uncompressed textual metadata)
*/
public function testPngNativeText() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'Png-native-test.png' );
$expected = "Some long image desc";
$this->assertArrayHasKey( 'text', $meta );
$meta = $meta['text'];
$this->assertArrayHasKey( 'ImageDescription', $meta );
$this->assertArrayHasKey( 'x-default', $meta['ImageDescription'] );
$this->assertArrayHasKey( '_type', $meta['ImageDescription'] );
$this->assertEquals( $expected, $meta['ImageDescription']['x-default'] );
}
/**
* tEXt tags must be encoded iso-8859-1 (vs iTXt which are utf-8)
* Make sure non-ascii characters get converted properly
*/
public function testPngNativeTextNonAscii() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'Png-native-test.png' );
// Note the Copyright symbol here is a utf-8 one
// (aka \xC2\xA9) where in the file its iso-8859-1
// encoded as just \xA9.
$expected = "© 2010 Bawolff";
$this->assertArrayHasKey( 'text', $meta );
$meta = $meta['text'];
$this->assertArrayHasKey( 'Copyright', $meta );
$this->assertArrayHasKey( 'x-default', $meta['Copyright'] );
$this->assertEquals( $expected, $meta['Copyright']['x-default'] );
}
/**
* Given a normal static PNG, check the animation metadata returned.
*/
public function testStaticPngAnimationMetadata() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'Png-native-test.png' );
$this->assertSame( 0, $meta['frameCount'] );
$this->assertSame( 1, $meta['loopCount'] );
$this->assertSame( 0.0, $meta['duration'] );
}
/**
* Given an animated APNG image file
* check it gets animated metadata right.
*/
public function testApngAnimationMetadata() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'Animated_PNG_example_bouncing_beach_ball.png' );
$this->assertEquals( 20, $meta['frameCount'] );
// Note loop count of 0 = infinity
$this->assertSame( 0, $meta['loopCount'] );
$this->assertEqualsWithDelta( 1.5, $meta['duration'], 0.00001, '' );
}
public function testPngBitDepth8() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'Png-native-test.png' );
$this->assertEquals( 8, $meta['bitDepth'] );
}
public function testPngBitDepth1() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'1bit-png.png' );
$this->assertSame( 1, $meta['bitDepth'] );
}
public function testPngIndexColour() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'Png-native-test.png' );
$this->assertEquals( 'index-coloured', $meta['colorType'] );
}
public function testPngRgbColour() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'rgb-png.png' );
$this->assertEquals( 'truecolour-alpha', $meta['colorType'] );
}
public function testPngRgbNoAlphaColour() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'rgb-na-png.png' );
$this->assertEquals( 'truecolour', $meta['colorType'] );
}
public function testPngGreyscaleColour() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'greyscale-png.png' );
$this->assertEquals( 'greyscale-alpha', $meta['colorType'] );
}
public function testPngGreyscaleNoAlphaColour() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'greyscale-na-png.png' );
$this->assertEquals( 'greyscale', $meta['colorType'] );
}
/**
* T286273 -- tEXt chunk replaced by null bytes
*/
public function testPngInvalidChunk() {
$meta = PNGMetadataExtractor::getMetadata( $this->filePath .
'tEXt-invalid-masked.png' );
$this->assertEquals( 10, $meta['width'] );
$this->assertEquals( 10, $meta['height'] );
}
}