2014-10-22 19:33:36 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test;
|
|
|
|
|
|
|
|
use OC\Log;
|
2016-09-07 21:25:35 +03:00
|
|
|
use OCP\IConfig;
|
2014-10-22 19:33:36 +04:00
|
|
|
|
|
|
|
class NullLogger extends Log {
|
|
|
|
public function __construct($logger = null) {
|
|
|
|
//disable original constructor
|
|
|
|
}
|
|
|
|
|
2020-03-26 11:30:18 +03:00
|
|
|
public function log(int $level, string $message, array $context = []) {
|
2014-10-22 19:33:36 +04:00
|
|
|
//noop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 16:38:20 +03:00
|
|
|
class TempManagerTest extends \Test\TestCase {
|
2014-10-22 19:33:36 +04:00
|
|
|
|
2015-08-29 19:36:21 +03:00
|
|
|
protected $baseDir = null;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-11 00:59:50 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2015-08-29 19:36:21 +03:00
|
|
|
$this->baseDir = $this->getManager()->getTempBaseDir() . $this->getUniqueID('/oc_tmp_test');
|
2014-10-22 19:33:36 +04:00
|
|
|
if (!is_dir($this->baseDir)) {
|
|
|
|
mkdir($this->baseDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function tearDown(): void {
|
2014-10-22 19:33:36 +04:00
|
|
|
\OC_Helper::rmdirr($this->baseDir);
|
2015-08-29 19:36:21 +03:00
|
|
|
$this->baseDir = null;
|
2014-11-11 00:59:50 +03:00
|
|
|
parent::tearDown();
|
2014-10-22 19:33:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-26 00:16:48 +03:00
|
|
|
* @param \OCP\ILogger $logger
|
2015-08-29 19:36:21 +03:00
|
|
|
* @param \OCP\IConfig $config
|
2014-10-22 19:33:36 +04:00
|
|
|
* @return \OC\TempManager
|
|
|
|
*/
|
2015-08-29 19:36:21 +03:00
|
|
|
protected function getManager($logger = null, $config = null) {
|
2014-10-22 19:33:36 +04:00
|
|
|
if (!$logger) {
|
|
|
|
$logger = new NullLogger();
|
|
|
|
}
|
2015-08-29 19:36:21 +03:00
|
|
|
if (!$config) {
|
2016-09-07 21:25:35 +03:00
|
|
|
$config = $this->createMock(IConfig::class);
|
2015-08-29 19:42:20 +03:00
|
|
|
$config->method('getSystemValue')
|
|
|
|
->with('tempdirectory', null)
|
|
|
|
->willReturn('/tmp');
|
2015-08-29 19:36:21 +03:00
|
|
|
}
|
|
|
|
$manager = new \OC\TempManager($logger, $config);
|
|
|
|
if ($this->baseDir) {
|
|
|
|
$manager->overrideTempBaseDir($this->baseDir);
|
|
|
|
}
|
|
|
|
return $manager;
|
2014-10-22 19:33:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFile() {
|
|
|
|
$manager = $this->getManager();
|
2015-03-19 17:35:36 +03:00
|
|
|
$file = $manager->getTemporaryFile('txt');
|
2014-10-22 19:33:36 +04:00
|
|
|
$this->assertStringEndsWith('.txt', $file);
|
|
|
|
$this->assertTrue(is_file($file));
|
|
|
|
$this->assertTrue(is_writable($file));
|
|
|
|
|
|
|
|
file_put_contents($file, 'bar');
|
|
|
|
$this->assertEquals('bar', file_get_contents($file));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFolder() {
|
|
|
|
$manager = $this->getManager();
|
|
|
|
$folder = $manager->getTemporaryFolder();
|
|
|
|
$this->assertStringEndsWith('/', $folder);
|
|
|
|
$this->assertTrue(is_dir($folder));
|
|
|
|
$this->assertTrue(is_writable($folder));
|
|
|
|
|
|
|
|
file_put_contents($folder . 'foo.txt', 'bar');
|
|
|
|
$this->assertEquals('bar', file_get_contents($folder . 'foo.txt'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCleanFiles() {
|
|
|
|
$manager = $this->getManager();
|
2015-03-19 17:35:36 +03:00
|
|
|
$file1 = $manager->getTemporaryFile('txt');
|
|
|
|
$file2 = $manager->getTemporaryFile('txt');
|
2014-10-22 19:33:36 +04:00
|
|
|
$this->assertTrue(file_exists($file1));
|
|
|
|
$this->assertTrue(file_exists($file2));
|
|
|
|
|
|
|
|
$manager->clean();
|
|
|
|
|
|
|
|
$this->assertFalse(file_exists($file1));
|
|
|
|
$this->assertFalse(file_exists($file2));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCleanFolder() {
|
|
|
|
$manager = $this->getManager();
|
|
|
|
$folder1 = $manager->getTemporaryFolder();
|
|
|
|
$folder2 = $manager->getTemporaryFolder();
|
|
|
|
touch($folder1 . 'foo.txt');
|
|
|
|
touch($folder1 . 'bar.txt');
|
|
|
|
$this->assertTrue(file_exists($folder1));
|
|
|
|
$this->assertTrue(file_exists($folder2));
|
|
|
|
$this->assertTrue(file_exists($folder1 . 'foo.txt'));
|
|
|
|
$this->assertTrue(file_exists($folder1 . 'bar.txt'));
|
|
|
|
|
|
|
|
$manager->clean();
|
|
|
|
|
|
|
|
$this->assertFalse(file_exists($folder1));
|
|
|
|
$this->assertFalse(file_exists($folder2));
|
|
|
|
$this->assertFalse(file_exists($folder1 . 'foo.txt'));
|
|
|
|
$this->assertFalse(file_exists($folder1 . 'bar.txt'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCleanOld() {
|
|
|
|
$manager = $this->getManager();
|
2015-03-19 17:35:36 +03:00
|
|
|
$oldFile = $manager->getTemporaryFile('txt');
|
|
|
|
$newFile = $manager->getTemporaryFile('txt');
|
2014-10-22 19:33:36 +04:00
|
|
|
$folder = $manager->getTemporaryFolder();
|
|
|
|
$nonOcFile = $this->baseDir . '/foo.txt';
|
|
|
|
file_put_contents($nonOcFile, 'bar');
|
|
|
|
|
|
|
|
$past = time() - 2 * 3600;
|
|
|
|
touch($oldFile, $past);
|
|
|
|
touch($folder, $past);
|
|
|
|
touch($nonOcFile, $past);
|
|
|
|
|
|
|
|
$manager2 = $this->getManager();
|
|
|
|
$manager2->cleanOld();
|
|
|
|
$this->assertFalse(file_exists($oldFile));
|
|
|
|
$this->assertFalse(file_exists($folder));
|
|
|
|
$this->assertTrue(file_exists($nonOcFile));
|
|
|
|
$this->assertTrue(file_exists($newFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogCantCreateFile() {
|
2016-06-07 17:20:07 +03:00
|
|
|
$this->markTestSkipped('TODO: Disable because fails on drone');
|
2014-11-05 18:47:27 +03:00
|
|
|
|
2016-09-07 21:25:35 +03:00
|
|
|
$logger = $this->createMock(NullLogger::class);
|
2014-10-22 19:33:36 +04:00
|
|
|
$manager = $this->getManager($logger);
|
|
|
|
chmod($this->baseDir, 0500);
|
|
|
|
$logger->expects($this->once())
|
|
|
|
->method('warning')
|
|
|
|
->with($this->stringContains('Can not create a temporary file in directory'));
|
2015-03-19 17:35:36 +03:00
|
|
|
$this->assertFalse($manager->getTemporaryFile('txt'));
|
2014-10-22 19:33:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogCantCreateFolder() {
|
2016-06-07 17:20:07 +03:00
|
|
|
$this->markTestSkipped('TODO: Disable because fails on drone');
|
2014-11-05 18:47:27 +03:00
|
|
|
|
2016-09-07 21:25:35 +03:00
|
|
|
$logger = $this->createMock(NullLogger::class);
|
2014-10-22 19:33:36 +04:00
|
|
|
$manager = $this->getManager($logger);
|
|
|
|
chmod($this->baseDir, 0500);
|
|
|
|
$logger->expects($this->once())
|
|
|
|
->method('warning')
|
|
|
|
->with($this->stringContains('Can not create a temporary folder in directory'));
|
|
|
|
$this->assertFalse($manager->getTemporaryFolder());
|
|
|
|
}
|
2015-03-27 01:14:24 +03:00
|
|
|
|
2015-04-23 15:29:15 +03:00
|
|
|
public function testBuildFileNameWithPostfix() {
|
2016-09-07 21:25:35 +03:00
|
|
|
$logger = $this->createMock(NullLogger::class);
|
2015-06-03 13:03:02 +03:00
|
|
|
$tmpManager = self::invokePrivate(
|
2015-03-27 01:14:24 +03:00
|
|
|
$this->getManager($logger),
|
2015-04-23 15:29:15 +03:00
|
|
|
'buildFileNameWithSuffix',
|
|
|
|
['/tmp/myTemporaryFile', 'postfix']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('/tmp/myTemporaryFile-.postfix', $tmpManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBuildFileNameWithoutPostfix() {
|
2016-09-07 21:25:35 +03:00
|
|
|
$logger = $this->createMock(NullLogger::class);
|
2015-06-03 13:03:02 +03:00
|
|
|
$tmpManager = self::invokePrivate(
|
2015-04-23 15:29:15 +03:00
|
|
|
$this->getManager($logger),
|
|
|
|
'buildFileNameWithSuffix',
|
|
|
|
['/tmp/myTemporaryFile', '']
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('/tmp/myTemporaryFile', $tmpManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBuildFileNameWithSuffixPathTraversal() {
|
2016-09-07 21:25:35 +03:00
|
|
|
$logger = $this->createMock(NullLogger::class);
|
2015-06-03 13:03:02 +03:00
|
|
|
$tmpManager = self::invokePrivate(
|
2015-04-23 15:29:15 +03:00
|
|
|
$this->getManager($logger),
|
|
|
|
'buildFileNameWithSuffix',
|
|
|
|
['foo', '../Traversal\\../FileName']
|
2015-03-27 01:14:24 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertStringEndsNotWith('./Traversal\\../FileName', $tmpManager);
|
|
|
|
$this->assertStringEndsWith('.Traversal..FileName', $tmpManager);
|
|
|
|
}
|
2015-08-29 19:42:20 +03:00
|
|
|
|
|
|
|
public function testGetTempBaseDirFromConfig() {
|
|
|
|
$dir = $this->getManager()->getTemporaryFolder();
|
|
|
|
|
2016-09-07 21:25:35 +03:00
|
|
|
$config = $this->createMock(IConfig::class);
|
2015-08-29 19:42:20 +03:00
|
|
|
$config->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('tempdirectory', null)
|
|
|
|
->willReturn($dir);
|
|
|
|
|
|
|
|
$this->baseDir = null; // prevent override
|
|
|
|
$tmpManager = $this->getManager(null, $config);
|
|
|
|
|
|
|
|
$this->assertEquals($dir, $tmpManager->getTempBaseDir());
|
|
|
|
}
|
2014-10-22 19:33:36 +04:00
|
|
|
}
|