~ubuntu-branches/ubuntu/quantal/gallery2/quantal

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/PlatformTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2007-09-10 20:22:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070910202219-0jsuntvqge4ade6b
Tags: 2.2.3-2
Add Slovak translation of Debconf templates.  (Thanks to 
Ivan Masá.  Closes: #441671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Gallery - a web based photo album viewer and editor
4
 
 * Copyright (C) 2000-2007 Bharat Mediratta
5
 
 *
6
 
 * This program is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License as published by
8
 
 * the Free Software Foundation; either version 2 of the License, or (at
9
 
 * your option) any later version.
10
 
 *
11
 
 * This program is distributed in the hope that it will be useful, but
12
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 
 * General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU General Public License
17
 
 * along with this program; if not, write to the Free Software
18
 
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19
 
 */
20
 
 
21
 
/**
22
 
 * Test platform functionality.
23
 
 * @package GalleryCore
24
 
 * @subpackage PHPUnit
25
 
 * @author Bharat Mediratta <bharat@menalto.com>
26
 
 * @version $Revision: 15728 $
27
 
 */
28
 
class PlatformTest extends GalleryTestCase {
29
 
 
30
 
    function PlatformTest($methodName) {
31
 
        $this->GalleryTestCase($methodName);
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        global $gallery;
36
 
        parent::setUp();
37
 
 
38
 
        $this->_platform =& $gallery->getPlatform();
39
 
        $this->_sourceFile = dirname(__FILE__) . '/../data/test1.gif';
40
 
        $this->_destFile = $gallery->getConfig('data.gallery.tmp') . 'test.dat';
41
 
    }
42
 
 
43
 
    function testCopy() {
44
 
        $this->assert($this->_platform->copy($this->_sourceFile, $this->_destFile));
45
 
        $this->assert($this->_platform->unlink($this->_destFile));
46
 
    }
47
 
 
48
 
    function testSymlink() {
49
 
        if ($this->_platform->isSymlinkSupported()) {
50
 
            $this->assert($this->_platform->symlink($this->_sourceFile, $this->_destFile));
51
 
            $this->assert($this->_platform->unlink($this->_destFile));
52
 
        }
53
 
    }
54
 
 
55
 
    function testMoveUploadedFile() {
56
 
        /* XXX: How do we test this?  We don't have an uploaded file :-( */
57
 
        $this->assert(!$this->_platform->move_uploaded_file($this->_sourceFile, $this->_destFile));
58
 
    }
59
 
 
60
 
    function testFileExists() {
61
 
        $this->assert($this->_platform->file_exists($this->_sourceFile));
62
 
        $this->assert(!$this->_platform->file_exists($this->_destFile));
63
 
    }
64
 
 
65
 
    function testIsLink() {
66
 
        $this->assert(!$this->_platform->is_link($this->_sourceFile));
67
 
    }
68
 
 
69
 
    function testIsDir() {
70
 
        $this->assert($this->_platform->is_dir(dirname(__FILE__)));
71
 
        $this->assert(!$this->_platform->is_dir(__FILE__));
72
 
    }
73
 
 
74
 
    function testIsFile() {
75
 
        $this->assert($this->_platform->is_file(__FILE__));
76
 
        $this->assert(!$this->_platform->is_file('bogus-file-name'));
77
 
    }
78
 
 
79
 
    function testIsWriteable() {
80
 
        /* On Win32, all files are writeable so the next assertion isn't that useful! */
81
 
        /* $this->assert(!$this->_platform->is_writeable(__FILE__)); */
82
 
 
83
 
        $this->assert($this->_platform->copy($this->_sourceFile, $this->_destFile));
84
 
        $this->assert($this->_platform->is_writeable($this->_destFile));
85
 
        $this->assert($this->_platform->unlink($this->_destFile));
86
 
    }
87
 
 
88
 
    function testIsReadable() {
89
 
        $this->assert($this->_platform->is_readable($this->_sourceFile));
90
 
    }
91
 
 
92
 
    function testIsUploadedFile() {
93
 
        $this->assert(!$this->_platform->is_uploaded_file($this->_sourceFile));
94
 
    }
95
 
 
96
 
    function testIsExecutable() {
97
 
        if (GalleryUtilities::isA($this->_platform, 'WinNtPlatform')) {
98
 
            $this->assert($this->_platform->is_executable($this->_sourceFile));
99
 
        } else {
100
 
            $this->assert(!$this->_platform->is_executable($this->_sourceFile));
101
 
        }
102
 
    }
103
 
 
104
 
    function testFilesize() {
105
 
        $this->assertEquals(1083,
106
 
                            $this->_platform->filesize($this->_sourceFile));
107
 
    }
108
 
 
109
 
    function testFile() {
110
 
        $lines = $this->_platform->file(__FILE__);
111
 
 
112
 
        /* --> Target Line <-- */
113
 
        $this->assertEquals("/* --> Target Line <-- */", trim($lines[__LINE__-2]));
114
 
    }
115
 
 
116
 
    function testFopen() {
117
 
        $fd = $this->_platform->fopen($this->_sourceFile, 'r');
118
 
        $this->assert($fd);
119
 
        fclose($fd);
120
 
    }
121
 
 
122
 
    function testOpendir() {
123
 
        $dir = $this->_platform->opendir(dirname(__FILE__));
124
 
        $this->assert($dir);
125
 
        $this->_platform->closedir($dir);
126
 
    }
127
 
 
128
 
    function testReaddir() {
129
 
        $dir = $this->_platform->opendir(dirname(__FILE__));
130
 
        $files = array();
131
 
        while ($filename = $this->_platform->readdir($dir)) {
132
 
            $files[$filename] = 1;
133
 
        }
134
 
        $this->_platform->closedir($dir);
135
 
 
136
 
        $this->assertEquals(1, $files[basename(__FILE__)]);
137
 
    }
138
 
 
139
 
    function testRename() {
140
 
        $this->assert($this->_platform->copy($this->_sourceFile, $this->_destFile));
141
 
        $this->assert($this->_platform->rename($this->_destFile,
142
 
                                               $this->_destFile . '-new'));
143
 
        $this->assert($this->_platform->unlink($this->_destFile . '-new'));
144
 
    }
145
 
 
146
 
    function testStat() {
147
 
        $this->assert($this->_platform->stat($this->_sourceFile));
148
 
    }
149
 
 
150
 
    function testUnlink() {
151
 
        $this->assert($this->_platform->copy($this->_sourceFile, $this->_destFile));
152
 
        $this->assert($this->_platform->unlink($this->_destFile));
153
 
    }
154
 
 
155
 
    function testRmdir() {
156
 
        $this->assert($this->_platform->mkdir($this->_destFile));
157
 
        $this->assert($this->_platform->mkdir($this->_destFile . '/1'));
158
 
 
159
 
        /* XXX: this generates a PHP error that I can't seem to turn off. */
160
 
        /* $this->assert(!$this->_platform->rmdir($this->_destFile)); */
161
 
 
162
 
        $this->assert($this->_platform->rmdir($this->_destFile . '/1'));
163
 
        $this->assert($this->_platform->rmdir($this->_destFile));
164
 
    }
165
 
 
166
 
    function testRecursiveRmdir() {
167
 
        $this->assert($this->_platform->mkdir($this->_destFile));
168
 
        $this->assert($this->_platform->mkdir($this->_destFile . '/1'));
169
 
        $this->assert($this->_platform->mkdir($this->_destFile . '/1/2'));
170
 
        $this->assert($this->_platform->mkdir($this->_destFile . '/1/2/3'));
171
 
        $this->assert($this->_platform->recursiveRmdir($this->_destFile), 'remove');
172
 
    }
173
 
 
174
 
    function testMkdir() {
175
 
        $this->assert($this->_platform->mkdir($this->_destFile), 'mkdir');
176
 
        $this->assert($this->_platform->recursiveRmdir($this->_destFile));
177
 
    }
178
 
 
179
 
    function testIsLegalPathComponent() {
180
 
        $this->assert($this->_platform->isLegalPathComponent('TheQuickBrownFox'));
181
 
        $this->assert(!$this->_platform->isLegalPathComponent('The!QuickBrownFox'));
182
 
    }
183
 
 
184
 
    function testLegalizePathComponent() {
185
 
        $this->assertEquals('The_Quick_',
186
 
                            $this->_platform->legalizePathComponent('The!Quick$'));
187
 
    }
188
 
 
189
 
    function testLegalizePathComponent2() {
190
 
        $this->assertEquals('test_php',
191
 
                            $this->_platform->legalizePathComponent('test.php'));
192
 
    }
193
 
 
194
 
    function testLegalizePathComponent3() {
195
 
        $this->assertEquals('test_php.jpg',
196
 
                            $this->_platform->legalizePathComponent('test.php.jpg'));
197
 
    }
198
 
 
199
 
    function testPathComponentLegalCharactersConserved() {
200
 
        $this->assertEquals(true,
201
 
                            $this->_platform->isLegalPathComponent('TheQuickBrownFox'));
202
 
        $this->assertEquals('TheQuickBrownFox',
203
 
                            $this->_platform->legalizePathComponent('TheQuickBrownFox'));
204
 
    }
205
 
 
206
 
    function testPathComponentIllegalCharactersToUnderscores() {
207
 
        $this->assertEquals(false,
208
 
                            $this->_platform->isLegalPathComponent('The Quick!Brown#Fox'));
209
 
        $this->assertEquals('The Quick_Brown#Fox',
210
 
                            $this->_platform->legalizePathComponent('The Quick!Brown#Fox'));
211
 
    }
212
 
 
213
 
    function testPathComponentDotInFileBaseToUnderscore() {
214
 
        $this->assertEquals(false,
215
 
                            $this->_platform->isLegalPathComponent('test.php.jpg'));
216
 
        $this->assertEquals('test_php.jpg',
217
 
                            $this->_platform->legalizePathComponent('test.php.jpg'));
218
 
    }
219
 
 
220
 
    function testPathComponentDotRecognizedExtensionConserved() {
221
 
        $this->assertEquals(true,
222
 
                            $this->_platform->isLegalPathComponent('test.jpg'));
223
 
        $this->assertEquals('test.jpg',
224
 
                            $this->_platform->legalizePathComponent('test.jpg'));
225
 
    }
226
 
 
227
 
    function testPathComponentDotUnrecognizedExtensionToUnderscore() {
228
 
        $this->assertEquals(false,
229
 
                            $this->_platform->isLegalPathComponent('test.foo'));
230
 
        $this->assertEquals('test_foo',
231
 
                            $this->_platform->legalizePathComponent('test.foo'));
232
 
    }
233
 
 
234
 
    function testPathComponentDotPhpExtensionToUnderscore() {
235
 
        $this->assertEquals(false,
236
 
                            $this->_platform->isLegalPathComponent('test.php'));
237
 
        $this->assertEquals('test_php',
238
 
                            $this->_platform->legalizePathComponent('test.php'));
239
 
    }
240
 
 
241
 
    function testPathComponentDotUnderscoreToUnderscoreUnderscore() {
242
 
        $this->assertEquals(false,
243
 
                            $this->_platform->isLegalPathComponent('._test'));
244
 
        $this->assertEquals('__test',
245
 
                            $this->_platform->legalizePathComponent('._test'));
246
 
    }
247
 
 
248
 
    /**
249
 
     * Make sure executing a bogus path fails.
250
 
     *
251
 
     * XXX: This is a pretty weak test.  It's hard to improve it without actually shipping a
252
 
     * known-good script though.
253
 
     */
254
 
    function testExec() {
255
 
        $results = $this->_platform->exec(array(array('/bogus/path')));
256
 
        $this->assertEquals(false, $results[0]);
257
 
    }
258
 
 
259
 
    /**
260
 
     * Make sure files are written with correct permissions by exec, copy, symlink,
261
 
     * atomicWrite; test chmod.
262
 
     */
263
 
    function testPermissions() {
264
 
        if (GalleryUtilities::isA($this->_platform, 'UnixPlatform')) {
265
 
            if ($this->_markPluginParametersForCleanup('module', 'core')) {
266
 
                return;
267
 
            }
268
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'permissions.file', '600');
269
 
            if ($ret) {
270
 
                return $this->failWithStatus($ret);
271
 
            }
272
 
            $this->_platform->resetPlatform();
273
 
            $this->_permissionTest(0600, 1, array('copy', 'atomicWrite', 'symlink'));
274
 
 
275
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'permissions.file', '664');
276
 
            if ($ret) {
277
 
                return $this->failWithStatus($ret);
278
 
            }
279
 
            $this->_platform->resetPlatform();
280
 
            $this->_permissionTest(0664, 2, array('copy', 'symlink', 'atomicWrite'));
281
 
 
282
 
            $this->assert($this->_platform->copy($this->_sourceFile, $this->_destFile), 'copy');
283
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'permissions.file', '612');
284
 
            if ($ret) {
285
 
                return $this->failWithStatus($ret);
286
 
            }
287
 
            $this->_platform->resetPlatform();
288
 
            $this->_permissionTest(0612, 3, array('chmod'));
289
 
 
290
 
            $this->assert($this->_platform->mkdir($this->_destFile), 'mkdir');
291
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'core',
292
 
                                                      'permissions.directory', '751');
293
 
            if ($ret) {
294
 
                return $this->failWithStatus($ret);
295
 
            }
296
 
            $this->_platform->resetPlatform();
297
 
            $this->_permissionTest(0751, 4, array('chmod'));
298
 
 
299
 
            /* exec now uses a default umask of 022 -- exec'ers must chmod output files */
300
 
            $this->_permissionTest(0644, 5, array('exec'));
301
 
 
302
 
            $this->_platform->resetPlatform();
303
 
        }
304
 
    }
305
 
 
306
 
    function _permissionTest($expectedPermissions, $testNumber, $tests) {
307
 
        global $gallery;
308
 
        foreach ($tests as $test) {
309
 
            $stat = $debug = '';
310
 
            switch ($test) {
311
 
            case 'exec':
312
 
                $gallery->startRecordingDebugSnippet();
313
 
                $results = $this->_platform->exec(
314
 
                                   array(array('echo', 'test', '>', $this->_destFile)));
315
 
                $debug = $gallery->stopRecordingDebugSnippet();
316
 
                $this->assert($results[0], "exec $testNumber $debug");
317
 
                $stat = $this->_platform->stat($this->_destFile);
318
 
                break;
319
 
 
320
 
            case 'copy':
321
 
                $this->assert($this->_platform->copy($this->_sourceFile, $this->_destFile),
322
 
                              "copy $testNumber");
323
 
                $stat = $this->_platform->stat($this->_destFile);
324
 
                break;
325
 
 
326
 
            case 'symlink':
327
 
                if (!$this->_platform->isSymlinkSupported()) {
328
 
                    $this->assert(false, 'symlink not supported');
329
 
                    continue 2;
330
 
                }
331
 
                $this->assert($this->_platform->symlink($this->_sourceFile, $this->_destFile),
332
 
                              "symlink $testNumber");
333
 
                $stat = lstat($this->_destFile);
334
 
                break;
335
 
 
336
 
            case 'atomicWrite':
337
 
                $this->assert($this->_platform->atomicWrite($this->_destFile, 'test'),
338
 
                              "atomicWrite $testNumber");
339
 
                $stat = $this->_platform->stat($this->_destFile);
340
 
                break;
341
 
 
342
 
            case 'chmod':
343
 
                $this->assert($this->_platform->chmod($this->_destFile), "chmod $testNumber");
344
 
                $this->_platform->clearstatcache(); /* is_dir call in chmod() cached old perm */
345
 
                $stat = $this->_platform->stat($this->_destFile);
346
 
            }
347
 
 
348
 
            $this->assert(!empty($stat), "$test stat $testNumber $debug");
349
 
            if ($test == 'symlink') {
350
 
                /* On some systems symlinks always have 777 permissions */
351
 
                $this->assert(($stat[2] & 0777) == $expectedPermissions ||
352
 
                              ($stat[2] & 0777) == 0777,
353
 
                    "$test permissions $testNumber (" . decoct($expectedPermissions) .
354
 
                    ' != ' . decoct($stat[2] & 0777) . ' or 777)');
355
 
            } else {
356
 
                $this->assertEquals(decoct($expectedPermissions), decoct($stat[2] & 0777),
357
 
                                    "$test permissions $testNumber");
358
 
            }
359
 
            if ($testNumber == 4) {
360
 
                $this->assert($this->_platform->rmdir($this->_destFile), "$test rmdir $testNumber");
361
 
            } else {
362
 
                $this->assert($this->_platform->unlink($this->_destFile),
363
 
                              "$test unlink $testNumber");
364
 
            }
365
 
            $this->_platform->clearstatcache();
366
 
        }
367
 
    }
368
 
 
369
 
    function _setupChmodTest() {
370
 
        $this->assert($this->_platform->mkdir($this->_destFile), 'mkdir');
371
 
        $this->assert($this->_platform->mkdir($this->_destFile . '/1'), 'mkdir/1');
372
 
        $this->assert($this->_platform->copy($this->_sourceFile, $this->_destFile . '/1/2'), 'cp');
373
 
        $this->assert($this->_platform->mkdir($this->_destFile . '/1/3'), 'mkdir/1/3');
374
 
        $this->assert($this->_platform->copy($this->_sourceFile, $this->_destFile . '/1/3/4'),
375
 
                      'cp2');
376
 
        $this->assert($this->_platform->mkdir($this->_destFile . '/1/5'), 'mkdir/1/5');
377
 
 
378
 
        $ret = $this->_markPluginParametersForCleanup('module', 'core');
379
 
        if ($ret) {
380
 
            return $ret;
381
 
        }
382
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'permissions.directory', '734');
383
 
        if ($ret) {
384
 
            return $ret;
385
 
        }
386
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'permissions.file', '652');
387
 
        if ($ret) {
388
 
            return $ret;
389
 
        }
390
 
        return null;
391
 
    }
392
 
 
393
 
    function testRecursiveChmod1() {
394
 
        /* Set dir and file permissions by system prefs */
395
 
        $ret = $this->_setupChmodTest();
396
 
        if ($ret) {
397
 
            return $this->failWithStatus($ret);
398
 
        }
399
 
 
400
 
        $this->_platform->resetPlatform();
401
 
        $this->assert($this->_platform->recursiveChmod($this->_destFile), 'set perms');
402
 
        $this->_platform->clearstatcache();
403
 
        $expected = GalleryUtilities::isA($this->_platform, 'WinNtPlatform') ? '777' : '734';
404
 
        $expected2 = GalleryUtilities::isA($this->_platform, 'WinNtPlatform') ? '777' : '652';
405
 
        $expected3 = GalleryUtilities::isA($this->_platform, 'WinNtPlatform') ? '666' : '652';
406
 
 
407
 
        $stat = $this->_platform->stat($this->_destFile);
408
 
        $this->assertEquals($expected, decoct($stat[2] & 0777), 'perm0');
409
 
        $stat = $this->_platform->stat($this->_destFile . '/1');
410
 
        $this->assertEquals($expected, decoct($stat[2] & 0777), 'perm1');
411
 
        $stat = $this->_platform->stat($this->_destFile . '/1/2');
412
 
        $this->assertEquals($expected3, decoct($stat[2] & 0777), 'perm2');
413
 
        $stat = $this->_platform->stat($this->_destFile . '/1/3');
414
 
        $this->assertEquals($expected, decoct($stat[2] & 0777), 'perm3');
415
 
        $stat = $this->_platform->stat($this->_destFile . '/1/3/4');
416
 
        $this->assertEquals($expected3, decoct($stat[2] & 0777), 'perm4');
417
 
        $stat = $this->_platform->stat($this->_destFile . '/1/5');
418
 
        $this->assertEquals($expected, decoct($stat[2] & 0777), 'perm5');
419
 
 
420
 
        $this->assert($this->_platform->recursiveRmdir($this->_destFile), 'remove');
421
 
        $this->_platform->resetPlatform();
422
 
    }
423
 
 
424
 
    function testRecursiveChmod2() {
425
 
        /* Set given dir permission, don't change file permissions */
426
 
        $ret = $this->_setupChmodTest();
427
 
        if ($ret) {
428
 
            return $this->failWithStatus($ret);
429
 
        }
430
 
 
431
 
        $fileStat = $this->_platform->stat($this->_destFile . '/1/2');
432
 
        $fileStat = decoct($fileStat[2]);
433
 
        $this->_platform->resetPlatform();
434
 
 
435
 
        $this->assert($this->_platform->recursiveChmod($this->_destFile, 0711, -1), 'set perms');
436
 
        $this->_platform->clearstatcache();
437
 
        $expected = GalleryUtilities::isA($this->_platform, 'WinNtPlatform') ? '777' : '711';
438
 
 
439
 
        $stat = $this->_platform->stat($this->_destFile);
440
 
        $this->assertEquals($expected, decoct($stat[2] & 0777), 'perm0');
441
 
        $stat = $this->_platform->stat($this->_destFile . '/1');
442
 
        $this->assertEquals($expected, decoct($stat[2] & 0777), 'perm1');
443
 
        $stat = $this->_platform->stat($this->_destFile . '/1/2');
444
 
        $this->assertEquals($fileStat, decoct($stat[2]), 'file2 should be unchanged');
445
 
        $stat = $this->_platform->stat($this->_destFile . '/1/3');
446
 
        $this->assertEquals($expected, decoct($stat[2] & 0777), 'perm3');
447
 
        $stat = $this->_platform->stat($this->_destFile . '/1/3/4');
448
 
        $this->assertEquals($fileStat, decoct($stat[2]), 'file4 should be unchanged');
449
 
        $stat = $this->_platform->stat($this->_destFile . '/1/5');
450
 
        $this->assertEquals($expected, decoct($stat[2] & 0777), 'perm5');
451
 
 
452
 
        $this->assert($this->_platform->recursiveRmdir($this->_destFile), 'remove');
453
 
        $this->_platform->resetPlatform();
454
 
    }
455
 
 
456
 
    function testRecursiveChmod3() {
457
 
        /* Set given file permission, don't change dir permissions */
458
 
        $ret = $this->_setupChmodTest();
459
 
        if ($ret) {
460
 
            return $this->failWithStatus($ret);
461
 
        }
462
 
 
463
 
        $dirStat = $this->_platform->stat($this->_destFile . '/1');
464
 
        $dirStat = decoct($dirStat[2]);
465
 
        $this->_platform->resetPlatform();
466
 
 
467
 
        $this->assert($this->_platform->recursiveChmod($this->_destFile, -1, 0640), 'set perms');
468
 
        $this->_platform->clearstatcache();
469
 
        $expected = GalleryUtilities::isA($this->_platform, 'WinNtPlatform') ? '777' : '640';
470
 
        $expected2 = GalleryUtilities::isA($this->_platform, 'WinNtPlatform') ? '666' : '640';
471
 
 
472
 
        $stat = $this->_platform->stat($this->_destFile);
473
 
        $this->assertEquals($dirStat, decoct($stat[2]), 'dir0 should be unchanged');
474
 
        $stat = $this->_platform->stat($this->_destFile . '/1');
475
 
        $this->assertEquals($dirStat, decoct($stat[2]), 'dir1 should be unchanged');
476
 
        $stat = $this->_platform->stat($this->_destFile . '/1/2');
477
 
        $this->assertEquals($expected2, decoct($stat[2] & 0777), 'perm2');
478
 
        $stat = $this->_platform->stat($this->_destFile . '/1/3');
479
 
        $this->assertEquals($dirStat, decoct($stat[2]), 'dir3 should be unchanged');
480
 
        $stat = $this->_platform->stat($this->_destFile . '/1/3/4');
481
 
        $this->assertEquals($expected2, decoct($stat[2] & 0777), 'perm4');
482
 
        $stat = $this->_platform->stat($this->_destFile . '/1/5');
483
 
        $this->assertEquals($dirStat, decoct($stat[2]), 'dir5 should be unchanged');
484
 
 
485
 
        $this->assert($this->_platform->recursiveRmdir($this->_destFile), 'remove');
486
 
        $this->_platform->resetPlatform();
487
 
    }
488
 
 
489
 
    function testGetDirectorySeparator() {
490
 
        $file = __FILE__;
491
 
        $realSeparator = $file[strlen(dirname(__FILE__))];
492
 
        $this->assertEquals($realSeparator, $this->_platform->getDirectorySeparator());
493
 
    }
494
 
 
495
 
    function testGetLineEnding() {
496
 
        if (GalleryUtilities::isA($this->_platform, 'UnixPlatform')) {
497
 
            $realLineEnding = "\n";
498
 
        } else if (GalleryUtilities::isA($this->_platform, 'WinNtPlatform')) {
499
 
            $realLineEnding = "\r\n";
500
 
        }
501
 
 
502
 
        $this->assertEquals($realLineEnding, $this->_platform->getLineEnding(),
503
 
                            "Line Endings Don't Match");
504
 
    }
505
 
 
506
 
    function testIsRestrictedByOpenBaseDir() {
507
 
        global $gallery;
508
 
        $isWin = GalleryUtilities::isA($this->_platform, 'WinNtPlatform');
509
 
 
510
 
        $this->assert(!$this->_platform->isRestrictedByOpenBaseDir(__FILE__), 'this file');
511
 
 
512
 
        $gallery->_phpVm = new PlatformTestPhpVm(dirname(__FILE__), $this);
513
 
        $this->assert(!$this->_platform->isRestrictedByOpenBaseDir(__FILE__), 'simple case');
514
 
 
515
 
        /* Bad paths are still checked even though no realpath */
516
 
        $this->assert(
517
 
            !$this->_platform->isRestrictedByOpenBaseDir(dirname(__FILE__) . '/bogus/path'),
518
 
            'bogus path "inside" valid dir');
519
 
        $this->assert(
520
 
            $this->_platform->isRestrictedByOpenBaseDir(dirname(__FILE__) . '/../bogus/path'),
521
 
            'bogus path using .. to get outside valid dir');
522
 
        $gallery->_phpVm = new PlatformTestPhpVm('', $this);
523
 
        $this->assert(!$this->_platform->isRestrictedByOpenBaseDir('/bogus/path'),
524
 
                      'bogus path with no restriction');
525
 
 
526
 
        /* Open_basedir with multiple entries */
527
 
        $platform = $isWin ? new PlatformTestOpenBaseDirWinNtPlatform()
528
 
                           : new PlatformTestOpenBaseDirUnixPlatform();
529
 
        $platform->setRealpathData(array('C:\\Test' => 'C:\\Test',
530
 
                                         'C:\\Test\\file' => 'C:\\Test\\file',
531
 
                                         'D:\\Fun\\' => 'D:\\Fun',
532
 
                                         'D:\\Fun\\file' => 'D:\\Fun\\file',
533
 
                                         '/test' => '/test', '/fun/' => '/fun',
534
 
                                         '/test/file' => '/test/file', '/fun/file' => '/fun/file',
535
 
                                         'c:\\TEST\\file' => 'C:\\Test\\file',
536
 
                                         '/TEST/file' => '/TEST/file'));
537
 
 
538
 
        $gallery->_phpVm =
539
 
            new PlatformTestPhpVm($isWin ? 'C:\\Test;D:\\Fun\\' : '/test:/fun/', $this);
540
 
        $this->assert(
541
 
            !$platform->isRestrictedByOpenBaseDir($isWin ? 'C:\\Test\\file' : '/test/file'),
542
 
            'first path');
543
 
        $this->assert(
544
 
            !$platform->isRestrictedByOpenBaseDir($isWin ? 'D:\\Fun\\file' : '/fun/file'),
545
 
            'second path');
546
 
        $this->assertEquals(!$isWin,
547
 
            $platform->isRestrictedByOpenBaseDir($isWin ? 'c:\\TEST\\file' : '/TEST/file'),
548
 
            'unix case sensitive, windows not');
549
 
 
550
 
        /* Symlinks are resolved in both given path and open_basedir list */
551
 
        if (!$isWin) {
552
 
            $platform->setRealpathData(array('/test/real' => '/test/real',
553
 
                                             '/test/real/file' => '/test/real/file',
554
 
                                             '/test/link' => '/test/real',
555
 
                                             '/test/link/file' => '/test/real/file',
556
 
                                             '/test/real/linktoetc/passwd' => '/etc/passwd',
557
 
                                             '/test/link/linktoetc/passwd' => '/etc/passwd'));
558
 
 
559
 
            $gallery->_phpVm = new PlatformTestPhpVm('/test/real', $this);
560
 
            $this->assert(!$platform->isRestrictedByOpenBaseDir('/test/link/file'),
561
 
                          'path with symlink, open_basedir has realpath');
562
 
            $this->assert($platform->isRestrictedByOpenBaseDir('/test/real/linktoetc/passwd'),
563
 
                          'symlink to outside, open_basedir has realpath');
564
 
            $this->assert($platform->isRestrictedByOpenBaseDir('/test/link/bogus/path'),
565
 
                          'invalid path with symlink, open_basedir has realpath');
566
 
 
567
 
            $gallery->_phpVm = new PlatformTestPhpVm('/test/link', $this);
568
 
            $this->assert(!$platform->isRestrictedByOpenBaseDir('/test/real/file'),
569
 
                          'realpath given, open_basedir has symlink');
570
 
            $this->assert($platform->isRestrictedByOpenBaseDir('/test/link/linktoetc/passwd'),
571
 
                          'symlink to outside, open_basedir has symlink');
572
 
            $this->assert($platform->isRestrictedByOpenBaseDir('/test/link/bogus/path'),
573
 
                          'invalid path with symlink, open_basedir has symlink');
574
 
            $this->assert(!$platform->isRestrictedByOpenBaseDir('/test/real/bogus/path'),
575
 
                          'invalid path, open_basedir has symlink');
576
 
        }
577
 
 
578
 
        /* Open_basedir entry can be a prefix and not a real dir */
579
 
        $platform->setRealpathData(array('/tes' => false, '/test' => '/test',
580
 
                                         '/testfile' => '/testfile',
581
 
                                         '/test/file' => '/test/file',
582
 
                                         '/toast/file' => '/toast/file',
583
 
                                         '/link/file' => '/test/file'));
584
 
 
585
 
        $gallery->_phpVm = new PlatformTestPhpVm('/tes', $this);
586
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir('/testfile'), 'file with prefix');
587
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir('/test/file'), 'dir with prefix');
588
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir('/tes/bad/path'),
589
 
                      'given path can be invalid and match a prefix');
590
 
                        /* though it would be restricted if /tes was a real dir? odd.. */
591
 
        $this->assert($platform->isRestrictedByOpenBaseDir('/toast/file'), 'not match prefix');
592
 
        if (!$isWin) {
593
 
            /* Symlink expansion with prefix (realpath of given path must match prefix) */
594
 
            $this->assert(!$platform->isRestrictedByOpenBaseDir('/link/file'),
595
 
                          'link, realpath has prefix');
596
 
 
597
 
            $platform->setRealpathData(array('/lin' => false, '/link' => '/test',
598
 
                                             '/linnen' => '/linnen',
599
 
                                             '/linkfile' => '/testfile',
600
 
                                             '/link/file' => '/test/file',
601
 
                                             '/lonk/file' => '/lonk/file',
602
 
                                             '/link/fu' => false, '/link/fun' => '/test/fun',
603
 
                                             '/test/fun' => '/test/fun'));
604
 
            $gallery->_phpVm = new PlatformTestPhpVm('/lin', $this);
605
 
            $this->assert(!$platform->isRestrictedByOpenBaseDir('/linnen'), 'nonlink with prefix');
606
 
            $this->assert($platform->isRestrictedByOpenBaseDir('/linkfile'), 'link with prefix');
607
 
            $this->assert($platform->isRestrictedByOpenBaseDir('/link/file'), 'dir with prefix');
608
 
            $this->assert($platform->isRestrictedByOpenBaseDir('/lonk/file'), 'not match prefix');
609
 
 
610
 
            $gallery->_phpVm = new PlatformTestPhpVm('/link/fu', $this);
611
 
            $this->assert($platform->isRestrictedByOpenBaseDir('/test/fun'),
612
 
                          'realpath given, open_basedir has prefix with link parent');
613
 
            $this->assert($platform->isRestrictedByOpenBaseDir('/link/fun'),
614
 
                          'valid link path given, open_basedir has prefix with link parent');
615
 
            $this->assert(!$platform->isRestrictedByOpenBaseDir('/link/fubar'),
616
 
                          'invalid link path given, open_basedir has prefix with link parent');
617
 
        }
618
 
 
619
 
        /* Open_basedir with . */
620
 
        $gallery->_phpVm = new PlatformTestPhpVm('.', $this);
621
 
        $this->assert(!$this->_platform->isRestrictedByOpenBaseDir(
622
 
            dirname(__FILE__) . '/../../../../lib/tools/phpunit/index.php'), 'dot basedir');
623
 
 
624
 
        /* Open_basedir with trailing slash */
625
 
        $s = $this->_platform->getDirectorySeparator();
626
 
        $platform->setRealpathData(array("${s}test${s}" => "${s}test",
627
 
                                         "${s}testing" => "${s}testing"));
628
 
        $gallery->_phpVm = new PlatformTestPhpVm("${s}test${s}", $this);
629
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir("${s}test"), 'no trailing slash');
630
 
        $this->assert($platform->isRestrictedByOpenBaseDir("${s}testing"),
631
 
                      'should not match basedir with trailing slash');
632
 
 
633
 
        /* Invalid paths (realpath returns false, our code does relative path and .. handling) */
634
 
        $platform->setRealpathData(array("${s}test" => "${s}test"));
635
 
        $gallery->_phpVm = new PlatformTestPhpVm("${s}test", $this);
636
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir("${s}test${s}bogus"),
637
 
                      'valid bogus path');
638
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir("${s}test${s}bogus${s}..${s}path"),
639
 
                      'single dotdot');
640
 
        $this->assert($platform->isRestrictedByOpenBaseDir("${s}test${s}bogus${s}..${s}..${s}path"),
641
 
                      'multiple dotdots');
642
 
        $this->assert($platform->isRestrictedByOpenBaseDir("${s}test${s}.."),
643
 
                      'dotdot at end');
644
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir("${s}.${s}test${s}bogus"),
645
 
                      'dot in valid bogus path');
646
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir("${s}.${s}test${s}.${s}bogus"),
647
 
                      'two dots in valid bogus path');
648
 
        $platform->setCwd("${s}test");
649
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir('bogus'),
650
 
                      'valid relative path');
651
 
        $this->assert($platform->isRestrictedByOpenBaseDir("..${s}bogus"),
652
 
                      'invalid relative path');
653
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir("bogus${s}path${s}..${s}..${s}test"),
654
 
                      'valid relative path with two dotdots');
655
 
    }
656
 
 
657
 
    function testIsRestrictedByOpenBaseDirForMatchAllOpenBaseDir() {
658
 
        global $gallery;
659
 
 
660
 
        $gallery->_phpVm = new PlatformTestPhpVm('/', $this);
661
 
        $platform = new PlatformTestOpenBaseDirUnixPlatform();
662
 
        $platform->setRealpathData(array('/test/file' => '/test/file',
663
 
                                         '/' => '/'));
664
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir('/'), '/ should be allowed');
665
 
        $this->assert(!$platform->isRestrictedByOpenBaseDir('/test/file'),
666
 
                      '/test/file should be allowed');
667
 
    }
668
 
 
669
 
    /**
670
 
     * Try opening a socket to the current web server.
671
 
     */
672
 
    function testFsockopen() {
673
 
        $fd = $this->_platform->fsockopen($_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'],
674
 
                                          $errno, $errstr, 30);
675
 
        $this->assert($fd, 'File descriptor should not be null/false');
676
 
        @$this->_platform->fclose($fd);
677
 
    }
678
 
 
679
 
    /**
680
 
     * Try opening a socket to a bogus web server.
681
 
     */
682
 
    function testFsockopenBadHost() {
683
 
        /* We know this is going to bomb so unset PHPunit's error handler */
684
 
        restore_error_handler();
685
 
 
686
 
        $fd = @$this->_platform->fsockopen('www.BOGUSBOGUSBOGUSBOGUS.com', $_SERVER['SERVER_PORT'],
687
 
                                           $errno, $errstr, 30);
688
 
        $this->assertEquals(false, $fd);
689
 
    }
690
 
 
691
 
    /**
692
 
     * Verify that we can write a few bytes to a file.
693
 
     */
694
 
    function testOpenWriteAndClose() {
695
 
        $fd = $this->_platform->fopen($this->_destFile, 'wb');
696
 
        $this->assert($fd, 'File descriptor should not be null');
697
 
 
698
 
        $buf = 'this is a test';
699
 
        $bytesWritten = $this->_platform->fwrite($fd, $buf, strlen($buf));
700
 
        $this->assertEquals(strlen($buf), $bytesWritten);
701
 
 
702
 
        $this->assert($this->_platform->fclose($fd), 'fclose');
703
 
        $this->assert($this->_platform->unlink($this->_destFile), 'unlink');
704
 
    }
705
 
 
706
 
    function testOpenAndReadTillEof() {
707
 
        $fd = $this->_platform->fopen($this->_sourceFile, 'rb');
708
 
        $this->assert($fd, 'File descriptor should not be null');
709
 
 
710
 
        $buf = '';
711
 
        while (!$this->_platform->feof($fd)) {
712
 
            $buf .= $this->_platform->fread($fd, 512);
713
 
        }
714
 
 
715
 
        $this->assert(1083, strlen($buf));
716
 
        $this->assert($this->_platform->fclose($fd));
717
 
    }
718
 
 
719
 
    function testOpenMissingFile() {
720
 
        /* We know this is going to bomb so unset PHPunit's error handler */
721
 
        restore_error_handler();
722
 
 
723
 
        /* And suppress warnings when we call fopen */
724
 
        $fd = @$this->_platform->fopen(sprintf('%s_BOGUS_BOGUS', __FILE__), 'rb');
725
 
        $this->assertEquals(false, $fd);
726
 
    }
727
 
 
728
 
    function testAtomicWrite() {
729
 
        /*
730
 
         * We should test if our operations are really concurrent-safe, but we cannot do that
731
 
         * without having multiple processes accessing the same file, which is not possible in our
732
 
         * test-framework (yet)
733
 
         */
734
 
        $testData1 = "this is a test\nmultiline\ntest";
735
 
        $this->assert($this->_platform->atomicWrite($this->_destFile, $testData1, 'wb'),
736
 
                      'write 1 failed');
737
 
        $output = implode('', $this->_platform->file($this->_destFile));
738
 
        $this->assertEquals($testData1, $output);
739
 
 
740
 
        /* Now write another text to the same file and see if it's ok */
741
 
        $testData2 = "this is a test\nmultiline\ntest\nmore\ntext";
742
 
        $this->assert($this->_platform->atomicWrite($this->_destFile, $testData2, 'wb'),
743
 
                      'write 2 failed');
744
 
        $output = implode('', $this->_platform->file($this->_destFile));
745
 
        $this->assertEquals($testData2, $output);
746
 
        $this->assert($this->_platform->unlink($this->_destFile), 'unlink');
747
 
    }
748
 
 
749
 
    function testSplitPath() {
750
 
        $this->assertEquals(array('dir', 'path', 'file.txt'),
751
 
                            $this->_platform->splitPath('dir/path/file.txt'), 'relative');
752
 
        if (GalleryUtilities::isA($this->_platform, 'WinNtPlatform')) {
753
 
            $this->assertEquals(array('c:\\', 'dir', 'path', 'file.txt'),
754
 
                                $this->_platform->splitPath('c:\dir\path\file.txt'), 'absolute');
755
 
            $this->assertEquals(array('D:\\', 'path', 'file.txt'),
756
 
                                $this->_platform->splitPath('D:\path/file.txt'), 'absolute2');
757
 
            $this->assertEquals(array('\\', 'test'),
758
 
                                $this->_platform->splitPath('\test'), 'absolute3');
759
 
        } else {
760
 
            $this->assertEquals(array('/', 'dir', 'path', 'file.txt'),
761
 
                                $this->_platform->splitPath('/dir/path/file.txt'), 'absolute');
762
 
        }
763
 
    }
764
 
 
765
 
    function testChdir() {
766
 
        global $gallery;
767
 
        $tmpdir = $gallery->getConfig('data.gallery.tmp');
768
 
        $oldCwd = $this->_platform->getcwd();
769
 
        $this->assert($oldCwd);
770
 
 
771
 
        /* Change working directory to g2data/tmp */
772
 
        $this->assert($this->_platform->chdir($tmpdir));
773
 
        $newCwd = $this->_platform->getcwd();
774
 
        $this->assertEquals(realpath($tmpdir), $newCwd);
775
 
 
776
 
        /* Go back to original directory */
777
 
        $this->_platform->chdir($oldCwd);
778
 
    }
779
 
 
780
 
    function testStrftime() {
781
 
        $this->assertEquals('2005 03', $this->_platform->strftime('%Y %m', 1110101111));
782
 
        /* For windows we replace %e with %d and convert leading zeros to spaces */
783
 
        $this->assertEquals( ' 6 11', $this->_platform->strftime('%e %S', 1110101111));
784
 
 
785
 
        /* Verify UTF-8 text in format string is not mangled (only % tokens convert to UTF-8) */
786
 
        $this->assertEquals("\xd0\x9d\xd0\xb5 2005 \xd0\xbd %\n03",
787
 
            $this->_platform->strftime("\xd0\x9d\xd0\xb5 %Y \xd0\xbd %%\n%m", 1110101111));
788
 
 
789
 
        /* How to test that strftime output is converted to UTF-8? */
790
 
    }
791
 
 
792
 
    function testFileGetAndPutContents() {
793
 
        $fileContents = $this->_platform->file_get_contents($this->_sourceFile);
794
 
        $this->assert(is_string($fileContents), 'contents not a string');
795
 
        $this->assert(false !== strpos($fileContents, 'GIF89'), 'invalid contents');
796
 
        $this->assert(1083, strlen($fileContents));
797
 
 
798
 
        $success = $this->_platform->file_put_contents($this->_destFile, $fileContents);
799
 
        $this->assert($success, 'file_put_contents error');
800
 
        $this->assertEquals(1083, $this->_platform->filesize($this->_destFile));
801
 
        $this->assert($this->_platform->unlink($this->_destFile), 'unlink');
802
 
    }
803
 
 
804
 
    function testMailUsesUnixLineEndings() {
805
 
        global $gallery;
806
 
 
807
 
        $ret = $this->_markPluginParametersForCleanup('module', 'core');
808
 
        if ($ret) {
809
 
            return $this->failWithStatus($ret);
810
 
        }
811
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'smtp.host', null);
812
 
        if ($ret) {
813
 
            return $this->failWithStatus($ret);
814
 
        }
815
 
 
816
 
        $gallery->_phpVm = new PlatformTestPhpVm(dirname(__FILE__), $this);
817
 
        $platform = new UnixPlatform();
818
 
        $result = $platform->mail("foo@example.com", "subject",
819
 
                                  "body line1\nline2\r\nline3\r\n",
820
 
                                  "header line1\nline2\r\nline3\r\n");
821
 
        $this->assert($result);
822
 
        $this->assertEquals("body line1\nline2\nline3\n", $this->_mail['body']);
823
 
        $this->assertEquals("header line1\nline2\nline3\n", $this->_mail['headers']);
824
 
    }
825
 
}
826
 
 
827
 
class PlatformTestPhpVm extends GalleryPhpVm {
828
 
 
829
 
    function PlatformTestPhpVm($iniVal, &$test) {
830
 
        $this->_iniVal = $iniVal;
831
 
        $this->_test =& $test;
832
 
    }
833
 
 
834
 
    function ini_get($varname) {
835
 
        if ($varname == 'open_basedir') {
836
 
            return $this->_iniVal;
837
 
        }
838
 
        return parent::ini_get($varname);
839
 
    }
840
 
 
841
 
    function mail($to, $subject, $body, $additionalHeaders=null, $additionalParameters=null) {
842
 
        $this->_test->_mail = array('to' => $to,
843
 
                                    'subj' => $subject,
844
 
                                    'body' => $body,
845
 
                                    'headers' => $additionalHeaders,
846
 
                                    'params' => $additionalParameters);
847
 
        return true;
848
 
    }
849
 
}
850
 
 
851
 
GalleryCoreApi::requireOnce('modules/core/classes/GalleryPlatform/UnixPlatform.class');
852
 
class PlatformTestOpenBaseDirUnixPlatform extends UnixPlatform {
853
 
 
854
 
    function setRealpathData($realpathData) {
855
 
        $this->_realpathData = $realpathData;
856
 
    }
857
 
 
858
 
    function setCwd($path) {
859
 
        $this->_cwd = $path;
860
 
    }
861
 
 
862
 
    function realpath($path) {
863
 
        return isset($this->_realpathData[$path]) ? $this->_realpathData[$path] : false;
864
 
    }
865
 
 
866
 
    function getcwd() {
867
 
        return isset($this->_cwd) ? $this->_cwd : parent::getcwd();
868
 
    }
869
 
 
870
 
    function getDirectorySeparator() {
871
 
        return '/';
872
 
    }
873
 
}
874
 
 
875
 
GalleryCoreApi::requireOnce('modules/core/classes/GalleryPlatform/WinNtPlatform.class');
876
 
class PlatformTestOpenBaseDirWinNtPlatform extends WinNtPlatform {
877
 
 
878
 
    function setRealpathData($realpathData) {
879
 
        $this->_realpathData = $realpathData;
880
 
    }
881
 
 
882
 
    function setCwd($path) {
883
 
        $this->_cwd = $path;
884
 
    }
885
 
 
886
 
    function realpath($path) {
887
 
        return isset($this->_realpathData[$path]) ? $this->_realpathData[$path] : false;
888
 
    }
889
 
 
890
 
    function getcwd() {
891
 
        return isset($this->_cwd) ? $this->_cwd : parent::getcwd();
892
 
    }
893
 
 
894
 
    function getDirectorySeparator() {
895
 
        return '\\';
896
 
    }
897
 
}
898
 
?>