aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/appframework/middleware/security/CORSMiddlewareTest.php75
-rw-r--r--tests/lib/encryption/keys/storage.php44
-rw-r--r--tests/lib/encryption/managertest.php6
-rw-r--r--tests/lib/encryption/utiltest.php20
-rw-r--r--tests/lib/files/cache/updater.php26
-rw-r--r--tests/lib/files/storage/wrapper/encryption.php49
-rw-r--r--tests/lib/files/storage/wrapper/quota.php34
-rw-r--r--tests/lib/files/stream/dummyencryptionwrapper.php37
-rw-r--r--tests/lib/files/stream/encryption.php50
-rw-r--r--tests/lib/httphelper.php75
-rw-r--r--tests/lib/public/util.php47
-rw-r--r--tests/settings/controller/CheckSetupControllerTest.php8
12 files changed, 425 insertions, 46 deletions
diff --git a/tests/lib/appframework/middleware/security/CORSMiddlewareTest.php b/tests/lib/appframework/middleware/security/CORSMiddlewareTest.php
index a4f3137cb11..92ea5450ab9 100644
--- a/tests/lib/appframework/middleware/security/CORSMiddlewareTest.php
+++ b/tests/lib/appframework/middleware/security/CORSMiddlewareTest.php
@@ -21,10 +21,12 @@ use OCP\AppFramework\Http\Response;
class CORSMiddlewareTest extends \Test\TestCase {
private $reflector;
+ private $session;
protected function setUp() {
parent::setUp();
$this->reflector = new ControllerMethodReflector();
+ $this->session = $this->getMock('\OCP\IUserSession');
}
/**
@@ -41,7 +43,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
$this->getMock('\OCP\IConfig')
);
$this->reflector->reflect($this, __FUNCTION__);
- $middleware = new CORSMiddleware($request, $this->reflector);
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
$response = $middleware->afterController($this, __FUNCTION__, new Response());
$headers = $response->getHeaders();
@@ -59,7 +61,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
$this->getMock('\OCP\Security\ISecureRandom'),
$this->getMock('\OCP\IConfig')
);
- $middleware = new CORSMiddleware($request, $this->reflector);
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
$response = $middleware->afterController($this, __FUNCTION__, new Response());
$headers = $response->getHeaders();
@@ -77,7 +79,7 @@ class CORSMiddlewareTest extends \Test\TestCase {
$this->getMock('\OCP\IConfig')
);
$this->reflector->reflect($this, __FUNCTION__);
- $middleware = new CORSMiddleware($request, $this->reflector);
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
$response = $middleware->afterController($this, __FUNCTION__, new Response());
$headers = $response->getHeaders();
@@ -100,11 +102,76 @@ class CORSMiddlewareTest extends \Test\TestCase {
$this->getMock('\OCP\IConfig')
);
$this->reflector->reflect($this, __FUNCTION__);
- $middleware = new CORSMiddleware($request, $this->reflector);
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
$response = new Response();
$response->addHeader('AcCess-control-Allow-Credentials ', 'TRUE');
$middleware->afterController($this, __FUNCTION__, $response);
}
+ /**
+ * @CORS
+ * @PublicPage
+ */
+ public function testNoCORSShouldAllowCookieAuth() {
+ $request = new Request(
+ [],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->reflector->reflect($this, __FUNCTION__);
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
+
+ $middleware->beforeController($this, __FUNCTION__, new Response());
+ }
+
+ /**
+ * @CORS
+ */
+ public function testCORSShouldRelogin() {
+ $request = new Request(
+ ['server' => [
+ 'PHP_AUTH_USER' => 'user',
+ 'PHP_AUTH_PW' => 'pass'
+ ]],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->session->expects($this->once())
+ ->method('logout');
+ $this->session->expects($this->once())
+ ->method('login')
+ ->with($this->equalTo('user'), $this->equalTo('pass'))
+ ->will($this->returnValue(true));
+ $this->reflector->reflect($this, __FUNCTION__);
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
+
+ $middleware->beforeController($this, __FUNCTION__, new Response());
+ }
+
+ /**
+ * @CORS
+ * @expectedException \OC\AppFramework\Middleware\Security\SecurityException
+ */
+ public function testCORSShouldNotAllowCookieAuth() {
+ $request = new Request(
+ ['server' => [
+ 'PHP_AUTH_USER' => 'user',
+ 'PHP_AUTH_PW' => 'pass'
+ ]],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->session->expects($this->once())
+ ->method('logout');
+ $this->session->expects($this->once())
+ ->method('login')
+ ->with($this->equalTo('user'), $this->equalTo('pass'))
+ ->will($this->returnValue(false));
+ $this->reflector->reflect($this, __FUNCTION__);
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
+
+ $middleware->beforeController($this, __FUNCTION__, new Response());
+ }
+
}
diff --git a/tests/lib/encryption/keys/storage.php b/tests/lib/encryption/keys/storage.php
index e67103fb6aa..45cd272cca0 100644
--- a/tests/lib/encryption/keys/storage.php
+++ b/tests/lib/encryption/keys/storage.php
@@ -76,7 +76,9 @@ class StorageTest extends TestCase {
$this->util->expects($this->any())
->method('getUidAndFilename')
->willReturn(array('user1', '/files/foo.txt'));
- $this->util->expects($this->any())
+ // we need to strip away the part file extension in order to reuse a
+ // existing key if it exists, otherwise versions will break
+ $this->util->expects($this->once())
->method('stripPartialFileExtension')
->willReturnArgument(0);
$this->util->expects($this->any())
@@ -276,7 +278,7 @@ class StorageTest extends TestCase {
/**
* @dataProvider dataProviderCopyRename
*/
- public function testRenameKeys($source, $target, $systemWideMount, $expectedSource, $expectedTarget) {
+ public function testRenameKeys($source, $target, $systemWideMountSource, $systemWideMountTarget, $expectedSource, $expectedTarget) {
$this->view->expects($this->any())
->method('file_exists')
->willReturn(true);
@@ -294,7 +296,12 @@ class StorageTest extends TestCase {
->will($this->returnCallback(array($this, 'getUidAndFilenameCallback')));
$this->util->expects($this->any())
->method('isSystemWideMountPoint')
- ->willReturn($systemWideMount);
+ ->willReturnCallback(function($path, $owner) use ($systemWideMountSource, $systemWideMountTarget) {
+ if(strpos($path, 'source.txt') !== false) {
+ return $systemWideMountSource;
+ }
+ return $systemWideMountTarget;
+ });
$this->storage->renameKeys($source, $target);
}
@@ -302,7 +309,7 @@ class StorageTest extends TestCase {
/**
* @dataProvider dataProviderCopyRename
*/
- public function testCopyKeys($source, $target, $systemWideMount, $expectedSource, $expectedTarget) {
+ public function testCopyKeys($source, $target, $systemWideMountSource, $systemWideMountTarget , $expectedSource, $expectedTarget) {
$this->view->expects($this->any())
->method('file_exists')
->willReturn(true);
@@ -320,7 +327,12 @@ class StorageTest extends TestCase {
->will($this->returnCallback(array($this, 'getUidAndFilenameCallback')));
$this->util->expects($this->any())
->method('isSystemWideMountPoint')
- ->willReturn($systemWideMount);
+ ->willReturnCallback(function($path, $owner) use ($systemWideMountSource, $systemWideMountTarget) {
+ if(strpos($path, 'source.txt') !== false) {
+ return $systemWideMountSource;
+ }
+ return $systemWideMountTarget;
+ });
$this->storage->copyKeys($source, $target);
}
@@ -336,14 +348,20 @@ class StorageTest extends TestCase {
public function dataProviderCopyRename() {
return array(
- array('/user1/files/foo.txt', '/user1/files/bar.txt', false,
- '/user1/files_encryption/keys/files/foo.txt/', '/user1/files_encryption/keys/files/bar.txt/'),
- array('/user1/files/foo/foo.txt', '/user1/files/bar.txt', false,
- '/user1/files_encryption/keys/files/foo/foo.txt/', '/user1/files_encryption/keys/files/bar.txt/'),
- array('/user1/files/foo.txt', '/user1/files/foo/bar.txt', false,
- '/user1/files_encryption/keys/files/foo.txt/', '/user1/files_encryption/keys/files/foo/bar.txt/'),
- array('/user1/files/foo.txt', '/user1/files/foo/bar.txt', true,
- '/files_encryption/keys/files/foo.txt/', '/files_encryption/keys/files/foo/bar.txt/'),
+ array('/user1/files/source.txt', '/user1/files/target.txt', false, false,
+ '/user1/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'),
+ array('/user1/files/foo/source.txt', '/user1/files/target.txt', false, false,
+ '/user1/files_encryption/keys/files/foo/source.txt/', '/user1/files_encryption/keys/files/target.txt/'),
+ array('/user1/files/source.txt', '/user1/files/foo/target.txt', false, false,
+ '/user1/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/foo/target.txt/'),
+ array('/user1/files/source.txt', '/user1/files/foo/target.txt', true, true,
+ '/files_encryption/keys/files/source.txt/', '/files_encryption/keys/files/foo/target.txt/'),
+ array('/user1/files/source.txt', '/user1/files/target.txt', false, true,
+ '/user1/files_encryption/keys/files/source.txt/', '/files_encryption/keys/files/target.txt/'),
+ array('/user1/files/source.txt', '/user1/files/target.txt', true, false,
+ '/files_encryption/keys/files/source.txt/', '/user1/files_encryption/keys/files/target.txt/'),
+
+
);
}
diff --git a/tests/lib/encryption/managertest.php b/tests/lib/encryption/managertest.php
index faca6474504..3b1e07ffd69 100644
--- a/tests/lib/encryption/managertest.php
+++ b/tests/lib/encryption/managertest.php
@@ -16,11 +16,15 @@ class ManagerTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject */
private $logger;
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ private $l10n;
+
public function setUp() {
parent::setUp();
$this->config = $this->getMock('\OCP\IConfig');
$this->logger = $this->getMock('\OCP\ILogger');
- $this->manager = new Manager($this->config, $this->logger);
+ $this->l10n = $this->getMock('\OCP\Il10n');
+ $this->manager = new Manager($this->config, $this->logger, $this->l10n);
}
public function testManagerIsDisabled() {
diff --git a/tests/lib/encryption/utiltest.php b/tests/lib/encryption/utiltest.php
index 0154fa30f7d..d3a4e211daa 100644
--- a/tests/lib/encryption/utiltest.php
+++ b/tests/lib/encryption/utiltest.php
@@ -174,4 +174,24 @@ class UtilTest extends TestCase {
);
}
+ /**
+ * @dataProvider dataTestStripPartialFileExtension
+ *
+ * @param string $path
+ * @param string $expected
+ */
+ public function testStripPartialFileExtension($path, $expected) {
+ $this->assertSame($expected,
+ $this->util->stripPartialFileExtension($path));
+ }
+
+ public function dataTestStripPartialFileExtension() {
+ return array(
+ array('/foo/test.txt', '/foo/test.txt'),
+ array('/foo/test.txt.part', '/foo/test.txt'),
+ array('/foo/test.txt.ocTransferId7567846853.part', '/foo/test.txt'),
+ array('/foo/test.txt.ocTransferId7567.part', '/foo/test.txt'),
+ );
+ }
+
}
diff --git a/tests/lib/files/cache/updater.php b/tests/lib/files/cache/updater.php
index 726ee360479..ea75c8dcd72 100644
--- a/tests/lib/files/cache/updater.php
+++ b/tests/lib/files/cache/updater.php
@@ -216,27 +216,38 @@ class Updater extends \Test\TestCase {
$this->storage->getScanner()->scan('');
+ $this->assertTrue($this->cache->inCache('foo'));
$this->assertTrue($this->cache->inCache('foo/foo.txt'));
$this->assertTrue($this->cache->inCache('foo/bar.txt'));
+ $this->assertTrue($this->cache->inCache('foo/bar'));
$this->assertTrue($this->cache->inCache('foo/bar/bar.txt'));
$cached = [];
+ $cached[] = $this->cache->get('foo');
$cached[] = $this->cache->get('foo/foo.txt');
$cached[] = $this->cache->get('foo/bar.txt');
+ $cached[] = $this->cache->get('foo/bar');
$cached[] = $this->cache->get('foo/bar/bar.txt');
- $this->view->rename('/foo', '/bar/foo');
+ // add extension to trigger the possible mimetype change
+ $this->view->rename('/foo', '/bar/foo.b');
+ $this->assertFalse($this->cache->inCache('foo'));
$this->assertFalse($this->cache->inCache('foo/foo.txt'));
$this->assertFalse($this->cache->inCache('foo/bar.txt'));
+ $this->assertFalse($this->cache->inCache('foo/bar'));
$this->assertFalse($this->cache->inCache('foo/bar/bar.txt'));
- $this->assertTrue($cache2->inCache('foo/foo.txt'));
- $this->assertTrue($cache2->inCache('foo/bar.txt'));
- $this->assertTrue($cache2->inCache('foo/bar/bar.txt'));
+ $this->assertTrue($cache2->inCache('foo.b'));
+ $this->assertTrue($cache2->inCache('foo.b/foo.txt'));
+ $this->assertTrue($cache2->inCache('foo.b/bar.txt'));
+ $this->assertTrue($cache2->inCache('foo.b/bar'));
+ $this->assertTrue($cache2->inCache('foo.b/bar/bar.txt'));
$cachedTarget = [];
- $cachedTarget[] = $cache2->get('foo/foo.txt');
- $cachedTarget[] = $cache2->get('foo/bar.txt');
- $cachedTarget[] = $cache2->get('foo/bar/bar.txt');
+ $cachedTarget[] = $cache2->get('foo.b');
+ $cachedTarget[] = $cache2->get('foo.b/foo.txt');
+ $cachedTarget[] = $cache2->get('foo.b/bar.txt');
+ $cachedTarget[] = $cache2->get('foo.b/bar');
+ $cachedTarget[] = $cache2->get('foo.b/bar/bar.txt');
foreach ($cached as $i => $old) {
$new = $cachedTarget[$i];
@@ -244,6 +255,7 @@ class Updater extends \Test\TestCase {
$this->assertEquals($old['size'], $new['size']);
$this->assertEquals($old['etag'], $new['etag']);
$this->assertEquals($old['fileid'], $new['fileid']);
+ $this->assertEquals($old['mimetype'], $new['mimetype']);
}
}
}
diff --git a/tests/lib/files/storage/wrapper/encryption.php b/tests/lib/files/storage/wrapper/encryption.php
index 39af648cb75..d286978d926 100644
--- a/tests/lib/files/storage/wrapper/encryption.php
+++ b/tests/lib/files/storage/wrapper/encryption.php
@@ -63,6 +63,11 @@ class Encryption extends \Test\Files\Storage\Storage {
*/
private $mount;
+ /**
+ * @var \OC\Files\Mount\Manager | \PHPUnit_Framework_MockObject_MockObject
+ */
+ private $mountManager;
+
/** @var integer dummy unencrypted size */
private $dummySize = -1;
@@ -86,7 +91,7 @@ class Encryption extends \Test\Files\Storage\Storage {
->disableOriginalConstructor()
->getMock();
- $this->util = $this->getMock('\OC\Encryption\Util', ['getUidAndFilename', 'isFile'], [new View(), new \OC\User\Manager(), $groupManager, $config]);
+ $this->util = $this->getMock('\OC\Encryption\Util', ['getUidAndFilename', 'isFile', 'isExcluded'], [new View(), new \OC\User\Manager(), $groupManager, $config]);
$this->util->expects($this->any())
->method('getUidAndFilename')
->willReturnCallback(function ($path) {
@@ -119,7 +124,10 @@ class Encryption extends \Test\Files\Storage\Storage {
->disableOriginalConstructor()->getMock();
$this->cache->expects($this->any())
->method('get')
- ->willReturn(['encrypted' => false]);
+ ->willReturnCallback(function($path) {return ['encrypted' => false, 'path' => $path];});
+
+ $this->mountManager = $this->getMockBuilder('\OC\Files\Mount\Manager')
+ ->disableOriginalConstructor()->getMock();
$this->instance = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
->setConstructorArgs(
@@ -130,7 +138,7 @@ class Encryption extends \Test\Files\Storage\Storage {
'mountPoint' => '/',
'mount' => $this->mount
],
- $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update
+ $this->encryptionManager, $this->util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager
]
)
->setMethods(['getMetaData', 'getCache', 'getEncryptionModule'])
@@ -138,7 +146,9 @@ class Encryption extends \Test\Files\Storage\Storage {
$this->instance->expects($this->any())
->method('getMetaData')
- ->willReturn(['encrypted' => true, 'size' => $this->dummySize]);
+ ->willReturnCallback(function ($path) {
+ return ['encrypted' => true, 'size' => $this->dummySize, 'path' => $path];
+ });
$this->instance->expects($this->any())
->method('getCache')
@@ -232,6 +242,8 @@ class Encryption extends \Test\Files\Storage\Storage {
}
$this->util->expects($this->any())
->method('isFile')->willReturn(true);
+ $this->util->expects($this->any())
+ ->method('isExcluded')->willReturn(false);
$this->encryptionManager->expects($this->once())
->method('isEnabled')->willReturn($encryptionEnabled);
if ($shouldUpdate) {
@@ -324,4 +336,33 @@ class Encryption extends \Test\Files\Storage\Storage {
array('/file.txt', false, false, false),
);
}
+
+ /**
+ * @dataProvider dataTestCopyKeys
+ *
+ * @param boolean $excluded
+ * @param boolean $expected
+ */
+ public function testCopyKeys($excluded, $expected) {
+ $this->util->expects($this->once())
+ ->method('isExcluded')
+ ->willReturn($excluded);
+
+ if ($excluded) {
+ $this->keyStore->expects($this->never())->method('copyKeys');
+ } else {
+ $this->keyStore->expects($this->once())->method('copyKeys')->willReturn(true);
+ }
+
+ $this->assertSame($expected,
+ \Test_Helper::invokePrivate($this->instance, 'copyKeys', ['/source', '/target'])
+ );
+ }
+
+ public function dataTestCopyKeys() {
+ return array(
+ array(true, false),
+ array(false, true),
+ );
+ }
}
diff --git a/tests/lib/files/storage/wrapper/quota.php b/tests/lib/files/storage/wrapper/quota.php
index a5828296be9..441f3a39d32 100644
--- a/tests/lib/files/storage/wrapper/quota.php
+++ b/tests/lib/files/storage/wrapper/quota.php
@@ -35,20 +35,21 @@ class Quota extends \Test\Files\Storage\Storage {
*/
protected function getLimitedStorage($limit) {
$storage = new \OC\Files\Storage\Local(array('datadir' => $this->tmpDir));
+ $storage->mkdir('files');
$storage->getScanner()->scan('');
return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $limit));
}
public function testFilePutContentsNotEnoughSpace() {
$instance = $this->getLimitedStorage(3);
- $this->assertFalse($instance->file_put_contents('foo', 'foobar'));
+ $this->assertFalse($instance->file_put_contents('files/foo', 'foobar'));
}
public function testCopyNotEnoughSpace() {
$instance = $this->getLimitedStorage(9);
- $this->assertEquals(6, $instance->file_put_contents('foo', 'foobar'));
+ $this->assertEquals(6, $instance->file_put_contents('files/foo', 'foobar'));
$instance->getScanner()->scan('');
- $this->assertFalse($instance->copy('foo', 'bar'));
+ $this->assertFalse($instance->copy('files/foo', 'files/bar'));
}
public function testFreeSpace() {
@@ -92,17 +93,17 @@ class Quota extends \Test\Files\Storage\Storage {
public function testFWriteNotEnoughSpace() {
$instance = $this->getLimitedStorage(9);
- $stream = $instance->fopen('foo', 'w+');
+ $stream = $instance->fopen('files/foo', 'w+');
$this->assertEquals(6, fwrite($stream, 'foobar'));
$this->assertEquals(3, fwrite($stream, 'qwerty'));
fclose($stream);
- $this->assertEquals('foobarqwe', $instance->file_get_contents('foo'));
+ $this->assertEquals('foobarqwe', $instance->file_get_contents('files/foo'));
}
public function testStreamCopyWithEnoughSpace() {
$instance = $this->getLimitedStorage(16);
$inputStream = fopen('data://text/plain,foobarqwerty', 'r');
- $outputStream = $instance->fopen('foo', 'w+');
+ $outputStream = $instance->fopen('files/foo', 'w+');
list($count, $result) = \OC_Helper::streamCopy($inputStream, $outputStream);
$this->assertEquals(12, $count);
$this->assertTrue($result);
@@ -113,7 +114,7 @@ class Quota extends \Test\Files\Storage\Storage {
public function testStreamCopyNotEnoughSpace() {
$instance = $this->getLimitedStorage(9);
$inputStream = fopen('data://text/plain,foobarqwerty', 'r');
- $outputStream = $instance->fopen('foo', 'w+');
+ $outputStream = $instance->fopen('files/foo', 'w+');
list($count, $result) = \OC_Helper::streamCopy($inputStream, $outputStream);
$this->assertEquals(9, $count);
$this->assertFalse($result);
@@ -139,16 +140,27 @@ class Quota extends \Test\Files\Storage\Storage {
$instance = $this->getLimitedStorage(9);
// create test file first
- $stream = $instance->fopen('foo', 'w+');
+ $stream = $instance->fopen('files/foo', 'w+');
fwrite($stream, 'blablacontent');
fclose($stream);
- $stream = $instance->fopen('foo', 'r');
+ $stream = $instance->fopen('files/foo', 'r');
$meta = stream_get_meta_data($stream);
$this->assertEquals('plainfile', $meta['wrapper_type']);
fclose($stream);
- $stream = $instance->fopen('foo', 'rb');
+ $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+');
$meta = stream_get_meta_data($stream);
$this->assertEquals('plainfile', $meta['wrapper_type']);
fclose($stream);
@@ -156,7 +168,7 @@ class Quota extends \Test\Files\Storage\Storage {
public function testReturnQuotaStreamOnWrite() {
$instance = $this->getLimitedStorage(9);
- $stream = $instance->fopen('foo', 'w+');
+ $stream = $instance->fopen('files/foo', 'w+');
$meta = stream_get_meta_data($stream);
$expected_type = defined('HHVM_VERSION') ? 'File' : 'user-space';
$this->assertEquals($expected_type, $meta['wrapper_type']);
diff --git a/tests/lib/files/stream/dummyencryptionwrapper.php b/tests/lib/files/stream/dummyencryptionwrapper.php
new file mode 100644
index 00000000000..bb512d99c66
--- /dev/null
+++ b/tests/lib/files/stream/dummyencryptionwrapper.php
@@ -0,0 +1,37 @@
+<?php
+/**
+ * @author Björn Schießle <schiessle@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+
+namespace Test\Files\Stream;
+
+class DummyEncryptionWrapper extends \OC\Files\Stream\Encryption {
+
+ /**
+ * simulate a non-seekable stream wrapper by always return false
+ *
+ * @param int $position
+ * @return bool
+ */
+ protected function parentStreamSeek($position) {
+ return false;
+ }
+
+}
diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php
index 040be7256a6..281ec0a14a0 100644
--- a/tests/lib/files/stream/encryption.php
+++ b/tests/lib/files/stream/encryption.php
@@ -15,7 +15,7 @@ class Encryption extends \Test\TestCase {
* @param integer $unencryptedSize
* @return resource
*/
- protected function getStream($fileName, $mode, $unencryptedSize) {
+ protected function getStream($fileName, $mode, $unencryptedSize, $wrapper = '\OC\Files\Stream\Encryption') {
clearstatcache();
$size = filesize($fileName);
$source = fopen($fileName, $mode);
@@ -44,9 +44,10 @@ class Encryption extends \Test\TestCase {
->method('getUidAndFilename')
->willReturn(['user1', $internalPath]);
- return \OC\Files\Stream\Encryption::wrap($source, $internalPath,
+
+ return $wrapper::wrap($source, $internalPath,
$fullPath, $header, $uid, $this->encryptionModule, $storage, $encStorage,
- $util, $file, $mode, $size, $unencryptedSize, 8192);
+ $util, $file, $mode, $size, $unencryptedSize, 8192, $wrapper);
}
/**
@@ -256,6 +257,49 @@ class Encryption extends \Test\TestCase {
}
/**
+ * simulate a non-seekable storage
+ *
+ * @dataProvider dataFilesProvider
+ */
+ public function testWriteToNonSeekableStorage($testFile) {
+
+ $wrapper = $this->getMockBuilder('\OC\Files\Stream\Encryption')
+ ->setMethods(['parentSeekStream'])->getMock();
+ $wrapper->expects($this->any())->method('parentSeekStream')->willReturn(false);
+
+ $expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/' . $testFile);
+ // write it
+ $fileName = tempnam("/tmp", "FOO");
+ $stream = $this->getStream($fileName, 'w+', 0, '\Test\Files\Stream\DummyEncryptionWrapper');
+ // while writing the file from the beginning to the end we should never try
+ // to read parts of the file. This should only happen for write operations
+ // in the middle of a file
+ $this->encryptionModule->expects($this->never())->method('decrypt');
+ fwrite($stream, $expectedData);
+ fclose($stream);
+
+ // read it all
+ $stream = $this->getStream($fileName, 'r', strlen($expectedData), '\Test\Files\Stream\DummyEncryptionWrapper');
+ $data = stream_get_contents($stream);
+ fclose($stream);
+
+ $this->assertEquals($expectedData, $data);
+
+ // another read test with a loop like we do in several places:
+ $stream = $this->getStream($fileName, 'r', strlen($expectedData));
+ $data = '';
+ while (!feof($stream)) {
+ $data .= fread($stream, 8192);
+ }
+ fclose($stream);
+
+ $this->assertEquals($expectedData, $data);
+
+ unlink($fileName);
+
+ }
+
+ /**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function buildMockModule() {
diff --git a/tests/lib/httphelper.php b/tests/lib/httphelper.php
index e8472ab553a..1d0981ba51b 100644
--- a/tests/lib/httphelper.php
+++ b/tests/lib/httphelper.php
@@ -12,15 +12,17 @@ class TestHTTPHelper extends \Test\TestCase {
private $config;
/** @var \OC\HTTPHelper */
private $httpHelperMock;
+ /** @var \OCP\Http\Client\IClientService */
+ private $clientService;
protected function setUp() {
parent::setUp();
$this->config = $this->getMockBuilder('\OCP\IConfig')
->disableOriginalConstructor()->getMock();
- $clientService = $this->getMock('\OCP\Http\Client\IClientService');
+ $this->clientService = $this->getMock('\OCP\Http\Client\IClientService');
$this->httpHelperMock = $this->getMockBuilder('\OC\HTTPHelper')
- ->setConstructorArgs(array($this->config, $clientService))
+ ->setConstructorArgs(array($this->config, $this->clientService))
->setMethods(array('getHeaders'))
->getMock();
}
@@ -44,4 +46,73 @@ class TestHTTPHelper extends \Test\TestCase {
public function testIsHTTP($url, $expected) {
$this->assertSame($expected, $this->httpHelperMock->isHTTPURL($url));
}
+
+ public function testPostSuccess() {
+ $client = $this->getMockBuilder('\OCP\Http\Client\IClient')
+ ->disableOriginalConstructor()->getMock();
+ $this->clientService
+ ->expects($this->once())
+ ->method('newClient')
+ ->will($this->returnValue($client));
+ $response = $this->getMockBuilder('\OCP\Http\Client\IResponse')
+ ->disableOriginalConstructor()->getMock();
+ $client
+ ->expects($this->once())
+ ->method('post')
+ ->with(
+ 'https://owncloud.org',
+ [
+ 'body' => [
+ 'Foo' => 'Bar',
+ ],
+ 'connect_timeout' => 10,
+
+ ]
+ )
+ ->will($this->returnValue($response));
+ $response
+ ->expects($this->once())
+ ->method('getBody')
+ ->will($this->returnValue('Body of the requested page'));
+
+
+ $response = $this->httpHelperMock->post('https://owncloud.org', ['Foo' => 'Bar']);
+ $expected = [
+ 'success' => true,
+ 'result' => 'Body of the requested page'
+ ];
+ $this->assertSame($expected, $response);
+ }
+
+ public function testPostException() {
+ $client = $this->getMockBuilder('\OCP\Http\Client\IClient')
+ ->disableOriginalConstructor()->getMock();
+ $this->clientService
+ ->expects($this->once())
+ ->method('newClient')
+ ->will($this->returnValue($client));
+ $client
+ ->expects($this->once())
+ ->method('post')
+ ->with(
+ 'https://owncloud.org',
+ [
+ 'body' => [
+ 'Foo' => 'Bar',
+ ],
+ 'connect_timeout' => 10,
+
+ ]
+ )
+ ->will($this->throwException(new \Exception('Something failed')));
+
+
+ $response = $this->httpHelperMock->post('https://owncloud.org', ['Foo' => 'Bar']);
+ $expected = [
+ 'success' => false,
+ 'result' => 'Something failed'
+ ];
+ $this->assertSame($expected, $response);
+ }
+
}
diff --git a/tests/lib/public/util.php b/tests/lib/public/util.php
new file mode 100644
index 00000000000..ebc4f70079b
--- /dev/null
+++ b/tests/lib/public/util.php
@@ -0,0 +1,47 @@
+<?php
+/**
+ * ownCloud
+ *
+ * @author Victor Dubiniuk
+ * @copyright 2015 Victor Dubiniuk victor.dubiniuk@owncloud.com
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class Test_Public_Util extends \Test\TestCase {
+ protected function setUp() {
+ parent::setUp();
+ OCP\Contacts::clear();
+ }
+
+ /**
+ * @dataProvider channelProvider
+ *
+ * @param string $channel
+ */
+ public function testOverrideChannel($channel) {
+ \OCP\Util::setChannel($channel);
+ $actual = \OCP\Util::getChannel($channel);
+ $this->assertEquals($channel, $actual);
+ }
+
+ public function channelProvider() {
+ return [
+ ['daily'],
+ ['beta'],
+ ['stable'],
+ ['production']
+ ];
+ }
+}
diff --git a/tests/settings/controller/CheckSetupControllerTest.php b/tests/settings/controller/CheckSetupControllerTest.php
index 26f9f4e9459..b21e78c831d 100644
--- a/tests/settings/controller/CheckSetupControllerTest.php
+++ b/tests/settings/controller/CheckSetupControllerTest.php
@@ -224,10 +224,14 @@ class CheckSetupControllerTest extends TestCase {
$this->util->expects($this->once())
->method('isHtaccessWorking')
->will($this->returnValue(true));
- $this->urlGenerator->expects($this->once())
+ $this->urlGenerator->expects($this->at(0))
->method('linkToDocs')
->with('admin-performance')
->willReturn('http://doc.owncloud.org/server/go.php?to=admin-performance');
+ $this->urlGenerator->expects($this->at(1))
+ ->method('linkToDocs')
+ ->with('admin-security')
+ ->willReturn('https://doc.owncloud.org/server/8.1/admin_manual/configuration_server/hardening.html');
$expected = new DataResponse(
[
@@ -235,6 +239,8 @@ class CheckSetupControllerTest extends TestCase {
'dataDirectoryProtected' => true,
'isMemcacheConfigured' => true,
'memcacheDocs' => 'http://doc.owncloud.org/server/go.php?to=admin-performance',
+ 'isUrandomAvailable' => \Test_Helper::invokePrivate($this->checkSetupController, 'isUrandomAvailable'),
+ 'securityDocs' => 'https://doc.owncloud.org/server/8.1/admin_manual/configuration_server/hardening.html',
]
);
$this->assertEquals($expected, $this->checkSetupController->check());