summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@arthur-schiwon.de>2016-06-11 15:34:43 +0200
committerArthur Schiwon <blizzz@arthur-schiwon.de>2016-06-11 15:34:43 +0200
commit42c66efea5ef512d3a3442112f820168e6499265 (patch)
tree97ef44632d653656608e71e096fd537bbd609936 /tests
parent75f37f550bb7895757325d3f9a3215bcc4471065 (diff)
parent52a0c939ab8674857bbfe9a9fb0ee7308eee960e (diff)
downloadnextcloud-server-42c66efea5ef512d3a3442112f820168e6499265.tar.gz
nextcloud-server-42c66efea5ef512d3a3442112f820168e6499265.zip
Merge branch 'master' of https://github.com/owncloud/core into downstream-160611
Diffstat (limited to 'tests')
-rw-r--r--tests/Core/Controller/LoginControllerTest.php48
-rw-r--r--tests/lib/Files/Cache/PropagatorTest.php125
-rw-r--r--tests/lib/Repair/AvatarPermissionsTest.php189
-rw-r--r--tests/lib/Repair/RemoveOldSharesTest.php160
4 files changed, 514 insertions, 8 deletions
diff --git a/tests/Core/Controller/LoginControllerTest.php b/tests/Core/Controller/LoginControllerTest.php
index ea9d6a44148..d6fa772d38b 100644
--- a/tests/Core/Controller/LoginControllerTest.php
+++ b/tests/Core/Controller/LoginControllerTest.php
@@ -29,6 +29,7 @@ use OCP\IConfig;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
+use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use Test\TestCase;
@@ -36,19 +37,19 @@ use Test\TestCase;
class LoginControllerTest extends TestCase {
/** @var LoginController */
private $loginController;
- /** @var IRequest */
+ /** @var IRequest | \PHPUnit_Framework_MockObject_MockObject */
private $request;
- /** @var IUserManager */
+ /** @var IUserManager | \PHPUnit_Framework_MockObject_MockObject */
private $userManager;
- /** @var IConfig */
+ /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
private $config;
- /** @var ISession */
+ /** @var ISession | \PHPUnit_Framework_MockObject_MockObject */
private $session;
- /** @var IUserSession */
+ /** @var IUserSession | \PHPUnit_Framework_MockObject_MockObject */
private $userSession;
- /** @var IURLGenerator */
+ /** @var IURLGenerator | \PHPUnit_Framework_MockObject_MockObject */
private $urlGenerator;
- /** @var Manager */
+ /** @var Manager | \PHPUnit_Framework_MockObject_MockObject */
private $twoFactorManager;
public function setUp() {
@@ -296,6 +297,7 @@ class LoginControllerTest extends TestCase {
}
public function testLoginWithValidCredentials() {
+ /** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->getMock('\OCP\IUser');
$password = 'secret';
$indexPageUrl = 'some url';
@@ -323,6 +325,7 @@ class LoginControllerTest extends TestCase {
}
public function testLoginWithValidCredentialsAndRedirectUrl() {
+ /** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->getMock('\OCP\IUser');
$user->expects($this->any())
->method('getUID')
@@ -352,6 +355,7 @@ class LoginControllerTest extends TestCase {
}
public function testLoginWithTwoFactorEnforced() {
+ /** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->getMock('\OCP\IUser');
$user->expects($this->any())
->method('getUID')
@@ -380,8 +384,36 @@ class LoginControllerTest extends TestCase {
->with('core.TwoFactorChallenge.selectChallenge')
->will($this->returnValue($challengeUrl));
- $expected = new \OCP\AppFramework\Http\RedirectResponse($challengeUrl);
+ $expected = new RedirectResponse($challengeUrl);
$this->assertEquals($expected, $this->loginController->tryLogin('john@doe.com', $password, null));
}
+ public function testToNotLeakLoginName() {
+ /** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */
+ $user = $this->getMock('\OCP\IUser');
+ $user->expects($this->any())
+ ->method('getUID')
+ ->will($this->returnValue('john'));
+
+ $this->userManager->expects($this->exactly(2))
+ ->method('checkPassword')
+ ->withConsecutive(
+ ['john@doe.com', 'just wrong'],
+ ['john', 'just wrong']
+ )
+ ->willReturn(false);
+
+ $this->userManager->expects($this->once())
+ ->method('getByEmail')
+ ->with('john@doe.com')
+ ->willReturn([$user]);
+
+ $this->urlGenerator->expects($this->once())
+ ->method('linkToRoute')
+ ->with('core.login.showLoginForm', ['user' => 'john@doe.com'])
+ ->will($this->returnValue(''));
+
+ $expected = new RedirectResponse('');
+ $this->assertEquals($expected, $this->loginController->tryLogin('john@doe.com', 'just wrong', null));
+ }
}
diff --git a/tests/lib/Files/Cache/PropagatorTest.php b/tests/lib/Files/Cache/PropagatorTest.php
new file mode 100644
index 00000000000..402b29c8c3e
--- /dev/null
+++ b/tests/lib/Files/Cache/PropagatorTest.php
@@ -0,0 +1,125 @@
+<?php
+/**
+ * Copyright (c) 2012 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\Cache;
+
+use OC\Files\Storage\Temporary;
+use OCP\Files\Cache\ICacheEntry;
+use OCP\Files\Storage\IStorage;
+use Test\TestCase;
+
+/**
+ * @group DB
+ */
+class PropagatorTest extends TestCase {
+ /** @var IStorage */
+ private $storage;
+
+ public function setUp() {
+ parent::setUp();
+ $this->storage = new Temporary();
+ $this->storage->mkdir('foo/bar');
+ $this->storage->file_put_contents('foo/bar/file.txt', 'bar');
+ $this->storage->getScanner()->scan('');
+ }
+
+ /**
+ * @param $paths
+ * @return ICacheEntry[]
+ */
+ private function getFileInfos($paths) {
+ $values = array_map(function ($path) {
+ return $this->storage->getCache()->get($path);
+ }, $paths);
+ return array_combine($paths, $values);
+ }
+
+ public function testEtagPropagation() {
+ $paths = ['', 'foo', 'foo/bar'];
+ $oldInfos = $this->getFileInfos($paths);
+ $this->storage->getPropagator()->propagateChange('foo/bar/file.txt', time());
+ $newInfos = $this->getFileInfos($paths);
+
+ foreach ($oldInfos as $i => $oldInfo) {
+ $this->assertNotEquals($oldInfo->getEtag(), $newInfos[$i]->getEtag());
+ }
+ }
+
+ public function testTimePropagation() {
+ $paths = ['', 'foo', 'foo/bar'];
+ $oldTime = time() - 200;
+ $targetTime = time() - 100;
+ $now = time();
+ $cache = $this->storage->getCache();
+ $cache->put('', ['mtime' => $now]);
+ $cache->put('foo', ['mtime' => $now]);
+ $cache->put('foo/bar', ['mtime' => $oldTime]);
+ $cache->put('foo/bar/file.txt', ['mtime' => $oldTime]);
+ $this->storage->getPropagator()->propagateChange('foo/bar/file.txt', $targetTime);
+ $newInfos = $this->getFileInfos($paths);
+
+ $this->assertEquals($targetTime, $newInfos['foo/bar']->getMTime());
+
+ // dont lower mtimes
+ $this->assertEquals($now, $newInfos['foo']->getMTime());
+ $this->assertEquals($now, $newInfos['']->getMTime());
+ }
+
+ public function testSizePropagation() {
+ $paths = ['', 'foo', 'foo/bar'];
+ $oldInfos = $this->getFileInfos($paths);
+ $this->storage->getPropagator()->propagateChange('foo/bar/file.txt', time(), 10);
+ $newInfos = $this->getFileInfos($paths);
+
+ foreach ($oldInfos as $i => $oldInfo) {
+ $this->assertEquals($oldInfo->getSize() + 10, $newInfos[$i]->getSize());
+ }
+ }
+
+ public function testBatchedPropagation() {
+ $this->storage->mkdir('foo/baz');
+ $this->storage->mkdir('asd');
+ $this->storage->file_put_contents('asd/file.txt', 'bar');
+ $this->storage->file_put_contents('foo/baz/file.txt', 'bar');
+ $this->storage->getScanner()->scan('');
+
+ $paths = ['', 'foo', 'foo/bar', 'asd', 'foo/baz'];
+
+ $oldInfos = $this->getFileInfos($paths);
+ $propagator = $this->storage->getPropagator();
+
+ $propagator->beginBatch();
+ $propagator->propagateChange('asd/file.txt', time(), 10);
+ $propagator->propagateChange('foo/bar/file.txt', time(), 2);
+
+ $newInfos = $this->getFileInfos($paths);
+
+ // no changes until we finish the batch
+ foreach ($oldInfos as $i => $oldInfo) {
+ $this->assertEquals($oldInfo->getSize(), $newInfos[$i]->getSize());
+ $this->assertEquals($oldInfo->getEtag(), $newInfos[$i]->getEtag());
+ $this->assertEquals($oldInfo->getMTime(), $newInfos[$i]->getMTime());
+ }
+
+ $propagator->commitBatch();
+
+ $newInfos = $this->getFileInfos($paths);
+
+ foreach ($oldInfos as $i => $oldInfo) {
+ if ($oldInfo->getPath() !== 'foo/baz') {
+ $this->assertNotEquals($oldInfo->getEtag(), $newInfos[$i]->getEtag());
+ }
+ }
+
+ $this->assertEquals($oldInfos['']->getSize() + 12, $newInfos['']->getSize());
+ $this->assertEquals($oldInfos['asd']->getSize() + 10, $newInfos['asd']->getSize());
+ $this->assertEquals($oldInfos['foo']->getSize() + 2, $newInfos['foo']->getSize());
+ $this->assertEquals($oldInfos['foo/bar']->getSize() + 2, $newInfos['foo/bar']->getSize());
+ $this->assertEquals($oldInfos['foo/baz']->getSize(), $newInfos['foo/baz']->getSize());
+ }
+}
diff --git a/tests/lib/Repair/AvatarPermissionsTest.php b/tests/lib/Repair/AvatarPermissionsTest.php
new file mode 100644
index 00000000000..e3f582dc512
--- /dev/null
+++ b/tests/lib/Repair/AvatarPermissionsTest.php
@@ -0,0 +1,189 @@
+<?php
+/**
+ * @author Roeland Jago Douma <rullzer@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, 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\Repair;
+
+/**
+ * Test for fixing the userRoot and avatar permissions
+ *
+ * @group DB
+ *
+ * @see \OC\Repair\AvatarPermissionsTest
+ */
+class AvatarPermissionsTest extends \Test\TestCase {
+
+ /** @var \OC\Repair\AvatarPermissions */
+ protected $repair;
+
+ /** @var \OCP\IDBConnection */
+ protected $connection;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->connection = \OC::$server->getDatabaseConnection();
+ $this->repair = new \OC\Repair\AvatarPermissions($this->connection);
+ $this->cleanUpTables();
+ }
+
+ protected function tearDown() {
+ $this->cleanUpTables();
+
+ parent::tearDown();
+ }
+
+ protected function cleanUpTables() {
+ $qb = $this->connection->getQueryBuilder();
+ $qb->delete('filecache')->execute();
+ $qb->delete('storages')->execute();
+ }
+
+ public function dataFixUserRootPermissions() {
+ return [
+ ['home::user', '', 0, 23],
+ ['home::user', 'foo', 0, 0],
+ ['home::user', 'avatar.jpg', 0, 0],
+ ['ABC::user', '', 0, 0],
+ ['ABC::user', 'foo', 0, 0],
+ ];
+ }
+
+ /**
+ * @dataProvider dataFixUserRootPermissions
+ *
+ * @param string $storageId
+ * @param string $path
+ * @param int $permissionsBefore
+ * @param int $permissionsAfter
+ */
+ public function testFixUserRootPermissions($storageId, $path, $permissionsBefore, $permissionsAfter) {
+ $userStorage = $this->addStorage($storageId);
+ $userHome = $this->addFileCacheEntry($userStorage, $path, $permissionsBefore);
+
+ $this->invokePrivate($this->repair, 'fixUserRootPermissions', []);
+
+ $this->verifyPermissions($userHome, $permissionsAfter);
+ }
+
+ public function dataFixAvatarPermissions() {
+ return [
+ ['home::user', '', 0, 0],
+ ['home::user', 'avatar.jpg', 0, 27],
+ ['home::user', 'avatar.png', 0, 27],
+ ['home::user', 'avatar.32.png', 0, 27],
+ ['home::user', 'mine.txt', 0, 0],
+ ['ABC::user', '', 0, 0],
+ ['ABC::user', 'avatar.jpg', 0, 0],
+ ['ABC::user', 'avatar.png', 0, 0],
+ ['ABC::user', 'avatar.32.png', 0, 0],
+ ['ABC::user', 'mine.txt', 0, 0],
+ ];
+ }
+
+ /**
+ * @dataProvider dataFixAvatarPermissions
+ *
+ * @param string $storageId
+ * @param string $path
+ * @param int $permissionsBefore
+ * @param int $permissionsAfter
+ */
+ public function testFixAvatarPermissions($storageId, $path, $permissionsBefore, $permissionsAfter) {
+ $userStorage = $this->addStorage($storageId);
+ $userHome = $this->addFileCacheEntry($userStorage, $path, $permissionsBefore);
+
+ $this->invokePrivate($this->repair, 'fixAvatarPermissions', []);
+
+ $this->verifyPermissions($userHome, $permissionsAfter);
+ }
+
+ /**
+ * Add a new storage
+ *
+ * @param string $id
+ * @return int The numeric id
+ */
+ protected function addStorage($id) {
+ $qb = $this->connection->getQueryBuilder();
+
+ $qb->insert('storages')
+ ->values([
+ 'id' => $qb->createNamedParameter($id)
+ ]);
+
+ $qb->execute();
+
+ return $qb->getLastInsertId();
+ }
+
+ /**
+ * Add a filecache entry
+ *
+ * @param int $storage
+ * @param string $path
+ * @param int $permissions
+ *
+ * @return int The fileid
+ */
+ protected function addFileCacheEntry($storage, $path, $permissions) {
+ $qb = $this->connection->getQueryBuilder();
+
+ $qb->insert('filecache')
+ ->values([
+ 'path' => $qb->createNamedParameter($path),
+ 'path_hash' => $qb->createNamedParameter(md5($path)),
+ 'parent' => $qb->createNamedParameter(42),
+ 'mimetype' => $qb->createNamedParameter(23),
+ 'mimepart' => $qb->createNamedParameter(32),
+ 'size' => $qb->createNamedParameter(16),
+ 'mtime' => $qb->createNamedParameter(1),
+ 'storage_mtime' => $qb->createNamedParameter(2),
+ 'encrypted' => $qb->createNamedParameter(0),
+ 'unencrypted_size' => $qb->createNamedParameter(0),
+ 'storage' => $qb->createNamedParameter($storage),
+ 'permissions' => $qb->createNamedParameter($permissions),
+ ]);
+
+ $qb->execute();
+
+ return $qb->getLastInsertId();
+ }
+
+ /**
+ * @param int $fileId
+ * @param int $permissions
+ */
+ protected function verifyPermissions($fileId, $permissions) {
+ $qb = $this->connection->getQueryBuilder();
+
+ $qb->select('permissions')
+ ->from('filecache')
+ ->where($qb->expr()->eq('fileid', $qb->createNamedParameter($fileId)));
+
+ $cursor = $qb->execute();
+
+ $data = $cursor->fetch();
+ $cursor->closeCursor();
+
+ $this->assertSame($permissions, (int)$data['permissions']);
+ }
+
+
+}
diff --git a/tests/lib/Repair/RemoveOldSharesTest.php b/tests/lib/Repair/RemoveOldSharesTest.php
new file mode 100644
index 00000000000..ac30585bdc5
--- /dev/null
+++ b/tests/lib/Repair/RemoveOldSharesTest.php
@@ -0,0 +1,160 @@
+<?php
+/**
+ * @author Roeland Jago Douma <rullzer@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, 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\Repair;
+
+use OC\Repair\RemoveOldShares;
+use OCP\IDBConnection;
+use OCP\Migration\IOutput;
+
+/**
+ * Class RemoveOldSharesTest
+ *
+ * @package Test\Repair
+ * @group DB
+ */
+class RemoveOldSharesTest extends \Test\TestCase {
+
+ /** @var RemoveOldShares */
+ protected $repair;
+
+ /** @var IDBConnection */
+ protected $connection;
+
+ /** @var IOutput */
+ private $outputMock;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->connection = \OC::$server->getDatabaseConnection();
+ $this->repair = new RemoveOldShares($this->connection);
+ }
+
+ protected function tearDown() {
+ $qb = $this->connection->getQueryBuilder();
+ $qb->delete('share');
+ $qb->execute();
+
+ return parent::tearDown();
+ }
+
+ public function testRun() {
+ $qb = $this->connection->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->createNamedParameter(0),
+ 'share_with' => $qb->createNamedParameter('foo'),
+ 'uid_owner' => $qb->createNamedParameter('owner'),
+ 'item_type' => $qb->createNamedParameter('file'),
+ 'item_source' => $qb->createNamedParameter(42),
+ 'item_target' => $qb->createNamedParameter('/target'),
+ 'file_source' => $qb->createNamedParameter(42),
+ 'file_target' => $qb->createNamedParameter('/target'),
+ 'permissions' => $qb->createNamedParameter(1),
+ ]);
+ $qb->execute();
+
+ $qb = $this->connection->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->createNamedParameter(0),
+ 'share_with' => $qb->createNamedParameter('foo'),
+ 'uid_owner' => $qb->createNamedParameter('owner'),
+ 'item_type' => $qb->createNamedParameter('calendar'),
+ 'item_source' => $qb->createNamedParameter(42),
+ 'item_target' => $qb->createNamedParameter('/target'),
+ 'file_source' => $qb->createNamedParameter(42),
+ 'file_target' => $qb->createNamedParameter('/target'),
+ 'permissions' => $qb->createNamedParameter(1),
+ ]);
+ $qb->execute();
+
+ $qb = $this->connection->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->createNamedParameter(0),
+ 'share_with' => $qb->createNamedParameter('foo'),
+ 'uid_owner' => $qb->createNamedParameter('owner'),
+ 'item_type' => $qb->createNamedParameter('event'),
+ 'item_source' => $qb->createNamedParameter(42),
+ 'item_target' => $qb->createNamedParameter('/target'),
+ 'file_source' => $qb->createNamedParameter(42),
+ 'file_target' => $qb->createNamedParameter('/target'),
+ 'permissions' => $qb->createNamedParameter(1),
+ ]);
+ $qb->execute();
+
+ $qb = $this->connection->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->createNamedParameter(0),
+ 'share_with' => $qb->createNamedParameter('foo'),
+ 'uid_owner' => $qb->createNamedParameter('owner'),
+ 'item_type' => $qb->createNamedParameter('contact'),
+ 'item_source' => $qb->createNamedParameter(42),
+ 'item_target' => $qb->createNamedParameter('/target'),
+ 'file_source' => $qb->createNamedParameter(42),
+ 'file_target' => $qb->createNamedParameter('/target'),
+ 'permissions' => $qb->createNamedParameter(1),
+ ]);
+ $qb->execute();
+
+ $qb = $this->connection->getQueryBuilder();
+ $qb->insert('share')
+ ->values([
+ 'share_type' => $qb->createNamedParameter(0),
+ 'share_with' => $qb->createNamedParameter('foo'),
+ 'uid_owner' => $qb->createNamedParameter('owner'),
+ 'item_type' => $qb->createNamedParameter('addressbook'),
+ 'item_source' => $qb->createNamedParameter(42),
+ 'item_target' => $qb->createNamedParameter('/target'),
+ 'file_source' => $qb->createNamedParameter(42),
+ 'file_target' => $qb->createNamedParameter('/target'),
+ 'permissions' => $qb->createNamedParameter(1),
+ ]);
+ $qb->execute();
+
+ $qb = $this->connection->getQueryBuilder();
+ $qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
+ ->from('share');
+
+ $cursor = $qb->execute();
+ $data = $cursor->fetchAll();
+ $cursor->closeCursor();
+ $this->assertEquals(5, $data[0]['count']);
+
+ $this->repair->run($this->outputMock);
+
+ $qb = $this->connection->getQueryBuilder();
+ $qb->select('*')
+ ->from('share');
+
+ $cursor = $qb->execute();
+ $data = $cursor->fetchAll();
+ $cursor->closeCursor();
+ $this->assertCount(1, $data);
+ $this->assertEquals('file', $data[0]['item_type']);
+ }
+}