2011-02-25 04:51:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
2013-10-21 08:46:11 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ZipDirectoryReader
|
|
|
|
|
*/
|
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 ZipDirectoryReaderTest extends MediaWikiIntegrationTestCase {
|
2017-12-29 23:22:37 +00:00
|
|
|
|
2013-10-21 21:09:13 +00:00
|
|
|
protected $zipDir;
|
|
|
|
|
protected $entries;
|
2011-02-25 04:51:17 +00:00
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2012-10-23 17:02:36 +00:00
|
|
|
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->zipDir = __DIR__ . '/../../data/zip';
|
2011-02-25 04:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function zipCallback( $entry ) {
|
2011-02-25 04:51:17 +00:00
|
|
|
$this->entries[] = $entry;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function readZipAssertError( $file, $error, $assertMessage ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->entries = [];
|
|
|
|
|
$status = ZipDirectoryReader::read( "{$this->zipDir}/$file", [ $this, 'zipCallback' ] );
|
2011-02-25 04:51:17 +00:00
|
|
|
$this->assertTrue( $status->hasMessage( $error ), $assertMessage );
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-09 18:24:07 +00:00
|
|
|
public function readZipAssertSuccess( $file, $assertMessage ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->entries = [];
|
|
|
|
|
$status = ZipDirectoryReader::read( "{$this->zipDir}/$file", [ $this, 'zipCallback' ] );
|
2011-02-25 04:51:17 +00:00
|
|
|
$this->assertTrue( $status->isOK(), $assertMessage );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testEmpty() {
|
2011-02-25 04:51:17 +00:00
|
|
|
$this->readZipAssertSuccess( 'empty.zip', 'Empty zip' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testMultiDisk0() {
|
2013-02-14 11:56:23 +00:00
|
|
|
$this->readZipAssertError( 'split.zip', 'zip-unsupported',
|
2011-02-25 04:51:17 +00:00
|
|
|
'Split zip error' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testNoSignature() {
|
2013-02-14 11:56:23 +00:00
|
|
|
$this->readZipAssertError( 'nosig.zip', 'zip-wrong-format',
|
2011-02-25 04:51:17 +00:00
|
|
|
'No signature should give "wrong format" error' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testSimple() {
|
2011-02-25 04:51:17 +00:00
|
|
|
$this->readZipAssertSuccess( 'class.zip', 'Simple ZIP' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( $this->entries, [ [
|
2011-02-25 04:51:17 +00:00
|
|
|
'name' => 'Class.class',
|
|
|
|
|
'mtime' => '20010115000000',
|
|
|
|
|
'size' => 1,
|
2016-02-17 09:09:32 +00:00
|
|
|
] ] );
|
2011-02-25 04:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testBadCentralEntrySignature() {
|
2011-02-25 04:51:17 +00:00
|
|
|
$this->readZipAssertError( 'wrong-central-entry-sig.zip', 'zip-bad',
|
|
|
|
|
'Bad central entry error' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testTrailingBytes() {
|
2019-01-15 03:15:18 +00:00
|
|
|
// Due to T40432 this is now zip-wrong-format instead of zip-bad
|
|
|
|
|
$this->readZipAssertError( 'trail.zip', 'zip-wrong-format',
|
2011-02-25 04:51:17 +00:00
|
|
|
'Trailing bytes error' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testWrongCDStart() {
|
2013-02-14 11:56:23 +00:00
|
|
|
$this->readZipAssertError( 'wrong-cd-start-disk.zip', 'zip-unsupported',
|
2011-02-25 04:51:17 +00:00
|
|
|
'Wrong CD start disk error' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testCentralDirectoryGap() {
|
2011-02-25 04:51:17 +00:00
|
|
|
$this->readZipAssertError( 'cd-gap.zip', 'zip-bad',
|
|
|
|
|
'CD gap error' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testCentralDirectoryTruncated() {
|
2011-02-25 04:51:17 +00:00
|
|
|
$this->readZipAssertError( 'cd-truncated.zip', 'zip-bad',
|
|
|
|
|
'CD truncated error (should hit unpack() overrun)' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testLooksLikeZip64() {
|
2011-02-25 04:51:17 +00:00
|
|
|
$this->readZipAssertError( 'looks-like-zip64.zip', 'zip-unsupported',
|
|
|
|
|
'A file which looks like ZIP64 but isn\'t, should give error' );
|
|
|
|
|
}
|
|
|
|
|
}
|