diff options
author | Joas Schilling <213943+nickvergessen@users.noreply.github.com> | 2023-11-27 10:40:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-27 10:40:50 +0100 |
commit | 3ccb301853ee849c62a3a08b5c7333a615bfa3db (patch) | |
tree | e61c745d794f95e9295af30c57700465e8e58d0f /tests | |
parent | 011390aacbeed174716a76f9944b5db6757d6d0c (diff) | |
parent | 4827fc3eda68042e1c72957c9395f96ae570761d (diff) | |
download | nextcloud-server-3ccb301853ee849c62a3a08b5c7333a615bfa3db.tar.gz nextcloud-server-3ccb301853ee849c62a3a08b5c7333a615bfa3db.zip |
Merge pull request #41578 from nextcloud/enh/noid/dispatcher-test-argument-range
Enable AppFramework dispatcher to enforce integer ranges
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/AppFramework/Http/DispatcherTest.php | 47 | ||||
-rw-r--r-- | tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php | 21 |
2 files changed, 68 insertions, 0 deletions
diff --git a/tests/lib/AppFramework/Http/DispatcherTest.php b/tests/lib/AppFramework/Http/DispatcherTest.php index bb7541c4d22..d9d9eb82a68 100644 --- a/tests/lib/AppFramework/Http/DispatcherTest.php +++ b/tests/lib/AppFramework/Http/DispatcherTest.php @@ -522,4 +522,51 @@ class DispatcherTest extends \Test\TestCase { $this->assertEquals('{"text":[3,true,4,1]}', $response[3]); } + + + public function rangeDataProvider(): array { + return [ + [PHP_INT_MIN, PHP_INT_MAX, 42, false], + [0, 12, -5, true], + [-12, 0, 5, true], + [7, 14, 5, true], + [7, 14, 10, false], + [-14, -7, -10, false], + ]; + } + + /** + * @dataProvider rangeDataProvider + */ + public function testEnsureParameterValueSatisfiesRange(int $min, int $max, int $input, bool $throw): void { + $this->reflector = $this->createMock(ControllerMethodReflector::class); + $this->reflector->expects($this->any()) + ->method('getRange') + ->willReturn([ + 'min' => $min, + 'max' => $max, + ]); + + $this->dispatcher = new Dispatcher( + $this->http, + $this->middlewareDispatcher, + $this->reflector, + $this->request, + $this->config, + \OC::$server->getDatabaseConnection(), + $this->logger, + $this->eventLogger, + $this->container, + ); + + if ($throw) { + $this->expectException(\OutOfRangeException::class); + } + + $this->invokePrivate($this->dispatcher, 'ensureParameterValueSatisfiesRange', ['myArgument', $input]); + if (!$throw) { + // do not mark this test risky + $this->assertTrue(true); + } + } } diff --git a/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php b/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php index 5452fb853b9..2ba2f34425b 100644 --- a/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php +++ b/tests/lib/AppFramework/Utility/ControllerMethodReflectorTest.php @@ -54,6 +54,14 @@ class MiddleController extends BaseController { public function test3() { } + + /** + * @psalm-param int<-4, 42> $rangedOne + * @psalm-param int<min, max> $rangedTwo + * @return void + */ + public function test4(int $rangedOne, int $rangedTwo) { + } } class EndController extends MiddleController { @@ -234,4 +242,17 @@ class ControllerMethodReflectorTest extends \Test\TestCase { $this->assertFalse($reader->hasAnnotation('Annotation')); } + + public function testRangeDetection() { + $reader = new ControllerMethodReflector(); + $reader->reflect('Test\AppFramework\Utility\EndController', 'test4'); + + $rangeInfo1 = $reader->getRange('rangedOne'); + $this->assertSame(-4, $rangeInfo1['min']); + $this->assertSame(42, $rangeInfo1['max']); + + $rangeInfo2 = $reader->getRange('rangedTwo'); + $this->assertSame(PHP_INT_MIN, $rangeInfo2['min']); + $this->assertSame(PHP_INT_MAX, $rangeInfo2['max']); + } } |