summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorBart Visscher <bartv@thisnet.nl>2014-03-31 21:38:54 +0200
committerBart Visscher <bartv@thisnet.nl>2014-03-31 21:38:54 +0200
commit6b061c236dd5730837b567f2c39a19af1617d33c (patch)
tree27de0d46deae2987ea398f2f2aee4e26786c6f48 /tests
parent8951328a87c16e5ebfe4d3e5c392347db1e54f92 (diff)
parentab696edba685cd6d2a64c2e48907f03197aae53f (diff)
downloadnextcloud-server-6b061c236dd5730837b567f2c39a19af1617d33c.tar.gz
nextcloud-server-6b061c236dd5730837b567f2c39a19af1617d33c.zip
Merge branch 'master' into type-hinting
Conflicts: lib/private/image.php lib/private/l10n.php lib/private/request.php lib/private/share/mailnotifications.php lib/private/template/base.php
Diffstat (limited to 'tests')
-rw-r--r--tests/bootstrap.php10
-rw-r--r--tests/lib/appframework/routing/RoutingTest.php8
-rw-r--r--tests/lib/files/cache/homecache.php1
-rw-r--r--tests/lib/files/cache/updater.php5
-rw-r--r--tests/lib/files/filesystem.php51
-rw-r--r--tests/lib/files/storage/storage.php47
-rw-r--r--tests/lib/files/storage/wrapper/quota.php34
-rw-r--r--tests/lib/helper.php4
-rw-r--r--tests/lib/helperstorage.php113
-rw-r--r--tests/lib/image.php24
-rw-r--r--tests/lib/mail.php30
-rw-r--r--tests/lib/request.php137
-rw-r--r--tests/lib/share/share.php12
-rw-r--r--tests/lib/urlgenerator.php30
-rw-r--r--tests/lib/utilcheckserver.php108
-rw-r--r--tests/phpunit-autotest.xml1
-rw-r--r--tests/phpunit.xml.dist3
-rw-r--r--tests/startsessionlistener.php44
-rw-r--r--tests/testcleanuplistener.php1
19 files changed, 624 insertions, 39 deletions
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index 581cfcff9f3..99374d68a5c 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -3,11 +3,17 @@
define('PHPUNIT_RUN', 1);
-require_once __DIR__.'/../lib/base.php';
+$configDir = getenv('CONFIG_DIR');
+if ($configDir) {
+ define('PHPUNIT_CONFIG_DIR', $configDir);
+}
+
+require_once __DIR__ . '/../lib/base.php';
-if(!class_exists('PHPUnit_Framework_TestCase')) {
+if (!class_exists('PHPUnit_Framework_TestCase')) {
require_once('PHPUnit/Autoload.php');
}
OC_Hook::clear();
OC_Log::$enabled = false;
+OC_FileProxy::clearProxies();
diff --git a/tests/lib/appframework/routing/RoutingTest.php b/tests/lib/appframework/routing/RoutingTest.php
index 56e625fd168..735dd7cef41 100644
--- a/tests/lib/appframework/routing/RoutingTest.php
+++ b/tests/lib/appframework/routing/RoutingTest.php
@@ -46,7 +46,7 @@ class RouteConfigTest extends \PHPUnit_Framework_TestCase
));
// router mock
- $router = $this->getMock("\OC_Router", array('create'));
+ $router = $this->getMock("\OC\Route\Router", array('create'));
// load route configuration
$container = new DIContainer('app1');
@@ -91,7 +91,7 @@ class RouteConfigTest extends \PHPUnit_Framework_TestCase
$route = $this->mockRoute($verb, $controllerName, $actionName);
// router mock
- $router = $this->getMock("\OC_Router", array('create'));
+ $router = $this->getMock("\OC\Route\Router", array('create'));
// we expect create to be called once:
$router
@@ -116,7 +116,7 @@ class RouteConfigTest extends \PHPUnit_Framework_TestCase
private function assertResource($yaml, $resourceName, $url, $controllerName, $paramName)
{
// router mock
- $router = $this->getMock("\OC_Router", array('create'));
+ $router = $this->getMock("\OC\Route\Router", array('create'));
// route mocks
$indexRoute = $this->mockRoute('GET', $controllerName, 'index');
@@ -174,7 +174,7 @@ class RouteConfigTest extends \PHPUnit_Framework_TestCase
private function mockRoute($verb, $controllerName, $actionName)
{
$container = new DIContainer('app1');
- $route = $this->getMock("\OC_Route", array('method', 'action'), array(), '', false);
+ $route = $this->getMock("\OC\Route\Route", array('method', 'action'), array(), '', false);
$route
->expects($this->exactly(1))
->method('method')
diff --git a/tests/lib/files/cache/homecache.php b/tests/lib/files/cache/homecache.php
index dbcf6e9caa0..80dc54c9d19 100644
--- a/tests/lib/files/cache/homecache.php
+++ b/tests/lib/files/cache/homecache.php
@@ -90,7 +90,6 @@ class HomeCache extends \PHPUnit_Framework_TestCase {
// check that files and root size ignored the unknown sizes
$this->assertEquals(1000, $this->cache->calculateFolderSize('files'));
- $this->assertEquals(1000, $this->cache->calculateFolderSize(''));
// clean up
$this->cache->remove('');
diff --git a/tests/lib/files/cache/updater.php b/tests/lib/files/cache/updater.php
index a6ee8c46661..e2fae0ae57f 100644
--- a/tests/lib/files/cache/updater.php
+++ b/tests/lib/files/cache/updater.php
@@ -65,11 +65,6 @@ class Updater extends \PHPUnit_Framework_TestCase {
Filesystem::mount($this->storage, array(), '/' . self::$user . '/files');
\OC_Hook::clear('OC_Filesystem');
-
- \OC_Hook::connect('OC_Filesystem', 'post_write', '\OC\Files\Cache\Updater', 'writeHook');
- \OC_Hook::connect('OC_Filesystem', 'post_delete', '\OC\Files\Cache\Updater', 'deleteHook');
- \OC_Hook::connect('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Updater', 'renameHook');
- \OC_Hook::connect('OC_Filesystem', 'post_touch', '\OC\Files\Cache\Updater', 'touchHook');
}
public function tearDown() {
diff --git a/tests/lib/files/filesystem.php b/tests/lib/files/filesystem.php
index 90f1dfe581b..53f528af793 100644
--- a/tests/lib/files/filesystem.php
+++ b/tests/lib/files/filesystem.php
@@ -226,4 +226,55 @@ class Filesystem extends \PHPUnit_Framework_TestCase {
$path = $arguments['path'];
$this->assertEquals($path, \OC\Files\Filesystem::normalizePath($path)); //the path passed to the hook should already be normalized
}
+
+ /**
+ * Test that the default cache dir is part of the user's home
+ */
+ public function testMountDefaultCacheDir() {
+ $userId = uniqid('user_');
+ $oldCachePath = \OC_Config::getValue('cache_path', '');
+ // no cache path configured
+ \OC_Config::setValue('cache_path', '');
+
+ \OC_User::createUser($userId, $userId);
+ \OC\Files\Filesystem::initMountPoints($userId);
+
+ $this->assertEquals(
+ '/' . $userId . '/',
+ \OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache')
+ );
+ list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache');
+ $this->assertInstanceOf('\OC\Files\Storage\Home', $storage);
+ $this->assertEquals('cache', $internalPath);
+ \OC_User::deleteUser($userId);
+
+ \OC_Config::setValue('cache_path', $oldCachePath);
+ }
+
+ /**
+ * Test that an external cache is mounted into
+ * the user's home
+ */
+ public function testMountExternalCacheDir() {
+ $userId = uniqid('user_');
+
+ $oldCachePath = \OC_Config::getValue('cache_path', '');
+ // set cache path to temp dir
+ $cachePath = \OC_Helper::tmpFolder() . '/extcache';
+ \OC_Config::setValue('cache_path', $cachePath);
+
+ \OC_User::createUser($userId, $userId);
+ \OC\Files\Filesystem::initMountPoints($userId);
+
+ $this->assertEquals(
+ '/' . $userId . '/cache/',
+ \OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache')
+ );
+ list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache');
+ $this->assertInstanceOf('\OC\Files\Storage\Local', $storage);
+ $this->assertEquals('', $internalPath);
+ \OC_User::deleteUser($userId);
+
+ \OC_Config::setValue('cache_path', $oldCachePath);
+ }
}
diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php
index f9291758606..f3bfba3feb8 100644
--- a/tests/lib/files/storage/storage.php
+++ b/tests/lib/files/storage/storage.php
@@ -64,17 +64,17 @@ abstract class Storage extends \PHPUnit_Framework_TestCase {
* @dataProvider directoryProvider
*/
public function testDirectories($directory) {
- $this->assertFalse($this->instance->file_exists('/'.$directory));
+ $this->assertFalse($this->instance->file_exists('/' . $directory));
- $this->assertTrue($this->instance->mkdir('/'.$directory));
+ $this->assertTrue($this->instance->mkdir('/' . $directory));
- $this->assertTrue($this->instance->file_exists('/'.$directory));
- $this->assertTrue($this->instance->is_dir('/'.$directory));
- $this->assertFalse($this->instance->is_file('/'.$directory));
- $this->assertEquals('dir', $this->instance->filetype('/'.$directory));
- $this->assertEquals(0, $this->instance->filesize('/'.$directory));
- $this->assertTrue($this->instance->isReadable('/'.$directory));
- $this->assertTrue($this->instance->isUpdatable('/'.$directory));
+ $this->assertTrue($this->instance->file_exists('/' . $directory));
+ $this->assertTrue($this->instance->is_dir('/' . $directory));
+ $this->assertFalse($this->instance->is_file('/' . $directory));
+ $this->assertEquals('dir', $this->instance->filetype('/' . $directory));
+ $this->assertEquals(0, $this->instance->filesize('/' . $directory));
+ $this->assertTrue($this->instance->isReadable('/' . $directory));
+ $this->assertTrue($this->instance->isUpdatable('/' . $directory));
$dh = $this->instance->opendir('/');
$content = array();
@@ -85,13 +85,13 @@ abstract class Storage extends \PHPUnit_Framework_TestCase {
}
$this->assertEquals(array($directory), $content);
- $this->assertFalse($this->instance->mkdir('/'.$directory)); //cant create existing folders
- $this->assertTrue($this->instance->rmdir('/'.$directory));
+ $this->assertFalse($this->instance->mkdir('/' . $directory)); //cant create existing folders
+ $this->assertTrue($this->instance->rmdir('/' . $directory));
$this->wait();
- $this->assertFalse($this->instance->file_exists('/'.$directory));
+ $this->assertFalse($this->instance->file_exists('/' . $directory));
- $this->assertFalse($this->instance->rmdir('/'.$directory)); //cant remove non existing folders
+ $this->assertFalse($this->instance->rmdir('/' . $directory)); //cant remove non existing folders
$dh = $this->instance->opendir('/');
$content = array();
@@ -103,8 +103,7 @@ abstract class Storage extends \PHPUnit_Framework_TestCase {
$this->assertEquals(array(), $content);
}
- public function directoryProvider()
- {
+ public function directoryProvider() {
return array(
array('folder'),
array(' folder'),
@@ -113,6 +112,7 @@ abstract class Storage extends \PHPUnit_Framework_TestCase {
array('spéciäl földer'),
);
}
+
/**
* test the various uses of file_get_contents and file_put_contents
*/
@@ -298,4 +298,21 @@ abstract class Storage extends \PHPUnit_Framework_TestCase {
$this->assertFalse($this->instance->file_exists('folder/bar'));
$this->assertFalse($this->instance->file_exists('folder'));
}
+
+ public function hashProvider(){
+ return array(
+ array('Foobar', 'md5'),
+ array('Foobar', 'sha1'),
+ array('Foobar', 'sha256'),
+ );
+ }
+
+ /**
+ * @dataProvider hashProvider
+ */
+ public function testHash($data, $type) {
+ $this->instance->file_put_contents('hash.txt', $data);
+ $this->assertEquals(hash($type, $data), $this->instance->hash($type, 'hash.txt'));
+ $this->assertEquals(hash($type, $data, true), $this->instance->hash($type, 'hash.txt', true));
+ }
}
diff --git a/tests/lib/files/storage/wrapper/quota.php b/tests/lib/files/storage/wrapper/quota.php
index 43eae78415d..777529fd85e 100644
--- a/tests/lib/files/storage/wrapper/quota.php
+++ b/tests/lib/files/storage/wrapper/quota.php
@@ -53,6 +53,40 @@ class Quota extends \Test\Files\Storage\Storage {
$this->assertEquals(9, $instance->free_space(''));
}
+ public function testFreeSpaceWithUsedSpace() {
+ $instance = $this->getLimitedStorage(9);
+ $instance->getCache()->put(
+ '', array('size' => 3, 'unencrypted_size' => 0)
+ );
+ $this->assertEquals(6, $instance->free_space(''));
+ }
+
+ 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(
+ '', array('size' => 3, 'unencrypted_size' => 0)
+ );
+ $this->assertEquals(6, $instance->free_space(''));
+ }
+
+ public function testFreeSpaceWithUsedSpaceAndEncryption() {
+ $instance = $this->getLimitedStorage(9);
+ $instance->getCache()->put(
+ '', array('size' => 7, 'unencrypted_size' => 3)
+ );
+ $this->assertEquals(6, $instance->free_space(''));
+ }
+
public function testFWriteNotEnoughSpace() {
$instance = $this->getLimitedStorage(9);
$stream = $instance->fopen('foo', 'w+');
diff --git a/tests/lib/helper.php b/tests/lib/helper.php
index 4311215795c..0943e6bc1b9 100644
--- a/tests/lib/helper.php
+++ b/tests/lib/helper.php
@@ -23,6 +23,7 @@ class Test_Helper extends PHPUnit_Framework_TestCase {
array('0 B', 0),
array('1 kB', 1024),
array('9.5 MB', 10000000),
+ array('1.3 GB', 1395864371),
array('465.7 GB', 500000000000),
array('454.7 TB', 500000000000000),
array('444.1 PB', 500000000000000000),
@@ -41,8 +42,9 @@ class Test_Helper extends PHPUnit_Framework_TestCase {
return array(
array(0.0, "0 B"),
array(1024.0, "1 kB"),
+ array(1395864371.0, '1.3 GB'),
array(9961472.0, "9.5 MB"),
- array(500041567436.8, "465.7 GB"),
+ array(500041567437.0, "465.7 GB"),
);
}
diff --git a/tests/lib/helperstorage.php b/tests/lib/helperstorage.php
new file mode 100644
index 00000000000..010a54e3bb0
--- /dev/null
+++ b/tests/lib/helperstorage.php
@@ -0,0 +1,113 @@
+<?php
+/**
+ * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+/**
+ * Test the storage functions of OC_Helper
+ */
+class Test_Helper_Storage extends PHPUnit_Framework_TestCase {
+ private $user;
+ private $storageMock;
+
+ public function setUp() {
+ $this->user = 'user_' . uniqid();
+ \OC\Files\Filesystem::tearDown();
+ \OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files');
+
+ $this->storageMock = $this->getMock(
+ '\OC\Files\Storage\Temporary',
+ array('free_space'),
+ array('')
+ );
+
+ \OC\Files\Filesystem::clearMounts();
+
+ $this->storageMock->expects($this->once())
+ ->method('free_space')
+ ->will($this->returnValue(12));
+ }
+
+ public function tearDown() {
+ $this->user = null;
+
+ $this->storageMock->getCache()->clear();
+ \OC\Files\Filesystem::tearDown();
+ }
+
+ /**
+ * Test getting the storage info
+ */
+ function testGetStorageInfo() {
+ \OC\Files\Filesystem::mount($this->storageMock, array(), '/' . $this->user . '/files');
+ $this->storageMock->file_put_contents('test.txt', '01234');
+
+ $storageInfo = \OC_Helper::getStorageInfo('');
+ $this->assertEquals(12, $storageInfo['free']);
+ $this->assertEquals(5, $storageInfo['used']);
+ $this->assertEquals(17, $storageInfo['total']);
+ }
+
+ /**
+ * Test getting the storage info with quota enabled
+ */
+ function testGetStorageInfoWithQuota() {
+ $this->storageMock->file_put_contents('test.txt', '01234');
+ $this->storageMock = new \OC\Files\Storage\Wrapper\Quota(
+ array(
+ 'storage' => $this->storageMock,
+ 'quota' => 7
+ )
+ );
+ \OC\Files\Filesystem::mount($this->storageMock, array(), '/' . $this->user . '/files');
+
+ $storageInfo = \OC_Helper::getStorageInfo('');
+ $this->assertEquals(2, $storageInfo['free']);
+ $this->assertEquals(5, $storageInfo['used']);
+ $this->assertEquals(7, $storageInfo['total']);
+ }
+
+ /**
+ * Test getting the storage info when data exceeds quota
+ */
+ function testGetStorageInfoWhenSizeExceedsQuota() {
+ $this->storageMock->file_put_contents('test.txt', '0123456789');
+ $this->storageMock = new \OC\Files\Storage\Wrapper\Quota(
+ array(
+ 'storage' => $this->storageMock,
+ 'quota' => 7
+ )
+ );
+ \OC\Files\Filesystem::mount($this->storageMock, array(), '/' . $this->user . '/files');
+
+ $storageInfo = \OC_Helper::getStorageInfo('');
+ $this->assertEquals(0, $storageInfo['free']);
+ $this->assertEquals(10, $storageInfo['used']);
+ // total = quota
+ $this->assertEquals(7, $storageInfo['total']);
+ }
+
+ /**
+ * Test getting the storage info when the remaining
+ * free storage space is less than the quota
+ */
+ function testGetStorageInfoWhenFreeSpaceLessThanQuota() {
+ $this->storageMock->file_put_contents('test.txt', '01234');
+ $this->storageMock = new \OC\Files\Storage\Wrapper\Quota(
+ array(
+ 'storage' => $this->storageMock,
+ 'quota' => 18
+ )
+ );
+ \OC\Files\Filesystem::mount($this->storageMock, array(), '/' . $this->user . '/files');
+
+ $storageInfo = \OC_Helper::getStorageInfo('');
+ $this->assertEquals(12, $storageInfo['free']);
+ $this->assertEquals(5, $storageInfo['used']);
+ // total = free + used (because quota > total)
+ $this->assertEquals(17, $storageInfo['total']);
+ }
+}
diff --git a/tests/lib/image.php b/tests/lib/image.php
index 4aba1b0bc61..131a9d86f3e 100644
--- a/tests/lib/image.php
+++ b/tests/lib/image.php
@@ -8,8 +8,8 @@
class Test_Image extends PHPUnit_Framework_TestCase {
public static function tearDownAfterClass() {
- unlink(OC::$SERVERROOT.'/tests/data/testimage2.png');
- unlink(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
+ @unlink(OC::$SERVERROOT.'/tests/data/testimage2.png');
+ @unlink(OC::$SERVERROOT.'/tests/data/testimage2.jpg');
}
public function testGetMimeTypeForFile() {
@@ -236,4 +236,24 @@ class Test_Image extends PHPUnit_Framework_TestCase {
$this->assertEquals(200, $img->width());
$this->assertEquals(200, $img->height());
}
+
+ function convertDataProvider() {
+ return array(
+ array( 'image/gif'),
+ array( 'image/jpeg'),
+ array( 'image/png'),
+ );
+ }
+
+ /**
+ * @dataProvider convertDataProvider
+ */
+ public function testConvert($mimeType) {
+ $img = new \OC_Image(OC::$SERVERROOT.'/tests/data/testimage.png');
+ $tempFile = tempnam(sys_get_temp_dir(), 'img-test');
+
+ $img->save($tempFile, $mimeType);
+ $actualMimeType = \OC_Image::getMimeTypeForFile($tempFile);
+ $this->assertEquals($mimeType, $actualMimeType);
+ }
}
diff --git a/tests/lib/mail.php b/tests/lib/mail.php
new file mode 100644
index 00000000000..a88a9d797ae
--- /dev/null
+++ b/tests/lib/mail.php
@@ -0,0 +1,30 @@
+<?php
+/**
+ * Copyright (c) 2014 Thomas Müller <deepdiver@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+class Test_Mail extends PHPUnit_Framework_TestCase {
+
+ /**
+ * @dataProvider buildAsciiEmailProvider
+ * @param $expected
+ * @param $address
+ */
+ public function testBuildAsciiEmail($expected, $address) {
+ $actual = \OC_Mail::buildAsciiEmail($address);
+ $this->assertEquals($expected, $actual);
+ }
+
+ function buildAsciiEmailProvider() {
+ return array(
+ array('info@example.com', 'info@example.com'),
+ array('info@xn--cjr6vy5ejyai80u.com', 'info@國際化域名.com'),
+ array('info@xn--mller-kva.de', 'info@müller.de'),
+ array('info@xn--mller-kva.xn--mller-kva.de', 'info@müller.müller.de'),
+ );
+ }
+
+}
diff --git a/tests/lib/request.php b/tests/lib/request.php
index 1d77acc70ae..bff84e1b03f 100644
--- a/tests/lib/request.php
+++ b/tests/lib/request.php
@@ -135,4 +135,141 @@ class Test_Request extends PHPUnit_Framework_TestCase {
),
);
}
+
+ public function testInsecureServerHost() {
+ unset($_SERVER['HTTP_X_FORWARDED_HOST']);
+ unset($_SERVER['HTTP_HOST']);
+ unset($_SERVER['SERVER_NAME']);
+ $_SERVER['SERVER_NAME'] = 'from.server.name:8080';
+ $host = OC_Request::insecureServerHost();
+ $this->assertEquals('from.server.name:8080', $host);
+
+ $_SERVER['HTTP_HOST'] = 'from.host.header:8080';
+ $host = OC_Request::insecureServerHost();
+ $this->assertEquals('from.host.header:8080', $host);
+
+ $_SERVER['HTTP_X_FORWARDED_HOST'] = 'from.forwarded.host:8080';
+ $host = OC_Request::insecureServerHost();
+ $this->assertEquals('from.forwarded.host:8080', $host);
+
+ $_SERVER['HTTP_X_FORWARDED_HOST'] = 'from.forwarded.host2:8080,another.one:9000';
+ $host = OC_Request::insecureServerHost();
+ $this->assertEquals('from.forwarded.host2:8080', $host);
+
+ // clean up
+ unset($_SERVER['HTTP_X_FORWARDED_HOST']);
+ unset($_SERVER['HTTP_HOST']);
+ unset($_SERVER['SERVER_NAME']);
+ }
+
+ public function testGetOverwriteHost() {
+ unset($_SERVER['REMOTE_ADDR']);
+ OC_Config::deleteKey('overwritecondaddr');
+ OC_Config::deleteKey('overwritehost');
+ $host = OC_Request::getOverwriteHost();
+ $this->assertNull($host);
+
+ OC_Config::setValue('overwritehost', '');
+ $host = OC_Request::getOverwriteHost();
+ $this->assertNull($host);
+
+ OC_Config::setValue('overwritehost', 'host.one.test:8080');
+ $host = OC_Request::getOverwriteHost();
+ $this->assertEquals('host.one.test:8080', $host);
+
+ $_SERVER['REMOTE_ADDR'] = 'somehost.test:8080';
+ OC_Config::setValue('overwritecondaddr', '^somehost\..*$');
+ $host = OC_Request::getOverwriteHost();
+ $this->assertEquals('host.one.test:8080', $host);
+
+ OC_Config::setValue('overwritecondaddr', '^somethingelse.*$');
+ $host = OC_Request::getOverwriteHost();
+ $this->assertNull($host);
+
+ // clean up
+ unset($_SERVER['REMOTE_ADDR']);
+ OC_Config::deleteKey('overwritecondaddr');
+ OC_Config::deleteKey('overwritehost');
+ }
+
+ /**
+ * @dataProvider trustedDomainDataProvider
+ */
+ public function testIsTrustedDomain($trustedDomains, $testDomain, $result) {
+ OC_Config::deleteKey('trusted_domains');
+ if ($trustedDomains !== null) {
+ OC_Config::setValue('trusted_domains', $trustedDomains);
+ }
+
+ $this->assertEquals($result, OC_Request::isTrustedDomain($testDomain));
+
+ // clean up
+ OC_Config::deleteKey('trusted_domains');
+ }
+
+ public function trustedDomainDataProvider() {
+ $trustedHostTestList = array('host.one.test:8080', 'host.two.test:8080');
+ return array(
+ // empty defaults to true
+ array(null, 'host.one.test:8080', true),
+ array('', 'host.one.test:8080', true),
+ array(array(), 'host.one.test:8080', true),
+
+ // trust list when defined
+ array($trustedHostTestList, 'host.two.test:8080', true),
+ array($trustedHostTestList, 'host.two.test:9999', false),
+ array($trustedHostTestList, 'host.three.test:8080', false),
+
+ // trust localhost regardless of trust list
+ array($trustedHostTestList, 'localhost', true),
+ array($trustedHostTestList, 'localhost:8080', true),
+ array($trustedHostTestList, '127.0.0.1', true),
+ array($trustedHostTestList, '127.0.0.1:8080', true),
+
+ // do not trust invalid localhosts
+ array($trustedHostTestList, 'localhost:1:2', false),
+ array($trustedHostTestList, 'localhost: evil.host', false),
+ );
+ }
+
+ public function testServerHost() {
+ OC_Config::deleteKey('overwritecondaddr');
+ OC_Config::setValue('overwritehost', 'overwritten.host:8080');
+ OC_Config::setValue(
+ 'trusted_domains',
+ array(
+ 'trusted.host:8080',
+ 'second.trusted.host:8080'
+ )
+ );
+ $_SERVER['HTTP_HOST'] = 'trusted.host:8080';
+
+ // CLI always gives localhost
+ $oldCLI = OC::$CLI;
+ OC::$CLI = true;
+ $host = OC_Request::serverHost();
+ $this->assertEquals('localhost', $host);
+ OC::$CLI = false;
+
+ // overwritehost overrides trusted domain
+ $host = OC_Request::serverHost();
+ $this->assertEquals('overwritten.host:8080', $host);
+
+ // trusted domain returned when used
+ OC_Config::deleteKey('overwritehost');
+ $host = OC_Request::serverHost();
+ $this->assertEquals('trusted.host:8080', $host);
+
+ // trusted domain returned when untrusted one in header
+ $_SERVER['HTTP_HOST'] = 'untrusted.host:8080';
+ OC_Config::deleteKey('overwritehost');
+ $host = OC_Request::serverHost();
+ $this->assertEquals('trusted.host:8080', $host);
+
+ // clean up
+ OC_Config::deleteKey('overwritecondaddr');
+ OC_Config::deleteKey('overwritehost');
+ unset($_SERVER['HTTP_HOST']);
+ OC::$CLI = $oldCLI;
+ }
}
diff --git a/tests/lib/share/share.php b/tests/lib/share/share.php
index b5cba9430aa..aae91fa1087 100644
--- a/tests/lib/share/share.php
+++ b/tests/lib/share/share.php
@@ -282,7 +282,7 @@ class Test_Share extends PHPUnit_Framework_TestCase {
OC_User::setUserId($this->user2);
$this->assertEquals(array(OCP\PERMISSION_READ), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
OC_User::setUserId($this->user3);
- $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt'));
+ $this->assertSame(array(), OCP\Share::getItemSharedWith('test', 'test.txt'));
// Reshare again, and then have owner unshare
OC_User::setUserId($this->user1);
@@ -292,9 +292,9 @@ class Test_Share extends PHPUnit_Framework_TestCase {
OC_User::setUserId($this->user1);
$this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
OC_User::setUserId($this->user2);
- $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt'));
+ $this->assertSame(array(), OCP\Share::getItemSharedWith('test', 'test.txt'));
OC_User::setUserId($this->user3);
- $this->assertFalse(OCP\Share::getItemSharedWith('test', 'test.txt'));
+ $this->assertSame(array(), OCP\Share::getItemSharedWith('test', 'test.txt'));
// Attempt target conflict
OC_User::setUserId($this->user1);
@@ -325,7 +325,7 @@ class Test_Share extends PHPUnit_Framework_TestCase {
);
OC_User::setUserId($this->user2);
- $this->assertFalse(
+ $this->assertSame(array(),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 2 no longer has access to test.txt after expiration.'
);
@@ -526,13 +526,13 @@ class Test_Share extends PHPUnit_Framework_TestCase {
);
OC_User::setUserId($this->user2);
- $this->assertFalse(
+ $this->assertSame(array(),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 2 no longer has access to test.txt after expiration.'
);
OC_User::setUserId($this->user3);
- $this->assertFalse(
+ $this->assertSame(array(),
OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
'Failed asserting that user 3 no longer has access to test.txt after expiration.'
);
diff --git a/tests/lib/urlgenerator.php b/tests/lib/urlgenerator.php
index 875a7f06580..8e605d88f32 100644
--- a/tests/lib/urlgenerator.php
+++ b/tests/lib/urlgenerator.php
@@ -12,17 +12,32 @@ class Test_Urlgenerator extends PHPUnit_Framework_TestCase {
/**
* @small
* @brief test absolute URL construction
- * @dataProvider provideURLs
+ * @dataProvider provideDocRootURLs
*/
- function testGetAbsoluteURL($url, $expectedResult) {
+ function testGetAbsoluteURLDocRoot($url, $expectedResult) {
+ \OC::$WEBROOT = '';
$urlGenerator = new \OC\URLGenerator(null);
$result = $urlGenerator->getAbsoluteURL($url);
$this->assertEquals($expectedResult, $result);
}
- public function provideURLs() {
+ /**
+ * @small
+ * @brief test absolute URL construction
+ * @dataProvider provideSubDirURLs
+ */
+ function testGetAbsoluteURLSubDir($url, $expectedResult) {
+
+ \OC::$WEBROOT = '/owncloud';
+ $urlGenerator = new \OC\URLGenerator(null);
+ $result = $urlGenerator->getAbsoluteURL($url);
+
+ $this->assertEquals($expectedResult, $result);
+ }
+
+ public function provideDocRootURLs() {
return array(
array("index.php", "http://localhost/index.php"),
array("/index.php", "http://localhost/index.php"),
@@ -30,5 +45,14 @@ class Test_Urlgenerator extends PHPUnit_Framework_TestCase {
array("apps/index.php", "http://localhost/apps/index.php"),
);
}
+
+ public function provideSubDirURLs() {
+ return array(
+ array("index.php", "http://localhost/owncloud/index.php"),
+ array("/index.php", "http://localhost/owncloud/index.php"),
+ array("/apps/index.php", "http://localhost/owncloud/apps/index.php"),
+ array("apps/index.php", "http://localhost/owncloud/apps/index.php"),
+ );
+ }
}
diff --git a/tests/lib/utilcheckserver.php b/tests/lib/utilcheckserver.php
new file mode 100644
index 00000000000..155d617c4ad
--- /dev/null
+++ b/tests/lib/utilcheckserver.php
@@ -0,0 +1,108 @@
+<?php
+/**
+ * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+/**
+ * Tests for server check functions
+ */
+class Test_Util_CheckServer extends PHPUnit_Framework_TestCase {
+
+ private $datadir;
+
+ public function setUp() {
+ $this->datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data');
+
+ file_put_contents($this->datadir . '/.ocdata', '');
+ }
+
+ public function tearDown() {
+ // clean up
+ @unlink($this->datadir . '/.ocdata');
+ }
+
+ /**
+ * Test that checkServer() returns no errors in the regular case.
+ */
+ public function testCheckServer() {
+ $result = \OC_Util::checkServer();
+ $this->assertEmpty($result);
+ }
+
+ /**
+ * Test that checkServer() does not check the data dir validity
+ * when the server is not installed yet (else the setup cannot
+ * be run...)
+ */
+ public function testCheckServerSkipDataDirValidityOnSetup() {
+ // simulate old version that didn't have it
+ unlink($this->datadir . '/.ocdata');
+
+ $session = \OC::$server->getSession();
+ $oldInstalled = \OC_Config::getValue('installed', false);
+
+ // simulate that the server isn't setup yet
+ \OC_Config::setValue('installed', false);
+
+ // even though ".ocdata" is missing, the error isn't
+ // triggered to allow setup to run
+ $result = \OC_Util::checkServer();
+ $this->assertEmpty($result);
+
+ // restore config
+ \OC_Config::setValue('installed', $oldInstalled);
+ }
+
+ /**
+ * Test that checkServer() does not check the data dir validity
+ * when an upgrade is required (else the upgrade cannot be
+ * performed...)
+ */
+ public function testCheckServerSkipDataDirValidityOnUpgrade() {
+ // simulate old version that didn't have it
+ unlink($this->datadir . '/.ocdata');
+
+ $session = \OC::$server->getSession();
+ $oldCurrentVersion = $session->get('OC_Version');
+ $oldInstallVersion = \OC_Config::getValue('version', '0.0.0');
+
+ // upgrade condition to simulate needUpgrade() === true
+ $session->set('OC_Version', array(6, 0, 0, 2));
+ \OC_Config::setValue('version', '6.0.0.1');
+
+ // even though ".ocdata" is missing, the error isn't
+ // triggered to allow for upgrade
+ $result = \OC_Util::checkServer();
+ $this->assertEmpty($result);
+
+ // restore versions
+ $session->set('OC_Version', $oldCurrentVersion);
+ \OC_Config::setValue('version', $oldInstallVersion);
+ }
+
+ /**
+ * Test that checkDataDirectoryValidity returns no error
+ * when ".ocdata" is present.
+ */
+ public function testCheckDataDirValidity() {
+ $result = \OC_Util::checkDataDirectoryValidity($this->datadir);
+ $this->assertEmpty($result);
+ }
+
+ /**
+ * Test that checkDataDirectoryValidity and checkServer
+ * both return an error when ".ocdata" is missing.
+ */
+ public function testCheckDataDirValidityWhenFileMissing() {
+ unlink($this->datadir . '/.ocdata');
+ $result = \OC_Util::checkDataDirectoryValidity($this->datadir);
+ $this->assertEquals(1, count($result));
+
+ $result = \OC_Util::checkServer();
+ $this->assertEquals(1, count($result));
+ }
+
+}
diff --git a/tests/phpunit-autotest.xml b/tests/phpunit-autotest.xml
index 1a2ab35491b..872ff2c2596 100644
--- a/tests/phpunit-autotest.xml
+++ b/tests/phpunit-autotest.xml
@@ -36,6 +36,7 @@
</whitelist>
</filter>
<listeners>
+ <listener class="StartSessionListener" file="startsessionlistener.php" />
<listener class="TestCleanupListener" file="testcleanuplistener.php">
<arguments>
<string>detail</string>
diff --git a/tests/phpunit.xml.dist b/tests/phpunit.xml.dist
index 71a4ff2762c..21c63ea0469 100644
--- a/tests/phpunit.xml.dist
+++ b/tests/phpunit.xml.dist
@@ -29,4 +29,7 @@
</exclude>
</whitelist>
</filter>
+ <listeners>
+ <listener class="StartSessionListener" file="startsessionlistener.php" />
+ </listeners>
</phpunit>
diff --git a/tests/startsessionlistener.php b/tests/startsessionlistener.php
new file mode 100644
index 00000000000..808a2a2226f
--- /dev/null
+++ b/tests/startsessionlistener.php
@@ -0,0 +1,44 @@
+<?php
+/**
+ * Copyright (c) 2014 Thomas Müller <deepdiver@owncloud.com>
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+/**
+ * Starts a new session before each test execution
+ */
+class StartSessionListener implements PHPUnit_Framework_TestListener {
+
+ public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {
+ }
+
+ public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {
+ }
+
+ public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
+ }
+
+ public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {
+ }
+
+ public function startTest(PHPUnit_Framework_Test $test) {
+ }
+
+ public function endTest(PHPUnit_Framework_Test $test, $time) {
+ // reopen the session - only allowed for memory session
+ if (\OC::$session instanceof \OC\Session\Memory) {
+ /** @var $session \OC\Session\Memory */
+ $session = \OC::$session;
+ $session->reopen();
+ }
+ }
+
+ public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {
+ }
+
+ public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
+ }
+
+}
diff --git a/tests/testcleanuplistener.php b/tests/testcleanuplistener.php
index 299a589ef4e..2083ffce67c 100644
--- a/tests/testcleanuplistener.php
+++ b/tests/testcleanuplistener.php
@@ -83,6 +83,7 @@ class TestCleanupListener implements PHPUnit_Framework_TestListener {
$knownEntries = array(
'owncloud.log' => true,
'owncloud.db' => true,
+ '.ocdata' => true,
'..' => true,
'.' => true
);