diff options
author | Joas Schilling <nickvergessen@gmx.de> | 2016-05-20 15:38:20 +0200 |
---|---|---|
committer | Thomas Müller <DeepDiver1975@users.noreply.github.com> | 2016-05-20 15:38:20 +0200 |
commit | 94ad54ec9b96d41a614fbbad4a97b34c41a6901f (patch) | |
tree | f3eb7cdda2704aaf0cd59d58efe66bcbd34cb67d /tests/lib/Command | |
parent | 2ef751b1ec28f7b5c7113af60ec8c9fa0ae1cf87 (diff) | |
download | nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.tar.gz nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.zip |
Move tests/ to PSR-4 (#24731)
* Move a-b to PSR-4
* Move c-d to PSR-4
* Move e+g to PSR-4
* Move h-l to PSR-4
* Move m-r to PSR-4
* Move s-u to PSR-4
* Move files/ to PSR-4
* Move remaining tests to PSR-4
* Remove Test\ from old autoloader
Diffstat (limited to 'tests/lib/Command')
-rw-r--r-- | tests/lib/Command/AsyncBusTest.php | 179 | ||||
-rw-r--r-- | tests/lib/Command/BackgroundJobsTest.php | 58 | ||||
-rw-r--r-- | tests/lib/Command/Integrity/SignAppTest.php | 253 | ||||
-rw-r--r-- | tests/lib/Command/Integrity/SignCoreTest.php | 209 |
4 files changed, 699 insertions, 0 deletions
diff --git a/tests/lib/Command/AsyncBusTest.php b/tests/lib/Command/AsyncBusTest.php new file mode 100644 index 00000000000..8c1713f1260 --- /dev/null +++ b/tests/lib/Command/AsyncBusTest.php @@ -0,0 +1,179 @@ +<?php + +/** + * Copyright (c) 2015 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\Command; + +use OC\Command\FileAccess; +use OCP\Command\IBus; +use OCP\Command\ICommand; +use Test\BackgroundJob\DummyJobList; +use Test\TestCase; + +class SimpleCommand implements ICommand { + public function handle() { + AsyncBusTest::$lastCommand = 'SimpleCommand'; + } +} + +class StateFullCommand implements ICommand { + private $state; + + function __construct($state) { + $this->state = $state; + } + + public function handle() { + AsyncBusTest::$lastCommand = $this->state; + } +} + +class FilesystemCommand implements ICommand { + use FileAccess; + + public function handle() { + AsyncBusTest::$lastCommand = 'FileAccess'; + } +} + +function basicFunction() { + AsyncBusTest::$lastCommand = 'function'; +} + +// clean class to prevent phpunit putting closure in $this +class ThisClosureTest { + private function privateMethod() { + AsyncBusTest::$lastCommand = 'closure-this'; + } + + public function test(IBus $bus) { + $bus->push(function () { + $this->privateMethod(); + }); + } +} + +class AsyncBusTest extends TestCase { + /** + * Basic way to check output from a command + * + * @var string + */ + public static $lastCommand; + + /** + * @var \OCP\BackgroundJob\IJobList + */ + private $jobList; + + /** + * @var \OCP\Command\IBus + */ + private $bus; + + public static function DummyCommand() { + self::$lastCommand = 'static'; + } + + public function setUp() { + $this->jobList = new DummyJobList(); + $this->bus = new \OC\Command\AsyncBus($this->jobList); + self::$lastCommand = ''; + } + + public function testSimpleCommand() { + $command = new SimpleCommand(); + $this->bus->push($command); + $this->runJobs(); + $this->assertEquals('SimpleCommand', self::$lastCommand); + } + + public function testStateFullCommand() { + $command = new StateFullCommand('foo'); + $this->bus->push($command); + $this->runJobs(); + $this->assertEquals('foo', self::$lastCommand); + } + + public function testStaticCallable() { + $this->bus->push(['\Test\Command\AsyncBusTest', 'DummyCommand']); + $this->runJobs(); + $this->assertEquals('static', self::$lastCommand); + } + + public function testMemberCallable() { + $command = new StateFullCommand('bar'); + $this->bus->push([$command, 'handle']); + $this->runJobs(); + $this->assertEquals('bar', self::$lastCommand); + } + + public function testFunctionCallable() { + $this->bus->push('\Test\Command\BasicFunction'); + $this->runJobs(); + $this->assertEquals('function', self::$lastCommand); + } + + public function testClosure() { + $this->bus->push(function () { + AsyncBusTest::$lastCommand = 'closure'; + }); + $this->runJobs(); + $this->assertEquals('closure', self::$lastCommand); + } + + public function testClosureSelf() { + $this->bus->push(function () { + self::$lastCommand = 'closure-self'; + }); + $this->runJobs(); + $this->assertEquals('closure-self', self::$lastCommand); + } + + + public function testClosureThis() { + // clean class to prevent phpunit putting closure in $this + $test = new ThisClosureTest(); + $test->test($this->bus); + $this->runJobs(); + $this->assertEquals('closure-this', self::$lastCommand); + } + + public function testClosureBind() { + $state = 'bar'; + $this->bus->push(function () use ($state) { + self::$lastCommand = 'closure-' . $state; + }); + $this->runJobs(); + $this->assertEquals('closure-bar', self::$lastCommand); + } + + public function testFileFileAccessCommand() { + $this->bus->push(new FilesystemCommand()); + $this->assertEquals('', self::$lastCommand); + $this->runJobs(); + $this->assertEquals('FileAccess', self::$lastCommand); + } + + public function testFileFileAccessCommandSync() { + $this->bus->requireSync('\OC\Command\FileAccess'); + $this->bus->push(new FilesystemCommand()); + $this->assertEquals('FileAccess', self::$lastCommand); + self::$lastCommand = ''; + $this->runJobs(); + $this->assertEquals('', self::$lastCommand); + } + + + private function runJobs() { + $jobs = $this->jobList->getAll(); + foreach ($jobs as $job) { + $job->execute($this->jobList); + } + } +} diff --git a/tests/lib/Command/BackgroundJobsTest.php b/tests/lib/Command/BackgroundJobsTest.php new file mode 100644 index 00000000000..6292d5d0e97 --- /dev/null +++ b/tests/lib/Command/BackgroundJobsTest.php @@ -0,0 +1,58 @@ +<?php +/** +* The MIT License (MIT) +* +* Copyright (c) 2015 Christian Kampka <christian@kampka.net> +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +namespace Test\Command; + +use Test\TestCase; +use Symfony\Component\Console\Input\StringInput; +use Symfony\Component\Console\Output\NullOutput; + +use OC\Core\Command\Background\Cron; +use OC\Core\Command\Background\WebCron; +use OC\Core\Command\Background\Ajax; + +class BackgroundJobsTest extends TestCase { + + public function testCronCommand() { + $config = \OC::$server->getConfig(); + $job = new Cron($config); + $job->run(new StringInput(''), new NullOutput()); + $this->assertEquals('cron', $config->getAppValue('core', 'backgroundjobs_mode')); + } + + public function testAjaxCommand() { + $config = \OC::$server->getConfig(); + $job = new Ajax($config); + $job->run(new StringInput(''), new NullOutput()); + $this->assertEquals('ajax', $config->getAppValue('core', 'backgroundjobs_mode')); + } + + public function testWebCronCommand() { + $config = \OC::$server->getConfig(); + $job = new WebCron($config); + $job->run(new StringInput(''), new NullOutput()); + $this->assertEquals('webcron', $config->getAppValue('core', 'backgroundjobs_mode')); + } +} diff --git a/tests/lib/Command/Integrity/SignAppTest.php b/tests/lib/Command/Integrity/SignAppTest.php new file mode 100644 index 00000000000..44a644c45df --- /dev/null +++ b/tests/lib/Command/Integrity/SignAppTest.php @@ -0,0 +1,253 @@ +<?php +/** + * @author Lukas Reschke <lukas@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\Command\Integrity; + +use OC\Core\Command\Integrity\SignApp; +use OC\IntegrityCheck\Checker; +use OC\IntegrityCheck\Helpers\FileAccessHelper; +use OCP\IURLGenerator; +use Test\TestCase; + +class SignAppTest extends TestCase { + /** @var Checker */ + private $checker; + /** @var SignApp */ + private $signApp; + /** @var FileAccessHelper */ + private $fileAccessHelper; + /** @var IURLGenerator */ + private $urlGenerator; + + public function setUp() { + parent::setUp(); + $this->checker = $this->getMockBuilder('\OC\IntegrityCheck\Checker') + ->disableOriginalConstructor()->getMock(); + $this->fileAccessHelper = $this->getMockBuilder('\OC\IntegrityCheck\Helpers\FileAccessHelper') + ->disableOriginalConstructor()->getMock(); + $this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator') + ->disableOriginalConstructor()->getMock(); + $this->signApp = new SignApp( + $this->checker, + $this->fileAccessHelper, + $this->urlGenerator + ); + } + + public function testExecuteWithMissingPath() { + $inputInterface = $this->getMock('\Symfony\Component\Console\Input\InputInterface'); + $outputInterface = $this->getMock('\Symfony\Component\Console\Output\OutputInterface'); + + $inputInterface + ->expects($this->at(0)) + ->method('getOption') + ->with('path') + ->will($this->returnValue(null)); + $inputInterface + ->expects($this->at(1)) + ->method('getOption') + ->with('privateKey') + ->will($this->returnValue('PrivateKey')); + $inputInterface + ->expects($this->at(2)) + ->method('getOption') + ->with('certificate') + ->will($this->returnValue('Certificate')); + + $outputInterface + ->expects($this->at(0)) + ->method('writeln') + ->with('This command requires the --path, --privateKey and --certificate.'); + + $this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]); + } + + public function testExecuteWithMissingPrivateKey() { + $inputInterface = $this->getMock('\Symfony\Component\Console\Input\InputInterface'); + $outputInterface = $this->getMock('\Symfony\Component\Console\Output\OutputInterface'); + + $inputInterface + ->expects($this->at(0)) + ->method('getOption') + ->with('path') + ->will($this->returnValue('AppId')); + $inputInterface + ->expects($this->at(1)) + ->method('getOption') + ->with('privateKey') + ->will($this->returnValue(null)); + $inputInterface + ->expects($this->at(2)) + ->method('getOption') + ->with('certificate') + ->will($this->returnValue('Certificate')); + + $outputInterface + ->expects($this->at(0)) + ->method('writeln') + ->with('This command requires the --path, --privateKey and --certificate.'); + + $this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]); + } + + public function testExecuteWithMissingCertificate() { + $inputInterface = $this->getMock('\Symfony\Component\Console\Input\InputInterface'); + $outputInterface = $this->getMock('\Symfony\Component\Console\Output\OutputInterface'); + + $inputInterface + ->expects($this->at(0)) + ->method('getOption') + ->with('path') + ->will($this->returnValue('AppId')); + $inputInterface + ->expects($this->at(1)) + ->method('getOption') + ->with('privateKey') + ->will($this->returnValue('privateKey')); + $inputInterface + ->expects($this->at(2)) + ->method('getOption') + ->with('certificate') + ->will($this->returnValue(null)); + + $outputInterface + ->expects($this->at(0)) + ->method('writeln') + ->with('This command requires the --path, --privateKey and --certificate.'); + + $this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]); + } + + public function testExecuteWithNotExistingPrivateKey() { + $inputInterface = $this->getMock('\Symfony\Component\Console\Input\InputInterface'); + $outputInterface = $this->getMock('\Symfony\Component\Console\Output\OutputInterface'); + + $inputInterface + ->expects($this->at(0)) + ->method('getOption') + ->with('path') + ->will($this->returnValue('AppId')); + $inputInterface + ->expects($this->at(1)) + ->method('getOption') + ->with('privateKey') + ->will($this->returnValue('privateKey')); + $inputInterface + ->expects($this->at(2)) + ->method('getOption') + ->with('certificate') + ->will($this->returnValue('certificate')); + + $this->fileAccessHelper + ->expects($this->at(0)) + ->method('file_get_contents') + ->with('privateKey') + ->will($this->returnValue(false)); + + $outputInterface + ->expects($this->at(0)) + ->method('writeln') + ->with('Private key "privateKey" does not exists.'); + + $this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]); + } + + public function testExecuteWithNotExistingCertificate() { + $inputInterface = $this->getMock('\Symfony\Component\Console\Input\InputInterface'); + $outputInterface = $this->getMock('\Symfony\Component\Console\Output\OutputInterface'); + + $inputInterface + ->expects($this->at(0)) + ->method('getOption') + ->with('path') + ->will($this->returnValue('AppId')); + $inputInterface + ->expects($this->at(1)) + ->method('getOption') + ->with('privateKey') + ->will($this->returnValue('privateKey')); + $inputInterface + ->expects($this->at(2)) + ->method('getOption') + ->with('certificate') + ->will($this->returnValue('certificate')); + + $this->fileAccessHelper + ->expects($this->at(0)) + ->method('file_get_contents') + ->with('privateKey') + ->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key')); + $this->fileAccessHelper + ->expects($this->at(1)) + ->method('file_get_contents') + ->with('certificate') + ->will($this->returnValue(false)); + + $outputInterface + ->expects($this->at(0)) + ->method('writeln') + ->with('Certificate "certificate" does not exists.'); + + $this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]); + } + + public function testExecute() { + $inputInterface = $this->getMock('\Symfony\Component\Console\Input\InputInterface'); + $outputInterface = $this->getMock('\Symfony\Component\Console\Output\OutputInterface'); + + $inputInterface + ->expects($this->at(0)) + ->method('getOption') + ->with('path') + ->will($this->returnValue('AppId')); + $inputInterface + ->expects($this->at(1)) + ->method('getOption') + ->with('privateKey') + ->will($this->returnValue('privateKey')); + $inputInterface + ->expects($this->at(2)) + ->method('getOption') + ->with('certificate') + ->will($this->returnValue('certificate')); + + $this->fileAccessHelper + ->expects($this->at(0)) + ->method('file_get_contents') + ->with('privateKey') + ->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key')); + $this->fileAccessHelper + ->expects($this->at(1)) + ->method('file_get_contents') + ->with('certificate') + ->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.crt')); + + $this->checker + ->expects($this->once()) + ->method('writeAppSignature'); + + $outputInterface + ->expects($this->at(0)) + ->method('writeln') + ->with('Successfully signed "AppId"'); + + $this->invokePrivate($this->signApp, 'execute', [$inputInterface, $outputInterface]); + } +} diff --git a/tests/lib/Command/Integrity/SignCoreTest.php b/tests/lib/Command/Integrity/SignCoreTest.php new file mode 100644 index 00000000000..ff1f6b23a95 --- /dev/null +++ b/tests/lib/Command/Integrity/SignCoreTest.php @@ -0,0 +1,209 @@ +<?php +/** + * @author Lukas Reschke <lukas@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\Command\Integrity; + +use OC\Core\Command\Integrity\SignCore; +use OC\IntegrityCheck\Checker; +use OC\IntegrityCheck\Helpers\FileAccessHelper; +use Test\TestCase; + +class SignCoreTest extends TestCase { + /** @var Checker */ + private $checker; + /** @var SignCore */ + private $signCore; + /** @var FileAccessHelper */ + private $fileAccessHelper; + + public function setUp() { + parent::setUp(); + $this->checker = $this->getMockBuilder('\OC\IntegrityCheck\Checker') + ->disableOriginalConstructor()->getMock(); + $this->fileAccessHelper = $this->getMockBuilder('\OC\IntegrityCheck\Helpers\FileAccessHelper') + ->disableOriginalConstructor()->getMock(); + $this->signCore = new SignCore( + $this->checker, + $this->fileAccessHelper + ); + } + + public function testExecuteWithMissingPrivateKey() { + $inputInterface = $this->getMock('\Symfony\Component\Console\Input\InputInterface'); + $outputInterface = $this->getMock('\Symfony\Component\Console\Output\OutputInterface'); + + $inputInterface + ->expects($this->at(0)) + ->method('getOption') + ->with('privateKey') + ->will($this->returnValue(null)); + $inputInterface + ->expects($this->at(1)) + ->method('getOption') + ->with('certificate') + ->will($this->returnValue('Certificate')); + + $outputInterface + ->expects($this->at(0)) + ->method('writeln') + ->with('--privateKey, --certificate and --path are required.'); + + $this->invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]); + } + + public function testExecuteWithMissingCertificate() { + $inputInterface = $this->getMock('\Symfony\Component\Console\Input\InputInterface'); + $outputInterface = $this->getMock('\Symfony\Component\Console\Output\OutputInterface'); + + $inputInterface + ->expects($this->at(0)) + ->method('getOption') + ->with('privateKey') + ->will($this->returnValue('privateKey')); + $inputInterface + ->expects($this->at(1)) + ->method('getOption') + ->with('certificate') + ->will($this->returnValue(null)); + + $outputInterface + ->expects($this->at(0)) + ->method('writeln') + ->with('--privateKey, --certificate and --path are required.'); + + $this->invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]); + } + + public function testExecuteWithNotExistingPrivateKey() { + $inputInterface = $this->getMock('\Symfony\Component\Console\Input\InputInterface'); + $outputInterface = $this->getMock('\Symfony\Component\Console\Output\OutputInterface'); + + $inputInterface + ->expects($this->at(0)) + ->method('getOption') + ->with('privateKey') + ->will($this->returnValue('privateKey')); + $inputInterface + ->expects($this->at(1)) + ->method('getOption') + ->with('certificate') + ->will($this->returnValue('certificate')); + $inputInterface + ->expects($this->at(2)) + ->method('getOption') + ->with('path') + ->will($this->returnValue('certificate')); + + $this->fileAccessHelper + ->expects($this->at(0)) + ->method('file_get_contents') + ->with('privateKey') + ->will($this->returnValue(false)); + + $outputInterface + ->expects($this->at(0)) + ->method('writeln') + ->with('Private key "privateKey" does not exists.'); + + $this->invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]); + } + + public function testExecuteWithNotExistingCertificate() { + $inputInterface = $this->getMock('\Symfony\Component\Console\Input\InputInterface'); + $outputInterface = $this->getMock('\Symfony\Component\Console\Output\OutputInterface'); + + $inputInterface + ->expects($this->at(0)) + ->method('getOption') + ->with('privateKey') + ->will($this->returnValue('privateKey')); + $inputInterface + ->expects($this->at(1)) + ->method('getOption') + ->with('certificate') + ->will($this->returnValue('certificate')); + $inputInterface + ->expects($this->at(2)) + ->method('getOption') + ->with('path') + ->will($this->returnValue('certificate')); + + $this->fileAccessHelper + ->expects($this->at(0)) + ->method('file_get_contents') + ->with('privateKey') + ->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key')); + $this->fileAccessHelper + ->expects($this->at(1)) + ->method('file_get_contents') + ->with('certificate') + ->will($this->returnValue(false)); + + $outputInterface + ->expects($this->at(0)) + ->method('writeln') + ->with('Certificate "certificate" does not exists.'); + + $this->invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]); + } + + public function testExecute() { + $inputInterface = $this->getMock('\Symfony\Component\Console\Input\InputInterface'); + $outputInterface = $this->getMock('\Symfony\Component\Console\Output\OutputInterface'); + + $inputInterface + ->expects($this->at(0)) + ->method('getOption') + ->with('privateKey') + ->will($this->returnValue('privateKey')); + $inputInterface + ->expects($this->at(1)) + ->method('getOption') + ->with('certificate') + ->will($this->returnValue('certificate')); + $inputInterface + ->expects($this->at(2)) + ->method('getOption') + ->with('path') + ->will($this->returnValue('certificate')); + + $this->fileAccessHelper + ->expects($this->at(0)) + ->method('file_get_contents') + ->with('privateKey') + ->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.key')); + $this->fileAccessHelper + ->expects($this->at(1)) + ->method('file_get_contents') + ->with('certificate') + ->will($this->returnValue(\OC::$SERVERROOT . '/tests/data/integritycheck/core.crt')); + + $this->checker + ->expects($this->once()) + ->method('writeCoreSignature'); + + $outputInterface + ->expects($this->at(0)) + ->method('writeln') + ->with('Successfully signed "core"'); + + $this->invokePrivate($this->signCore, 'execute', [$inputInterface, $outputInterface]); + } +} |