From 296c00ca9c01aebeb2c727f1a981ffea39c5994f Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Thu, 20 Jul 2023 18:57:07 +0200 Subject: [PATCH] feat: rename DefaultOs to Linux Signed-off-by: Daniel Kesselberg --- README.md | 22 +++++- lib/OperatingSystems/Dummy.php | 68 +++++++++++++++++ .../{DefaultOs.php => Linux.php} | 2 +- lib/Os.php | 35 ++++----- tests/data/{df_tp => linux_df_tp} | 0 tests/data/{meminfo => linux_meminfo} | 0 tests/data/{uptime => linux_uptime} | 0 tests/lib/DummyTest.php | 73 +++++++++++++++++++ .../lib/{DefaultOsTest.php => LinuxTest.php} | 19 ++--- 9 files changed, 188 insertions(+), 31 deletions(-) create mode 100644 lib/OperatingSystems/Dummy.php rename lib/OperatingSystems/{DefaultOs.php => Linux.php} (99%) rename tests/data/{df_tp => linux_df_tp} (100%) rename tests/data/{meminfo => linux_meminfo} (100%) rename tests/data/{uptime => linux_uptime} (100%) create mode 100644 tests/lib/DummyTest.php rename tests/lib/{DefaultOsTest.php => LinuxTest.php} (94%) diff --git a/README.md b/README.md index 01228195..9bd84b01 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,26 @@ php occ serverinfo:update-storage-statistics -v --output=json_pretty } ``` -Show phpinfo +##### Restricted mode (>= Nextcloud 28) + +To obtain information about your server, the serverinfo app reads files outside the application directory (e.g. /proc on Linux) or executes shell commands (e.g. df on Linux). + +If you don't want that (for example, to avoid open_basedir warnings) enable the restricted mode. + +Enable: + +``php occ config:app:set --value=yes serverinfo restricted_mode`` + +Disable: + +``php occ config:app:delete serverinfo restricted_mode`` + +##### Show phpinfo (>= Nextcloud 28) + +Enable: ``php occ config:app:set --value=yes serverinfo phpinfo`` + +Disable: + +``php occ config:app:delete serverinfo phpinfo`` diff --git a/lib/OperatingSystems/Dummy.php b/lib/OperatingSystems/Dummy.php new file mode 100644 index 00000000..f52eacdd --- /dev/null +++ b/lib/OperatingSystems/Dummy.php @@ -0,0 +1,68 @@ + + * + * @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 + * + */ + +namespace OCA\ServerInfo\OperatingSystems; + +use OCA\ServerInfo\Resources\Memory; + +class Dummy implements IOperatingSystem { + public function supported(): bool { + return false; + } + + public function getMemory(): Memory { + return new Memory(); + } + + public function getCpuName(): string { + return 'Unknown Processor'; + } + + public function getTime(): string { + return ''; + } + + public function getUptime(): int { + return -1; + } + + public function getNetworkInfo(): array { + return [ + 'hostname' => \gethostname(), + 'dns' => '', + 'gateway' => '', + ]; + } + + public function getNetworkInterfaces(): array { + return []; + } + + public function getDiskInfo(): array { + return []; + } + + public function getThermalZones(): array { + return []; + } +} diff --git a/lib/OperatingSystems/DefaultOs.php b/lib/OperatingSystems/Linux.php similarity index 99% rename from lib/OperatingSystems/DefaultOs.php rename to lib/OperatingSystems/Linux.php index 23f06147..dc028848 100644 --- a/lib/OperatingSystems/DefaultOs.php +++ b/lib/OperatingSystems/Linux.php @@ -28,7 +28,7 @@ use OCA\ServerInfo\Resources\NetInterface; use RuntimeException; -class DefaultOs implements IOperatingSystem { +class Linux implements IOperatingSystem { private const AF_INET = 2; private const AF_INET6 = 10; diff --git a/lib/Os.php b/lib/Os.php index b3448f77..3058ac63 100644 --- a/lib/Os.php +++ b/lib/Os.php @@ -23,28 +23,23 @@ namespace OCA\ServerInfo; -use OCA\ServerInfo\OperatingSystems\DefaultOs; +use OCA\ServerInfo\OperatingSystems\Dummy; use OCA\ServerInfo\OperatingSystems\FreeBSD; use OCA\ServerInfo\OperatingSystems\IOperatingSystem; +use OCA\ServerInfo\OperatingSystems\Linux; use OCA\ServerInfo\Resources\Memory; +use OCP\IConfig; class Os implements IOperatingSystem { - protected IOperatingSystem $backend; + private IOperatingSystem $backend; - /** - * Os constructor. - */ - public function __construct() { - if (PHP_OS === 'FreeBSD') { - $this->backend = new FreeBSD(); - } else { - $this->backend = new DefaultOs(); - } + public function __construct(IConfig $config) { + $restrictedMode = $config->getAppValue('serverinfo', 'restricted_mode', 'no') === 'yes'; + $this->backend = $this->getBackend($restrictedMode ? 'Dummy' : PHP_OS); } public function supported(): bool { - $data = $this->backend->supported(); - return $data; + return $this->backend->supported(); } public function getHostname(): string { @@ -101,8 +96,7 @@ public function getDiskData(): array { } public function getNetworkInfo(): array { - $data = $this->backend->getNetworkInfo(); - return $data; + return $this->backend->getNetworkInfo(); } public function getNetworkInterfaces(): array { @@ -110,7 +104,14 @@ public function getNetworkInterfaces(): array { } public function getThermalZones(): array { - $data = $this->backend->getThermalZones(); - return $data; + return $this->backend->getThermalZones(); + } + + private function getBackend(string $os): IOperatingSystem { + return match ($os) { + 'Linux' => new Linux(), + 'FreeBSD' => new FreeBSD(), + default => new Dummy(), + }; } } diff --git a/tests/data/df_tp b/tests/data/linux_df_tp similarity index 100% rename from tests/data/df_tp rename to tests/data/linux_df_tp diff --git a/tests/data/meminfo b/tests/data/linux_meminfo similarity index 100% rename from tests/data/meminfo rename to tests/data/linux_meminfo diff --git a/tests/data/uptime b/tests/data/linux_uptime similarity index 100% rename from tests/data/uptime rename to tests/data/linux_uptime diff --git a/tests/lib/DummyTest.php b/tests/lib/DummyTest.php new file mode 100644 index 00000000..99d34300 --- /dev/null +++ b/tests/lib/DummyTest.php @@ -0,0 +1,73 @@ + + * + * @author Daniel Kesselberg + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see . + * + */ + +namespace OCA\ServerInfo\Tests; + +use OCA\ServerInfo\OperatingSystems\Dummy; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class DummyTest extends TestCase { + /** @var Dummy|MockObject */ + protected $os; + + protected function setUp(): void { + parent::setUp(); // TODO: Change the autogenerated stub + + $this->os = new Dummy(); + } + + public function testGetMemory(): void { + $memory = $this->os->getMemory(); + + $this->assertEquals(-1, $memory->getMemTotal()); + $this->assertEquals(-1, $memory->getMemFree()); + $this->assertEquals(-1, $memory->getMemAvailable()); + $this->assertEquals(-1, $memory->getSwapTotal()); + $this->assertEquals(-1, $memory->getSwapFree()); + } + + public function testGetCpuName(): void { + $this->assertEquals('Unknown Processor', $this->os->getCpuName()); + } + + public function testGetUptime(): void { + $this->assertEquals(-1, $this->os->getUptime()); + } + + + public function testGetDiskInfo(): void { + $this->assertEquals([], $this->os->getDiskInfo()); + } + + public function testSupported(): void { + $this->assertFalse($this->os->supported()); + } + + public function testGetNetworkInterfaces(): void { + $this->assertEquals([], $this->os->getNetworkInterfaces()); + } +} diff --git a/tests/lib/DefaultOsTest.php b/tests/lib/LinuxTest.php similarity index 94% rename from tests/lib/DefaultOsTest.php rename to tests/lib/LinuxTest.php index 51b81e3e..7d979c0a 100644 --- a/tests/lib/DefaultOsTest.php +++ b/tests/lib/LinuxTest.php @@ -26,7 +26,7 @@ namespace OCA\ServerInfo\Tests; -use OCA\ServerInfo\OperatingSystems\DefaultOs; +use OCA\ServerInfo\OperatingSystems\Linux; use OCA\ServerInfo\Resources\Disk; use OCA\ServerInfo\Resources\Memory; use OCA\ServerInfo\Resources\NetInterface; @@ -34,19 +34,14 @@ use RuntimeException; use Test\TestCase; -/** - * Class DefaultOsTest - * - * @package OCA\ServerInfo\Tests - */ -class DefaultOsTest extends TestCase { - /** @var DefaultOs&MockObject */ +class LinuxTest extends TestCase { + /** @var Linux&MockObject */ protected $os; protected function setUp(): void { parent::setUp(); // TODO: Change the autogenerated stub - $this->os = $this->getMockBuilder(DefaultOs::class) + $this->os = $this->getMockBuilder(Linux::class) ->disableOriginalConstructor() ->disableOriginalClone() ->disableArgumentCloning() @@ -58,7 +53,7 @@ protected function setUp(): void { public function testGetMemory(): void { $this->os->method('readContent') ->with('/proc/meminfo') - ->willReturn(file_get_contents(__DIR__ . '/../data/meminfo')); + ->willReturn(file_get_contents(__DIR__ . '/../data/linux_meminfo')); $memory = $this->os->getMemory(); @@ -144,7 +139,7 @@ public function testGetCpuNameInvalidData(): void { public function testGetUptime(): void { $this->os->method('readContent') ->with('/proc/uptime') - ->willReturn(file_get_contents(__DIR__ . '/../data/uptime')); + ->willReturn(file_get_contents(__DIR__ . '/../data/linux_uptime')); $this->assertEquals(13278, $this->os->getUptime()); } @@ -160,7 +155,7 @@ public function testGetUptimeNoData(): void { public function testGetDiskInfo(): void { $this->os->method('executeCommand') ->with('df -TPk') - ->willReturn(file_get_contents(__DIR__ . '/../data/df_tp')); + ->willReturn(file_get_contents(__DIR__ . '/../data/linux_df_tp')); $disk1 = new Disk(); $disk1->setDevice('/dev/mapper/homestead--vg-root');