Merge "Chinese Conversion Table Update 2017-6"
[lhc/web/wiklou.git] / tests / phpunit / includes / utils / ZipDirectoryReaderTest.php
1 <?php
2
3 /**
4 * @covers ZipDirectoryReader
5 * NOTE: this test is more like an integration test than a unit test
6 */
7 class ZipDirectoryReaderTest extends PHPUnit_Framework_TestCase {
8
9 use MediaWikiCoversValidator;
10
11 protected $zipDir;
12 protected $entries;
13
14 protected function setUp() {
15 parent::setUp();
16 $this->zipDir = __DIR__ . '/../../data/zip';
17 }
18
19 function zipCallback( $entry ) {
20 $this->entries[] = $entry;
21 }
22
23 function readZipAssertError( $file, $error, $assertMessage ) {
24 $this->entries = [];
25 $status = ZipDirectoryReader::read( "{$this->zipDir}/$file", [ $this, 'zipCallback' ] );
26 $this->assertTrue( $status->hasMessage( $error ), $assertMessage );
27 }
28
29 function readZipAssertSuccess( $file, $assertMessage ) {
30 $this->entries = [];
31 $status = ZipDirectoryReader::read( "{$this->zipDir}/$file", [ $this, 'zipCallback' ] );
32 $this->assertTrue( $status->isOK(), $assertMessage );
33 }
34
35 public function testEmpty() {
36 $this->readZipAssertSuccess( 'empty.zip', 'Empty zip' );
37 }
38
39 public function testMultiDisk0() {
40 $this->readZipAssertError( 'split.zip', 'zip-unsupported',
41 'Split zip error' );
42 }
43
44 public function testNoSignature() {
45 $this->readZipAssertError( 'nosig.zip', 'zip-wrong-format',
46 'No signature should give "wrong format" error' );
47 }
48
49 public function testSimple() {
50 $this->readZipAssertSuccess( 'class.zip', 'Simple ZIP' );
51 $this->assertEquals( $this->entries, [ [
52 'name' => 'Class.class',
53 'mtime' => '20010115000000',
54 'size' => 1,
55 ] ] );
56 }
57
58 public function testBadCentralEntrySignature() {
59 $this->readZipAssertError( 'wrong-central-entry-sig.zip', 'zip-bad',
60 'Bad central entry error' );
61 }
62
63 public function testTrailingBytes() {
64 $this->readZipAssertError( 'trail.zip', 'zip-bad',
65 'Trailing bytes error' );
66 }
67
68 public function testWrongCDStart() {
69 $this->readZipAssertError( 'wrong-cd-start-disk.zip', 'zip-unsupported',
70 'Wrong CD start disk error' );
71 }
72
73 public function testCentralDirectoryGap() {
74 $this->readZipAssertError( 'cd-gap.zip', 'zip-bad',
75 'CD gap error' );
76 }
77
78 public function testCentralDirectoryTruncated() {
79 $this->readZipAssertError( 'cd-truncated.zip', 'zip-bad',
80 'CD truncated error (should hit unpack() overrun)' );
81 }
82
83 public function testLooksLikeZip64() {
84 $this->readZipAssertError( 'looks-like-zip64.zip', 'zip-unsupported',
85 'A file which looks like ZIP64 but isn\'t, should give error' );
86 }
87 }