Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ZipTestCase.php « PhpZip « tests « zip « nelexa « vendor - github.com/CarnetApp/CarnetNextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6de8537a10cd48df5f9b783105c704dd19f92cbe (plain)
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
128
129
130
131
132
133
134
135
136
137
<?php

namespace PhpZip;

use PhpZip\Model\EndOfCentralDirectory;
use PhpZip\Util\FilesUtil;

/**
 * PHPUnit test case and helper methods.
 */
class ZipTestCase extends \PHPUnit_Framework_TestCase
{

    /**
     * @var string
     */
    protected $outputFilename;
    /**
     * @var string
     */
    protected $outputDirname;

    /**
     * Before test
     */
    protected function setUp()
    {
        parent::setUp();

        $id = uniqid('phpzip');
        $tempDir = sys_get_temp_dir() . '/phpunit-phpzip';
        if (!is_dir($tempDir)) {
            if (!mkdir($tempDir, 0755, true)) {
                throw new \RuntimeException("Dir " . $tempDir . " can't created");
            }
        }
        $this->outputFilename = $tempDir . '/' . $id . '.zip';
        $this->outputDirname = $tempDir . '/' . $id;
    }

    /**
     * After test
     */
    protected function tearDown()
    {
        parent::tearDown();

        if ($this->outputFilename !== null && file_exists($this->outputFilename)) {
            unlink($this->outputFilename);
        }
        if ($this->outputDirname !== null && is_dir($this->outputDirname)) {
            FilesUtil::removeDir($this->outputDirname);
        }
    }

    /**
     * Assert correct zip archive.
     *
     * @param string $filename
     * @param string|null $password
     */
    public static function assertCorrectZipArchive($filename, $password = null)
    {
        if (DIRECTORY_SEPARATOR !== '\\' && `which unzip`) {
            $command = "unzip";
            if ($password !== null) {
                $command .= " -P " . escapeshellarg($password);
            }
            $command .= " -t " . escapeshellarg($filename);
            exec($command, $output, $returnCode);

            $output = implode(PHP_EOL, $output);

            if ($password !== null && $returnCode === 81) {
                if (`which 7z`) {
                    // WinZip 99-character limit
                    // @see https://sourceforge.net/p/p7zip/discussion/383044/thread/c859a2f0/
                    $password = substr($password, 0, 99);

                    $command = "7z t -p" . escapeshellarg($password) . " " . escapeshellarg($filename);
                    exec($command, $output, $returnCode);

                    $output = implode(PHP_EOL, $output);

                    self::assertEquals($returnCode, 0);
                    self::assertNotContains(' Errors', $output);
                    self::assertContains(' Ok', $output);
                } else {
                    fwrite(STDERR, 'Program unzip cannot support this function.' . PHP_EOL);
                    fwrite(STDERR, 'Please install 7z. For Ubuntu-like: sudo apt-get install p7zip-full' . PHP_EOL);
                }
            } else {
                self::assertEquals($returnCode, 0, $output);
                self::assertNotContains('incorrect password', $output);
                self::assertContains(' OK', $output);
                self::assertContains('No errors', $output);
            }
        }
    }

    /**
     * Assert correct empty zip archive.
     *
     * @param $filename
     */
    public static function assertCorrectEmptyZip($filename)
    {
        if (DIRECTORY_SEPARATOR !== '\\' && `which zipinfo`) {
            exec("zipinfo " . escapeshellarg($filename), $output, $returnCode);

            $output = implode(PHP_EOL, $output);

            self::assertContains('Empty zipfile', $output);
        }
        $actualEmptyZipData = pack('VVVVVv', EndOfCentralDirectory::END_OF_CENTRAL_DIRECTORY_RECORD_SIG, 0, 0, 0, 0, 0);
        self::assertEquals(file_get_contents($filename), $actualEmptyZipData);
    }

    /**
     * @param string $filename
     * @param bool $showErrors
     * @return bool|null If null - can not install zipalign
     */
    public static function doZipAlignVerify($filename, $showErrors = false)
    {
        if (DIRECTORY_SEPARATOR !== '\\' && `which zipalign`) {
            exec("zipalign -c -v 4 " . escapeshellarg($filename), $output, $returnCode);
            if ($showErrors && $returnCode !== 0) {
                fwrite(STDERR, implode(PHP_EOL, $output));
            }
            return $returnCode === 0;
        } else {
            fwrite(STDERR, 'Can not find program "zipalign" for test' . PHP_EOL);
            return null;
        }
    }
}