diff options
author | Reno Reckling <e-github@wthack.de> | 2024-03-06 14:24:00 +0100 |
---|---|---|
committer | Daniel Kesselberg <mail@danielkesselberg.de> | 2024-09-20 17:00:06 +0200 |
commit | ef7e857881b72b607f36a41c9f178c12db068b7a (patch) | |
tree | f697f622ce17ecaee56e69c70072c8f23f720566 /tests | |
parent | bc5222726b7f6f04308231ff55b8e89ade582d37 (diff) | |
download | nextcloud-server-ef7e857881b72b607f36a41c9f178c12db068b7a.tar.gz nextcloud-server-ef7e857881b72b607f36a41c9f178c12db068b7a.zip |
feat: make search path for BinaryFinder customizable.
This feature is important for nextcloud running on
distributions like NixOS, where all the standard
search paths do not exist.
Also added tests.
This fixes issue #43922
Co-authored-by: Daniel <mail@danielkesselberg.de>
Signed-off-by: Reno Reckling <e-github@wthack.de>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/BinaryFinderTest.php | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/tests/lib/BinaryFinderTest.php b/tests/lib/BinaryFinderTest.php new file mode 100644 index 00000000000..42306e49eac --- /dev/null +++ b/tests/lib/BinaryFinderTest.php @@ -0,0 +1,84 @@ +<?php + +declare(strict_types = 1); +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test; + +use OC\BinaryFinder; +use OC\Memcache\ArrayCache; +use OCP\ICache; +use OCP\ICacheFactory; +use OCP\IConfig; + +class BinaryFinderTest extends TestCase { + private ICache $cache; + private ICacheFactory $cacheFactory; + private $oldEnv; + + protected function setUp(): void { + $this->oldEnv = getenv('PATH'); + // BinaryFinder always includes the "PATH" environment variable into the search path, + // which we want to avoid in this test because they are not usually found in webserver + // deployments. + putenv('PATH=""'); + $this->cacheFactory = $this->createMock(ICacheFactory::class); + $this->cache = new ArrayCache(); + $this->cacheFactory->method('createLocal')->with('findBinaryPath')->willReturn($this->cache); + } + + protected function tearDown(): void { + putenv('PATH=' . $this->oldEnv); + } + + public function testDefaultFindsCat() { + $config = $this->createMock(IConfig::class); + $config + ->method('getSystemValue') + ->with('binary_search_paths', $this->anything()) + ->will($this->returnCallback(function ($key, $default) { + return $default; + })); + $finder = new BinaryFinder($this->cacheFactory, $config); + $this->assertEquals($finder->findBinaryPath('cat'), '/usr/bin/cat'); + $this->assertEquals($this->cache->get('cat'), '/usr/bin/cat'); + } + + public function testDefaultDoesNotFindCata() { + $config = $this->createMock(IConfig::class); + $config + ->method('getSystemValue') + ->with('binary_search_paths', $this->anything()) + ->will($this->returnCallback(function ($key, $default) { + return $default; + })); + $finder = new BinaryFinder($this->cacheFactory, $config); + $this->assertFalse($finder->findBinaryPath('cata')); + $this->assertFalse($this->cache->get('cata')); + } + + public function testCustomPathFindsCat() { + $config = $this->createMock(IConfig::class); + $config + ->method('getSystemValue') + ->with('binary_search_paths', $this->anything()) + ->willReturn(['/usr/bin']); + $finder = new BinaryFinder($this->cacheFactory, $config); + $this->assertEquals($finder->findBinaryPath('cat'), '/usr/bin/cat'); + $this->assertEquals($this->cache->get('cat'), '/usr/bin/cat'); + } + + public function testWrongCustomPathDoesNotFindCat() { + $config = $this->createMock(IConfig::class); + $config + ->method('getSystemValue') + ->with('binary_search_paths') + ->willReturn(['/wrong']); + $finder = new BinaryFinder($this->cacheFactory, $config); + $this->assertFalse($finder->findBinaryPath('cat')); + $this->assertFalse($this->cache->get('cat')); + } +} |