|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Files_External\Tests; |
| 11 | + |
| 12 | +use OCA\Files_External\Lib\PortHelper; |
| 13 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 14 | +use Test\TestCase; |
| 15 | + |
| 16 | +class PortHelperTest extends TestCase { |
| 17 | + public static function portProvider(): array { |
| 18 | + return [ |
| 19 | + // valid ports are returned as integers |
| 20 | + 'integer port' => [2121, 2121], |
| 21 | + 'numeric string port' => ['2121', 2121], |
| 22 | + 'padded numeric string port' => ['0022', 22], |
| 23 | + 'lowest valid port' => [1, 1], |
| 24 | + 'highest valid port' => [65535, 65535], |
| 25 | + 'highest valid port as string' => ['65535', 65535], |
| 26 | + |
| 27 | + // unset or empty values fall back |
| 28 | + 'null port' => [null, 21], |
| 29 | + 'empty port' => ['', 21], |
| 30 | + 'whitespace port' => [' ', 21], |
| 31 | + 'array port' => [[2121], 21], |
| 32 | + |
| 33 | + // non integer values fall back |
| 34 | + 'non numeric port' => ['ftp', 21], |
| 35 | + 'float port' => [21.5, 21], |
| 36 | + 'integer float port' => [2121.0, 21], |
| 37 | + 'decimal string port' => ['21.5', 21], |
| 38 | + 'exponential string port' => ['1e3', 21], |
| 39 | + 'hexadecimal string port' => ['0x15', 21], |
| 40 | + 'signed string port' => ['+2121', 21], |
| 41 | + 'padded string port' => [' 2121', 21], |
| 42 | + 'boolean port' => [true, 21], |
| 43 | + |
| 44 | + // out of range values fall back |
| 45 | + 'zero port' => [0, 21], |
| 46 | + 'zero string port' => ['0', 21], |
| 47 | + 'negative port' => [-2121, 21], |
| 48 | + 'negative string port' => ['-2121', 21], |
| 49 | + 'too large port' => [65536, 21], |
| 50 | + 'too large string port' => ['65536', 21], |
| 51 | + 'way too large string port' => ['999999999999999999999999', 21], |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + #[DataProvider('portProvider')] |
| 56 | + public function testParsePort(mixed $port, int $expectedPort): void { |
| 57 | + $this->assertSame($expectedPort, PortHelper::parsePort($port, 21)); |
| 58 | + } |
| 59 | + |
| 60 | + public function testParsePortReturnsGivenFallback(): void { |
| 61 | + $this->assertSame(22, PortHelper::parsePort('', 22)); |
| 62 | + } |
| 63 | +} |
0 commit comments