2013-07-02 20:22:49 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 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\Files\Storage\Wrapper;
|
|
|
|
|
|
|
|
//ensure the constants are loaded
|
2015-12-02 17:28:49 +03:00
|
|
|
use OC\Files\Cache\CacheEntry;
|
|
|
|
|
2013-07-02 20:22:49 +04:00
|
|
|
\OC::$loader->load('\OC\Files\Filesystem');
|
|
|
|
|
2015-11-20 13:27:11 +03:00
|
|
|
/**
|
|
|
|
* Class Quota
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
|
|
|
* @package Test\Files\Storage\Wrapper
|
|
|
|
*/
|
2013-07-02 20:22:49 +04:00
|
|
|
class Quota extends \Test\Files\Storage\Storage {
|
|
|
|
/**
|
|
|
|
* @var string tmpDir
|
|
|
|
*/
|
|
|
|
private $tmpDir;
|
|
|
|
|
2014-11-07 17:23:15 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2015-12-18 13:19:53 +03:00
|
|
|
$this->tmpDir = \OC::$server->getTempManager()->getTemporaryFolder();
|
2013-07-02 20:22:49 +04:00
|
|
|
$storage = new \OC\Files\Storage\Local(array('datadir' => $this->tmpDir));
|
|
|
|
$this->instance = new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => 10000000));
|
|
|
|
}
|
|
|
|
|
2014-11-07 17:23:15 +03:00
|
|
|
protected function tearDown() {
|
2013-07-02 20:22:49 +04:00
|
|
|
\OC_Helper::rmdirr($this->tmpDir);
|
2014-11-07 17:23:15 +03:00
|
|
|
parent::tearDown();
|
2013-07-02 20:22:49 +04:00
|
|
|
}
|
|
|
|
|
2014-02-19 12:31:54 +04:00
|
|
|
/**
|
|
|
|
* @param integer $limit
|
|
|
|
*/
|
2013-07-02 20:22:49 +04:00
|
|
|
protected function getLimitedStorage($limit) {
|
|
|
|
$storage = new \OC\Files\Storage\Local(array('datadir' => $this->tmpDir));
|
2015-05-28 19:31:20 +03:00
|
|
|
$storage->mkdir('files');
|
2013-07-02 20:22:49 +04:00
|
|
|
$storage->getScanner()->scan('');
|
|
|
|
return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $limit));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFilePutContentsNotEnoughSpace() {
|
|
|
|
$instance = $this->getLimitedStorage(3);
|
2015-05-28 19:31:20 +03:00
|
|
|
$this->assertFalse($instance->file_put_contents('files/foo', 'foobar'));
|
2013-07-02 20:22:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCopyNotEnoughSpace() {
|
|
|
|
$instance = $this->getLimitedStorage(9);
|
2015-05-28 19:31:20 +03:00
|
|
|
$this->assertEquals(6, $instance->file_put_contents('files/foo', 'foobar'));
|
2013-07-02 20:22:49 +04:00
|
|
|
$instance->getScanner()->scan('');
|
2015-05-28 19:31:20 +03:00
|
|
|
$this->assertFalse($instance->copy('files/foo', 'files/bar'));
|
2013-07-02 20:22:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFreeSpace() {
|
|
|
|
$instance = $this->getLimitedStorage(9);
|
|
|
|
$this->assertEquals(9, $instance->free_space(''));
|
|
|
|
}
|
|
|
|
|
2014-03-07 14:25:29 +04:00
|
|
|
public function testFreeSpaceWithUsedSpace() {
|
|
|
|
$instance = $this->getLimitedStorage(9);
|
|
|
|
$instance->getCache()->put(
|
2015-03-30 18:29:05 +03:00
|
|
|
'', array('size' => 3)
|
2014-03-07 14:25:29 +04:00
|
|
|
);
|
|
|
|
$this->assertEquals(6, $instance->free_space(''));
|
|
|
|
}
|
|
|
|
|
2014-03-19 22:07:11 +04:00
|
|
|
public function testFreeSpaceWithUnknownDiskSpace() {
|
|
|
|
$storage = $this->getMock(
|
|
|
|
'\OC\Files\Storage\Local',
|
|
|
|
array('free_space'),
|
|
|
|
array(array('datadir' => $this->tmpDir))
|
|
|
|
);
|
|
|
|
$storage->expects($this->any())
|
|
|
|
->method('free_space')
|
|
|
|
->will($this->returnValue(-2));
|
|
|
|
$storage->getScanner()->scan('');
|
|
|
|
|
|
|
|
$instance = new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => 9));
|
|
|
|
$instance->getCache()->put(
|
2015-03-30 18:29:05 +03:00
|
|
|
'', array('size' => 3)
|
2014-03-19 22:07:11 +04:00
|
|
|
);
|
|
|
|
$this->assertEquals(6, $instance->free_space(''));
|
|
|
|
}
|
|
|
|
|
2014-03-07 14:25:29 +04:00
|
|
|
public function testFreeSpaceWithUsedSpaceAndEncryption() {
|
|
|
|
$instance = $this->getLimitedStorage(9);
|
|
|
|
$instance->getCache()->put(
|
2015-03-30 18:29:05 +03:00
|
|
|
'', array('size' => 7)
|
2014-03-07 14:25:29 +04:00
|
|
|
);
|
2015-04-07 15:30:01 +03:00
|
|
|
$this->assertEquals(2, $instance->free_space(''));
|
2014-03-07 14:25:29 +04:00
|
|
|
}
|
|
|
|
|
2013-07-02 20:22:49 +04:00
|
|
|
public function testFWriteNotEnoughSpace() {
|
|
|
|
$instance = $this->getLimitedStorage(9);
|
2015-05-28 19:31:20 +03:00
|
|
|
$stream = $instance->fopen('files/foo', 'w+');
|
2013-07-02 20:22:49 +04:00
|
|
|
$this->assertEquals(6, fwrite($stream, 'foobar'));
|
|
|
|
$this->assertEquals(3, fwrite($stream, 'qwerty'));
|
|
|
|
fclose($stream);
|
2015-05-28 19:31:20 +03:00
|
|
|
$this->assertEquals('foobarqwe', $instance->file_get_contents('files/foo'));
|
2013-07-02 20:22:49 +04:00
|
|
|
}
|
2013-10-25 14:33:16 +04:00
|
|
|
|
2015-02-23 14:31:22 +03:00
|
|
|
public function testStreamCopyWithEnoughSpace() {
|
|
|
|
$instance = $this->getLimitedStorage(16);
|
|
|
|
$inputStream = fopen('data://text/plain,foobarqwerty', 'r');
|
2015-05-28 19:31:20 +03:00
|
|
|
$outputStream = $instance->fopen('files/foo', 'w+');
|
2015-02-23 14:31:22 +03:00
|
|
|
list($count, $result) = \OC_Helper::streamCopy($inputStream, $outputStream);
|
|
|
|
$this->assertEquals(12, $count);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
fclose($inputStream);
|
|
|
|
fclose($outputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testStreamCopyNotEnoughSpace() {
|
|
|
|
$instance = $this->getLimitedStorage(9);
|
|
|
|
$inputStream = fopen('data://text/plain,foobarqwerty', 'r');
|
2015-05-28 19:31:20 +03:00
|
|
|
$outputStream = $instance->fopen('files/foo', 'w+');
|
2015-02-23 14:31:22 +03:00
|
|
|
list($count, $result) = \OC_Helper::streamCopy($inputStream, $outputStream);
|
|
|
|
$this->assertEquals(9, $count);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
fclose($inputStream);
|
|
|
|
fclose($outputStream);
|
|
|
|
}
|
|
|
|
|
2014-02-04 19:05:12 +04:00
|
|
|
public function testReturnFalseWhenFopenFailed() {
|
2014-01-19 21:49:51 +04:00
|
|
|
$failStorage = $this->getMock(
|
|
|
|
'\OC\Files\Storage\Local',
|
|
|
|
array('fopen'),
|
|
|
|
array(array('datadir' => $this->tmpDir)));
|
|
|
|
$failStorage->expects($this->any())
|
|
|
|
->method('fopen')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$instance = new \OC\Files\Storage\Wrapper\Quota(array('storage' => $failStorage, 'quota' => 1000));
|
|
|
|
|
|
|
|
$this->assertFalse($instance->fopen('failedfopen', 'r'));
|
|
|
|
}
|
|
|
|
|
2014-02-04 19:05:12 +04:00
|
|
|
public function testReturnRegularStreamOnRead() {
|
2013-10-25 14:33:16 +04:00
|
|
|
$instance = $this->getLimitedStorage(9);
|
|
|
|
|
|
|
|
// create test file first
|
2015-05-28 19:31:20 +03:00
|
|
|
$stream = $instance->fopen('files/foo', 'w+');
|
2013-10-25 14:33:16 +04:00
|
|
|
fwrite($stream, 'blablacontent');
|
|
|
|
fclose($stream);
|
|
|
|
|
2015-05-28 19:31:20 +03:00
|
|
|
$stream = $instance->fopen('files/foo', 'r');
|
2013-10-25 14:33:16 +04:00
|
|
|
$meta = stream_get_meta_data($stream);
|
|
|
|
$this->assertEquals('plainfile', $meta['wrapper_type']);
|
|
|
|
fclose($stream);
|
2014-01-19 21:49:51 +04:00
|
|
|
|
2015-05-28 19:31:20 +03:00
|
|
|
$stream = $instance->fopen('files/foo', 'rb');
|
|
|
|
$meta = stream_get_meta_data($stream);
|
|
|
|
$this->assertEquals('plainfile', $meta['wrapper_type']);
|
|
|
|
fclose($stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReturnRegularStreamWhenOutsideFiles() {
|
|
|
|
$instance = $this->getLimitedStorage(9);
|
|
|
|
$instance->mkdir('files_other');
|
|
|
|
|
|
|
|
// create test file first
|
|
|
|
$stream = $instance->fopen('files_other/foo', 'w+');
|
2014-01-19 21:49:51 +04:00
|
|
|
$meta = stream_get_meta_data($stream);
|
|
|
|
$this->assertEquals('plainfile', $meta['wrapper_type']);
|
|
|
|
fclose($stream);
|
2013-10-25 14:33:16 +04:00
|
|
|
}
|
|
|
|
|
2014-02-04 19:05:12 +04:00
|
|
|
public function testReturnQuotaStreamOnWrite() {
|
2013-10-25 14:33:16 +04:00
|
|
|
$instance = $this->getLimitedStorage(9);
|
2015-05-28 19:31:20 +03:00
|
|
|
$stream = $instance->fopen('files/foo', 'w+');
|
2013-10-25 14:33:16 +04:00
|
|
|
$meta = stream_get_meta_data($stream);
|
2014-12-19 02:54:33 +03:00
|
|
|
$expected_type = defined('HHVM_VERSION') ? 'File' : 'user-space';
|
|
|
|
$this->assertEquals($expected_type, $meta['wrapper_type']);
|
2013-10-25 14:33:16 +04:00
|
|
|
fclose($stream);
|
|
|
|
}
|
2014-02-04 19:05:12 +04:00
|
|
|
|
|
|
|
public function testSpaceRoot() {
|
|
|
|
$storage = $this->getMockBuilder('\OC\Files\Storage\Local')->disableOriginalConstructor()->getMock();
|
|
|
|
$cache = $this->getMockBuilder('\OC\Files\Cache\Cache')->disableOriginalConstructor()->getMock();
|
|
|
|
$storage->expects($this->once())
|
|
|
|
->method('getCache')
|
|
|
|
->will($this->returnValue($cache));
|
|
|
|
$storage->expects($this->once())
|
|
|
|
->method('free_space')
|
|
|
|
->will($this->returnValue(2048));
|
|
|
|
$cache->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('files')
|
2015-12-02 17:28:49 +03:00
|
|
|
->will($this->returnValue(new CacheEntry(['size' => 50])));
|
2014-02-04 19:05:12 +04:00
|
|
|
|
|
|
|
$instance = new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => 1024, 'root' => 'files'));
|
|
|
|
|
|
|
|
$this->assertEquals(1024 - 50, $instance->free_space(''));
|
|
|
|
}
|
2014-05-29 15:45:50 +04:00
|
|
|
|
|
|
|
public function testInstanceOfStorageWrapper() {
|
|
|
|
$this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Local'));
|
|
|
|
$this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Wrapper\Wrapper'));
|
|
|
|
$this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota'));
|
|
|
|
}
|
2013-07-02 20:22:49 +04:00
|
|
|
}
|