1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
<?php
/**
* @todo Could use a test of extended XMP segments. Hard to find programs that
* create example files, and creating my own in vim probably wouldn't
* 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.
*
* @group Media
* @covers \JpegMetadataExtractor
*/
class JpegMetadataExtractorTest extends MediaWikiIntegrationTestCase {
private const FILE_PATH = __DIR__ . '/../../data/media/';
/**
* We also use this test to test padding bytes don't
* screw stuff up
*
* @param string $file Filename
*
* @dataProvider provideUtf8Comment
*/
public function testUtf8Comment( $file ) {
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . $file );
$this->assertEquals( [ 'UTF-8 JPEG Comment — ¼' ], $res['COM'] );
}
public static function provideUtf8Comment() {
return [
[ 'jpeg-comment-utf.jpg' ],
[ 'jpeg-padding-even.jpg' ],
[ 'jpeg-padding-odd.jpg' ],
];
}
/** The file is iso-8859-1, but it should get auto converted */
public function testIso88591Comment() {
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-comment-iso8859-1.jpg' );
$this->assertEquals( [ 'ISO-8859-1 JPEG Comment - ¼' ], $res['COM'] );
}
/** 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( self::FILE_PATH . 'jpeg-comment-binary.jpg' );
$this->assertSame( [], $res['COM'] );
}
/* Very rarely a file can have multiple comments.
* Order of comments is based on order inside the file.
*/
public function testMultipleComment() {
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-comment-multiple.jpg' );
$this->assertEquals( [ 'foo', 'bar' ], $res['COM'] );
}
public function testXMPExtraction() {
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-xmp-psir.jpg' );
$expected = file_get_contents( self::FILE_PATH . 'jpeg-xmp-psir.xmp' );
$this->assertEquals( $expected, $res['XMP'] );
}
public function testPSIRExtraction() {
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-xmp-psir.jpg' );
$expected = '50686f746f73686f7020332e30003842494d04040000000'
. '000181c02190004746573741c02190003666f6f1c020000020004';
$this->assertEquals( $expected, bin2hex( $res['PSIR'][0] ) );
}
public function testXMPExtractionNullChar() {
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-xmp-nullchar.jpg' );
$expected = file_get_contents( self::FILE_PATH . 'jpeg-xmp-psir.xmp' );
$this->assertEquals( $expected, $res['XMP'] );
}
public function testXMPExtractionAltAppId() {
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-xmp-alt.jpg' );
$expected = file_get_contents( self::FILE_PATH . 'jpeg-xmp-psir.xmp' );
$this->assertEquals( $expected, $res['XMP'] );
}
public function testIPTCHashComparisionNoHash() {
$segments = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-xmp-psir.jpg' );
$res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
$this->assertEquals( 'iptc-no-hash', $res );
}
public function testIPTCHashComparisionBadHash() {
$segments = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-iptc-bad-hash.jpg' );
$res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
$this->assertEquals( 'iptc-bad-hash', $res );
}
public function testIPTCHashComparisionGoodHash() {
$segments = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-iptc-good-hash.jpg' );
$res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] );
$this->assertEquals( 'iptc-good-hash', $res );
}
public function testExifByteOrder() {
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'exif-user-comment.jpg' );
$expected = 'BE';
$this->assertEquals( $expected, $res['byteOrder'] );
}
public function testInfiniteRead() {
// test file truncated right after a segment, which previously
// caused an infinite loop looking for the next segment byte.
// Should get past infinite loop and throw in StringUtils::unpack()
$this->expectException( InvalidJpegException::class );
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . '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).
$this->expectException( InvalidJpegException::class );
$res = JpegMetadataExtractor::segmentSplitter( self::FILE_PATH . 'jpeg-segment-loop2.jpg' );
}
}
|