aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/AppFramework
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/AppFramework')
-rw-r--r--tests/lib/AppFramework/AppTest.php165
-rw-r--r--tests/lib/AppFramework/Controller/ApiControllerTest.php58
-rw-r--r--tests/lib/AppFramework/Controller/ControllerTest.php226
-rw-r--r--tests/lib/AppFramework/Controller/OCSControllerTest.php148
-rw-r--r--tests/lib/AppFramework/Db/EntityTest.php227
-rw-r--r--tests/lib/AppFramework/Db/MapperTest.php291
-rw-r--r--tests/lib/AppFramework/Db/MapperTestUtility.php209
-rw-r--r--tests/lib/AppFramework/DependencyInjection/DIContainerTest.php88
-rw-r--r--tests/lib/AppFramework/Http/ContentSecurityPolicyTest.php429
-rw-r--r--tests/lib/AppFramework/Http/DataResponseTest.php91
-rw-r--r--tests/lib/AppFramework/Http/DispatcherTest.php466
-rw-r--r--tests/lib/AppFramework/Http/DownloadResponseTest.php56
-rw-r--r--tests/lib/AppFramework/Http/EmptyContentSecurityPolicyTest.php430
-rw-r--r--tests/lib/AppFramework/Http/HttpTest.php99
-rw-r--r--tests/lib/AppFramework/Http/JSONResponseTest.php123
-rw-r--r--tests/lib/AppFramework/Http/OCSResponseTest.php72
-rw-r--r--tests/lib/AppFramework/Http/RedirectResponseTest.php57
-rw-r--r--tests/lib/AppFramework/Http/RequestStream.php110
-rw-r--r--tests/lib/AppFramework/Http/RequestTest.php1453
-rw-r--r--tests/lib/AppFramework/Http/ResponseTest.php267
-rw-r--r--tests/lib/AppFramework/Http/StreamResponseTest.php100
-rw-r--r--tests/lib/AppFramework/Http/TemplateResponseTest.php99
-rw-r--r--tests/lib/AppFramework/Middleware/MiddlewareDispatcherTest.php292
-rw-r--r--tests/lib/AppFramework/Middleware/MiddlewareTest.php99
-rw-r--r--tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php236
-rw-r--r--tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php453
-rw-r--r--tests/lib/AppFramework/Middleware/SessionMiddlewareTest.php95
-rw-r--r--tests/lib/AppFramework/Routing/RoutingTest.php283
-rw-r--r--tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php210
-rw-r--r--tests/lib/AppFramework/Utility/SimpleContainerTest.php222
30 files changed, 7154 insertions, 0 deletions
diff --git a/tests/lib/AppFramework/AppTest.php b/tests/lib/AppFramework/AppTest.php
new file mode 100644
index 00000000000..7288e686d52
--- /dev/null
+++ b/tests/lib/AppFramework/AppTest.php
@@ -0,0 +1,165 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework;
+
+use OC\AppFramework\App;
+use OCP\AppFramework\Http\Response;
+
+
+function rrmdir($directory) {
+ $files = array_diff(scandir($directory), array('.','..'));
+ foreach ($files as $file) {
+ if (is_dir($directory . '/' . $file)) {
+ rrmdir($directory . '/' . $file);
+ } else {
+ unlink($directory . '/' . $file);
+ }
+ }
+ return rmdir($directory);
+}
+
+
+class AppTest extends \Test\TestCase {
+
+ private $container;
+ private $io;
+ private $api;
+ private $controller;
+ private $dispatcher;
+ private $params;
+ private $headers;
+ private $output;
+ private $controllerName;
+ private $controllerMethod;
+ private $appPath;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->container = new \OC\AppFramework\DependencyInjection\DIContainer('test', array());
+ $this->controller = $this->getMockBuilder(
+ 'OCP\AppFramework\Controller')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->dispatcher = $this->getMockBuilder(
+ 'OC\AppFramework\Http\Dispatcher')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->io = $this->getMockBuilder('OCP\\AppFramework\\Http\\IOutput')->getMock();
+
+ $this->headers = array('key' => 'value');
+ $this->output = 'hi';
+ $this->controllerName = 'Controller';
+ $this->controllerMethod = 'method';
+
+ $this->container[$this->controllerName] = $this->controller;
+ $this->container['Dispatcher'] = $this->dispatcher;
+ $this->container['OCP\\AppFramework\\Http\\IOutput'] = $this->io;
+ $this->container['urlParams'] = array();
+
+ $this->appPath = __DIR__ . '/../../../apps/namespacetestapp';
+ $infoXmlPath = $this->appPath . '/appinfo/info.xml';
+ mkdir($this->appPath . '/appinfo', 0777, true);
+
+ $xml = '<?xml version="1.0" encoding="UTF-8"?>' .
+ '<info>' .
+ '<id>namespacetestapp</id>' .
+ '<namespace>NameSpaceTestApp</namespace>' .
+ '</info>';
+ file_put_contents($infoXmlPath, $xml);
+ }
+
+
+ public function testControllerNameAndMethodAreBeingPassed(){
+ $return = array(null, array(), array(), null, new Response());
+ $this->dispatcher->expects($this->once())
+ ->method('dispatch')
+ ->with($this->equalTo($this->controller),
+ $this->equalTo($this->controllerMethod))
+ ->will($this->returnValue($return));
+
+ $this->io->expects($this->never())
+ ->method('setOutput');
+
+ App::main($this->controllerName, $this->controllerMethod,
+ $this->container);
+ }
+
+
+ public function testBuildAppNamespace() {
+ $ns = App::buildAppNamespace('someapp');
+ $this->assertEquals('OCA\Someapp', $ns);
+ }
+
+
+ public function testBuildAppNamespaceCore() {
+ $ns = App::buildAppNamespace('someapp', 'OC\\');
+ $this->assertEquals('OC\Someapp', $ns);
+ }
+
+
+ public function testBuildAppNamespaceInfoXml() {
+ $ns = App::buildAppNamespace('namespacetestapp', 'OCA\\');
+ $this->assertEquals('OCA\NameSpaceTestApp', $ns);
+ }
+
+
+ protected function tearDown() {
+ rrmdir($this->appPath);
+ parent::tearDown();
+ }
+
+
+ public function testOutputIsPrinted(){
+ $return = [null, [], [], $this->output, new Response()];
+ $this->dispatcher->expects($this->once())
+ ->method('dispatch')
+ ->with($this->equalTo($this->controller),
+ $this->equalTo($this->controllerMethod))
+ ->will($this->returnValue($return));
+ $this->io->expects($this->once())
+ ->method('setOutput')
+ ->with($this->equalTo($this->output));
+ App::main($this->controllerName, $this->controllerMethod, $this->container, []);
+ }
+
+
+ public function testCallbackIsCalled(){
+ $mock = $this->getMockBuilder('OCP\AppFramework\Http\ICallbackResponse')
+ ->getMock();
+
+ $return = [null, [], [], $this->output, $mock];
+ $this->dispatcher->expects($this->once())
+ ->method('dispatch')
+ ->with($this->equalTo($this->controller),
+ $this->equalTo($this->controllerMethod))
+ ->will($this->returnValue($return));
+ $mock->expects($this->once())
+ ->method('callback');
+ App::main($this->controllerName, $this->controllerMethod, $this->container, []);
+ }
+
+}
diff --git a/tests/lib/AppFramework/Controller/ApiControllerTest.php b/tests/lib/AppFramework/Controller/ApiControllerTest.php
new file mode 100644
index 00000000000..783eecf93e5
--- /dev/null
+++ b/tests/lib/AppFramework/Controller/ApiControllerTest.php
@@ -0,0 +1,58 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Controller;
+
+use OC\AppFramework\Http\Request;
+use OCP\AppFramework\ApiController;
+
+
+class ChildApiController extends ApiController {};
+
+
+class ApiControllerTest extends \Test\TestCase {
+ /** @var ChildApiController */
+ protected $controller;
+
+ public function testCors() {
+ $request = new Request(
+ ['server' => ['HTTP_ORIGIN' => 'test']],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->controller = new ChildApiController('app', $request, 'verbs',
+ 'headers', 100);
+
+ $response = $this->controller->preflightedCors();
+
+ $headers = $response->getHeaders();
+
+ $this->assertEquals('test', $headers['Access-Control-Allow-Origin']);
+ $this->assertEquals('verbs', $headers['Access-Control-Allow-Methods']);
+ $this->assertEquals('headers', $headers['Access-Control-Allow-Headers']);
+ $this->assertEquals('false', $headers['Access-Control-Allow-Credentials']);
+ $this->assertEquals(100, $headers['Access-Control-Max-Age']);
+ }
+
+}
diff --git a/tests/lib/AppFramework/Controller/ControllerTest.php b/tests/lib/AppFramework/Controller/ControllerTest.php
new file mode 100644
index 00000000000..521799a46ce
--- /dev/null
+++ b/tests/lib/AppFramework/Controller/ControllerTest.php
@@ -0,0 +1,226 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Controller;
+
+use OC\AppFramework\Http\Request;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http\TemplateResponse;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\AppFramework\Http\DataResponse;
+
+
+class ChildController extends Controller {
+
+ public function __construct($appName, $request) {
+ parent::__construct($appName, $request);
+ $this->registerResponder('tom', function ($respone) {
+ return 'hi';
+ });
+ }
+
+ public function custom($in) {
+ $this->registerResponder('json', function ($response) {
+ return new JSONResponse(array(strlen($response)));
+ });
+
+ return $in;
+ }
+
+ public function customDataResponse($in) {
+ $response = new DataResponse($in, 300);
+ $response->addHeader('test', 'something');
+ return $response;
+ }
+};
+
+class ControllerTest extends \Test\TestCase {
+
+ /**
+ * @var Controller
+ */
+ private $controller;
+ private $app;
+
+ protected function setUp(){
+ parent::setUp();
+
+ $request = new Request(
+ [
+ 'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
+ 'post' => ['name' => 'Jane Doe', 'nickname' => 'Janey'],
+ 'urlParams' => ['name' => 'Johnny Weissmüller'],
+ 'files' => ['file' => 'filevalue'],
+ 'env' => ['PATH' => 'daheim'],
+ 'session' => ['sezession' => 'kein'],
+ 'method' => 'hi',
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+
+ $this->app = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
+ array('getAppName'), array('test'));
+ $this->app->expects($this->any())
+ ->method('getAppName')
+ ->will($this->returnValue('apptemplate_advanced'));
+
+ $this->controller = new ChildController($this->app, $request);
+ }
+
+
+ public function testParamsGet(){
+ $this->assertEquals('Johnny Weissmüller', $this->controller->params('name', 'Tarzan'));
+ }
+
+
+ public function testParamsGetDefault(){
+ $this->assertEquals('Tarzan', $this->controller->params('Ape Man', 'Tarzan'));
+ }
+
+
+ public function testParamsFile(){
+ $this->assertEquals('filevalue', $this->controller->params('file', 'filevalue'));
+ }
+
+
+ public function testGetUploadedFile(){
+ $this->assertEquals('filevalue', $this->controller->getUploadedFile('file'));
+ }
+
+
+
+ public function testGetUploadedFileDefault(){
+ $this->assertEquals('default', $this->controller->params('files', 'default'));
+ }
+
+
+ public function testGetParams(){
+ $params = array(
+ 'name' => 'Johnny Weissmüller',
+ 'nickname' => 'Janey',
+ );
+
+ $this->assertEquals($params, $this->controller->getParams());
+ }
+
+
+ public function testRender(){
+ $this->assertTrue($this->controller->render('') instanceof TemplateResponse);
+ }
+
+
+ public function testSetParams(){
+ $params = array('john' => 'foo');
+ $response = $this->controller->render('home', $params);
+
+ $this->assertEquals($params, $response->getParams());
+ }
+
+
+ public function testRenderHeaders(){
+ $headers = array('one', 'two');
+ $response = $this->controller->render('', array(), '', $headers);
+
+ $this->assertTrue(in_array($headers[0], $response->getHeaders()));
+ $this->assertTrue(in_array($headers[1], $response->getHeaders()));
+ }
+
+
+ public function testGetRequestMethod(){
+ $this->assertEquals('hi', $this->controller->method());
+ }
+
+
+ public function testGetEnvVariable(){
+ $this->assertEquals('daheim', $this->controller->env('PATH'));
+ }
+
+
+ /**
+ * @expectedException \DomainException
+ */
+ public function testFormatResonseInvalidFormat() {
+ $this->controller->buildResponse(null, 'test');
+ }
+
+
+ public function testFormat() {
+ $response = $this->controller->buildResponse(array('hi'), 'json');
+
+ $this->assertEquals(array('hi'), $response->getData());
+ }
+
+
+ public function testFormatDataResponseJSON() {
+ $expectedHeaders = [
+ 'test' => 'something',
+ 'Cache-Control' => 'no-cache, must-revalidate',
+ 'Content-Type' => 'application/json; charset=utf-8',
+ 'Content-Security-Policy' => "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'",
+ ];
+
+ $response = $this->controller->customDataResponse(array('hi'));
+ $response = $this->controller->buildResponse($response, 'json');
+
+ $this->assertEquals(array('hi'), $response->getData());
+ $this->assertEquals(300, $response->getStatus());
+ $this->assertEquals($expectedHeaders, $response->getHeaders());
+ }
+
+
+ public function testCustomFormatter() {
+ $response = $this->controller->custom('hi');
+ $response = $this->controller->buildResponse($response, 'json');
+
+ $this->assertEquals(array(2), $response->getData());
+ }
+
+
+ public function testDefaultResponderToJSON() {
+ $responder = $this->controller->getResponderByHTTPHeader('*/*');
+
+ $this->assertEquals('json', $responder);
+ }
+
+
+ public function testResponderAcceptHeaderParsed() {
+ $responder = $this->controller->getResponderByHTTPHeader(
+ '*/*, application/tom, application/json'
+ );
+
+ $this->assertEquals('tom', $responder);
+ }
+
+
+ public function testResponderAcceptHeaderParsedUpperCase() {
+ $responder = $this->controller->getResponderByHTTPHeader(
+ '*/*, apPlication/ToM, application/json'
+ );
+
+ $this->assertEquals('tom', $responder);
+ }
+
+
+}
diff --git a/tests/lib/AppFramework/Controller/OCSControllerTest.php b/tests/lib/AppFramework/Controller/OCSControllerTest.php
new file mode 100644
index 00000000000..f69740d4496
--- /dev/null
+++ b/tests/lib/AppFramework/Controller/OCSControllerTest.php
@@ -0,0 +1,148 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2015 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Controller;
+
+use OC\AppFramework\Http\Request;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\OCSController;
+
+
+class ChildOCSController extends OCSController {}
+
+
+class OCSControllerTest extends \Test\TestCase {
+
+ private $controller;
+
+ public function testCors() {
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTP_ORIGIN' => 'test',
+ ],
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $controller = new ChildOCSController('app', $request, 'verbs',
+ 'headers', 100);
+
+ $response = $controller->preflightedCors();
+
+ $headers = $response->getHeaders();
+
+ $this->assertEquals('test', $headers['Access-Control-Allow-Origin']);
+ $this->assertEquals('verbs', $headers['Access-Control-Allow-Methods']);
+ $this->assertEquals('headers', $headers['Access-Control-Allow-Headers']);
+ $this->assertEquals('false', $headers['Access-Control-Allow-Credentials']);
+ $this->assertEquals(100, $headers['Access-Control-Max-Age']);
+ }
+
+
+ public function testXML() {
+ $controller = new ChildOCSController('app', new Request(
+ [],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ ));
+ $expected = "<?xml version=\"1.0\"?>\n" .
+ "<ocs>\n" .
+ " <meta>\n" .
+ " <status>failure</status>\n" .
+ " <statuscode>400</statuscode>\n" .
+ " <message>OK</message>\n" .
+ " <totalitems></totalitems>\n" .
+ " <itemsperpage></itemsperpage>\n" .
+ " </meta>\n" .
+ " <data>\n" .
+ " <test>hi</test>\n" .
+ " </data>\n" .
+ "</ocs>\n";
+
+ $params = [
+ 'data' => [
+ 'test' => 'hi'
+ ],
+ 'statuscode' => 400
+ ];
+
+ $out = $controller->buildResponse($params, 'xml')->render();
+ $this->assertEquals($expected, $out);
+ }
+
+
+ public function testXMLDataResponse() {
+ $controller = new ChildOCSController('app', new Request(
+ [],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ ));
+ $expected = "<?xml version=\"1.0\"?>\n" .
+ "<ocs>\n" .
+ " <meta>\n" .
+ " <status>failure</status>\n" .
+ " <statuscode>400</statuscode>\n" .
+ " <message>OK</message>\n" .
+ " <totalitems></totalitems>\n" .
+ " <itemsperpage></itemsperpage>\n" .
+ " </meta>\n" .
+ " <data>\n" .
+ " <test>hi</test>\n" .
+ " </data>\n" .
+ "</ocs>\n";
+
+ $params = new DataResponse([
+ 'data' => [
+ 'test' => 'hi'
+ ],
+ 'statuscode' => 400
+ ]);
+
+ $out = $controller->buildResponse($params, 'xml')->render();
+ $this->assertEquals($expected, $out);
+ }
+
+
+ public function testJSON() {
+ $controller = new ChildOCSController('app', new Request(
+ [],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ ));
+ $expected = '{"ocs":{"meta":{"status":"failure","statuscode":400,"message":"OK",' .
+ '"totalitems":"","itemsperpage":""},"data":{"test":"hi"}}}';
+ $params = [
+ 'data' => [
+ 'test' => 'hi'
+ ],
+ 'statuscode' => 400
+ ];
+
+ $out = $controller->buildResponse($params, 'json')->render();
+ $this->assertEquals($expected, $out);
+ }
+
+
+}
diff --git a/tests/lib/AppFramework/Db/EntityTest.php b/tests/lib/AppFramework/Db/EntityTest.php
new file mode 100644
index 00000000000..e1a3d7533be
--- /dev/null
+++ b/tests/lib/AppFramework/Db/EntityTest.php
@@ -0,0 +1,227 @@
+<?php
+
+/**
+* ownCloud - App Framework
+*
+* @author Bernhard Posselt
+* @copyright 2012 Bernhard Posselt dev@bernhard-posselt.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/>.
+*
+*/
+
+namespace Test\AppFramework\Db;
+
+
+use OCP\AppFramework\Db\Entity;
+
+
+/**
+ * @method integer getId()
+ * @method void setId(integer $id)
+ * @method integer getTestId()
+ * @method void setTestId(integer $id)
+ * @method string getName()
+ * @method void setName(string $name)
+ * @method string getEmail()
+ * @method void setEmail(string $email)
+ * @method string getPreName()
+ * @method void setPreName(string $preName)
+ */
+class TestEntity extends Entity {
+ protected $name;
+ protected $email;
+ protected $testId;
+ protected $preName;
+
+ public function __construct($name=null){
+ $this->addType('testId', 'integer');
+ $this->name = $name;
+ }
+};
+
+
+class EntityTest extends \Test\TestCase {
+
+ private $entity;
+
+ protected function setUp(){
+ parent::setUp();
+ $this->entity = new TestEntity();
+ }
+
+
+ public function testResetUpdatedFields(){
+ $entity = new TestEntity();
+ $entity->setId(3);
+ $entity->resetUpdatedFields();
+
+ $this->assertEquals(array(), $entity->getUpdatedFields());
+ }
+
+
+ public function testFromRow(){
+ $row = array(
+ 'pre_name' => 'john',
+ 'email' => 'john@something.com'
+ );
+ $this->entity = TestEntity::fromRow($row);
+
+ $this->assertEquals($row['pre_name'], $this->entity->getPreName());
+ $this->assertEquals($row['email'], $this->entity->getEmail());
+ }
+
+
+ public function testGetSetId(){
+ $id = 3;
+ $this->entity->setId(3);
+
+ $this->assertEquals($id, $this->entity->getId());
+ }
+
+
+ public function testColumnToPropertyNoReplacement(){
+ $column = 'my';
+ $this->assertEquals('my',
+ $this->entity->columnToProperty($column));
+ }
+
+
+ public function testColumnToProperty(){
+ $column = 'my_attribute';
+ $this->assertEquals('myAttribute',
+ $this->entity->columnToProperty($column));
+ }
+
+
+ public function testPropertyToColumnNoReplacement(){
+ $property = 'my';
+ $this->assertEquals('my',
+ $this->entity->propertyToColumn($property));
+ }
+
+
+ public function testSetterMarksFieldUpdated(){
+ $this->entity->setId(3);
+
+ $this->assertContains('id', $this->entity->getUpdatedFields());
+ }
+
+
+ public function testCallShouldOnlyWorkForGetterSetter(){
+ $this->setExpectedException('\BadFunctionCallException');
+
+ $this->entity->something();
+ }
+
+
+ public function testGetterShouldFailIfAttributeNotDefined(){
+ $this->setExpectedException('\BadFunctionCallException');
+
+ $this->entity->getTest();
+ }
+
+
+ public function testSetterShouldFailIfAttributeNotDefined(){
+ $this->setExpectedException('\BadFunctionCallException');
+
+ $this->entity->setTest();
+ }
+
+
+ public function testFromRowShouldNotAssignEmptyArray(){
+ $row = array();
+ $entity2 = new TestEntity();
+
+ $this->entity = TestEntity::fromRow($row);
+ $this->assertEquals($entity2, $this->entity);
+ }
+
+
+ public function testIdGetsConvertedToInt(){
+ $row = array('id' => '4');
+
+ $this->entity = TestEntity::fromRow($row);
+ $this->assertSame(4, $this->entity->getId());
+ }
+
+
+ public function testSetType(){
+ $row = array('testId' => '4');
+
+ $this->entity = TestEntity::fromRow($row);
+ $this->assertSame(4, $this->entity->getTestId());
+ }
+
+
+ public function testFromParams(){
+ $params = array(
+ 'testId' => 4,
+ 'email' => 'john@doe'
+ );
+
+ $entity = TestEntity::fromParams($params);
+
+ $this->assertEquals($params['testId'], $entity->getTestId());
+ $this->assertEquals($params['email'], $entity->getEmail());
+ $this->assertTrue($entity instanceof TestEntity);
+ }
+
+ public function testSlugify(){
+ $entity = new TestEntity();
+ $entity->setName('Slugify this!');
+ $this->assertEquals('slugify-this', $entity->slugify('name'));
+ $entity->setName('°!"§$%&/()=?`´ß\}][{³²#\'+~*-_.:,;<>|äöüÄÖÜSlugify this!');
+ $this->assertEquals('slugify-this', $entity->slugify('name'));
+ }
+
+
+ public function testSetterCasts() {
+ $entity = new TestEntity();
+ $entity->setId('3');
+ $this->assertSame(3, $entity->getId());
+ }
+
+
+ public function testSetterDoesNotCastOnNull() {
+ $entity = new TestEntity();
+ $entity->setId(null);
+ $this->assertSame(null, $entity->getId());
+ }
+
+
+ public function testGetFieldTypes() {
+ $entity = new TestEntity();
+ $this->assertEquals(array(
+ 'id' => 'integer',
+ 'testId' => 'integer'
+ ), $entity->getFieldTypes());
+ }
+
+
+ public function testGetItInt() {
+ $entity = new TestEntity();
+ $entity->setId(3);
+ $this->assertEquals('integer', gettype($entity->getId()));
+ }
+
+
+ public function testFieldsNotMarkedUpdatedIfNothingChanges() {
+ $entity = new TestEntity('hey');
+ $entity->setName('hey');
+ $this->assertEquals(0, count($entity->getUpdatedFields()));
+ }
+
+
+}
diff --git a/tests/lib/AppFramework/Db/MapperTest.php b/tests/lib/AppFramework/Db/MapperTest.php
new file mode 100644
index 00000000000..108e4eee28e
--- /dev/null
+++ b/tests/lib/AppFramework/Db/MapperTest.php
@@ -0,0 +1,291 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Db;
+
+use \OCP\IDBConnection;
+use \OCP\AppFramework\Db\Entity;
+use \OCP\AppFramework\Db\Mapper;
+
+/**
+ * @method integer getId()
+ * @method void setId(integer $id)
+ * @method string getEmail()
+ * @method void setEmail(string $email)
+ * @method string getPreName()
+ * @method void setPreName(string $preName)
+ */
+class Example extends Entity {
+ protected $preName;
+ protected $email;
+};
+
+
+class ExampleMapper extends Mapper {
+ public function __construct(IDBConnection $db){ parent::__construct($db, 'table'); }
+ public function find($table, $id){ return $this->findOneQuery($table, $id); }
+ public function findOneEntity($table, $id){ return $this->findEntity($table, $id); }
+ public function findAllEntities($table){ return $this->findEntities($table); }
+ public function mapRow($row){ return $this->mapRowToEntity($row); }
+ public function execSql($sql, $params){ return $this->execute($sql, $params); }
+}
+
+
+class MapperTest extends MapperTestUtility {
+
+ /**
+ * @var Mapper
+ */
+ private $mapper;
+
+ protected function setUp(){
+ parent::setUp();
+ $this->mapper = new ExampleMapper($this->db);
+ }
+
+
+ public function testMapperShouldSetTableName(){
+ $this->assertEquals('*PREFIX*table', $this->mapper->getTableName());
+ }
+
+
+ public function testFindQuery(){
+ $sql = 'hi';
+ $params = array('jo');
+ $rows = array(
+ array('hi')
+ );
+ $this->setMapperResult($sql, $params, $rows);
+ $this->mapper->find($sql, $params);
+ }
+
+ public function testFindEntity(){
+ $sql = 'hi';
+ $params = array('jo');
+ $rows = array(
+ array('pre_name' => 'hi')
+ );
+ $this->setMapperResult($sql, $params, $rows, null, null, true);
+ $this->mapper->findOneEntity($sql, $params);
+ }
+
+ public function testFindNotFound(){
+ $sql = 'hi';
+ $params = array('jo');
+ $rows = array();
+ $this->setMapperResult($sql, $params, $rows);
+ $this->setExpectedException(
+ '\OCP\AppFramework\Db\DoesNotExistException');
+ $this->mapper->find($sql, $params);
+ }
+
+ public function testFindEntityNotFound(){
+ $sql = 'hi';
+ $params = array('jo');
+ $rows = array();
+ $this->setMapperResult($sql, $params, $rows, null, null, true);
+ $this->setExpectedException(
+ '\OCP\AppFramework\Db\DoesNotExistException');
+ $this->mapper->findOneEntity($sql, $params);
+ }
+
+ public function testFindMultiple(){
+ $sql = 'hi';
+ $params = array('jo');
+ $rows = array(
+ array('jo'), array('ho')
+ );
+ $this->setMapperResult($sql, $params, $rows, null, null, true);
+ $this->setExpectedException(
+ '\OCP\AppFramework\Db\MultipleObjectsReturnedException');
+ $this->mapper->find($sql, $params);
+ }
+
+ public function testFindEntityMultiple(){
+ $sql = 'hi';
+ $params = array('jo');
+ $rows = array(
+ array('jo'), array('ho')
+ );
+ $this->setMapperResult($sql, $params, $rows, null, null, true);
+ $this->setExpectedException(
+ '\OCP\AppFramework\Db\MultipleObjectsReturnedException');
+ $this->mapper->findOneEntity($sql, $params);
+ }
+
+
+ public function testDelete(){
+ $sql = 'DELETE FROM `*PREFIX*table` WHERE `id` = ?';
+ $params = array(2);
+
+ $this->setMapperResult($sql, $params, [], null, null, true);
+ $entity = new Example();
+ $entity->setId($params[0]);
+
+ $this->mapper->delete($entity);
+ }
+
+
+ public function testCreate(){
+ $this->db->expects($this->once())
+ ->method('lastInsertId')
+ ->with($this->equalTo('*PREFIX*table'))
+ ->will($this->returnValue(3));
+ $this->mapper = new ExampleMapper($this->db);
+
+ $sql = 'INSERT INTO `*PREFIX*table`(`pre_name`,`email`) ' .
+ 'VALUES(?,?)';
+ $params = array('john', 'my@email');
+ $entity = new Example();
+ $entity->setPreName($params[0]);
+ $entity->setEmail($params[1]);
+
+ $this->setMapperResult($sql, $params, [], null, null, true);
+
+ $this->mapper->insert($entity);
+ }
+
+
+ public function testCreateShouldReturnItemWithCorrectInsertId(){
+ $this->db->expects($this->once())
+ ->method('lastInsertId')
+ ->with($this->equalTo('*PREFIX*table'))
+ ->will($this->returnValue(3));
+ $this->mapper = new ExampleMapper($this->db);
+
+ $sql = 'INSERT INTO `*PREFIX*table`(`pre_name`,`email`) ' .
+ 'VALUES(?,?)';
+ $params = array('john', 'my@email');
+ $entity = new Example();
+ $entity->setPreName($params[0]);
+ $entity->setEmail($params[1]);
+
+ $this->setMapperResult($sql, $params);
+
+ $result = $this->mapper->insert($entity);
+
+ $this->assertEquals(3, $result->getId());
+ }
+
+
+ public function testAssocParameters() {
+ $sql = 'test';
+ $params = [':test' => 1, ':a' => 2];
+
+ $this->setMapperResult($sql, $params);
+ $this->mapper->execSql($sql, $params);
+ }
+
+
+ public function testUpdate(){
+ $sql = 'UPDATE `*PREFIX*table` ' .
+ 'SET ' .
+ '`pre_name` = ?,'.
+ '`email` = ? ' .
+ 'WHERE `id` = ?';
+
+ $params = array('john', 'my@email', 1);
+ $entity = new Example();
+ $entity->setPreName($params[0]);
+ $entity->setEmail($params[1]);
+ $entity->setId($params[2]);
+
+ $this->setMapperResult($sql, $params, [], null, null, true);
+
+ $this->mapper->update($entity);
+ }
+
+
+ public function testUpdateNoId(){
+ $params = array('john', 'my@email');
+ $entity = new Example();
+ $entity->setPreName($params[0]);
+ $entity->setEmail($params[1]);
+
+ $this->setExpectedException('InvalidArgumentException');
+
+ $this->mapper->update($entity);
+ }
+
+
+ public function testUpdateNothingChangedNoQuery(){
+ $params = array('john', 'my@email');
+ $entity = new Example();
+ $entity->setId(3);
+ $entity->setEmail($params[1]);
+ $entity->resetUpdatedFields();
+
+ $this->db->expects($this->never())
+ ->method('prepare');
+
+ $this->mapper->update($entity);
+ }
+
+
+ public function testMapRowToEntity(){
+ $entity1 = $this->mapper->mapRow(array('pre_name' => 'test1', 'email' => 'test2'));
+ $entity2 = new Example();
+ $entity2->setPreName('test1');
+ $entity2->setEmail('test2');
+ $entity2->resetUpdatedFields();
+ $this->assertEquals($entity2, $entity1);
+ }
+
+ public function testFindEntities(){
+ $sql = 'hi';
+ $rows = array(
+ array('pre_name' => 'hi')
+ );
+ $entity = new Example();
+ $entity->setPreName('hi');
+ $entity->resetUpdatedFields();
+ $this->setMapperResult($sql, array(), $rows, null, null, true);
+ $result = $this->mapper->findAllEntities($sql);
+ $this->assertEquals(array($entity), $result);
+ }
+
+ public function testFindEntitiesNotFound(){
+ $sql = 'hi';
+ $rows = array();
+ $this->setMapperResult($sql, array(), $rows);
+ $result = $this->mapper->findAllEntities($sql);
+ $this->assertEquals(array(), $result);
+ }
+
+ public function testFindEntitiesMultiple(){
+ $sql = 'hi';
+ $rows = array(
+ array('pre_name' => 'jo'), array('email' => 'ho')
+ );
+ $entity1 = new Example();
+ $entity1->setPreName('jo');
+ $entity1->resetUpdatedFields();
+ $entity2 = new Example();
+ $entity2->setEmail('ho');
+ $entity2->resetUpdatedFields();
+ $this->setMapperResult($sql, array(), $rows);
+ $result = $this->mapper->findAllEntities($sql);
+ $this->assertEquals(array($entity1, $entity2), $result);
+ }
+}
diff --git a/tests/lib/AppFramework/Db/MapperTestUtility.php b/tests/lib/AppFramework/Db/MapperTestUtility.php
new file mode 100644
index 00000000000..69f0ad4eb67
--- /dev/null
+++ b/tests/lib/AppFramework/Db/MapperTestUtility.php
@@ -0,0 +1,209 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Db;
+
+
+/**
+ * Simple utility class for testing mappers
+ */
+abstract class MapperTestUtility extends \Test\TestCase {
+ protected $db;
+ private $query;
+ private $queryAt;
+ private $prepareAt;
+ private $fetchAt;
+ private $iterators;
+
+
+ /**
+ * Run this function before the actual test to either set or initialize the
+ * db. After this the db can be accessed by using $this->db
+ */
+ protected function setUp(){
+ parent::setUp();
+
+ $this->db = $this->getMockBuilder(
+ '\OCP\IDBConnection')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->query = $this->getMock('\PDOStatement');
+ $this->queryAt = 0;
+ $this->prepareAt = 0;
+ $this->iterators = [];
+ $this->fetchAt = 0;
+ }
+
+ /**
+ * Checks if an array is associative
+ * @param array $array
+ * @return bool true if associative
+ */
+ private function isAssocArray(array $array) {
+ return array_values($array) !== $array;
+ }
+
+ /**
+ * Returns the correct PDO constant based on the value type
+ * @param $value
+ * @return int PDO constant
+ */
+ private function getPDOType($value) {
+ switch (gettype($value)) {
+ case 'integer':
+ return \PDO::PARAM_INT;
+ case 'boolean':
+ return \PDO::PARAM_BOOL;
+ default:
+ return \PDO::PARAM_STR;
+ }
+ }
+
+ /**
+ * Create mocks and set expected results for database queries
+ * @param string $sql the sql query that you expect to receive
+ * @param array $arguments the expected arguments for the prepare query
+ * method
+ * @param array $returnRows the rows that should be returned for the result
+ * of the database query. If not provided, it wont be assumed that fetch
+ * will be called on the result
+ */
+ protected function setMapperResult($sql, $arguments=array(), $returnRows=array(),
+ $limit=null, $offset=null, $expectClose=false){
+ if($limit === null && $offset === null) {
+ $this->db->expects($this->at($this->prepareAt))
+ ->method('prepare')
+ ->with($this->equalTo($sql))
+ ->will(($this->returnValue($this->query)));
+ } elseif($limit !== null && $offset === null) {
+ $this->db->expects($this->at($this->prepareAt))
+ ->method('prepare')
+ ->with($this->equalTo($sql), $this->equalTo($limit))
+ ->will(($this->returnValue($this->query)));
+ } elseif($limit === null && $offset !== null) {
+ $this->db->expects($this->at($this->prepareAt))
+ ->method('prepare')
+ ->with($this->equalTo($sql),
+ $this->equalTo(null),
+ $this->equalTo($offset))
+ ->will(($this->returnValue($this->query)));
+ } else {
+ $this->db->expects($this->at($this->prepareAt))
+ ->method('prepare')
+ ->with($this->equalTo($sql),
+ $this->equalTo($limit),
+ $this->equalTo($offset))
+ ->will(($this->returnValue($this->query)));
+ }
+
+ $this->iterators[] = new ArgumentIterator($returnRows);
+
+ $iterators = $this->iterators;
+ $fetchAt = $this->fetchAt;
+
+ $this->query->expects($this->any())
+ ->method('fetch')
+ ->will($this->returnCallback(
+ function() use ($iterators, $fetchAt){
+ $iterator = $iterators[$fetchAt];
+ $result = $iterator->next();
+
+ if($result === false) {
+ $fetchAt++;
+ }
+
+ $this->queryAt++;
+
+ return $result;
+ }
+ ));
+
+ if ($this->isAssocArray($arguments)) {
+ foreach($arguments as $key => $argument) {
+ $pdoConstant = $this->getPDOType($argument);
+ $this->query->expects($this->at($this->queryAt))
+ ->method('bindValue')
+ ->with($this->equalTo($key),
+ $this->equalTo($argument),
+ $this->equalTo($pdoConstant));
+ $this->queryAt++;
+ }
+ } else {
+ $index = 1;
+ foreach($arguments as $argument) {
+ $pdoConstant = $this->getPDOType($argument);
+ $this->query->expects($this->at($this->queryAt))
+ ->method('bindValue')
+ ->with($this->equalTo($index),
+ $this->equalTo($argument),
+ $this->equalTo($pdoConstant));
+ $index++;
+ $this->queryAt++;
+ }
+ }
+
+ $this->query->expects($this->at($this->queryAt))
+ ->method('execute')
+ ->will($this->returnCallback(function($sql, $p=null, $o=null, $s=null) {
+
+ }));
+ $this->queryAt++;
+
+
+
+ if ($expectClose) {
+ $closing = $this->at($this->queryAt);
+ } else {
+ $closing = $this->any();
+ }
+ $this->query->expects($closing)->method('closeCursor');
+ $this->queryAt++;
+
+ $this->prepareAt++;
+ $this->fetchAt++;
+ }
+
+
+}
+
+
+class ArgumentIterator {
+
+ private $arguments;
+
+ public function __construct($arguments){
+ $this->arguments = $arguments;
+ }
+
+ public function next(){
+ $result = array_shift($this->arguments);
+ if($result === null){
+ return false;
+ } else {
+ return $result;
+ }
+ }
+}
+
diff --git a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php
new file mode 100644
index 00000000000..5aa000fa25a
--- /dev/null
+++ b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php
@@ -0,0 +1,88 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @author Morris Jobke
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
+ * @copyright 2013 Morris Jobke <morris.jobke@gmail.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\DependencyInjection;
+
+
+use \OC\AppFramework\Http\Request;
+
+class DIContainerTest extends \Test\TestCase {
+
+ private $container;
+ private $api;
+
+ protected function setUp(){
+ parent::setUp();
+ $this->container = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
+ ['isAdminUser'], ['name']
+ );
+ $this->api = $this->getMock('OC\AppFramework\Core\API', array(), array('hi'));
+ }
+
+ public function testProvidesAPI(){
+ $this->assertTrue(isset($this->container['API']));
+ }
+
+
+ public function testProvidesRequest(){
+ $this->assertTrue(isset($this->container['Request']));
+ }
+
+
+ public function testProvidesSecurityMiddleware(){
+ $this->assertTrue(isset($this->container['SecurityMiddleware']));
+ }
+
+
+ public function testProvidesMiddlewareDispatcher(){
+ $this->assertTrue(isset($this->container['MiddlewareDispatcher']));
+ }
+
+
+ public function testProvidesAppName(){
+ $this->assertTrue(isset($this->container['AppName']));
+ }
+
+
+ public function testAppNameIsSetCorrectly(){
+ $this->assertEquals('name', $this->container['AppName']);
+ }
+
+
+ public function testMiddlewareDispatcherIncludesSecurityMiddleware(){
+ $this->container['Request'] = new Request(
+ ['method' => 'GET'],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $security = $this->container['SecurityMiddleware'];
+ $dispatcher = $this->container['MiddlewareDispatcher'];
+
+ $this->assertContains($security, $dispatcher->getMiddlewares());
+ }
+
+
+}
diff --git a/tests/lib/AppFramework/Http/ContentSecurityPolicyTest.php b/tests/lib/AppFramework/Http/ContentSecurityPolicyTest.php
new file mode 100644
index 00000000000..82f2b3873b7
--- /dev/null
+++ b/tests/lib/AppFramework/Http/ContentSecurityPolicyTest.php
@@ -0,0 +1,429 @@
+<?php
+/**
+ * Copyright (c) 2015 Lukas Reschke lukas@owncloud.com
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+
+namespace Test\AppFramework\Http;
+
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\ContentSecurityPolicy;
+
+/**
+ * Class ContentSecurityPolicyTest
+ *
+ * @package OC\AppFramework\Http
+ */
+class ContentSecurityPolicyTest extends \Test\TestCase {
+
+ /** @var ContentSecurityPolicy */
+ private $contentSecurityPolicy;
+
+ public function setUp() {
+ parent::setUp();
+ $this->contentSecurityPolicy = new ContentSecurityPolicy();
+ }
+
+ public function testGetPolicyDefault() {
+ $defaultPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+ $this->assertSame($defaultPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyScriptDomainValid() {
+ $expectedPolicy = "default-src 'none';script-src 'self' www.owncloud.com 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyScriptDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' www.owncloud.com www.owncloud.org 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowScriptDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowScriptDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowScriptDomainMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' www.owncloud.com 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowScriptDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowScriptDomainMultipleStacked() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowScriptDomain('www.owncloud.org')->disallowScriptDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyScriptAllowInline() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-inline' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->allowInlineScript(true);
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyScriptAllowInlineWithDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' www.owncloud.com 'unsafe-inline' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->allowInlineScript(true);
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyScriptDisallowInlineAndEval() {
+ $expectedPolicy = "default-src 'none';script-src 'self';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->allowInlineScript(false);
+ $this->contentSecurityPolicy->allowEvalScript(false);
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyStyleDomainValid() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' www.owncloud.com 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyStyleDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' www.owncloud.com www.owncloud.org 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowStyleDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowStyleDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowStyleDomainMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' www.owncloud.com 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowStyleDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowStyleDomainMultipleStacked() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowStyleDomain('www.owncloud.org')->disallowStyleDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyStyleAllowInline() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->allowInlineStyle(true);
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyStyleAllowInlineWithDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' www.owncloud.com 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyStyleDisallowInline() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->allowInlineStyle(false);
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyImageDomainValid() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob: www.owncloud.com;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyImageDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob: www.owncloud.com www.owncloud.org;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowImageDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowImageDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowImageDomainMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob: www.owncloud.com;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowImageDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowImageDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowImageDomain('www.owncloud.org')->disallowImageDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyFontDomainValid() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self' www.owncloud.com;connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyFontDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self' www.owncloud.com www.owncloud.org;connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFontDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFontDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFontDomainMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self' www.owncloud.com;connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFontDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFontDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFontDomain('www.owncloud.org')->disallowFontDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyConnectDomainValid() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self' www.owncloud.com;media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyConnectDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self' www.owncloud.com www.owncloud.org;media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowConnectDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowConnectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowConnectDomainMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self' www.owncloud.com;media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowConnectDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowConnectDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowConnectDomain('www.owncloud.org')->disallowConnectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyMediaDomainValid() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self' www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyMediaDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self' www.owncloud.com www.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowMediaDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowMediaDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowMediaDomainMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self' www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowMediaDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowMediaDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowMediaDomain('www.owncloud.org')->disallowMediaDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyObjectDomainValid() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self';object-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyObjectDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self';object-src www.owncloud.com www.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowObjectDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowObjectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowObjectDomainMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self';object-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowObjectDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowObjectDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowObjectDomain('www.owncloud.org')->disallowObjectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetAllowedFrameDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self';frame-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyFrameDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self';frame-src www.owncloud.com www.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFrameDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFrameDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFrameDomainMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self';frame-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFrameDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFrameDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFrameDomain('www.owncloud.org')->disallowFrameDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetAllowedChildSrcDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self';child-src child.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyChildSrcValidMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self';child-src child.owncloud.com child.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowChildSrcDomain() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowChildSrcDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowChildSrcDomainMultiple() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self';child-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowChildSrcDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowChildSrcDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowChildSrcDomain('www.owncloud.org')->disallowChildSrcDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+}
diff --git a/tests/lib/AppFramework/Http/DataResponseTest.php b/tests/lib/AppFramework/Http/DataResponseTest.php
new file mode 100644
index 00000000000..cde553cdf5f
--- /dev/null
+++ b/tests/lib/AppFramework/Http/DataResponseTest.php
@@ -0,0 +1,91 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2014 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Http;
+
+
+use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\Http;
+
+
+class DataResponseTest extends \Test\TestCase {
+
+ /**
+ * @var DataResponse
+ */
+ private $response;
+
+ protected function setUp() {
+ parent::setUp();
+ $this->response = new DataResponse();
+ }
+
+
+ public function testSetData() {
+ $params = array('hi', 'yo');
+ $this->response->setData($params);
+
+ $this->assertEquals(array('hi', 'yo'), $this->response->getData());
+ }
+
+
+ public function testConstructorAllowsToSetData() {
+ $data = array('hi');
+ $code = 300;
+ $response = new DataResponse($data, $code);
+
+ $this->assertEquals($data, $response->getData());
+ $this->assertEquals($code, $response->getStatus());
+ }
+
+
+ public function testConstructorAllowsToSetHeaders() {
+ $data = array('hi');
+ $code = 300;
+ $headers = array('test' => 'something');
+ $response = new DataResponse($data, $code, $headers);
+
+ $expectedHeaders = [
+ 'Cache-Control' => 'no-cache, must-revalidate',
+ 'Content-Security-Policy' => "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'",
+ ];
+ $expectedHeaders = array_merge($expectedHeaders, $headers);
+
+ $this->assertEquals($data, $response->getData());
+ $this->assertEquals($code, $response->getStatus());
+ $this->assertEquals($expectedHeaders, $response->getHeaders());
+ }
+
+
+ public function testChainability() {
+ $params = array('hi', 'yo');
+ $this->response->setData($params)
+ ->setStatus(Http::STATUS_NOT_FOUND);
+
+ $this->assertEquals(Http::STATUS_NOT_FOUND, $this->response->getStatus());
+ $this->assertEquals(array('hi', 'yo'), $this->response->getData());
+ }
+
+
+}
diff --git a/tests/lib/AppFramework/Http/DispatcherTest.php b/tests/lib/AppFramework/Http/DispatcherTest.php
new file mode 100644
index 00000000000..6df6f7fa7fe
--- /dev/null
+++ b/tests/lib/AppFramework/Http/DispatcherTest.php
@@ -0,0 +1,466 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Http;
+
+use OC\AppFramework\Http\Dispatcher;
+use OC\AppFramework\Http\Request;
+use OC\AppFramework\Utility\ControllerMethodReflector;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\Controller;
+
+
+class TestController extends Controller {
+ /**
+ * @param string $appName
+ * @param \OCP\IRequest $request
+ */
+ public function __construct($appName, $request) {
+ parent::__construct($appName, $request);
+ }
+
+ /**
+ * @param int $int
+ * @param bool $bool
+ * @param int $test
+ * @param int $test2
+ * @return array
+ */
+ public function exec($int, $bool, $test=4, $test2=1) {
+ $this->registerResponder('text', function($in) {
+ return new JSONResponse(array('text' => $in));
+ });
+ return array($int, $bool, $test, $test2);
+ }
+
+
+ /**
+ * @param int $int
+ * @param bool $bool
+ * @param int $test
+ * @param int $test2
+ * @return DataResponse
+ */
+ public function execDataResponse($int, $bool, $test=4, $test2=1) {
+ return new DataResponse(array(
+ 'text' => array($int, $bool, $test, $test2)
+ ));
+ }
+
+}
+
+
+class DispatcherTest extends \Test\TestCase {
+ private $middlewareDispatcher;
+ private $dispatcher;
+ private $controllerMethod;
+ private $response;
+ private $request;
+ private $lastModified;
+ private $etag;
+ private $http;
+ private $reflector;
+
+ protected function setUp() {
+ parent::setUp();
+ $this->controllerMethod = 'test';
+
+ $app = $this->getMockBuilder(
+ 'OC\AppFramework\DependencyInjection\DIContainer')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $request = $this->getMockBuilder(
+ '\OC\AppFramework\Http\Request')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->http = $this->getMockBuilder(
+ '\OC\AppFramework\Http')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->middlewareDispatcher = $this->getMockBuilder(
+ '\OC\AppFramework\Middleware\MiddlewareDispatcher')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->controller = $this->getMock(
+ '\OCP\AppFramework\Controller',
+ array($this->controllerMethod), array($app, $request));
+
+ $this->request = $this->getMockBuilder(
+ '\OC\AppFramework\Http\Request')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->reflector = new ControllerMethodReflector();
+
+ $this->dispatcher = new Dispatcher(
+ $this->http, $this->middlewareDispatcher, $this->reflector,
+ $this->request
+ );
+
+ $this->response = $this->getMockBuilder(
+ '\OCP\AppFramework\Http\Response')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
+ $this->etag = 'hi';
+ }
+
+
+ /**
+ * @param string $out
+ * @param string $httpHeaders
+ */
+ private function setMiddlewareExpectations($out=null,
+ $httpHeaders=null, $responseHeaders=array(),
+ $ex=false, $catchEx=true) {
+
+ if($ex) {
+ $exception = new \Exception();
+ $this->middlewareDispatcher->expects($this->once())
+ ->method('beforeController')
+ ->with($this->equalTo($this->controller),
+ $this->equalTo($this->controllerMethod))
+ ->will($this->throwException($exception));
+ if($catchEx) {
+ $this->middlewareDispatcher->expects($this->once())
+ ->method('afterException')
+ ->with($this->equalTo($this->controller),
+ $this->equalTo($this->controllerMethod),
+ $this->equalTo($exception))
+ ->will($this->returnValue($this->response));
+ } else {
+ $this->middlewareDispatcher->expects($this->once())
+ ->method('afterException')
+ ->with($this->equalTo($this->controller),
+ $this->equalTo($this->controllerMethod),
+ $this->equalTo($exception))
+ ->will($this->returnValue(null));
+ return;
+ }
+ } else {
+ $this->middlewareDispatcher->expects($this->once())
+ ->method('beforeController')
+ ->with($this->equalTo($this->controller),
+ $this->equalTo($this->controllerMethod));
+ $this->controller->expects($this->once())
+ ->method($this->controllerMethod)
+ ->will($this->returnValue($this->response));
+ }
+
+ $this->response->expects($this->once())
+ ->method('render')
+ ->will($this->returnValue($out));
+ $this->response->expects($this->once())
+ ->method('getStatus')
+ ->will($this->returnValue(Http::STATUS_OK));
+ $this->response->expects($this->once())
+ ->method('getLastModified')
+ ->will($this->returnValue($this->lastModified));
+ $this->response->expects($this->once())
+ ->method('getETag')
+ ->will($this->returnValue($this->etag));
+ $this->response->expects($this->once())
+ ->method('getHeaders')
+ ->will($this->returnValue($responseHeaders));
+ $this->http->expects($this->once())
+ ->method('getStatusHeader')
+ ->with($this->equalTo(Http::STATUS_OK),
+ $this->equalTo($this->lastModified),
+ $this->equalTo($this->etag))
+ ->will($this->returnValue($httpHeaders));
+
+ $this->middlewareDispatcher->expects($this->once())
+ ->method('afterController')
+ ->with($this->equalTo($this->controller),
+ $this->equalTo($this->controllerMethod),
+ $this->equalTo($this->response))
+ ->will($this->returnValue($this->response));
+
+ $this->middlewareDispatcher->expects($this->once())
+ ->method('afterController')
+ ->with($this->equalTo($this->controller),
+ $this->equalTo($this->controllerMethod),
+ $this->equalTo($this->response))
+ ->will($this->returnValue($this->response));
+
+ $this->middlewareDispatcher->expects($this->once())
+ ->method('beforeOutput')
+ ->with($this->equalTo($this->controller),
+ $this->equalTo($this->controllerMethod),
+ $this->equalTo($out))
+ ->will($this->returnValue($out));
+ }
+
+
+ public function testDispatcherReturnsArrayWith2Entries() {
+ $this->setMiddlewareExpectations();
+
+ $response = $this->dispatcher->dispatch($this->controller,
+ $this->controllerMethod);
+ $this->assertNull($response[0]);
+ $this->assertEquals(array(), $response[1]);
+ $this->assertNull($response[2]);
+ }
+
+
+ public function testHeadersAndOutputAreReturned(){
+ $out = 'yo';
+ $httpHeaders = 'Http';
+ $responseHeaders = array('hell' => 'yeah');
+ $this->setMiddlewareExpectations($out, $httpHeaders, $responseHeaders);
+
+ $response = $this->dispatcher->dispatch($this->controller,
+ $this->controllerMethod);
+
+ $this->assertEquals($httpHeaders, $response[0]);
+ $this->assertEquals($responseHeaders, $response[1]);
+ $this->assertEquals($out, $response[3]);
+ }
+
+
+ public function testExceptionCallsAfterException() {
+ $out = 'yo';
+ $httpHeaders = 'Http';
+ $responseHeaders = array('hell' => 'yeah');
+ $this->setMiddlewareExpectations($out, $httpHeaders, $responseHeaders, true);
+
+ $response = $this->dispatcher->dispatch($this->controller,
+ $this->controllerMethod);
+
+ $this->assertEquals($httpHeaders, $response[0]);
+ $this->assertEquals($responseHeaders, $response[1]);
+ $this->assertEquals($out, $response[3]);
+ }
+
+
+ public function testExceptionThrowsIfCanNotBeHandledByAfterException() {
+ $out = 'yo';
+ $httpHeaders = 'Http';
+ $responseHeaders = array('hell' => 'yeah');
+ $this->setMiddlewareExpectations($out, $httpHeaders, $responseHeaders, true, false);
+
+ $this->setExpectedException('\Exception');
+ $response = $this->dispatcher->dispatch($this->controller,
+ $this->controllerMethod);
+
+ }
+
+
+ private function dispatcherPassthrough() {
+ $this->middlewareDispatcher->expects($this->once())
+ ->method('beforeController');
+ $this->middlewareDispatcher->expects($this->once())
+ ->method('afterController')
+ ->will($this->returnCallback(function($a, $b, $in) {
+ return $in;
+ }));
+ $this->middlewareDispatcher->expects($this->once())
+ ->method('beforeOutput')
+ ->will($this->returnCallback(function($a, $b, $in) {
+ return $in;
+ }));
+ }
+
+
+ public function testControllerParametersInjected() {
+ $this->request = new Request(
+ [
+ 'post' => [
+ 'int' => '3',
+ 'bool' => 'false'
+ ],
+ 'method' => 'POST'
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->dispatcher = new Dispatcher(
+ $this->http, $this->middlewareDispatcher, $this->reflector,
+ $this->request
+ );
+ $controller = new TestController('app', $this->request);
+
+ // reflector is supposed to be called once
+ $this->dispatcherPassthrough();
+ $response = $this->dispatcher->dispatch($controller, 'exec');
+
+ $this->assertEquals('[3,true,4,1]', $response[3]);
+ }
+
+
+ public function testControllerParametersInjectedDefaultOverwritten() {
+ $this->request = new Request(
+ [
+ 'post' => [
+ 'int' => '3',
+ 'bool' => 'false',
+ 'test2' => 7
+ ],
+ 'method' => 'POST',
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->dispatcher = new Dispatcher(
+ $this->http, $this->middlewareDispatcher, $this->reflector,
+ $this->request
+ );
+ $controller = new TestController('app', $this->request);
+
+ // reflector is supposed to be called once
+ $this->dispatcherPassthrough();
+ $response = $this->dispatcher->dispatch($controller, 'exec');
+
+ $this->assertEquals('[3,true,4,7]', $response[3]);
+ }
+
+
+
+ public function testResponseTransformedByUrlFormat() {
+ $this->request = new Request(
+ [
+ 'post' => [
+ 'int' => '3',
+ 'bool' => 'false'
+ ],
+ 'urlParams' => [
+ 'format' => 'text'
+ ],
+ 'method' => 'GET'
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->dispatcher = new Dispatcher(
+ $this->http, $this->middlewareDispatcher, $this->reflector,
+ $this->request
+ );
+ $controller = new TestController('app', $this->request);
+
+ // reflector is supposed to be called once
+ $this->dispatcherPassthrough();
+ $response = $this->dispatcher->dispatch($controller, 'exec');
+
+ $this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
+ }
+
+
+ public function testResponseTransformsDataResponse() {
+ $this->request = new Request(
+ [
+ 'post' => [
+ 'int' => '3',
+ 'bool' => 'false'
+ ],
+ 'urlParams' => [
+ 'format' => 'json'
+ ],
+ 'method' => 'GET'
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->dispatcher = new Dispatcher(
+ $this->http, $this->middlewareDispatcher, $this->reflector,
+ $this->request
+ );
+ $controller = new TestController('app', $this->request);
+
+ // reflector is supposed to be called once
+ $this->dispatcherPassthrough();
+ $response = $this->dispatcher->dispatch($controller, 'execDataResponse');
+
+ $this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
+ }
+
+
+ public function testResponseTransformedByAcceptHeader() {
+ $this->request = new Request(
+ [
+ 'post' => [
+ 'int' => '3',
+ 'bool' => 'false'
+ ],
+ 'server' => [
+ 'HTTP_ACCEPT' => 'application/text, test',
+ 'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded'
+ ],
+ 'method' => 'PUT'
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->dispatcher = new Dispatcher(
+ $this->http, $this->middlewareDispatcher, $this->reflector,
+ $this->request
+ );
+ $controller = new TestController('app', $this->request);
+
+ // reflector is supposed to be called once
+ $this->dispatcherPassthrough();
+ $response = $this->dispatcher->dispatch($controller, 'exec');
+
+ $this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
+ }
+
+
+ public function testResponsePrimarilyTransformedByParameterFormat() {
+ $this->request = new Request(
+ [
+ 'post' => [
+ 'int' => '3',
+ 'bool' => 'false'
+ ],
+ 'get' => [
+ 'format' => 'text'
+ ],
+ 'server' => [
+ 'HTTP_ACCEPT' => 'application/json, test'
+ ],
+ 'method' => 'POST'
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->dispatcher = new Dispatcher(
+ $this->http, $this->middlewareDispatcher, $this->reflector,
+ $this->request
+ );
+ $controller = new TestController('app', $this->request);
+
+ // reflector is supposed to be called once
+ $this->dispatcherPassthrough();
+ $response = $this->dispatcher->dispatch($controller, 'exec');
+
+ $this->assertEquals('{"text":[3,true,4,1]}', $response[3]);
+ }
+
+
+
+
+}
diff --git a/tests/lib/AppFramework/Http/DownloadResponseTest.php b/tests/lib/AppFramework/Http/DownloadResponseTest.php
new file mode 100644
index 00000000000..07c932b2725
--- /dev/null
+++ b/tests/lib/AppFramework/Http/DownloadResponseTest.php
@@ -0,0 +1,56 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Http;
+
+
+use OCP\AppFramework\Http\DownloadResponse;
+
+class ChildDownloadResponse extends DownloadResponse {
+
+};
+
+
+class DownloadResponseTest extends \Test\TestCase {
+
+ /**
+ * @var ChildDownloadResponse
+ */
+ protected $response;
+
+ protected function setUp(){
+ parent::setUp();
+ $this->response = new ChildDownloadResponse('file', 'content');
+ }
+
+
+ public function testHeaders() {
+ $headers = $this->response->getHeaders();
+
+ $this->assertContains('attachment; filename="file"', $headers['Content-Disposition']);
+ $this->assertContains('content', $headers['Content-Type']);
+ }
+
+
+}
diff --git a/tests/lib/AppFramework/Http/EmptyContentSecurityPolicyTest.php b/tests/lib/AppFramework/Http/EmptyContentSecurityPolicyTest.php
new file mode 100644
index 00000000000..248c3d808d2
--- /dev/null
+++ b/tests/lib/AppFramework/Http/EmptyContentSecurityPolicyTest.php
@@ -0,0 +1,430 @@
+<?php
+/**
+ * Copyright (c) 2015 Lukas Reschke lukas@owncloud.com
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+
+namespace Test\AppFramework\Http;
+
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
+
+/**
+ * Class ContentSecurityPolicyTest
+ *
+ * @package OC\AppFramework\Http
+ */
+class EmptyContentSecurityPolicyTest extends \Test\TestCase {
+
+ /** @var EmptyContentSecurityPolicy */
+ private $contentSecurityPolicy;
+
+ public function setUp() {
+ parent::setUp();
+ $this->contentSecurityPolicy = new EmptyContentSecurityPolicy();
+ }
+
+ public function testGetPolicyDefault() {
+ $defaultPolicy = "default-src 'none'";
+ $this->assertSame($defaultPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyScriptDomainValid() {
+ $expectedPolicy = "default-src 'none';script-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyScriptDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';script-src www.owncloud.com www.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowScriptDomain() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowScriptDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowScriptDomainMultiple() {
+ $expectedPolicy = "default-src 'none';script-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowScriptDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowScriptDomainMultipleStacked() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowScriptDomain('www.owncloud.org')->disallowScriptDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyScriptAllowInline() {
+ $expectedPolicy = "default-src 'none';script-src 'unsafe-inline'";
+
+ $this->contentSecurityPolicy->allowInlineScript(true);
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyScriptAllowInlineWithDomain() {
+ $expectedPolicy = "default-src 'none';script-src www.owncloud.com 'unsafe-inline'";
+
+ $this->contentSecurityPolicy->addAllowedScriptDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->allowInlineScript(true);
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyScriptAllowInlineAndEval() {
+ $expectedPolicy = "default-src 'none';script-src 'unsafe-inline' 'unsafe-eval'";
+
+ $this->contentSecurityPolicy->allowInlineScript(true);
+ $this->contentSecurityPolicy->allowEvalScript(true);
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyStyleDomainValid() {
+ $expectedPolicy = "default-src 'none';style-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyStyleDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';style-src www.owncloud.com www.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowStyleDomain() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowStyleDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowStyleDomainMultiple() {
+ $expectedPolicy = "default-src 'none';style-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowStyleDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowStyleDomainMultipleStacked() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowStyleDomain('www.owncloud.org')->disallowStyleDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyStyleAllowInline() {
+ $expectedPolicy = "default-src 'none';style-src 'unsafe-inline'";
+
+ $this->contentSecurityPolicy->allowInlineStyle(true);
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyStyleAllowInlineWithDomain() {
+ $expectedPolicy = "default-src 'none';style-src www.owncloud.com 'unsafe-inline'";
+
+ $this->contentSecurityPolicy->addAllowedStyleDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->allowInlineStyle(true);
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyStyleDisallowInline() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->allowInlineStyle(false);
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyImageDomainValid() {
+ $expectedPolicy = "default-src 'none';img-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyImageDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';img-src www.owncloud.com www.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowImageDomain() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowImageDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowImageDomainMultiple() {
+ $expectedPolicy = "default-src 'none';img-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowImageDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowImageDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedImageDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowImageDomain('www.owncloud.org')->disallowImageDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyFontDomainValid() {
+ $expectedPolicy = "default-src 'none';font-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyFontDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';font-src www.owncloud.com www.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFontDomain() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFontDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFontDomainMultiple() {
+ $expectedPolicy = "default-src 'none';font-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFontDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFontDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedFontDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFontDomain('www.owncloud.org')->disallowFontDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyConnectDomainValid() {
+ $expectedPolicy = "default-src 'none';connect-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyConnectDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';connect-src www.owncloud.com www.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowConnectDomain() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowConnectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowConnectDomainMultiple() {
+ $expectedPolicy = "default-src 'none';connect-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowConnectDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowConnectDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedConnectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowConnectDomain('www.owncloud.org')->disallowConnectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyMediaDomainValid() {
+ $expectedPolicy = "default-src 'none';media-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyMediaDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';media-src www.owncloud.com www.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowMediaDomain() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowMediaDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowMediaDomainMultiple() {
+ $expectedPolicy = "default-src 'none';media-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowMediaDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowMediaDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedMediaDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowMediaDomain('www.owncloud.org')->disallowMediaDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyObjectDomainValid() {
+ $expectedPolicy = "default-src 'none';object-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyObjectDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';object-src www.owncloud.com www.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowObjectDomain() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowObjectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowObjectDomainMultiple() {
+ $expectedPolicy = "default-src 'none';object-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowObjectDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowObjectDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedObjectDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowObjectDomain('www.owncloud.org')->disallowObjectDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetAllowedFrameDomain() {
+ $expectedPolicy = "default-src 'none';frame-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyFrameDomainValidMultiple() {
+ $expectedPolicy = "default-src 'none';frame-src www.owncloud.com www.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFrameDomain() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFrameDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFrameDomainMultiple() {
+ $expectedPolicy = "default-src 'none';frame-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFrameDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowFrameDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedFrameDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowFrameDomain('www.owncloud.org')->disallowFrameDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetAllowedChildSrcDomain() {
+ $expectedPolicy = "default-src 'none';child-src child.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyChildSrcValidMultiple() {
+ $expectedPolicy = "default-src 'none';child-src child.owncloud.com child.owncloud.org";
+
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.com');
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('child.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowChildSrcDomain() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowChildSrcDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowChildSrcDomainMultiple() {
+ $expectedPolicy = "default-src 'none';child-src www.owncloud.com";
+
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowChildSrcDomain('www.owncloud.org');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+
+ public function testGetPolicyDisallowChildSrcDomainMultipleStakes() {
+ $expectedPolicy = "default-src 'none'";
+
+ $this->contentSecurityPolicy->addAllowedChildSrcDomain('www.owncloud.com');
+ $this->contentSecurityPolicy->disallowChildSrcDomain('www.owncloud.org')->disallowChildSrcDomain('www.owncloud.com');
+ $this->assertSame($expectedPolicy, $this->contentSecurityPolicy->buildPolicy());
+ }
+}
diff --git a/tests/lib/AppFramework/Http/HttpTest.php b/tests/lib/AppFramework/Http/HttpTest.php
new file mode 100644
index 00000000000..66256d1a86f
--- /dev/null
+++ b/tests/lib/AppFramework/Http/HttpTest.php
@@ -0,0 +1,99 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Http;
+
+use OC\AppFramework\Http;
+
+
+class HttpTest extends \Test\TestCase {
+
+ private $server;
+
+ /**
+ * @var Http
+ */
+ private $http;
+
+ protected function setUp(){
+ parent::setUp();
+
+ $this->server = array();
+ $this->http = new Http($this->server);
+ }
+
+
+ public function testProtocol() {
+ $header = $this->http->getStatusHeader(Http::STATUS_TEMPORARY_REDIRECT);
+ $this->assertEquals('HTTP/1.1 307 Temporary Redirect', $header);
+ }
+
+
+ public function testProtocol10() {
+ $this->http = new Http($this->server, 'HTTP/1.0');
+ $header = $this->http->getStatusHeader(Http::STATUS_OK);
+ $this->assertEquals('HTTP/1.0 200 OK', $header);
+ }
+
+
+ public function testEtagMatchReturnsNotModified() {
+ $http = new Http(array('HTTP_IF_NONE_MATCH' => 'hi'));
+
+ $header = $http->getStatusHeader(Http::STATUS_OK, null, 'hi');
+ $this->assertEquals('HTTP/1.1 304 Not Modified', $header);
+ }
+
+
+ public function testQuotedEtagMatchReturnsNotModified() {
+ $http = new Http(array('HTTP_IF_NONE_MATCH' => '"hi"'));
+
+ $header = $http->getStatusHeader(Http::STATUS_OK, null, 'hi');
+ $this->assertEquals('HTTP/1.1 304 Not Modified', $header);
+ }
+
+
+ public function testLastModifiedMatchReturnsNotModified() {
+ $dateTime = new \DateTime(null, new \DateTimeZone('GMT'));
+ $dateTime->setTimestamp('12');
+
+ $http = new Http(
+ array(
+ 'HTTP_IF_MODIFIED_SINCE' => 'Thu, 01 Jan 1970 00:00:12 +0000')
+ );
+
+ $header = $http->getStatusHeader(Http::STATUS_OK, $dateTime);
+ $this->assertEquals('HTTP/1.1 304 Not Modified', $header);
+ }
+
+
+
+ public function testTempRedirectBecomesFoundInHttp10() {
+ $http = new Http(array(), 'HTTP/1.0');
+
+ $header = $http->getStatusHeader(Http::STATUS_TEMPORARY_REDIRECT);
+ $this->assertEquals('HTTP/1.0 302 Found', $header);
+ }
+ // TODO: write unittests for http codes
+
+}
diff --git a/tests/lib/AppFramework/Http/JSONResponseTest.php b/tests/lib/AppFramework/Http/JSONResponseTest.php
new file mode 100644
index 00000000000..23a55e7eee8
--- /dev/null
+++ b/tests/lib/AppFramework/Http/JSONResponseTest.php
@@ -0,0 +1,123 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @author Morris Jobke
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
+ * @copyright 2013 Morris Jobke <morris.jobke@gmail.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Http;
+
+
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\AppFramework\Http;
+
+class JSONResponseTest extends \Test\TestCase {
+
+ /**
+ * @var JSONResponse
+ */
+ private $json;
+
+ protected function setUp() {
+ parent::setUp();
+ $this->json = new JSONResponse();
+ }
+
+
+ public function testHeader() {
+ $headers = $this->json->getHeaders();
+ $this->assertEquals('application/json; charset=utf-8', $headers['Content-Type']);
+ }
+
+
+ public function testSetData() {
+ $params = array('hi', 'yo');
+ $this->json->setData($params);
+
+ $this->assertEquals(array('hi', 'yo'), $this->json->getData());
+ }
+
+
+ public function testSetRender() {
+ $params = array('test' => 'hi');
+ $this->json->setData($params);
+
+ $expected = '{"test":"hi"}';
+
+ $this->assertEquals($expected, $this->json->render());
+ }
+
+ /**
+ * @return array
+ */
+ public function renderDataProvider() {
+ return [
+ [
+ ['test' => 'hi'], '{"test":"hi"}',
+ ],
+ [
+ ['<h1>test' => '<h1>hi'], '{"\u003Ch1\u003Etest":"\u003Ch1\u003Ehi"}',
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider renderDataProvider
+ * @param array $input
+ * @param string $expected
+ */
+ public function testRender(array $input, $expected) {
+ $this->json->setData($input);
+ $this->assertEquals($expected, $this->json->render());
+ }
+
+ /**
+ * @expectedException \Exception
+ * @expectedExceptionMessage Could not json_encode due to invalid non UTF-8 characters in the array: array (
+ * @requires PHP 5.5
+ */
+ public function testRenderWithNonUtf8Encoding() {
+ $params = ['test' => hex2bin('e9')];
+ $this->json->setData($params);
+ $this->json->render();
+ }
+
+ public function testConstructorAllowsToSetData() {
+ $data = array('hi');
+ $code = 300;
+ $response = new JSONResponse($data, $code);
+
+ $expected = '["hi"]';
+ $this->assertEquals($expected, $response->render());
+ $this->assertEquals($code, $response->getStatus());
+ }
+
+ public function testChainability() {
+ $params = array('hi', 'yo');
+ $this->json->setData($params)
+ ->setStatus(Http::STATUS_NOT_FOUND);
+
+ $this->assertEquals(Http::STATUS_NOT_FOUND, $this->json->getStatus());
+ $this->assertEquals(array('hi', 'yo'), $this->json->getData());
+ }
+
+}
diff --git a/tests/lib/AppFramework/Http/OCSResponseTest.php b/tests/lib/AppFramework/Http/OCSResponseTest.php
new file mode 100644
index 00000000000..adac66a231c
--- /dev/null
+++ b/tests/lib/AppFramework/Http/OCSResponseTest.php
@@ -0,0 +1,72 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2015 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Http;
+
+
+use OCP\AppFramework\Http\OCSResponse;
+
+
+class OCSResponseTest extends \Test\TestCase {
+
+
+ public function testHeadersJSON() {
+ $response = new OCSResponse('json', 1, 2, 3);
+ $type = $response->getHeaders()['Content-Type'];
+ $this->assertEquals('application/json; charset=utf-8', $type);
+ }
+
+
+ public function testHeadersXML() {
+ $response = new OCSResponse('xml', 1, 2, 3);
+ $type = $response->getHeaders()['Content-Type'];
+ $this->assertEquals('application/xml; charset=utf-8', $type);
+ }
+
+
+ public function testRender() {
+ $response = new OCSResponse(
+ 'xml', 2, 'message', ['test' => 'hi'], 3, 4
+ );
+ $out = $response->render();
+ $expected = "<?xml version=\"1.0\"?>\n" .
+ "<ocs>\n" .
+ " <meta>\n" .
+ " <status>failure</status>\n" .
+ " <statuscode>2</statuscode>\n" .
+ " <message>message</message>\n" .
+ " <totalitems>3</totalitems>\n" .
+ " <itemsperpage>4</itemsperpage>\n" .
+ " </meta>\n" .
+ " <data>\n" .
+ " <test>hi</test>\n" .
+ " </data>\n" .
+ "</ocs>\n";
+
+ $this->assertEquals($expected, $out);
+
+ }
+
+
+}
diff --git a/tests/lib/AppFramework/Http/RedirectResponseTest.php b/tests/lib/AppFramework/Http/RedirectResponseTest.php
new file mode 100644
index 00000000000..5ef82a1221d
--- /dev/null
+++ b/tests/lib/AppFramework/Http/RedirectResponseTest.php
@@ -0,0 +1,57 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Http;
+
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\RedirectResponse;
+
+
+class RedirectResponseTest extends \Test\TestCase {
+
+ /**
+ * @var RedirectResponse
+ */
+ protected $response;
+
+ protected function setUp(){
+ parent::setUp();
+ $this->response = new RedirectResponse('/url');
+ }
+
+
+ public function testHeaders() {
+ $headers = $this->response->getHeaders();
+ $this->assertEquals('/url', $headers['Location']);
+ $this->assertEquals(Http::STATUS_SEE_OTHER,
+ $this->response->getStatus());
+ }
+
+
+ public function testGetRedirectUrl(){
+ $this->assertEquals('/url', $this->response->getRedirectUrl());
+ }
+
+
+}
diff --git a/tests/lib/AppFramework/Http/RequestStream.php b/tests/lib/AppFramework/Http/RequestStream.php
new file mode 100644
index 00000000000..8f6dffd4e91
--- /dev/null
+++ b/tests/lib/AppFramework/Http/RequestStream.php
@@ -0,0 +1,110 @@
+<?php
+
+namespace Test\AppFramework\Http;
+
+/**
+ * Copy of http://dk1.php.net/manual/en/stream.streamwrapper.example-1.php
+ * Used to simulate php://input for Request tests
+ */
+class RequestStream {
+ protected $position;
+ protected $varname;
+
+ function stream_open($path, $mode, $options, &$opened_path) {
+ $url = parse_url($path);
+ $this->varname = $url["host"];
+ $this->position = 0;
+
+ return true;
+ }
+
+ function stream_read($count) {
+ $ret = substr($GLOBALS[$this->varname], $this->position, $count);
+ $this->position += strlen($ret);
+ return $ret;
+ }
+
+ function stream_write($data) {
+ $left = substr($GLOBALS[$this->varname], 0, $this->position);
+ $right = substr($GLOBALS[$this->varname], $this->position + strlen($data));
+ $GLOBALS[$this->varname] = $left . $data . $right;
+ $this->position += strlen($data);
+ return strlen($data);
+ }
+
+ function stream_tell() {
+ return $this->position;
+ }
+
+ function stream_eof() {
+ return $this->position >= strlen($GLOBALS[$this->varname]);
+ }
+
+ function stream_seek($offset, $whence) {
+ switch ($whence) {
+ case SEEK_SET:
+ if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) {
+ $this->position = $offset;
+ return true;
+ } else {
+ return false;
+ }
+ break;
+
+ case SEEK_CUR:
+ if ($offset >= 0) {
+ $this->position += $offset;
+ return true;
+ } else {
+ return false;
+ }
+ break;
+
+ case SEEK_END:
+ if (strlen($GLOBALS[$this->varname]) + $offset >= 0) {
+ $this->position = strlen($GLOBALS[$this->varname]) + $offset;
+ return true;
+ } else {
+ return false;
+ }
+ break;
+
+ default:
+ return false;
+ }
+ }
+
+ public function stream_stat() {
+ $size = strlen($GLOBALS[$this->varname]);
+ $time = time();
+ $data = array(
+ 'dev' => 0,
+ 'ino' => 0,
+ 'mode' => 0777,
+ 'nlink' => 1,
+ 'uid' => 0,
+ 'gid' => 0,
+ 'rdev' => '',
+ 'size' => $size,
+ 'atime' => $time,
+ 'mtime' => $time,
+ 'ctime' => $time,
+ 'blksize' => -1,
+ 'blocks' => -1,
+ );
+ return array_values($data) + $data;
+ //return false;
+ }
+
+ function stream_metadata($path, $option, $var) {
+ if($option == STREAM_META_TOUCH) {
+ $url = parse_url($path);
+ $varname = $url["host"];
+ if(!isset($GLOBALS[$varname])) {
+ $GLOBALS[$varname] = '';
+ }
+ return true;
+ }
+ return false;
+ }
+}
diff --git a/tests/lib/AppFramework/Http/RequestTest.php b/tests/lib/AppFramework/Http/RequestTest.php
new file mode 100644
index 00000000000..ddc2403d866
--- /dev/null
+++ b/tests/lib/AppFramework/Http/RequestTest.php
@@ -0,0 +1,1453 @@
+<?php
+/**
+ * @copyright 2013 Thomas Tanghus (thomas@tanghus.net)
+ * @copyright 2015 Lukas Reschke lukas@owncloud.com
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
+ */
+
+namespace Test\AppFramework\Http;
+
+use OC\AppFramework\Http\Request;
+use OC\Security\CSRF\CsrfToken;
+use OC\Security\CSRF\CsrfTokenManager;
+use OCP\Security\ISecureRandom;
+use OCP\IConfig;
+
+/**
+ * Class RequestTest
+ *
+ * @package OC\AppFramework\Http
+ */
+class RequestTest extends \Test\TestCase {
+ /** @var string */
+ protected $stream = 'fakeinput://data';
+ /** @var ISecureRandom */
+ protected $secureRandom;
+ /** @var IConfig */
+ protected $config;
+ /** @var CsrfTokenManager */
+ protected $csrfTokenManager;
+
+ protected function setUp() {
+ parent::setUp();
+
+ if (in_array('fakeinput', stream_get_wrappers())) {
+ stream_wrapper_unregister('fakeinput');
+ }
+ stream_wrapper_register('fakeinput', 'Test\AppFramework\Http\RequestStream');
+
+ $this->secureRandom = $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock();
+ $this->config = $this->getMockBuilder('\OCP\IConfig')->getMock();
+ $this->csrfTokenManager = $this->getMockBuilder('\OC\Security\CSRF\CsrfTokenManager')
+ ->disableOriginalConstructor()->getMock();
+ }
+
+ protected function tearDown() {
+ stream_wrapper_unregister('fakeinput');
+ parent::tearDown();
+ }
+
+ public function testRequestAccessors() {
+ $vars = array(
+ 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
+ 'method' => 'GET',
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ // Countable
+ $this->assertSame(2, count($request));
+ // Array access
+ $this->assertSame('Joey', $request['nickname']);
+ // "Magic" accessors
+ $this->assertSame('Joey', $request->{'nickname'});
+ $this->assertTrue(isset($request['nickname']));
+ $this->assertTrue(isset($request->{'nickname'}));
+ $this->assertFalse(isset($request->{'flickname'}));
+ // Only testing 'get', but same approach for post, files etc.
+ $this->assertSame('Joey', $request->get['nickname']);
+ // Always returns null if variable not set.
+ $this->assertSame(null, $request->{'flickname'});
+
+ }
+
+ // urlParams has precedence over POST which has precedence over GET
+ public function testPrecedence() {
+ $vars = array(
+ 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
+ 'post' => array('name' => 'Jane Doe', 'nickname' => 'Janey'),
+ 'urlParams' => array('user' => 'jw', 'name' => 'Johnny Weissmüller'),
+ 'method' => 'GET'
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame(3, count($request));
+ $this->assertSame('Janey', $request->{'nickname'});
+ $this->assertSame('Johnny Weissmüller', $request->{'name'});
+ }
+
+
+ /**
+ * @expectedException \RuntimeException
+ */
+ public function testImmutableArrayAccess() {
+ $vars = array(
+ 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
+ 'method' => 'GET'
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $request['nickname'] = 'Janey';
+ }
+
+ /**
+ * @expectedException \RuntimeException
+ */
+ public function testImmutableMagicAccess() {
+ $vars = array(
+ 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
+ 'method' => 'GET'
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $request->{'nickname'} = 'Janey';
+ }
+
+ /**
+ * @expectedException \LogicException
+ */
+ public function testGetTheMethodRight() {
+ $vars = array(
+ 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
+ 'method' => 'GET',
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $request->post;
+ }
+
+ public function testTheMethodIsRight() {
+ $vars = array(
+ 'get' => array('name' => 'John Q. Public', 'nickname' => 'Joey'),
+ 'method' => 'GET',
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('GET', $request->method);
+ $result = $request->get;
+ $this->assertSame('John Q. Public', $result['name']);
+ $this->assertSame('Joey', $result['nickname']);
+ }
+
+ public function testJsonPost() {
+ global $data;
+ $data = '{"name": "John Q. Public", "nickname": "Joey"}';
+ $vars = array(
+ 'method' => 'POST',
+ 'server' => array('CONTENT_TYPE' => 'application/json; utf-8')
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('POST', $request->method);
+ $result = $request->post;
+ $this->assertSame('John Q. Public', $result['name']);
+ $this->assertSame('Joey', $result['nickname']);
+ $this->assertSame('Joey', $request->params['nickname']);
+ $this->assertSame('Joey', $request['nickname']);
+ }
+
+ public function testNotJsonPost() {
+ global $data;
+ $data = 'this is not valid json';
+ $vars = array(
+ 'method' => 'POST',
+ 'server' => array('CONTENT_TYPE' => 'application/json; utf-8')
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertEquals('POST', $request->method);
+ $result = $request->post;
+ // ensure there's no error attempting to decode the content
+ }
+
+ public function testPatch() {
+ global $data;
+ $data = http_build_query(array('name' => 'John Q. Public', 'nickname' => 'Joey'), '', '&');
+
+ $vars = array(
+ 'method' => 'PATCH',
+ 'server' => array('CONTENT_TYPE' => 'application/x-www-form-urlencoded'),
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('PATCH', $request->method);
+ $result = $request->patch;
+
+ $this->assertSame('John Q. Public', $result['name']);
+ $this->assertSame('Joey', $result['nickname']);
+ }
+
+ public function testJsonPatchAndPut() {
+ global $data;
+
+ // PUT content
+ $data = '{"name": "John Q. Public", "nickname": "Joey"}';
+ $vars = array(
+ 'method' => 'PUT',
+ 'server' => array('CONTENT_TYPE' => 'application/json; utf-8'),
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('PUT', $request->method);
+ $result = $request->put;
+
+ $this->assertSame('John Q. Public', $result['name']);
+ $this->assertSame('Joey', $result['nickname']);
+
+ // PATCH content
+ $data = '{"name": "John Q. Public", "nickname": null}';
+ $vars = array(
+ 'method' => 'PATCH',
+ 'server' => array('CONTENT_TYPE' => 'application/json; utf-8'),
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('PATCH', $request->method);
+ $result = $request->patch;
+
+ $this->assertSame('John Q. Public', $result['name']);
+ $this->assertSame(null, $result['nickname']);
+ }
+
+ public function testPutStream() {
+ global $data;
+ $data = file_get_contents(__DIR__ . '/../../../data/testimage.png');
+
+ $vars = array(
+ 'put' => $data,
+ 'method' => 'PUT',
+ 'server' => array('CONTENT_TYPE' => 'image/png'),
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('PUT', $request->method);
+ $resource = $request->put;
+ $contents = stream_get_contents($resource);
+ $this->assertSame($data, $contents);
+
+ try {
+ $resource = $request->put;
+ } catch(\LogicException $e) {
+ return;
+ }
+ $this->fail('Expected LogicException.');
+
+ }
+
+
+ public function testSetUrlParameters() {
+ $vars = array(
+ 'post' => array(),
+ 'method' => 'POST',
+ 'urlParams' => array('id' => '2'),
+ );
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $newParams = array('id' => '3', 'test' => 'test2');
+ $request->setUrlParameters($newParams);
+ $this->assertSame('test2', $request->getParam('test'));
+ $this->assertEquals('3', $request->getParam('id'));
+ $this->assertEquals('3', $request->getParams()['id']);
+ }
+
+ public function testGetIdWithModUnique() {
+ $vars = [
+ 'server' => [
+ 'UNIQUE_ID' => 'GeneratedUniqueIdByModUnique'
+ ],
+ ];
+
+ $request = new Request(
+ $vars,
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('GeneratedUniqueIdByModUnique', $request->getId());
+ }
+
+ public function testGetIdWithoutModUnique() {
+ $this->secureRandom->expects($this->once())
+ ->method('generate')
+ ->with('20')
+ ->will($this->returnValue('GeneratedByOwnCloudItself'));
+
+ $request = new Request(
+ [],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('GeneratedByOwnCloudItself', $request->getId());
+ }
+
+ public function testGetIdWithoutModUniqueStable() {
+ $request = new Request(
+ [],
+ \OC::$server->getSecureRandom(),
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+ $firstId = $request->getId();
+ $secondId = $request->getId();
+ $this->assertSame($firstId, $secondId);
+ }
+
+ public function testGetRemoteAddressWithoutTrustedRemote() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->with('trusted_proxies')
+ ->will($this->returnValue([]));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'REMOTE_ADDR' => '10.0.0.2',
+ 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
+ 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('10.0.0.2', $request->getRemoteAddress());
+ }
+
+ public function testGetRemoteAddressWithNoTrustedHeader() {
+ $this->config
+ ->expects($this->at(0))
+ ->method('getSystemValue')
+ ->with('trusted_proxies')
+ ->will($this->returnValue(['10.0.0.2']));
+ $this->config
+ ->expects($this->at(1))
+ ->method('getSystemValue')
+ ->with('forwarded_for_headers')
+ ->will($this->returnValue([]));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'REMOTE_ADDR' => '10.0.0.2',
+ 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
+ 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('10.0.0.2', $request->getRemoteAddress());
+ }
+
+ public function testGetRemoteAddressWithSingleTrustedRemote() {
+ $this->config
+ ->expects($this->at(0))
+ ->method('getSystemValue')
+ ->with('trusted_proxies')
+ ->will($this->returnValue(['10.0.0.2']));
+ $this->config
+ ->expects($this->at(1))
+ ->method('getSystemValue')
+ ->with('forwarded_for_headers')
+ ->will($this->returnValue(['HTTP_X_FORWARDED']));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'REMOTE_ADDR' => '10.0.0.2',
+ 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
+ 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('10.4.0.5', $request->getRemoteAddress());
+ }
+
+ public function testGetRemoteAddressVerifyPriorityHeader() {
+ $this->config
+ ->expects($this->at(0))
+ ->method('getSystemValue')
+ ->with('trusted_proxies')
+ ->will($this->returnValue(['10.0.0.2']));
+ $this->config
+ ->expects($this->at(1))
+ ->method('getSystemValue')
+ ->with('forwarded_for_headers')
+ ->will($this->returnValue([
+ 'HTTP_CLIENT_IP',
+ 'HTTP_X_FORWARDED_FOR',
+ 'HTTP_X_FORWARDED'
+ ]));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'REMOTE_ADDR' => '10.0.0.2',
+ 'HTTP_X_FORWARDED' => '10.4.0.5, 10.4.0.4',
+ 'HTTP_X_FORWARDED_FOR' => '192.168.0.233'
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('192.168.0.233', $request->getRemoteAddress());
+ }
+
+ /**
+ * @return array
+ */
+ public function httpProtocolProvider() {
+ return [
+ // Valid HTTP 1.0
+ ['HTTP/1.0', 'HTTP/1.0'],
+ ['http/1.0', 'HTTP/1.0'],
+ ['HTTp/1.0', 'HTTP/1.0'],
+
+ // Valid HTTP 1.1
+ ['HTTP/1.1', 'HTTP/1.1'],
+ ['http/1.1', 'HTTP/1.1'],
+ ['HTTp/1.1', 'HTTP/1.1'],
+
+ // Valid HTTP 2.0
+ ['HTTP/2', 'HTTP/2'],
+ ['http/2', 'HTTP/2'],
+ ['HTTp/2', 'HTTP/2'],
+
+ // Invalid
+ ['HTTp/394', 'HTTP/1.1'],
+ ['InvalidProvider/1.1', 'HTTP/1.1'],
+ [null, 'HTTP/1.1'],
+ ['', 'HTTP/1.1'],
+
+ ];
+ }
+
+ /**
+ * @dataProvider httpProtocolProvider
+ *
+ * @param mixed $input
+ * @param string $expected
+ */
+ public function testGetHttpProtocol($input, $expected) {
+ $request = new Request(
+ [
+ 'server' => [
+ 'SERVER_PROTOCOL' => $input,
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame($expected, $request->getHttpProtocol());
+ }
+
+ public function testGetServerProtocolWithOverride() {
+ $this->config
+ ->expects($this->at(0))
+ ->method('getSystemValue')
+ ->with('overwriteprotocol')
+ ->will($this->returnValue('customProtocol'));
+ $this->config
+ ->expects($this->at(1))
+ ->method('getSystemValue')
+ ->with('overwritecondaddr')
+ ->will($this->returnValue(''));
+ $this->config
+ ->expects($this->at(2))
+ ->method('getSystemValue')
+ ->with('overwriteprotocol')
+ ->will($this->returnValue('customProtocol'));
+
+ $request = new Request(
+ [],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('customProtocol', $request->getServerProtocol());
+ }
+
+ public function testGetServerProtocolWithProtoValid() {
+ $this->config
+ ->expects($this->exactly(2))
+ ->method('getSystemValue')
+ ->with('overwriteprotocol')
+ ->will($this->returnValue(''));
+
+ $requestHttps = new Request(
+ [
+ 'server' => [
+ 'HTTP_X_FORWARDED_PROTO' => 'HtTpS'
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+ $requestHttp = new Request(
+ [
+ 'server' => [
+ 'HTTP_X_FORWARDED_PROTO' => 'HTTp'
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+
+ $this->assertSame('https', $requestHttps->getServerProtocol());
+ $this->assertSame('http', $requestHttp->getServerProtocol());
+ }
+
+ public function testGetServerProtocolWithHttpsServerValueOn() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->with('overwriteprotocol')
+ ->will($this->returnValue(''));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTPS' => 'on'
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+ $this->assertSame('https', $request->getServerProtocol());
+ }
+
+ public function testGetServerProtocolWithHttpsServerValueOff() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->with('overwriteprotocol')
+ ->will($this->returnValue(''));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTPS' => 'off'
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+ $this->assertSame('http', $request->getServerProtocol());
+ }
+
+ public function testGetServerProtocolWithHttpsServerValueEmpty() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->with('overwriteprotocol')
+ ->will($this->returnValue(''));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTPS' => ''
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+ $this->assertSame('http', $request->getServerProtocol());
+ }
+
+ public function testGetServerProtocolDefault() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->with('overwriteprotocol')
+ ->will($this->returnValue(''));
+
+ $request = new Request(
+ [],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+ $this->assertSame('http', $request->getServerProtocol());
+ }
+
+ public function testGetServerProtocolBehindLoadBalancers() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->with('overwriteprotocol')
+ ->will($this->returnValue(''));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTP_X_FORWARDED_PROTO' => 'https,http,http'
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('https', $request->getServerProtocol());
+ }
+
+ /**
+ * @dataProvider userAgentProvider
+ * @param string $testAgent
+ * @param array $userAgent
+ * @param bool $matches
+ */
+ public function testUserAgent($testAgent, $userAgent, $matches) {
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTP_USER_AGENT' => $testAgent,
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame($matches, $request->isUserAgent($userAgent));
+ }
+
+ /**
+ * @dataProvider userAgentProvider
+ * @param string $testAgent
+ * @param array $userAgent
+ * @param bool $matches
+ */
+ public function testUndefinedUserAgent($testAgent, $userAgent, $matches) {
+ $request = new Request(
+ [],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertFalse($request->isUserAgent($userAgent));
+ }
+
+ /**
+ * @return array
+ */
+ function userAgentProvider() {
+ return [
+ [
+ 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
+ [
+ Request::USER_AGENT_IE
+ ],
+ true,
+ ],
+ [
+ 'Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0',
+ [
+ Request::USER_AGENT_IE
+ ],
+ false,
+ ],
+ [
+ 'Mozilla/5.0 (Linux; Android 4.4; Nexus 4 Build/KRT16S) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36',
+ [
+ Request::USER_AGENT_ANDROID_MOBILE_CHROME
+ ],
+ true,
+ ],
+ [
+ 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
+ [
+ Request::USER_AGENT_ANDROID_MOBILE_CHROME
+ ],
+ false,
+ ],
+ [
+ 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
+ [
+ Request::USER_AGENT_IE,
+ Request::USER_AGENT_ANDROID_MOBILE_CHROME,
+ ],
+ true,
+ ],
+ [
+ 'Mozilla/5.0 (Linux; Android 4.4; Nexus 4 Build/KRT16S) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36',
+ [
+ Request::USER_AGENT_IE,
+ Request::USER_AGENT_ANDROID_MOBILE_CHROME,
+ ],
+ true,
+ ],
+ [
+ 'Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Firefox/24.0',
+ [
+ Request::USER_AGENT_FREEBOX
+ ],
+ false,
+ ],
+ [
+ 'Mozilla/5.0',
+ [
+ Request::USER_AGENT_FREEBOX
+ ],
+ true,
+ ],
+ [
+ 'Fake Mozilla/5.0',
+ [
+ Request::USER_AGENT_FREEBOX
+ ],
+ false,
+ ],
+ ];
+ }
+
+ public function testInsecureServerHostServerNameHeader() {
+ $request = new Request(
+ [
+ 'server' => [
+ 'SERVER_NAME' => 'from.server.name:8080',
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('from.server.name:8080', $request->getInsecureServerHost());
+ }
+
+ public function testInsecureServerHostHttpHostHeader() {
+ $request = new Request(
+ [
+ 'server' => [
+ 'SERVER_NAME' => 'from.server.name:8080',
+ 'HTTP_HOST' => 'from.host.header:8080',
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('from.host.header:8080', $request->getInsecureServerHost());
+ }
+
+ public function testInsecureServerHostHttpFromForwardedHeaderSingle() {
+ $request = new Request(
+ [
+ 'server' => [
+ 'SERVER_NAME' => 'from.server.name:8080',
+ 'HTTP_HOST' => 'from.host.header:8080',
+ 'HTTP_X_FORWARDED_HOST' => 'from.forwarded.host:8080',
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('from.forwarded.host:8080', $request->getInsecureServerHost());
+ }
+
+ public function testInsecureServerHostHttpFromForwardedHeaderStacked() {
+ $request = new Request(
+ [
+ 'server' => [
+ 'SERVER_NAME' => 'from.server.name:8080',
+ 'HTTP_HOST' => 'from.host.header:8080',
+ 'HTTP_X_FORWARDED_HOST' => 'from.forwarded.host2:8080,another.one:9000',
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('from.forwarded.host2:8080', $request->getInsecureServerHost());
+ }
+
+ public function testGetServerHostWithOverwriteHost() {
+ $this->config
+ ->expects($this->at(0))
+ ->method('getSystemValue')
+ ->with('overwritehost')
+ ->will($this->returnValue('my.overwritten.host'));
+ $this->config
+ ->expects($this->at(1))
+ ->method('getSystemValue')
+ ->with('overwritecondaddr')
+ ->will($this->returnValue(''));
+ $this->config
+ ->expects($this->at(2))
+ ->method('getSystemValue')
+ ->with('overwritehost')
+ ->will($this->returnValue('my.overwritten.host'));
+
+ $request = new Request(
+ [],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('my.overwritten.host', $request->getServerHost());
+ }
+
+ public function testGetServerHostWithTrustedDomain() {
+ $this->config
+ ->expects($this->at(3))
+ ->method('getSystemValue')
+ ->with('trusted_domains')
+ ->will($this->returnValue(['my.trusted.host']));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTP_X_FORWARDED_HOST' => 'my.trusted.host',
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('my.trusted.host', $request->getServerHost());
+ }
+
+ public function testGetServerHostWithUntrustedDomain() {
+ $this->config
+ ->expects($this->at(3))
+ ->method('getSystemValue')
+ ->with('trusted_domains')
+ ->will($this->returnValue(['my.trusted.host']));
+ $this->config
+ ->expects($this->at(4))
+ ->method('getSystemValue')
+ ->with('trusted_domains')
+ ->will($this->returnValue(['my.trusted.host']));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTP_X_FORWARDED_HOST' => 'my.untrusted.host',
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('my.trusted.host', $request->getServerHost());
+ }
+
+ public function testGetServerHostWithNoTrustedDomain() {
+ $this->config
+ ->expects($this->at(3))
+ ->method('getSystemValue')
+ ->with('trusted_domains')
+ ->will($this->returnValue([]));
+ $this->config
+ ->expects($this->at(4))
+ ->method('getSystemValue')
+ ->with('trusted_domains')
+ ->will($this->returnValue([]));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTP_X_FORWARDED_HOST' => 'my.untrusted.host',
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('', $request->getServerHost());
+ }
+
+ public function testGetOverwriteHostDefaultNull() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->with('overwritehost')
+ ->will($this->returnValue(''));
+ $request = new Request(
+ [],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertNull(self::invokePrivate($request, 'getOverwriteHost'));
+ }
+
+ public function testGetOverwriteHostWithOverwrite() {
+ $this->config
+ ->expects($this->at(0))
+ ->method('getSystemValue')
+ ->with('overwritehost')
+ ->will($this->returnValue('www.owncloud.org'));
+ $this->config
+ ->expects($this->at(1))
+ ->method('getSystemValue')
+ ->with('overwritecondaddr')
+ ->will($this->returnValue(''));
+ $this->config
+ ->expects($this->at(2))
+ ->method('getSystemValue')
+ ->with('overwritehost')
+ ->will($this->returnValue('www.owncloud.org'));
+
+ $request = new Request(
+ [],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('www.owncloud.org', self::invokePrivate($request, 'getOverwriteHost'));
+ }
+
+ public function testGetPathInfoWithSetEnv() {
+ $request = new Request(
+ [
+ 'server' => [
+ 'PATH_INFO' => 'apps/files/',
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('apps/files/', $request->getPathInfo());
+ }
+
+ /**
+ * @expectedException \Exception
+ * @expectedExceptionMessage The requested uri(/foo.php) cannot be processed by the script '/var/www/index.php')
+ */
+ public function testGetPathInfoNotProcessible() {
+ $request = new Request(
+ [
+ 'server' => [
+ 'REQUEST_URI' => '/foo.php',
+ 'SCRIPT_NAME' => '/var/www/index.php',
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $request->getPathInfo();
+ }
+
+ /**
+ * @expectedException \Exception
+ * @expectedExceptionMessage The requested uri(/foo.php) cannot be processed by the script '/var/www/index.php')
+ */
+ public function testGetRawPathInfoNotProcessible() {
+ $request = new Request(
+ [
+ 'server' => [
+ 'REQUEST_URI' => '/foo.php',
+ 'SCRIPT_NAME' => '/var/www/index.php',
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $request->getRawPathInfo();
+ }
+
+ /**
+ * @dataProvider genericPathInfoProvider
+ * @param string $requestUri
+ * @param string $scriptName
+ * @param string $expected
+ */
+ public function testGetPathInfoWithoutSetEnvGeneric($requestUri, $scriptName, $expected) {
+ $request = new Request(
+ [
+ 'server' => [
+ 'REQUEST_URI' => $requestUri,
+ 'SCRIPT_NAME' => $scriptName,
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame($expected, $request->getPathInfo());
+ }
+
+ /**
+ * @dataProvider genericPathInfoProvider
+ * @param string $requestUri
+ * @param string $scriptName
+ * @param string $expected
+ */
+ public function testGetRawPathInfoWithoutSetEnvGeneric($requestUri, $scriptName, $expected) {
+ $request = new Request(
+ [
+ 'server' => [
+ 'REQUEST_URI' => $requestUri,
+ 'SCRIPT_NAME' => $scriptName,
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame($expected, $request->getRawPathInfo());
+ }
+
+ /**
+ * @dataProvider rawPathInfoProvider
+ * @param string $requestUri
+ * @param string $scriptName
+ * @param string $expected
+ */
+ public function testGetRawPathInfoWithoutSetEnv($requestUri, $scriptName, $expected) {
+ $request = new Request(
+ [
+ 'server' => [
+ 'REQUEST_URI' => $requestUri,
+ 'SCRIPT_NAME' => $scriptName,
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame($expected, $request->getRawPathInfo());
+ }
+
+ /**
+ * @dataProvider pathInfoProvider
+ * @param string $requestUri
+ * @param string $scriptName
+ * @param string $expected
+ */
+ public function testGetPathInfoWithoutSetEnv($requestUri, $scriptName, $expected) {
+ $request = new Request(
+ [
+ 'server' => [
+ 'REQUEST_URI' => $requestUri,
+ 'SCRIPT_NAME' => $scriptName,
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame($expected, $request->getPathInfo());
+ }
+
+ /**
+ * @return array
+ */
+ public function genericPathInfoProvider() {
+ return [
+ ['/core/index.php?XDEBUG_SESSION_START=14600', '/core/index.php', ''],
+ ['/index.php/apps/files/', 'index.php', '/apps/files/'],
+ ['/index.php/apps/files/../&amp;/&?someQueryParameter=QueryParam', 'index.php', '/apps/files/../&amp;/&'],
+ ['/remote.php/漢字編碼方法 / 汉字编码方法', 'remote.php', '/漢字編碼方法 / 汉字编码方法'],
+ ['///removeTrailin//gSlashes///', 'remote.php', '/removeTrailin/gSlashes/'],
+ ['/', '/', ''],
+ ['', '', ''],
+ ];
+ }
+
+ /**
+ * @return array
+ */
+ public function rawPathInfoProvider() {
+ return [
+ ['/foo%2Fbar/subfolder', '', 'foo%2Fbar/subfolder'],
+ ];
+ }
+
+ /**
+ * @return array
+ */
+ public function pathInfoProvider() {
+ return [
+ ['/foo%2Fbar/subfolder', '', 'foo/bar/subfolder'],
+ ];
+ }
+
+ public function testGetRequestUriWithoutOverwrite() {
+ $this->config
+ ->expects($this->once())
+ ->method('getSystemValue')
+ ->with('overwritewebroot')
+ ->will($this->returnValue(''));
+
+ $request = new Request(
+ [
+ 'server' => [
+ 'REQUEST_URI' => '/test.php'
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ );
+
+ $this->assertSame('/test.php', $request->getRequestUri());
+ }
+
+ public function providesGetRequestUriWithOverwriteData() {
+ return [
+ ['/scriptname.php/some/PathInfo', '/owncloud/', ''],
+ ['/scriptname.php/some/PathInfo', '/owncloud/', '123'],
+ ];
+ }
+
+ /**
+ * @dataProvider providesGetRequestUriWithOverwriteData
+ */
+ public function testGetRequestUriWithOverwrite($expectedUri, $overwriteWebRoot, $overwriteCondAddr) {
+ $this->config
+ ->expects($this->at(0))
+ ->method('getSystemValue')
+ ->with('overwritewebroot')
+ ->will($this->returnValue($overwriteWebRoot));
+ $this->config
+ ->expects($this->at(1))
+ ->method('getSystemValue')
+ ->with('overwritecondaddr')
+ ->will($this->returnValue($overwriteCondAddr));
+
+ $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
+ ->setMethods(['getScriptName'])
+ ->setConstructorArgs([
+ [
+ 'server' => [
+ 'REQUEST_URI' => '/test.php/some/PathInfo',
+ 'SCRIPT_NAME' => '/test.php',
+ ]
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ ])
+ ->getMock();
+ $request
+ ->expects($this->once())
+ ->method('getScriptName')
+ ->will($this->returnValue('/scriptname.php'));
+
+ $this->assertSame($expectedUri, $request->getRequestUri());
+ }
+
+ public function testPassesCSRFCheckWithGet() {
+ /** @var Request $request */
+ $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
+ ->setMethods(['getScriptName'])
+ ->setConstructorArgs([
+ [
+ 'get' => [
+ 'requesttoken' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ ])
+ ->getMock();
+ $token = new CsrfToken('AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds');
+ $this->csrfTokenManager
+ ->expects($this->once())
+ ->method('isTokenValid')
+ ->with($token)
+ ->willReturn(true);
+
+ $this->assertTrue($request->passesCSRFCheck());
+ }
+
+ public function testPassesCSRFCheckWithPost() {
+ /** @var Request $request */
+ $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
+ ->setMethods(['getScriptName'])
+ ->setConstructorArgs([
+ [
+ 'post' => [
+ 'requesttoken' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ ])
+ ->getMock();
+ $token = new CsrfToken('AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds');
+ $this->csrfTokenManager
+ ->expects($this->once())
+ ->method('isTokenValid')
+ ->with($token)
+ ->willReturn(true);
+
+ $this->assertTrue($request->passesCSRFCheck());
+ }
+
+ public function testPassesCSRFCheckWithHeader() {
+ /** @var Request $request */
+ $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
+ ->setMethods(['getScriptName'])
+ ->setConstructorArgs([
+ [
+ 'server' => [
+ 'HTTP_REQUESTTOKEN' => 'AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds',
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ ])
+ ->getMock();
+ $token = new CsrfToken('AAAHGxsTCTc3BgMQESAcNR0OAR0=:MyTotalSecretShareds');
+ $this->csrfTokenManager
+ ->expects($this->once())
+ ->method('isTokenValid')
+ ->with($token)
+ ->willReturn(true);
+
+ $this->assertTrue($request->passesCSRFCheck());
+ }
+
+ /**
+ * @return array
+ */
+ public function invalidTokenDataProvider() {
+ return [
+ ['InvalidSentToken'],
+ ['InvalidSentToken:InvalidSecret'],
+ [null],
+ [''],
+ ];
+ }
+
+ /**
+ * @dataProvider invalidTokenDataProvider
+ * @param string $invalidToken
+ */
+ public function testPassesCSRFCheckWithInvalidToken($invalidToken) {
+ /** @var Request $request */
+ $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
+ ->setMethods(['getScriptName'])
+ ->setConstructorArgs([
+ [
+ 'server' => [
+ 'HTTP_REQUESTTOKEN' => $invalidToken,
+ ],
+ ],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ ])
+ ->getMock();
+
+ $token = new CsrfToken($invalidToken);
+ $this->csrfTokenManager
+ ->expects($this->any())
+ ->method('isTokenValid')
+ ->with($token)
+ ->willReturn(false);
+
+ $this->assertFalse($request->passesCSRFCheck());
+ }
+
+ public function testPassesCSRFCheckWithoutTokenFail() {
+ /** @var Request $request */
+ $request = $this->getMockBuilder('\OC\AppFramework\Http\Request')
+ ->setMethods(['getScriptName'])
+ ->setConstructorArgs([
+ [],
+ $this->secureRandom,
+ $this->config,
+ $this->csrfTokenManager,
+ $this->stream
+ ])
+ ->getMock();
+
+ $this->assertFalse($request->passesCSRFCheck());
+ }
+
+}
diff --git a/tests/lib/AppFramework/Http/ResponseTest.php b/tests/lib/AppFramework/Http/ResponseTest.php
new file mode 100644
index 00000000000..0c582f8f6ea
--- /dev/null
+++ b/tests/lib/AppFramework/Http/ResponseTest.php
@@ -0,0 +1,267 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Http;
+
+
+use OCP\AppFramework\Http\Response;
+use OCP\AppFramework\Http;
+
+
+class ResponseTest extends \Test\TestCase {
+
+ /**
+ * @var \OCP\AppFramework\Http\Response
+ */
+ private $childResponse;
+
+ protected function setUp(){
+ parent::setUp();
+ $this->childResponse = new Response();
+ }
+
+
+ public function testAddHeader(){
+ $this->childResponse->addHeader(' hello ', 'world');
+ $headers = $this->childResponse->getHeaders();
+ $this->assertEquals('world', $headers['hello']);
+ }
+
+
+ public function testSetHeaders() {
+ $expected = array(
+ 'Last-Modified' => 1,
+ 'ETag' => 3,
+ 'Something-Else' => 'hi'
+ );
+
+ $this->childResponse->setHeaders($expected);
+ $headers = $this->childResponse->getHeaders();
+ $expected['Content-Security-Policy'] = "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'";
+
+ $this->assertEquals($expected, $headers);
+ }
+
+ public function testOverwriteCsp() {
+ $expected = [
+ 'Content-Security-Policy' => "default-src 'none';script-src 'self' 'unsafe-inline' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self';font-src 'self';connect-src 'self';media-src 'self'",
+ ];
+ $policy = new Http\ContentSecurityPolicy();
+ $policy->allowInlineScript(true);
+
+ $this->childResponse->setContentSecurityPolicy($policy);
+ $headers = $this->childResponse->getHeaders();
+
+ $this->assertEquals(array_merge($expected, $headers), $headers);
+ }
+
+ public function testGetCsp() {
+ $policy = new Http\ContentSecurityPolicy();
+ $policy->allowInlineScript(true);
+
+ $this->childResponse->setContentSecurityPolicy($policy);
+ $this->assertEquals($policy, $this->childResponse->getContentSecurityPolicy());
+ }
+
+ public function testGetCspEmpty() {
+ $this->assertNull($this->childResponse->getContentSecurityPolicy());
+ }
+
+ public function testAddHeaderValueNullDeletesIt(){
+ $this->childResponse->addHeader('hello', 'world');
+ $this->childResponse->addHeader('hello', null);
+ $this->assertEquals(2, count($this->childResponse->getHeaders()));
+ }
+
+
+ public function testCacheHeadersAreDisabledByDefault(){
+ $headers = $this->childResponse->getHeaders();
+ $this->assertEquals('no-cache, must-revalidate', $headers['Cache-Control']);
+ }
+
+
+ public function testAddCookie() {
+ $this->childResponse->addCookie('foo', 'bar');
+ $this->childResponse->addCookie('bar', 'foo', new \DateTime('1970-01-01'));
+
+ $expectedResponse = array(
+ 'foo' => array(
+ 'value' => 'bar',
+ 'expireDate' => null,
+ ),
+ 'bar' => array(
+ 'value' => 'foo',
+ 'expireDate' => new \DateTime('1970-01-01')
+ )
+ );
+ $this->assertEquals($expectedResponse, $this->childResponse->getCookies());
+ }
+
+
+ function testSetCookies() {
+ $expected = array(
+ 'foo' => array(
+ 'value' => 'bar',
+ 'expireDate' => null,
+ ),
+ 'bar' => array(
+ 'value' => 'foo',
+ 'expireDate' => new \DateTime('1970-01-01')
+ )
+ );
+
+ $this->childResponse->setCookies($expected);
+ $cookies = $this->childResponse->getCookies();
+
+ $this->assertEquals($expected, $cookies);
+ }
+
+
+ function testInvalidateCookie() {
+ $this->childResponse->addCookie('foo', 'bar');
+ $this->childResponse->invalidateCookie('foo');
+ $expected = array(
+ 'foo' => array(
+ 'value' => 'expired',
+ 'expireDate' => new \DateTime('1971-01-01')
+ )
+ );
+
+ $cookies = $this->childResponse->getCookies();
+
+ $this->assertEquals($expected, $cookies);
+ }
+
+
+ function testInvalidateCookies() {
+ $this->childResponse->addCookie('foo', 'bar');
+ $this->childResponse->addCookie('bar', 'foo');
+ $expected = array(
+ 'foo' => array(
+ 'value' => 'bar',
+ 'expireDate' => null
+ ),
+ 'bar' => array(
+ 'value' => 'foo',
+ 'expireDate' => null
+ )
+ );
+ $cookies = $this->childResponse->getCookies();
+ $this->assertEquals($expected, $cookies);
+
+ $this->childResponse->invalidateCookies(array('foo', 'bar'));
+ $expected = array(
+ 'foo' => array(
+ 'value' => 'expired',
+ 'expireDate' => new \DateTime('1971-01-01')
+ ),
+ 'bar' => array(
+ 'value' => 'expired',
+ 'expireDate' => new \DateTime('1971-01-01')
+ )
+ );
+
+ $cookies = $this->childResponse->getCookies();
+ $this->assertEquals($expected, $cookies);
+ }
+
+
+ public function testRenderReturnNullByDefault(){
+ $this->assertEquals(null, $this->childResponse->render());
+ }
+
+
+ public function testGetStatus() {
+ $default = $this->childResponse->getStatus();
+
+ $this->childResponse->setStatus(Http::STATUS_NOT_FOUND);
+
+ $this->assertEquals(Http::STATUS_OK, $default);
+ $this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
+ }
+
+
+ public function testGetEtag() {
+ $this->childResponse->setEtag('hi');
+ $this->assertSame('hi', $this->childResponse->getEtag());
+ }
+
+
+ public function testGetLastModified() {
+ $lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
+ $lastModified->setTimestamp(1);
+ $this->childResponse->setLastModified($lastModified);
+ $this->assertEquals($lastModified, $this->childResponse->getLastModified());
+ }
+
+
+
+ public function testCacheSecondsZero() {
+ $this->childResponse->cacheFor(0);
+
+ $headers = $this->childResponse->getHeaders();
+ $this->assertEquals('no-cache, no-store, must-revalidate', $headers['Cache-Control']);
+ }
+
+
+ public function testCacheSeconds() {
+ $this->childResponse->cacheFor(33);
+
+ $headers = $this->childResponse->getHeaders();
+ $this->assertEquals('max-age=33, must-revalidate',
+ $headers['Cache-Control']);
+ }
+
+
+
+ public function testEtagLastModifiedHeaders() {
+ $lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
+ $lastModified->setTimestamp(1);
+ $this->childResponse->setLastModified($lastModified);
+ $headers = $this->childResponse->getHeaders();
+ $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
+ }
+
+ public function testChainability() {
+ $lastModified = new \DateTime(null, new \DateTimeZone('GMT'));
+ $lastModified->setTimestamp(1);
+
+ $this->childResponse->setEtag('hi')
+ ->setStatus(Http::STATUS_NOT_FOUND)
+ ->setLastModified($lastModified)
+ ->cacheFor(33)
+ ->addHeader('hello', 'world');
+
+ $headers = $this->childResponse->getHeaders();
+
+ $this->assertEquals('world', $headers['hello']);
+ $this->assertEquals(Http::STATUS_NOT_FOUND, $this->childResponse->getStatus());
+ $this->assertEquals('hi', $this->childResponse->getEtag());
+ $this->assertEquals('Thu, 01 Jan 1970 00:00:01 +0000', $headers['Last-Modified']);
+ $this->assertEquals('max-age=33, must-revalidate',
+ $headers['Cache-Control']);
+
+ }
+
+}
diff --git a/tests/lib/AppFramework/Http/StreamResponseTest.php b/tests/lib/AppFramework/Http/StreamResponseTest.php
new file mode 100644
index 00000000000..1f761d6b89c
--- /dev/null
+++ b/tests/lib/AppFramework/Http/StreamResponseTest.php
@@ -0,0 +1,100 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2015 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Http;
+
+
+use OCP\AppFramework\Http\StreamResponse;
+use OCP\AppFramework\Http\IOutput;
+use OCP\AppFramework\Http;
+
+
+class StreamResponseTest extends \Test\TestCase {
+
+ /** @var IOutput */
+ private $output;
+
+ protected function setUp() {
+ parent::setUp();
+ $this->output = $this->getMock('OCP\\AppFramework\\Http\\IOutput');
+ }
+
+ public function testOutputNotModified(){
+ $path = __FILE__;
+ $this->output->expects($this->once())
+ ->method('getHttpResponseCode')
+ ->will($this->returnValue(Http::STATUS_NOT_MODIFIED));
+ $this->output->expects($this->never())
+ ->method('setReadfile');
+ $response = new StreamResponse($path);
+
+ $response->callback($this->output);
+ }
+
+ public function testOutputOk(){
+ $path = __FILE__;
+ $this->output->expects($this->once())
+ ->method('getHttpResponseCode')
+ ->will($this->returnValue(Http::STATUS_OK));
+ $this->output->expects($this->once())
+ ->method('setReadfile')
+ ->with($this->equalTo($path))
+ ->will($this->returnValue(true));
+ $response = new StreamResponse($path);
+
+ $response->callback($this->output);
+ }
+
+ public function testOutputNotFound(){
+ $path = __FILE__ . 'test';
+ $this->output->expects($this->once())
+ ->method('getHttpResponseCode')
+ ->will($this->returnValue(Http::STATUS_OK));
+ $this->output->expects($this->never())
+ ->method('setReadfile');
+ $this->output->expects($this->once())
+ ->method('setHttpResponseCode')
+ ->with($this->equalTo(Http::STATUS_NOT_FOUND));
+ $response = new StreamResponse($path);
+
+ $response->callback($this->output);
+ }
+
+ public function testOutputReadFileError(){
+ $path = __FILE__;
+ $this->output->expects($this->once())
+ ->method('getHttpResponseCode')
+ ->will($this->returnValue(Http::STATUS_OK));
+ $this->output->expects($this->once())
+ ->method('setReadfile')
+ ->will($this->returnValue(false));
+ $this->output->expects($this->once())
+ ->method('setHttpResponseCode')
+ ->with($this->equalTo(Http::STATUS_BAD_REQUEST));
+ $response = new StreamResponse($path);
+
+ $response->callback($this->output);
+ }
+
+}
diff --git a/tests/lib/AppFramework/Http/TemplateResponseTest.php b/tests/lib/AppFramework/Http/TemplateResponseTest.php
new file mode 100644
index 00000000000..87fb6864f78
--- /dev/null
+++ b/tests/lib/AppFramework/Http/TemplateResponseTest.php
@@ -0,0 +1,99 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Http;
+
+use OCP\AppFramework\Http\TemplateResponse;
+use OCP\AppFramework\Http;
+
+
+class TemplateResponseTest extends \Test\TestCase {
+
+ /**
+ * @var \OCP\AppFramework\Http\TemplateResponse
+ */
+ private $tpl;
+
+ /**
+ * @var \OCP\AppFramework\IApi
+ */
+ private $api;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->api = $this->getMock('OC\AppFramework\Core\API',
+ array('getAppName'), array('test'));
+ $this->api->expects($this->any())
+ ->method('getAppName')
+ ->will($this->returnValue('app'));
+
+ $this->tpl = new TemplateResponse($this->api, 'home');
+ }
+
+
+ public function testSetParamsConstructor(){
+ $params = array('hi' => 'yo');
+ $this->tpl = new TemplateResponse($this->api, 'home', $params);
+
+ $this->assertEquals(array('hi' => 'yo'), $this->tpl->getParams());
+ }
+
+
+ public function testSetRenderAsConstructor(){
+ $renderAs = 'myrender';
+ $this->tpl = new TemplateResponse($this->api, 'home', array(), $renderAs);
+
+ $this->assertEquals($renderAs, $this->tpl->getRenderAs());
+ }
+
+
+ public function testSetParams(){
+ $params = array('hi' => 'yo');
+ $this->tpl->setParams($params);
+
+ $this->assertEquals(array('hi' => 'yo'), $this->tpl->getParams());
+ }
+
+
+ public function testGetTemplateName(){
+ $this->assertEquals('home', $this->tpl->getTemplateName());
+ }
+
+ public function testGetRenderAs(){
+ $render = 'myrender';
+ $this->tpl->renderAs($render);
+ $this->assertEquals($render, $this->tpl->getRenderAs());
+ }
+
+ public function testChainability() {
+ $params = array('hi' => 'yo');
+ $this->tpl->setParams($params)
+ ->setStatus(Http::STATUS_NOT_FOUND);
+
+ $this->assertEquals(Http::STATUS_NOT_FOUND, $this->tpl->getStatus());
+ $this->assertEquals(array('hi' => 'yo'), $this->tpl->getParams());
+ }
+
+}
diff --git a/tests/lib/AppFramework/Middleware/MiddlewareDispatcherTest.php b/tests/lib/AppFramework/Middleware/MiddlewareDispatcherTest.php
new file mode 100644
index 00000000000..f81aca106d6
--- /dev/null
+++ b/tests/lib/AppFramework/Middleware/MiddlewareDispatcherTest.php
@@ -0,0 +1,292 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Middleware;
+
+use OC\AppFramework\Http\Request;
+use OC\AppFramework\Middleware\MiddlewareDispatcher;
+use OCP\AppFramework\Middleware;
+use OCP\AppFramework\Http\Response;
+
+
+// needed to test ordering
+class TestMiddleware extends Middleware {
+ public static $beforeControllerCalled = 0;
+ public static $afterControllerCalled = 0;
+ public static $afterExceptionCalled = 0;
+ public static $beforeOutputCalled = 0;
+
+ public $beforeControllerOrder = 0;
+ public $afterControllerOrder = 0;
+ public $afterExceptionOrder = 0;
+ public $beforeOutputOrder = 0;
+
+ public $controller;
+ public $methodName;
+ public $exception;
+ public $response;
+ public $output;
+
+ private $beforeControllerThrowsEx;
+
+ /**
+ * @param boolean $beforeControllerThrowsEx
+ */
+ public function __construct($beforeControllerThrowsEx) {
+ self::$beforeControllerCalled = 0;
+ self::$afterControllerCalled = 0;
+ self::$afterExceptionCalled = 0;
+ self::$beforeOutputCalled = 0;
+ $this->beforeControllerThrowsEx = $beforeControllerThrowsEx;
+ }
+
+ public function beforeController($controller, $methodName){
+ self::$beforeControllerCalled++;
+ $this->beforeControllerOrder = self::$beforeControllerCalled;
+ $this->controller = $controller;
+ $this->methodName = $methodName;
+ if($this->beforeControllerThrowsEx){
+ throw new \Exception();
+ }
+ }
+
+ public function afterException($controller, $methodName, \Exception $exception){
+ self::$afterExceptionCalled++;
+ $this->afterExceptionOrder = self::$afterExceptionCalled;
+ $this->controller = $controller;
+ $this->methodName = $methodName;
+ $this->exception = $exception;
+ parent::afterException($controller, $methodName, $exception);
+ }
+
+ public function afterController($controller, $methodName, Response $response){
+ self::$afterControllerCalled++;
+ $this->afterControllerOrder = self::$afterControllerCalled;
+ $this->controller = $controller;
+ $this->methodName = $methodName;
+ $this->response = $response;
+ return parent::afterController($controller, $methodName, $response);
+ }
+
+ public function beforeOutput($controller, $methodName, $output){
+ self::$beforeOutputCalled++;
+ $this->beforeOutputOrder = self::$beforeOutputCalled;
+ $this->controller = $controller;
+ $this->methodName = $methodName;
+ $this->output = $output;
+ return parent::beforeOutput($controller, $methodName, $output);
+ }
+}
+
+
+class MiddlewareDispatcherTest extends \Test\TestCase {
+
+ public $exception;
+ public $response;
+ private $out;
+ private $method;
+ private $controller;
+
+ /**
+ * @var MiddlewareDispatcher
+ */
+ private $dispatcher;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->dispatcher = new MiddlewareDispatcher();
+ $this->controller = $this->getControllerMock();
+ $this->method = 'method';
+ $this->response = new Response();
+ $this->out = 'hi';
+ $this->exception = new \Exception();
+ }
+
+
+ private function getControllerMock(){
+ return $this->getMock(
+ 'OCP\AppFramework\Controller',
+ ['method'],
+ ['app',
+ new Request(
+ ['method' => 'GET'],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ )
+ ]
+ );
+ }
+
+
+ private function getMiddleware($beforeControllerThrowsEx=false){
+ $m1 = new TestMiddleware($beforeControllerThrowsEx);
+ $this->dispatcher->registerMiddleware($m1);
+ return $m1;
+ }
+
+
+ public function testAfterExceptionShouldReturnResponseOfMiddleware(){
+ $response = new Response();
+ $m1 = $this->getMock('\OCP\AppFramework\Middleware',
+ array('afterException', 'beforeController'));
+ $m1->expects($this->never())
+ ->method('afterException');
+
+ $m2 = $this->getMock('OCP\AppFramework\Middleware',
+ array('afterException', 'beforeController'));
+ $m2->expects($this->once())
+ ->method('afterException')
+ ->will($this->returnValue($response));
+
+ $this->dispatcher->registerMiddleware($m1);
+ $this->dispatcher->registerMiddleware($m2);
+
+ $this->dispatcher->beforeController($this->controller, $this->method);
+ $this->assertEquals($response, $this->dispatcher->afterException($this->controller, $this->method, $this->exception));
+ }
+
+
+ public function testAfterExceptionShouldThrowAgainWhenNotHandled(){
+ $m1 = new TestMiddleware(false);
+ $m2 = new TestMiddleware(true);
+
+ $this->dispatcher->registerMiddleware($m1);
+ $this->dispatcher->registerMiddleware($m2);
+
+ $this->setExpectedException('\Exception');
+ $this->dispatcher->beforeController($this->controller, $this->method);
+ $this->dispatcher->afterException($this->controller, $this->method, $this->exception);
+ }
+
+
+ public function testBeforeControllerCorrectArguments(){
+ $m1 = $this->getMiddleware();
+ $this->dispatcher->beforeController($this->controller, $this->method);
+
+ $this->assertEquals($this->controller, $m1->controller);
+ $this->assertEquals($this->method, $m1->methodName);
+ }
+
+
+ public function testAfterControllerCorrectArguments(){
+ $m1 = $this->getMiddleware();
+
+ $this->dispatcher->afterController($this->controller, $this->method, $this->response);
+
+ $this->assertEquals($this->controller, $m1->controller);
+ $this->assertEquals($this->method, $m1->methodName);
+ $this->assertEquals($this->response, $m1->response);
+ }
+
+
+ public function testAfterExceptionCorrectArguments(){
+ $m1 = $this->getMiddleware();
+
+ $this->setExpectedException('\Exception');
+
+ $this->dispatcher->beforeController($this->controller, $this->method);
+ $this->dispatcher->afterException($this->controller, $this->method, $this->exception);
+
+ $this->assertEquals($this->controller, $m1->controller);
+ $this->assertEquals($this->method, $m1->methodName);
+ $this->assertEquals($this->exception, $m1->exception);
+ }
+
+
+ public function testBeforeOutputCorrectArguments(){
+ $m1 = $this->getMiddleware();
+
+ $this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
+
+ $this->assertEquals($this->controller, $m1->controller);
+ $this->assertEquals($this->method, $m1->methodName);
+ $this->assertEquals($this->out, $m1->output);
+ }
+
+
+ public function testBeforeControllerOrder(){
+ $m1 = $this->getMiddleware();
+ $m2 = $this->getMiddleware();
+
+ $this->dispatcher->beforeController($this->controller, $this->method);
+
+ $this->assertEquals(1, $m1->beforeControllerOrder);
+ $this->assertEquals(2, $m2->beforeControllerOrder);
+ }
+
+ public function testAfterControllerOrder(){
+ $m1 = $this->getMiddleware();
+ $m2 = $this->getMiddleware();
+
+ $this->dispatcher->afterController($this->controller, $this->method, $this->response);
+
+ $this->assertEquals(2, $m1->afterControllerOrder);
+ $this->assertEquals(1, $m2->afterControllerOrder);
+ }
+
+
+ public function testAfterExceptionOrder(){
+ $m1 = $this->getMiddleware();
+ $m2 = $this->getMiddleware();
+
+ $this->setExpectedException('\Exception');
+ $this->dispatcher->beforeController($this->controller, $this->method);
+ $this->dispatcher->afterException($this->controller, $this->method, $this->exception);
+
+ $this->assertEquals(1, $m1->afterExceptionOrder);
+ $this->assertEquals(1, $m2->afterExceptionOrder);
+ }
+
+
+ public function testBeforeOutputOrder(){
+ $m1 = $this->getMiddleware();
+ $m2 = $this->getMiddleware();
+
+ $this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
+
+ $this->assertEquals(2, $m1->beforeOutputOrder);
+ $this->assertEquals(1, $m2->beforeOutputOrder);
+ }
+
+
+ public function testExceptionShouldRunAfterExceptionOfOnlyPreviouslyExecutedMiddlewares(){
+ $m1 = $this->getMiddleware();
+ $m2 = $this->getMiddleware(true);
+ $m3 = $this->getMock('\OCP\AppFramework\Middleware');
+ $m3->expects($this->never())
+ ->method('afterException');
+ $m3->expects($this->never())
+ ->method('beforeController');
+ $m3->expects($this->never())
+ ->method('afterController');
+
+ $this->dispatcher->registerMiddleware($m3);
+
+ $this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
+
+ $this->assertEquals(2, $m1->beforeOutputOrder);
+ $this->assertEquals(1, $m2->beforeOutputOrder);
+ }
+}
diff --git a/tests/lib/AppFramework/Middleware/MiddlewareTest.php b/tests/lib/AppFramework/Middleware/MiddlewareTest.php
new file mode 100644
index 00000000000..013403a9a4a
--- /dev/null
+++ b/tests/lib/AppFramework/Middleware/MiddlewareTest.php
@@ -0,0 +1,99 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Middleware;
+
+use OC\AppFramework\Http\Request;
+use OCP\AppFramework\Middleware;
+use OCP\AppFramework\Http\Response;
+
+class ChildMiddleware extends Middleware {};
+
+
+class MiddlewareTest extends \Test\TestCase {
+
+ /**
+ * @var Middleware
+ */
+ private $middleware;
+ private $controller;
+ private $exception;
+ private $api;
+ /** @var Response */
+ private $response;
+
+ protected function setUp(){
+ parent::setUp();
+
+ $this->middleware = new ChildMiddleware();
+
+ $this->api = $this->getMockBuilder(
+ 'OC\AppFramework\DependencyInjection\DIContainer')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->controller = $this->getMock(
+ 'OCP\AppFramework\Controller',
+ [],
+ [
+ $this->api,
+ new Request(
+ [],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ )
+ ]
+ );
+ $this->exception = new \Exception();
+ $this->response = $this->getMock('OCP\AppFramework\Http\Response');
+ }
+
+
+ public function testBeforeController() {
+ $this->middleware->beforeController($this->controller, null);
+ $this->assertNull(null);
+ }
+
+
+ public function testAfterExceptionRaiseAgainWhenUnhandled() {
+ $this->setExpectedException('Exception');
+ $afterEx = $this->middleware->afterException($this->controller, null, $this->exception);
+ }
+
+
+ public function testAfterControllerReturnResponseWhenUnhandled() {
+ $response = $this->middleware->afterController($this->controller, null, $this->response);
+
+ $this->assertEquals($this->response, $response);
+ }
+
+
+ public function testBeforeOutputReturnOutputhenUnhandled() {
+ $output = $this->middleware->beforeOutput($this->controller, null, 'test');
+
+ $this->assertEquals('test', $output);
+ }
+
+
+}
diff --git a/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php
new file mode 100644
index 00000000000..8e53c9202cf
--- /dev/null
+++ b/tests/lib/AppFramework/Middleware/Security/CORSMiddlewareTest.php
@@ -0,0 +1,236 @@
+<?php
+/**
+ * ownCloud - App Framework
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @copyright Bernhard Posselt 2014
+ */
+
+
+namespace Test\AppFramework\Middleware\Security;
+
+use OC\AppFramework\Http\Request;
+use OC\AppFramework\Middleware\Security\CORSMiddleware;
+use OC\AppFramework\Utility\ControllerMethodReflector;
+use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\JSONResponse;
+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');
+ }
+
+ /**
+ * @CORS
+ */
+ public function testSetCORSAPIHeader() {
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTP_ORIGIN' => 'test'
+ ]
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->reflector->reflect($this, __FUNCTION__);
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
+
+ $response = $middleware->afterController($this, __FUNCTION__, new Response());
+ $headers = $response->getHeaders();
+ $this->assertEquals('test', $headers['Access-Control-Allow-Origin']);
+ }
+
+
+ public function testNoAnnotationNoCORSHEADER() {
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTP_ORIGIN' => 'test'
+ ]
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
+
+ $response = $middleware->afterController($this, __FUNCTION__, new Response());
+ $headers = $response->getHeaders();
+ $this->assertFalse(array_key_exists('Access-Control-Allow-Origin', $headers));
+ }
+
+
+ /**
+ * @CORS
+ */
+ public function testNoOriginHeaderNoCORSHEADER() {
+ $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);
+
+ $response = $middleware->afterController($this, __FUNCTION__, new Response());
+ $headers = $response->getHeaders();
+ $this->assertFalse(array_key_exists('Access-Control-Allow-Origin', $headers));
+ }
+
+
+ /**
+ * @CORS
+ * @expectedException \OC\AppFramework\Middleware\Security\Exceptions\SecurityException
+ */
+ public function testCorsIgnoredIfWithCredentialsHeaderPresent() {
+ $request = new Request(
+ [
+ 'server' => [
+ 'HTTP_ORIGIN' => 'test'
+ ]
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->reflector->reflect($this, __FUNCTION__);
+ $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);
+ $this->session->expects($this->never())
+ ->method('logout');
+ $this->session->expects($this->never())
+ ->method('login')
+ ->with($this->equalTo('user'), $this->equalTo('pass'))
+ ->will($this->returnValue(true));
+ $this->reflector->reflect($this, __FUNCTION__);
+
+ $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\Exceptions\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());
+ }
+
+ public function testAfterExceptionWithSecurityExceptionNoStatus() {
+ $request = new Request(
+ ['server' => [
+ 'PHP_AUTH_USER' => 'user',
+ 'PHP_AUTH_PW' => 'pass'
+ ]],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
+ $response = $middleware->afterException($this, __FUNCTION__, new SecurityException('A security exception'));
+
+ $expected = new JSONResponse(['message' => 'A security exception'], 500);
+ $this->assertEquals($expected, $response);
+ }
+
+ public function testAfterExceptionWithSecurityExceptionWithStatus() {
+ $request = new Request(
+ ['server' => [
+ 'PHP_AUTH_USER' => 'user',
+ 'PHP_AUTH_PW' => 'pass'
+ ]],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
+ $response = $middleware->afterException($this, __FUNCTION__, new SecurityException('A security exception', 501));
+
+ $expected = new JSONResponse(['message' => 'A security exception'], 501);
+ $this->assertEquals($expected, $response);
+ }
+
+ /**
+ * @expectedException \Exception
+ * @expectedExceptionMessage A regular exception
+ */
+ public function testAfterExceptionWithRegularException() {
+ $request = new Request(
+ ['server' => [
+ 'PHP_AUTH_USER' => 'user',
+ 'PHP_AUTH_PW' => 'pass'
+ ]],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $middleware = new CORSMiddleware($request, $this->reflector, $this->session);
+ $middleware->afterException($this, __FUNCTION__, new \Exception('A regular exception'));
+ }
+
+}
diff --git a/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php b/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php
new file mode 100644
index 00000000000..8cdba76d835
--- /dev/null
+++ b/tests/lib/AppFramework/Middleware/Security/SecurityMiddlewareTest.php
@@ -0,0 +1,453 @@
+<?php
+/**
+ * @author Bernhard Posselt <dev@bernhard-posselt.com>
+ * @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\AppFramework\Middleware\Security;
+
+use OC\AppFramework\Http;
+use OC\AppFramework\Http\Request;
+use OC\AppFramework\Middleware\Security\Exceptions\AppNotEnabledException;
+use OC\AppFramework\Middleware\Security\Exceptions\CrossSiteRequestForgeryException;
+use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
+use OC\AppFramework\Middleware\Security\Exceptions\NotLoggedInException;
+use OC\AppFramework\Middleware\Security\Exceptions\SecurityException;
+use OC\AppFramework\Middleware\Security\SecurityMiddleware;
+use OC\AppFramework\Utility\ControllerMethodReflector;
+use OC\Security\CSP\ContentSecurityPolicy;
+use OCP\AppFramework\Http\RedirectResponse;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\AppFramework\Http\TemplateResponse;
+
+
+class SecurityMiddlewareTest extends \Test\TestCase {
+
+ private $middleware;
+ private $controller;
+ private $secException;
+ private $secAjaxException;
+ private $request;
+ private $reader;
+ private $logger;
+ private $navigationManager;
+ private $urlGenerator;
+ private $contentSecurityPolicyManager;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->controller = $this->getMockBuilder('OCP\AppFramework\Controller')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->reader = new ControllerMethodReflector();
+ $this->logger = $this->getMockBuilder(
+ 'OCP\ILogger')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->navigationManager = $this->getMockBuilder(
+ 'OCP\INavigationManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->urlGenerator = $this->getMockBuilder(
+ 'OCP\IURLGenerator')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->request = $this->getMockBuilder(
+ 'OCP\IRequest')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->contentSecurityPolicyManager = $this->getMockBuilder(
+ 'OC\Security\CSP\ContentSecurityPolicyManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->middleware = $this->getMiddleware(true, true);
+ $this->secException = new SecurityException('hey', false);
+ $this->secAjaxException = new SecurityException('hey', true);
+ }
+
+ /**
+ * @param bool $isLoggedIn
+ * @param bool $isAdminUser
+ * @return SecurityMiddleware
+ */
+ private function getMiddleware($isLoggedIn, $isAdminUser) {
+ return new SecurityMiddleware(
+ $this->request,
+ $this->reader,
+ $this->navigationManager,
+ $this->urlGenerator,
+ $this->logger,
+ 'files',
+ $isLoggedIn,
+ $isAdminUser,
+ $this->contentSecurityPolicyManager
+ );
+ }
+
+
+ /**
+ * @PublicPage
+ * @NoCSRFRequired
+ */
+ public function testSetNavigationEntry(){
+ $this->navigationManager->expects($this->once())
+ ->method('setActiveEntry')
+ ->with($this->equalTo('files'));
+
+ $this->reader->reflect(__CLASS__, __FUNCTION__);
+ $this->middleware->beforeController(__CLASS__, __FUNCTION__);
+ }
+
+
+ /**
+ * @param string $method
+ * @param string $test
+ */
+ private function ajaxExceptionStatus($method, $test, $status) {
+ $isLoggedIn = false;
+ $isAdminUser = false;
+
+ // isAdminUser requires isLoggedIn call to return true
+ if ($test === 'isAdminUser') {
+ $isLoggedIn = true;
+ }
+
+ $sec = $this->getMiddleware($isLoggedIn, $isAdminUser);
+
+ try {
+ $this->reader->reflect(__CLASS__, $method);
+ $sec->beforeController(__CLASS__, $method);
+ } catch (SecurityException $ex){
+ $this->assertEquals($status, $ex->getCode());
+ }
+
+ // add assertion if everything should work fine otherwise phpunit will
+ // complain
+ if ($status === 0) {
+ $this->assertTrue(true);
+ }
+ }
+
+ public function testAjaxStatusLoggedInCheck() {
+ $this->ajaxExceptionStatus(
+ __FUNCTION__,
+ 'isLoggedIn',
+ Http::STATUS_UNAUTHORIZED
+ );
+ }
+
+ /**
+ * @NoCSRFRequired
+ */
+ public function testAjaxNotAdminCheck() {
+ $this->ajaxExceptionStatus(
+ __FUNCTION__,
+ 'isAdminUser',
+ Http::STATUS_FORBIDDEN
+ );
+ }
+
+ /**
+ * @PublicPage
+ */
+ public function testAjaxStatusCSRFCheck() {
+ $this->ajaxExceptionStatus(
+ __FUNCTION__,
+ 'passesCSRFCheck',
+ Http::STATUS_PRECONDITION_FAILED
+ );
+ }
+
+ /**
+ * @PublicPage
+ * @NoCSRFRequired
+ */
+ public function testAjaxStatusAllGood() {
+ $this->ajaxExceptionStatus(
+ __FUNCTION__,
+ 'isLoggedIn',
+ 0
+ );
+ $this->ajaxExceptionStatus(
+ __FUNCTION__,
+ 'isAdminUser',
+ 0
+ );
+ $this->ajaxExceptionStatus(
+ __FUNCTION__,
+ 'isSubAdminUser',
+ 0
+ );
+ $this->ajaxExceptionStatus(
+ __FUNCTION__,
+ 'passesCSRFCheck',
+ 0
+ );
+ }
+
+
+ /**
+ * @PublicPage
+ * @NoCSRFRequired
+ */
+ public function testNoChecks(){
+ $this->request->expects($this->never())
+ ->method('passesCSRFCheck')
+ ->will($this->returnValue(false));
+
+ $sec = $this->getMiddleware(false, false);
+
+ $this->reader->reflect(__CLASS__, __FUNCTION__);
+ $sec->beforeController(__CLASS__, __FUNCTION__);
+ }
+
+
+ /**
+ * @param string $method
+ * @param string $expects
+ */
+ private function securityCheck($method, $expects, $shouldFail=false){
+ // admin check requires login
+ if ($expects === 'isAdminUser') {
+ $isLoggedIn = true;
+ $isAdminUser = !$shouldFail;
+ } else {
+ $isLoggedIn = !$shouldFail;
+ $isAdminUser = false;
+ }
+
+ $sec = $this->getMiddleware($isLoggedIn, $isAdminUser);
+
+ if($shouldFail) {
+ $this->setExpectedException('\OC\AppFramework\Middleware\Security\Exceptions\SecurityException');
+ } else {
+ $this->assertTrue(true);
+ }
+
+ $this->reader->reflect(__CLASS__, $method);
+ $sec->beforeController(__CLASS__, $method);
+ }
+
+
+ /**
+ * @PublicPage
+ * @expectedException \OC\AppFramework\Middleware\Security\Exceptions\CrossSiteRequestForgeryException
+ */
+ public function testCsrfCheck(){
+ $this->request->expects($this->once())
+ ->method('passesCSRFCheck')
+ ->will($this->returnValue(false));
+
+ $this->reader->reflect(__CLASS__, __FUNCTION__);
+ $this->middleware->beforeController(__CLASS__, __FUNCTION__);
+ }
+
+
+ /**
+ * @PublicPage
+ * @NoCSRFRequired
+ */
+ public function testNoCsrfCheck(){
+ $this->request->expects($this->never())
+ ->method('passesCSRFCheck')
+ ->will($this->returnValue(false));
+
+ $this->reader->reflect(__CLASS__, __FUNCTION__);
+ $this->middleware->beforeController(__CLASS__, __FUNCTION__);
+ }
+
+
+ /**
+ * @PublicPage
+ */
+ public function testFailCsrfCheck(){
+ $this->request->expects($this->once())
+ ->method('passesCSRFCheck')
+ ->will($this->returnValue(true));
+
+ $this->reader->reflect(__CLASS__, __FUNCTION__);
+ $this->middleware->beforeController(__CLASS__, __FUNCTION__);
+ }
+
+
+ /**
+ * @NoCSRFRequired
+ * @NoAdminRequired
+ */
+ public function testLoggedInCheck(){
+ $this->securityCheck(__FUNCTION__, 'isLoggedIn');
+ }
+
+
+ /**
+ * @NoCSRFRequired
+ * @NoAdminRequired
+ */
+ public function testFailLoggedInCheck(){
+ $this->securityCheck(__FUNCTION__, 'isLoggedIn', true);
+ }
+
+
+ /**
+ * @NoCSRFRequired
+ */
+ public function testIsAdminCheck(){
+ $this->securityCheck(__FUNCTION__, 'isAdminUser');
+ }
+
+
+ /**
+ * @NoCSRFRequired
+ */
+ public function testFailIsAdminCheck(){
+ $this->securityCheck(__FUNCTION__, 'isAdminUser', true);
+ }
+
+
+ public function testAfterExceptionNotCaughtThrowsItAgain(){
+ $ex = new \Exception();
+ $this->setExpectedException('\Exception');
+ $this->middleware->afterException($this->controller, 'test', $ex);
+ }
+
+ public function testAfterExceptionReturnsRedirectForNotLoggedInUser() {
+ $this->request = new Request(
+ [
+ 'server' =>
+ [
+ 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
+ 'REQUEST_URI' => 'owncloud/index.php/apps/specialapp'
+ ]
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->middleware = $this->getMiddleware(false, false);
+ $this->urlGenerator
+ ->expects($this->once())
+ ->method('linkToRoute')
+ ->with(
+ 'core.login.showLoginForm',
+ [
+ 'redirect_url' => 'owncloud%2Findex.php%2Fapps%2Fspecialapp',
+ ]
+ )
+ ->will($this->returnValue('http://localhost/index.php/login?redirect_url=owncloud%2Findex.php%2Fapps%2Fspecialapp'));
+ $this->logger
+ ->expects($this->once())
+ ->method('debug')
+ ->with('Current user is not logged in');
+ $response = $this->middleware->afterException(
+ $this->controller,
+ 'test',
+ new NotLoggedInException()
+ );
+
+ $expected = new RedirectResponse('http://localhost/index.php/login?redirect_url=owncloud%2Findex.php%2Fapps%2Fspecialapp');
+ $this->assertEquals($expected , $response);
+ }
+
+ /**
+ * @return array
+ */
+ public function exceptionProvider() {
+ return [
+ [
+ new AppNotEnabledException(),
+ ],
+ [
+ new CrossSiteRequestForgeryException(),
+ ],
+ [
+ new NotAdminException(),
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider exceptionProvider
+ * @param SecurityException $exception
+ */
+ public function testAfterExceptionReturnsTemplateResponse(SecurityException $exception) {
+ $this->request = new Request(
+ [
+ 'server' =>
+ [
+ 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
+ 'REQUEST_URI' => 'owncloud/index.php/apps/specialapp'
+ ]
+ ],
+ $this->getMock('\OCP\Security\ISecureRandom'),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->middleware = $this->getMiddleware(false, false);
+ $this->logger
+ ->expects($this->once())
+ ->method('debug')
+ ->with($exception->getMessage());
+ $response = $this->middleware->afterException(
+ $this->controller,
+ 'test',
+ $exception
+ );
+
+ $expected = new TemplateResponse('core', '403', ['file' => $exception->getMessage()], 'guest');
+ $expected->setStatus($exception->getCode());
+ $this->assertEquals($expected , $response);
+ }
+
+
+ public function testAfterAjaxExceptionReturnsJSONError(){
+ $response = $this->middleware->afterException($this->controller, 'test',
+ $this->secAjaxException);
+
+ $this->assertTrue($response instanceof JSONResponse);
+ }
+
+ public function testAfterController() {
+ $response = $this->getMockBuilder('\OCP\AppFramework\Http\Response')->disableOriginalConstructor()->getMock();
+ $defaultPolicy = new ContentSecurityPolicy();
+ $defaultPolicy->addAllowedImageDomain('defaultpolicy');
+ $currentPolicy = new ContentSecurityPolicy();
+ $currentPolicy->addAllowedConnectDomain('currentPolicy');
+ $mergedPolicy = new ContentSecurityPolicy();
+ $mergedPolicy->addAllowedMediaDomain('mergedPolicy');
+ $response
+ ->expects($this->exactly(2))
+ ->method('getContentSecurityPolicy')
+ ->willReturn($currentPolicy);
+ $this->contentSecurityPolicyManager
+ ->expects($this->once())
+ ->method('getDefaultPolicy')
+ ->willReturn($defaultPolicy);
+ $this->contentSecurityPolicyManager
+ ->expects($this->once())
+ ->method('mergePolicies')
+ ->with($defaultPolicy, $currentPolicy)
+ ->willReturn($mergedPolicy);
+ $response->expects($this->once())
+ ->method('setContentSecurityPolicy')
+ ->with($mergedPolicy);
+
+ $this->middleware->afterController($this->controller, 'test', $response);
+ }
+}
diff --git a/tests/lib/AppFramework/Middleware/SessionMiddlewareTest.php b/tests/lib/AppFramework/Middleware/SessionMiddlewareTest.php
new file mode 100644
index 00000000000..17fcc1904c1
--- /dev/null
+++ b/tests/lib/AppFramework/Middleware/SessionMiddlewareTest.php
@@ -0,0 +1,95 @@
+<?php
+/**
+ * ownCloud - App Framework
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ *
+ * @author Thomas Müller <deepdiver@owncloud.com>
+ * @copyright Thomas Müller 2014
+ */
+
+
+namespace Test\AppFramework\Middleware;
+
+use OC\AppFramework\Http\Request;
+use OC\AppFramework\Middleware\SessionMiddleware;
+use OC\AppFramework\Utility\ControllerMethodReflector;
+use OCP\AppFramework\Http\Response;
+
+
+class SessionMiddlewareTest extends \Test\TestCase {
+
+ /**
+ * @var ControllerMethodReflector
+ */
+ private $reflector;
+
+ /**
+ * @var Request
+ */
+ private $request;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->request = new Request(
+ [],
+ $this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
+ $this->getMock('\OCP\IConfig')
+ );
+ $this->reflector = new ControllerMethodReflector();
+ }
+
+ /**
+ * @UseSession
+ */
+ public function testSessionNotClosedOnBeforeController() {
+ $session = $this->getSessionMock(0);
+
+ $this->reflector->reflect($this, __FUNCTION__);
+ $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
+ $middleware->beforeController($this, __FUNCTION__);
+ }
+
+ /**
+ * @UseSession
+ */
+ public function testSessionClosedOnAfterController() {
+ $session = $this->getSessionMock(1);
+
+ $this->reflector->reflect($this, __FUNCTION__);
+ $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
+ $middleware->afterController($this, __FUNCTION__, new Response());
+ }
+
+ public function testSessionClosedOnBeforeController() {
+ $session = $this->getSessionMock(1);
+
+ $this->reflector->reflect($this, __FUNCTION__);
+ $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
+ $middleware->beforeController($this, __FUNCTION__);
+ }
+
+ public function testSessionNotClosedOnAfterController() {
+ $session = $this->getSessionMock(0);
+
+ $this->reflector->reflect($this, __FUNCTION__);
+ $middleware = new SessionMiddleware($this->request, $this->reflector, $session);
+ $middleware->afterController($this, __FUNCTION__, new Response());
+ }
+
+ /**
+ * @return mixed
+ */
+ private function getSessionMock($expectedCloseCount) {
+ $session = $this->getMockBuilder('\OC\Session\Memory')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $session->expects($this->exactly($expectedCloseCount))
+ ->method('close');
+ return $session;
+ }
+
+}
diff --git a/tests/lib/AppFramework/Routing/RoutingTest.php b/tests/lib/AppFramework/Routing/RoutingTest.php
new file mode 100644
index 00000000000..326c156af98
--- /dev/null
+++ b/tests/lib/AppFramework/Routing/RoutingTest.php
@@ -0,0 +1,283 @@
+<?php
+
+namespace Test\AppFramework\Routing;
+
+use OC\AppFramework\DependencyInjection\DIContainer;
+use OC\AppFramework\Routing\RouteActionHandler;
+use OC\AppFramework\Routing\RouteConfig;
+
+class RoutingTest extends \Test\TestCase
+{
+
+ public function testSimpleRoute()
+ {
+ $routes = array('routes' => array(
+ array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'GET')
+ ));
+
+ $this->assertSimpleRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
+ }
+
+ public function testSimpleRouteWithMissingVerb()
+ {
+ $routes = array('routes' => array(
+ array('name' => 'folders#open', 'url' => '/folders/{folderId}/open')
+ ));
+
+ $this->assertSimpleRoute($routes, 'folders.open', 'GET', '/folders/{folderId}/open', 'FoldersController', 'open');
+ }
+
+ public function testSimpleRouteWithLowercaseVerb()
+ {
+ $routes = array('routes' => array(
+ array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete')
+ ));
+
+ $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open');
+ }
+
+ public function testSimpleRouteWithRequirements()
+ {
+ $routes = array('routes' => array(
+ array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'requirements' => array('something'))
+ ));
+
+ $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array('something'));
+ }
+
+ public function testSimpleRouteWithDefaults()
+ {
+ $routes = array('routes' => array(
+ array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', array(), 'defaults' => array('param' => 'foobar'))
+ ));
+
+ $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array(), array('param' => 'foobar'));
+ }
+
+ public function testSimpleRouteWithPostfix()
+ {
+ $routes = array('routes' => array(
+ array('name' => 'folders#open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete', 'postfix' => '_something')
+ ));
+
+ $this->assertSimpleRoute($routes, 'folders.open', 'DELETE', '/folders/{folderId}/open', 'FoldersController', 'open', array(), array(), '_something');
+ }
+
+
+ /**
+ * @expectedException \UnexpectedValueException
+ */
+ public function testSimpleRouteWithBrokenName()
+ {
+ $routes = array('routes' => array(
+ array('name' => 'folders_open', 'url' => '/folders/{folderId}/open', 'verb' => 'delete')
+ ));
+
+ // router mock
+ $router = $this->getMock("\OC\Route\Router", array('create'), [\OC::$server->getLogger()]);
+
+ // load route configuration
+ $container = new DIContainer('app1');
+ $config = new RouteConfig($container, $router, $routes);
+
+ $config->register();
+ }
+
+ public function testSimpleRouteWithUnderScoreNames()
+ {
+ $routes = array('routes' => array(
+ array('name' => 'admin_folders#open_current', 'url' => '/folders/{folderId}/open', 'verb' => 'delete')
+ ));
+
+ $this->assertSimpleRoute($routes, 'admin_folders.open_current', 'DELETE', '/folders/{folderId}/open', 'AdminFoldersController', 'openCurrent');
+ }
+
+ public function testResource()
+ {
+ $routes = array('resources' => array('account' => array('url' => '/accounts')));
+
+ $this->assertResource($routes, 'account', '/accounts', 'AccountController', 'id');
+ }
+
+ public function testResourceWithUnderScoreName()
+ {
+ $routes = array('resources' => array('admin_accounts' => array('url' => '/admin/accounts')));
+
+ $this->assertResource($routes, 'admin_accounts', '/admin/accounts', 'AdminAccountsController', 'id');
+ }
+
+ /**
+ * @param string $name
+ * @param string $verb
+ * @param string $url
+ * @param string $controllerName
+ * @param string $actionName
+ */
+ private function assertSimpleRoute($routes, $name, $verb, $url, $controllerName, $actionName, array $requirements=array(), array $defaults=array(), $postfix='')
+ {
+ if ($postfix) {
+ $name .= $postfix;
+ }
+
+ // route mocks
+ $container = new DIContainer('app1');
+ $route = $this->mockRoute($container, $verb, $controllerName, $actionName, $requirements, $defaults);
+
+ // router mock
+ $router = $this->getMock("\OC\Route\Router", array('create'), [\OC::$server->getLogger()]);
+
+ // we expect create to be called once:
+ $router
+ ->expects($this->once())
+ ->method('create')
+ ->with($this->equalTo('app1.' . $name), $this->equalTo($url))
+ ->will($this->returnValue($route));
+
+ // load route configuration
+ $config = new RouteConfig($container, $router, $routes);
+
+ $config->register();
+ }
+
+ /**
+ * @param string $resourceName
+ * @param string $url
+ * @param string $controllerName
+ * @param string $paramName
+ */
+ private function assertResource($yaml, $resourceName, $url, $controllerName, $paramName)
+ {
+ // router mock
+ $router = $this->getMock("\OC\Route\Router", array('create'), [\OC::$server->getLogger()]);
+
+ // route mocks
+ $container = new DIContainer('app1');
+ $indexRoute = $this->mockRoute($container, 'GET', $controllerName, 'index');
+ $showRoute = $this->mockRoute($container, 'GET', $controllerName, 'show');
+ $createRoute = $this->mockRoute($container, 'POST', $controllerName, 'create');
+ $updateRoute = $this->mockRoute($container, 'PUT', $controllerName, 'update');
+ $destroyRoute = $this->mockRoute($container, 'DELETE', $controllerName, 'destroy');
+
+ $urlWithParam = $url . '/{' . $paramName . '}';
+
+ // we expect create to be called once:
+ $router
+ ->expects($this->at(0))
+ ->method('create')
+ ->with($this->equalTo('app1.' . $resourceName . '.index'), $this->equalTo($url))
+ ->will($this->returnValue($indexRoute));
+
+ $router
+ ->expects($this->at(1))
+ ->method('create')
+ ->with($this->equalTo('app1.' . $resourceName . '.show'), $this->equalTo($urlWithParam))
+ ->will($this->returnValue($showRoute));
+
+ $router
+ ->expects($this->at(2))
+ ->method('create')
+ ->with($this->equalTo('app1.' . $resourceName . '.create'), $this->equalTo($url))
+ ->will($this->returnValue($createRoute));
+
+ $router
+ ->expects($this->at(3))
+ ->method('create')
+ ->with($this->equalTo('app1.' . $resourceName . '.update'), $this->equalTo($urlWithParam))
+ ->will($this->returnValue($updateRoute));
+
+ $router
+ ->expects($this->at(4))
+ ->method('create')
+ ->with($this->equalTo('app1.' . $resourceName . '.destroy'), $this->equalTo($urlWithParam))
+ ->will($this->returnValue($destroyRoute));
+
+ // load route configuration
+ $config = new RouteConfig($container, $router, $yaml);
+
+ $config->register();
+ }
+
+ /**
+ * @param DIContainer $container
+ * @param string $verb
+ * @param string $controllerName
+ * @param string $actionName
+ * @param array $requirements
+ * @param array $defaults
+ * @return \PHPUnit_Framework_MockObject_MockObject
+ */
+ private function mockRoute(
+ DIContainer $container,
+ $verb,
+ $controllerName,
+ $actionName,
+ array $requirements=array(),
+ array $defaults=array()
+ ) {
+ $route = $this->getMock("\OC\Route\Route", array('method', 'action', 'requirements', 'defaults'), array(), '', false);
+ $route
+ ->expects($this->exactly(1))
+ ->method('method')
+ ->with($this->equalTo($verb))
+ ->will($this->returnValue($route));
+
+ $route
+ ->expects($this->exactly(1))
+ ->method('action')
+ ->with($this->equalTo(new RouteActionHandler($container, $controllerName, $actionName)))
+ ->will($this->returnValue($route));
+
+ if(count($requirements) > 0) {
+ $route
+ ->expects($this->exactly(1))
+ ->method('requirements')
+ ->with($this->equalTo($requirements))
+ ->will($this->returnValue($route));
+ }
+
+ if (count($defaults) > 0) {
+ $route
+ ->expects($this->exactly(1))
+ ->method('defaults')
+ ->with($this->equalTo($defaults))
+ ->will($this->returnValue($route));
+ }
+
+ return $route;
+ }
+
+}
+
+/*
+#
+# sample routes.yaml for ownCloud
+#
+# the section simple describes one route
+
+routes:
+ - name: folders#open
+ url: /folders/{folderId}/open
+ verb: GET
+ # controller: name.split()[0]
+ # action: name.split()[1]
+
+# for a resource following actions will be generated:
+# - index
+# - create
+# - show
+# - update
+# - destroy
+# - new
+resources:
+ accounts:
+ url: /accounts
+
+ folders:
+ url: /accounts/{accountId}/folders
+ # actions can be used to define additional actions on the resource
+ actions:
+ - name: validate
+ verb: GET
+ on-collection: false
+
+ * */
diff --git a/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php b/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php
new file mode 100644
index 00000000000..92d767e9987
--- /dev/null
+++ b/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php
@@ -0,0 +1,210 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+
+
+namespace Test\AppFramework\Utility;
+
+
+use OC\AppFramework\Utility\ControllerMethodReflector;
+
+class BaseController {
+
+ /**
+ * @Annotation
+ */
+ public function test(){}
+
+ /**
+ * @Annotation
+ */
+ public function test2(){}
+
+ /**
+ * @Annotation
+ */
+ public function test3(){}
+
+}
+
+class MiddleController extends BaseController {
+
+ /**
+ * @NoAnnotation
+ */
+ public function test2() {}
+
+ public function test3() {}
+
+}
+
+class EndController extends MiddleController {}
+
+class ControllerMethodReflectorTest extends \Test\TestCase {
+
+
+ /**
+ * @Annotation
+ */
+ public function testReadAnnotation(){
+ $reader = new ControllerMethodReflector();
+ $reader->reflect(
+ '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
+ 'testReadAnnotation'
+ );
+
+ $this->assertTrue($reader->hasAnnotation('Annotation'));
+ }
+
+
+ /**
+ * @Annotation
+ * @param test
+ */
+ public function testReadAnnotationNoLowercase(){
+ $reader = new ControllerMethodReflector();
+ $reader->reflect(
+ '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
+ 'testReadAnnotationNoLowercase'
+ );
+
+ $this->assertTrue($reader->hasAnnotation('Annotation'));
+ $this->assertFalse($reader->hasAnnotation('param'));
+ }
+
+
+ /**
+ * @Annotation
+ * @param int $test
+ */
+ public function testReadTypeIntAnnotations(){
+ $reader = new ControllerMethodReflector();
+ $reader->reflect(
+ '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
+ 'testReadTypeIntAnnotations'
+ );
+
+ $this->assertEquals('int', $reader->getType('test'));
+ }
+
+ /**
+ * @Annotation
+ * @param int $a
+ * @param int $b
+ */
+ public function arguments3($a, float $b, int $c, $d){}
+
+ /**
+ * @requires PHP 7
+ */
+ public function testReadTypeIntAnnotationsScalarTypes(){
+ $reader = new ControllerMethodReflector();
+ $reader->reflect(
+ '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
+ 'arguments3'
+ );
+
+ $this->assertEquals('int', $reader->getType('a'));
+ $this->assertEquals('float', $reader->getType('b'));
+ $this->assertEquals('int', $reader->getType('c'));
+ $this->assertNull($reader->getType('d'));
+ }
+
+
+ /**
+ * @Annotation
+ * @param double $test something special
+ */
+ public function testReadTypeDoubleAnnotations(){
+ $reader = new ControllerMethodReflector();
+ $reader->reflect(
+ '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
+ 'testReadTypeDoubleAnnotations'
+ );
+
+ $this->assertEquals('double', $reader->getType('test'));
+ }
+
+ /**
+ * @Annotation
+ * @param string $foo
+ */
+ public function testReadTypeWhitespaceAnnotations(){
+ $reader = new ControllerMethodReflector();
+ $reader->reflect(
+ '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
+ 'testReadTypeWhitespaceAnnotations'
+ );
+
+ $this->assertEquals('string', $reader->getType('foo'));
+ }
+
+
+ public function arguments($arg, $arg2='hi') {}
+ public function testReflectParameters() {
+ $reader = new ControllerMethodReflector();
+ $reader->reflect(
+ '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
+ 'arguments'
+ );
+
+ $this->assertEquals(array('arg' => null, 'arg2' => 'hi'), $reader->getParameters());
+ }
+
+
+ public function arguments2($arg) {}
+ public function testReflectParameters2() {
+ $reader = new ControllerMethodReflector();
+ $reader->reflect(
+ '\Test\AppFramework\Utility\ControllerMethodReflectorTest',
+ 'arguments2'
+ );
+
+ $this->assertEquals(array('arg' => null), $reader->getParameters());
+ }
+
+
+ public function testInheritance() {
+ $reader = new ControllerMethodReflector();
+ $reader->reflect('Test\AppFramework\Utility\EndController', 'test');
+
+ $this->assertTrue($reader->hasAnnotation('Annotation'));
+ }
+
+
+ public function testInheritanceOverride() {
+ $reader = new ControllerMethodReflector();
+ $reader->reflect('Test\AppFramework\Utility\EndController', 'test2');
+
+ $this->assertTrue($reader->hasAnnotation('NoAnnotation'));
+ $this->assertFalse($reader->hasAnnotation('Annotation'));
+ }
+
+
+ public function testInheritanceOverrideNoDocblock() {
+ $reader = new ControllerMethodReflector();
+ $reader->reflect('Test\AppFramework\Utility\EndController', 'test3');
+
+ $this->assertFalse($reader->hasAnnotation('Annotation'));
+ }
+
+}
diff --git a/tests/lib/AppFramework/Utility/SimpleContainerTest.php b/tests/lib/AppFramework/Utility/SimpleContainerTest.php
new file mode 100644
index 00000000000..e21e0ca13f2
--- /dev/null
+++ b/tests/lib/AppFramework/Utility/SimpleContainerTest.php
@@ -0,0 +1,222 @@
+<?php
+
+/**
+ * ownCloud - App Framework
+ *
+ * @author Bernhard Posselt
+ * @copyright 2014 Bernhard Posselt <dev@bernhard-posselt.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/>.
+ *
+ */
+namespace Test\AppFramework\Utility;
+
+use OC\AppFramework\Utility\SimpleContainer;
+
+
+interface TestInterface {}
+
+class ClassEmptyConstructor implements IInterfaceConstructor {}
+
+class ClassSimpleConstructor implements IInterfaceConstructor {
+ public $test;
+ public function __construct($test) {
+ $this->test = $test;
+ }
+}
+
+class ClassComplexConstructor {
+ public $class;
+ public $test;
+ public function __construct(ClassSimpleConstructor $class, $test) {
+ $this->class = $class;
+ $this->test = $test;
+ }
+}
+
+interface IInterfaceConstructor {}
+class ClassInterfaceConstructor {
+ public $class;
+ public $test;
+ public function __construct(IInterfaceConstructor $class, $test) {
+ $this->class = $class;
+ $this->test = $test;
+ }
+}
+
+
+class SimpleContainerTest extends \Test\TestCase {
+
+
+ private $container;
+
+ public function setUp() {
+ $this->container = new SimpleContainer();
+ }
+
+
+ public function testRegister() {
+ $this->container->registerParameter('test', 'abc');
+ $this->assertEquals('abc', $this->container->query('test'));
+ }
+
+
+ /**
+ * @expectedException \OCP\AppFramework\QueryException
+ */
+ public function testNothingRegistered() {
+ $this->container->query('something really hard');
+ }
+
+
+ /**
+ * @expectedException \OCP\AppFramework\QueryException
+ */
+ public function testNotAClass() {
+ $this->container->query('Test\AppFramework\Utility\TestInterface');
+ }
+
+
+ public function testNoConstructorClass() {
+ $object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
+ $this->assertTrue($object instanceof ClassEmptyConstructor);
+ }
+
+
+ public function testInstancesOnlyOnce() {
+ $object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
+ $object2 = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
+ $this->assertSame($object, $object2);
+ }
+
+ public function testConstructorSimple() {
+ $this->container->registerParameter('test', 'abc');
+ $object = $this->container->query(
+ 'Test\AppFramework\Utility\ClassSimpleConstructor'
+ );
+ $this->assertTrue($object instanceof ClassSimpleConstructor);
+ $this->assertEquals('abc', $object->test);
+ }
+
+
+ public function testConstructorComplex() {
+ $this->container->registerParameter('test', 'abc');
+ $object = $this->container->query(
+ 'Test\AppFramework\Utility\ClassComplexConstructor'
+ );
+ $this->assertTrue($object instanceof ClassComplexConstructor);
+ $this->assertEquals('abc', $object->class->test);
+ $this->assertEquals('abc', $object->test);
+ }
+
+
+ public function testConstructorComplexInterface() {
+ $this->container->registerParameter('test', 'abc');
+ $this->container->registerService(
+ 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
+ return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
+ });
+ $object = $this->container->query(
+ 'Test\AppFramework\Utility\ClassInterfaceConstructor'
+ );
+ $this->assertTrue($object instanceof ClassInterfaceConstructor);
+ $this->assertEquals('abc', $object->class->test);
+ $this->assertEquals('abc', $object->test);
+ }
+
+
+ public function testOverrideService() {
+ $this->container->registerService(
+ 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
+ return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
+ });
+ $this->container->registerService(
+ 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
+ return $c->query('Test\AppFramework\Utility\ClassEmptyConstructor');
+ });
+ $object = $this->container->query(
+ 'Test\AppFramework\Utility\IInterfaceConstructor'
+ );
+ $this->assertTrue($object instanceof ClassEmptyConstructor);
+ }
+
+ public function testRegisterAliasParamter() {
+ $this->container->registerParameter('test', 'abc');
+ $this->container->registerAlias('test1', 'test');
+ $this->assertEquals('abc', $this->container->query('test1'));
+ }
+
+ public function testRegisterAliasService() {
+ $this->container->registerService('test', function() {
+ return new \StdClass;
+ }, true);
+ $this->container->registerAlias('test1', 'test');
+ $this->assertSame(
+ $this->container->query('test'), $this->container->query('test'));
+ $this->assertSame(
+ $this->container->query('test1'), $this->container->query('test1'));
+ $this->assertSame(
+ $this->container->query('test'), $this->container->query('test1'));
+ }
+
+ public function sanitizeNameProvider() {
+ return [
+ ['ABC\\Foo', 'ABC\\Foo'],
+ ['\\ABC\\Foo', '\\ABC\\Foo'],
+ ['\\ABC\\Foo', 'ABC\\Foo'],
+ ['ABC\\Foo', '\\ABC\\Foo'],
+ ];
+ }
+
+ /**
+ * @dataProvider sanitizeNameProvider
+ */
+ public function testSanitizeName($register, $query) {
+ $this->container->registerService($register, function() {
+ return 'abc';
+ });
+ $this->assertEquals('abc', $this->container->query($query));
+ }
+
+ /**
+ * @expectedException \OCP\AppFramework\QueryException
+ */
+ public function testConstructorComplexNoTestParameterFound() {
+ $object = $this->container->query(
+ 'Test\AppFramework\Utility\ClassComplexConstructor'
+ );
+ }
+
+ public function testRegisterFactory() {
+ $this->container->registerService('test', function() {
+ return new \StdClass();
+ }, false);
+ $this->assertNotSame(
+ $this->container->query('test'), $this->container->query('test'));
+ }
+
+ public function testRegisterAliasFactory() {
+ $this->container->registerService('test', function() {
+ return new \StdClass();
+ }, false);
+ $this->container->registerAlias('test1', 'test');
+ $this->assertNotSame(
+ $this->container->query('test'), $this->container->query('test'));
+ $this->assertNotSame(
+ $this->container->query('test1'), $this->container->query('test1'));
+ $this->assertNotSame(
+ $this->container->query('test'), $this->container->query('test1'));
+ }
+
+}