summaryrefslogtreecommitdiff
path: root/_test/tests/inc/tar.test.php
blob: 15453b16db94295bc31b5ff2ba4231b17b478b35 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php

class Tar_TestCase extends DokuWikiTest {
    /**
     * file extensions that several tests use
     */
    protected $extensions = array('tar');

    public function setUp() {
        parent::setUp();
        if (extension_loaded('zlib')) {
            $this->extensions[] = 'tgz';
        }
        if (extension_loaded('bz2')) {
            $this->extensions[] = 'tbz';
        }
    }

    /*
     * dependency for tests needing zlib extension to pass
     */
    public function test_ext_zlib() {
        if (!extension_loaded('zlib')) {
            $this->markTestSkipped('skipping all zlib tests.  Need zlib extension');
        }
    }

    /*
     * dependency for tests needing zlib extension to pass
     */
    public function test_ext_bz2() {
        if (!extension_loaded('bz2')) {
            $this->markTestSkipped('skipping all bzip2 tests.  Need bz2 extension');
        }
    }

    /**
     * simple test that checks that the given filenames and contents can be grepped from
     * the uncompressed tar stream
     *
     * No check for format correctness
     */
    public function test_createdynamic() {
        $tar = new Tar();

        $dir  = dirname(__FILE__).'/tar';
        $tdir = ltrim($dir,'/');

        $tar->create();
        $tar->AddFile("$dir/testdata1.txt");
        $tar->AddFile("$dir/foobar/testdata2.txt", 'noway/testdata2.txt');
        $tar->addData('another/testdata3.txt', 'testcontent3');

        $data = $tar->getArchive();

        $this->assertTrue(strpos($data, 'testcontent1') !== false, 'Content in TAR');
        $this->assertTrue(strpos($data, 'testcontent2') !== false, 'Content in TAR');
        $this->assertTrue(strpos($data, 'testcontent3') !== false, 'Content in TAR');

        // fullpath might be too long to be stored as full path FS#2802
        $this->assertTrue(strpos($data, "$tdir") !== false, 'Path in TAR');
        $this->assertTrue(strpos($data, "testdata1.txt") !== false, 'File in TAR');

        $this->assertTrue(strpos($data, 'noway/testdata2.txt') !== false, 'Path in TAR');
        $this->assertTrue(strpos($data, 'another/testdata3.txt') !== false, 'Path in TAR');

        // fullpath might be too long to be stored as full path FS#2802
        $this->assertTrue(strpos($data, "$tdir/foobar") === false, 'Path not in TAR');
        $this->assertTrue(strpos($data, "foobar.txt") === false, 'File not in TAR');

        $this->assertTrue(strpos($data, "foobar") === false, 'Path not in TAR');
    }

    /**
     * simple test that checks that the given filenames and contents can be grepped from the
     * uncompressed tar file
     *
     * No check for format correctness
     */
    public function test_createfile() {
        $tar = new Tar();

        $dir = dirname(__FILE__).'/tar';
        $tdir = ltrim($dir,'/');
        $tmp = tempnam(sys_get_temp_dir(), 'dwtartest');

        $tar->create($tmp, Tar::COMPRESS_NONE);
        $tar->AddFile("$dir/testdata1.txt");
        $tar->AddFile("$dir/foobar/testdata2.txt", 'noway/testdata2.txt');
        $tar->addData('another/testdata3.txt', 'testcontent3');
        $tar->close();

        $this->assertTrue(filesize($tmp) > 30); //arbitrary non-zero number
        $data = file_get_contents($tmp);

        $this->assertTrue(strpos($data, 'testcontent1') !== false, 'Content in TAR');
        $this->assertTrue(strpos($data, 'testcontent2') !== false, 'Content in TAR');
        $this->assertTrue(strpos($data, 'testcontent3') !== false, 'Content in TAR');

        // fullpath might be too long to be stored as full path FS#2802
        $this->assertTrue(strpos($data, "$tdir") !== false, "Path in TAR '$tdir'");
        $this->assertTrue(strpos($data, "testdata1.txt") !== false, 'File in TAR');

        $this->assertTrue(strpos($data, 'noway/testdata2.txt') !== false, 'Path in TAR');
        $this->assertTrue(strpos($data, 'another/testdata3.txt') !== false, 'Path in TAR');

        // fullpath might be too long to be stored as full path FS#2802
        $this->assertTrue(strpos($data, "$tdir/foobar") === false, 'Path not in TAR');
        $this->assertTrue(strpos($data, "foobar.txt") === false, 'File not in TAR');

        $this->assertTrue(strpos($data, "foobar") === false, 'Path not in TAR');

        @unlink($tmp);
    }

    /**
     * List the contents of the prebuilt TAR files
     */
    public function test_tarcontent() {
        $dir = dirname(__FILE__).'/tar';

        foreach($this->extensions as $ext) {
            $tar  = new Tar();
            $file = "$dir/test.$ext";

            $tar->open($file);
            $content = $tar->contents();

            $this->assertCount(4, $content, "Contents of $file");
            $this->assertEquals('tar/testdata1.txt', $content[1]['filename'], "Contents of $file");
            $this->assertEquals(13, $content[1]['size'], "Contents of $file");

            $this->assertEquals('tar/foobar/testdata2.txt', $content[3]['filename'], "Contents of $file");
            $this->assertEquals(13, $content[1]['size'], "Contents of $file");
        }
    }

    /**
     * Extract the prebuilt tar files
     */
    public function test_tarextract() {
        $dir = dirname(__FILE__).'/tar';
        $out = sys_get_temp_dir().'/dwtartest'.md5(time());

        foreach($this->extensions as $ext) {
            $tar  = new Tar();
            $file = "$dir/test.$ext";

            $tar->open($file);
            $tar->extract($out);

            clearstatcache();

            $this->assertFileExists($out.'/tar/testdata1.txt', "Extracted $file");
            $this->assertEquals(13, filesize($out.'/tar/testdata1.txt'), "Extracted $file");

            $this->assertFileExists($out.'/tar/foobar/testdata2.txt', "Extracted $file");
            $this->assertEquals(13, filesize($out.'/tar/foobar/testdata2.txt'), "Extracted $file");

            TestUtils::rdelete($out);
        }
    }

    /**
     * Extract the prebuilt tar files with component stripping
     */
    public function test_compstripextract() {
        $dir = dirname(__FILE__).'/tar';
        $out = sys_get_temp_dir().'/dwtartest'.md5(time());

        foreach($this->extensions as $ext) {
            $tar  = new Tar();
            $file = "$dir/test.$ext";

            $tar->open($file);
            $tar->extract($out, 1);

            clearstatcache();

            $this->assertFileExists($out.'/testdata1.txt', "Extracted $file");
            $this->assertEquals(13, filesize($out.'/testdata1.txt'), "Extracted $file");

            $this->assertFileExists($out.'/foobar/testdata2.txt', "Extracted $file");
            $this->assertEquals(13, filesize($out.'/foobar/testdata2.txt'), "Extracted $file");

            TestUtils::rdelete($out);
        }
    }

    /**
     * Extract the prebuilt tar files with prefix stripping
     */
    public function test_prefixstripextract() {
        $dir = dirname(__FILE__).'/tar';
        $out = sys_get_temp_dir().'/dwtartest'.md5(time());

        foreach($this->extensions as $ext) {
            $tar  = new Tar();
            $file = "$dir/test.$ext";

            $tar->open($file);
            $tar->extract($out, 'tar/foobar/');

            clearstatcache();

            $this->assertFileExists($out.'/tar/testdata1.txt', "Extracted $file");
            $this->assertEquals(13, filesize($out.'/tar/testdata1.txt'), "Extracted $file");

            $this->assertFileExists($out.'/testdata2.txt', "Extracted $file");
            $this->assertEquals(13, filesize($out.'/testdata2.txt'), "Extracted $file");

            TestUtils::rdelete($out);
        }
    }

    /**
     * Extract the prebuilt tar files with include regex
     */
    public function test_includeextract() {
        $dir = dirname(__FILE__).'/tar';
        $out = sys_get_temp_dir().'/dwtartest'.md5(time());

        foreach($this->extensions as $ext) {
            $tar  = new Tar();
            $file = "$dir/test.$ext";

            $tar->open($file);
            $tar->extract($out, '', '', '/\/foobar\//');

            clearstatcache();

            $this->assertFileNotExists($out.'/tar/testdata1.txt', "Extracted $file");

            $this->assertFileExists($out.'/tar/foobar/testdata2.txt', "Extracted $file");
            $this->assertEquals(13, filesize($out.'/tar/foobar/testdata2.txt'), "Extracted $file");

            TestUtils::rdelete($out);
        }
    }

    /**
     * Extract the prebuilt tar files with exclude regex
     */
    public function test_excludeextract() {
        $dir = dirname(__FILE__).'/tar';
        $out = sys_get_temp_dir().'/dwtartest'.md5(time());

        foreach($this->extensions as $ext) {
            $tar  = new Tar();
            $file = "$dir/test.$ext";

            $tar->open($file);
            $tar->extract($out, '', '/\/foobar\//');

            clearstatcache();

            $this->assertFileExists($out.'/tar/testdata1.txt', "Extracted $file");
            $this->assertEquals(13, filesize($out.'/tar/testdata1.txt'), "Extracted $file");

            $this->assertFileNotExists($out.'/tar/foobar/testdata2.txt', "Extracted $file");

            TestUtils::rdelete($out);
        }
    }

    /**
     * Check the extension to compression guesser
     */
    public function test_filetype() {
        $tar = new Tar();
        $this->assertEquals(Tar::COMPRESS_NONE, $tar->filetype('foo'));
        $this->assertEquals(Tar::COMPRESS_GZIP, $tar->filetype('foo.tgz'));
        $this->assertEquals(Tar::COMPRESS_GZIP, $tar->filetype('foo.tGZ'));
        $this->assertEquals(Tar::COMPRESS_GZIP, $tar->filetype('foo.tar.GZ'));
        $this->assertEquals(Tar::COMPRESS_GZIP, $tar->filetype('foo.tar.gz'));
        $this->assertEquals(Tar::COMPRESS_BZIP, $tar->filetype('foo.tbz'));
        $this->assertEquals(Tar::COMPRESS_BZIP, $tar->filetype('foo.tBZ'));
        $this->assertEquals(Tar::COMPRESS_BZIP, $tar->filetype('foo.tar.BZ2'));
        $this->assertEquals(Tar::COMPRESS_BZIP, $tar->filetype('foo.tar.bz2'));
    }

    /**
     * @depends test_ext_zlib
     */
    public function test_longpathextract() {
        $dir = dirname(__FILE__).'/tar';
        $out = sys_get_temp_dir().'/dwtartest'.md5(time());

        foreach(array('ustar', 'gnu') as $format) {
            $tar = new Tar();
            $tar->open("$dir/longpath-$format.tgz");
            $tar->extract($out);

            $this->assertFileExists($out.'/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/1234567890/test.txt');

            TestUtils::rdelete($out);
        }
    }

    // FS#1442
    public function test_createlongfile() {
        $tar = new Tar();
        $tmp = tempnam(sys_get_temp_dir(), 'dwtartest');

        $path = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.txt';

        $tar->create($tmp, Tar::COMPRESS_NONE);
        $tar->addData($path, 'testcontent1');
        $tar->close();

        $this->assertTrue(filesize($tmp) > 30); //arbitrary non-zero number
        $data = file_get_contents($tmp);

        // We should find the complete path and a longlink entry
        $this->assertTrue(strpos($data, 'testcontent1') !== false, 'content in TAR');
        $this->assertTrue(strpos($data, $path) !== false, 'path in TAR');
        $this->assertTrue(strpos($data, '@LongLink') !== false, '@LongLink in TAR');

        @unlink($tmp);
    }

    public function test_createlongpathustar() {
        $tar = new Tar();
        $tmp = tempnam(sys_get_temp_dir(), 'dwtartest');

        $path = '';
        for($i=0; $i<11; $i++) $path .= '1234567890/';
        $path = rtrim($path,'/');

        $tar->create($tmp, Tar::COMPRESS_NONE);
        $tar->addData("$path/test.txt", 'testcontent1');
        $tar->close();

        $this->assertTrue(filesize($tmp) > 30); //arbitrary non-zero number
        $data = file_get_contents($tmp);

        // We should find the path and filename separated, no longlink entry
        $this->assertTrue(strpos($data, 'testcontent1') !== false, 'content in TAR');
        $this->assertTrue(strpos($data, 'test.txt') !== false, 'filename in TAR');
        $this->assertTrue(strpos($data, $path) !== false, 'path in TAR');
        $this->assertFalse(strpos($data, "$path/test.txt") !== false, 'full filename in TAR');
        $this->assertFalse(strpos($data, '@LongLink') !== false, '@LongLink in TAR');

        @unlink($tmp);
    }

    public function test_createlongpathgnu() {
        $tar = new Tar();
        $tmp = tempnam(sys_get_temp_dir(), 'dwtartest');

        $path = '';
        for($i=0; $i<20; $i++) $path .= '1234567890/';
        $path = rtrim($path,'/');

        $tar->create($tmp, Tar::COMPRESS_NONE);
        $tar->addData("$path/test.txt", 'testcontent1');
        $tar->close();

        $this->assertTrue(filesize($tmp) > 30); //arbitrary non-zero number
        $data = file_get_contents($tmp);

        // We should find the complete path/filename and a longlink entry
        $this->assertTrue(strpos($data, 'testcontent1') !== false, 'content in TAR');
        $this->assertTrue(strpos($data, 'test.txt') !== false, 'filename in TAR');
        $this->assertTrue(strpos($data, $path) !== false, 'path in TAR');
        $this->assertTrue(strpos($data, "$path/test.txt") !== false, 'full filename in TAR');
        $this->assertTrue(strpos($data, '@LongLink') !== false, '@LongLink in TAR');

        @unlink($tmp);
    }

    /**
     * Extract a tarbomomb
     * @depends test_ext_zlib
     */
    public function test_tarbomb() {
        $dir = dirname(__FILE__).'/tar';
        $out = sys_get_temp_dir().'/dwtartest'.md5(time());

        $tar  = new Tar();

        $tar->open("$dir/tarbomb.tgz");
        $tar->extract($out);

        clearstatcache();

        $this->assertFileExists($out.'/AAAAAAAAAAAAAAAAA/BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB.txt');

        TestUtils::rdelete($out);
    }

    /**
     * A single zero file should be just a header block + the footer
     */
    public function test_zerofile(){
        $dir = dirname(__FILE__).'/tar';
        $tar = new Tar();
        $tar->create();
        $tar->addFile("$dir/zero.txt", 'zero.txt');
        $file = $tar->getArchive(Tar::COMPRESS_NONE);

        $this->assertEquals(512*3, strlen($file)); // 1 header block + 2 footer blocks
    }

    public function test_zerodata(){
        $tar = new Tar();
        $tar->create();
        $tar->addData('zero.txt','');
        $file = $tar->getArchive(Tar::COMPRESS_NONE);

        $this->assertEquals(512*3, strlen($file)); // 1 header block + 2 footer blocks
    }

    /**
     * A file of exactly one block should be just a header block + data block + the footer
     */
    public function test_blockfile(){
        $dir = dirname(__FILE__).'/tar';
        $tar = new Tar();
        $tar->create();
        $tar->addFile("$dir/block.txt", 'block.txt');
        $file = $tar->getArchive(Tar::COMPRESS_NONE);

        $this->assertEquals(512*4, strlen($file)); // 1 header block + data block + 2 footer blocks
    }

    public function test_blockdata(){
        $tar = new Tar();
        $tar->create();
        $tar->addData('block.txt', str_pad('', 512, 'x'));
        $file = $tar->getArchive(Tar::COMPRESS_NONE);

        $this->assertEquals(512*4, strlen($file)); // 1 header block + data block + 2 footer blocks
    }


    public function test_cleanPath(){
        $tar = new Tar();
        $tests = array (
            '/foo/bar' => 'foo/bar',
            '/foo/bar/' => 'foo/bar',
            'foo//bar' => 'foo/bar',
            'foo/0/bar' => 'foo/0/bar',
            'foo/../bar' => 'bar',
            'foo/bang/bang/../../bar' => 'foo/bar',
            'foo/../../bar' => 'bar',
            'foo/.././../bar' => 'bar',
        );

        foreach($tests as $in => $out){
            $this->assertEquals($out, $tar->cleanPath($in), "Input: $in");
        }
    }
}