|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Test; |
| 10 | + |
| 11 | +use OC\BinaryFinder; |
| 12 | +use OC\Memcache\ArrayCache; |
| 13 | +use OCP\ICache; |
| 14 | +use OCP\ICacheFactory; |
| 15 | +use OCP\IConfig; |
| 16 | + |
| 17 | +class BinaryFinderTest extends TestCase { |
| 18 | + private ICache $cache; |
| 19 | + private ICacheFactory $cacheFactory; |
| 20 | + private $oldEnv; |
| 21 | + |
| 22 | + protected function setUp(): void { |
| 23 | + $this->oldEnv = getenv('PATH'); |
| 24 | + // BinaryFinder always includes the "PATH" environment variable into the search path, |
| 25 | + // which we want to avoid in this test because they are not usually found in webserver |
| 26 | + // deployments. |
| 27 | + putenv('PATH=""'); |
| 28 | + $this->cacheFactory = $this->createMock(ICacheFactory::class); |
| 29 | + $this->cache = new ArrayCache(); |
| 30 | + $this->cacheFactory->method('createLocal')->with('findBinaryPath')->willReturn($this->cache); |
| 31 | + } |
| 32 | + |
| 33 | + protected function tearDown(): void { |
| 34 | + putenv('PATH=' . $this->oldEnv); |
| 35 | + } |
| 36 | + |
| 37 | + public function testDefaultFindsCat() { |
| 38 | + $config = $this->createMock(IConfig::class); |
| 39 | + $config |
| 40 | + ->method('getSystemValue') |
| 41 | + ->with('binary_search_paths', $this->anything()) |
| 42 | + ->will($this->returnCallback(function ($key, $default) { |
| 43 | + return $default; |
| 44 | + })); |
| 45 | + $finder = new BinaryFinder($this->cacheFactory, $config); |
| 46 | + $this->assertEquals($finder->findBinaryPath('cat'), '/usr/bin/cat'); |
| 47 | + $this->assertEquals($this->cache->get('cat'), '/usr/bin/cat'); |
| 48 | + } |
| 49 | + |
| 50 | + public function testDefaultDoesNotFindCata() { |
| 51 | + $config = $this->createMock(IConfig::class); |
| 52 | + $config |
| 53 | + ->method('getSystemValue') |
| 54 | + ->with('binary_search_paths', $this->anything()) |
| 55 | + ->will($this->returnCallback(function ($key, $default) { |
| 56 | + return $default; |
| 57 | + })); |
| 58 | + $finder = new BinaryFinder($this->cacheFactory, $config); |
| 59 | + $this->assertFalse($finder->findBinaryPath('cata')); |
| 60 | + $this->assertFalse($this->cache->get('cata')); |
| 61 | + } |
| 62 | + |
| 63 | + public function testCustomPathFindsCat() { |
| 64 | + $config = $this->createMock(IConfig::class); |
| 65 | + $config |
| 66 | + ->method('getSystemValue') |
| 67 | + ->with('binary_search_paths', $this->anything()) |
| 68 | + ->willReturn(['/usr/bin']); |
| 69 | + $finder = new BinaryFinder($this->cacheFactory, $config); |
| 70 | + $this->assertEquals($finder->findBinaryPath('cat'), '/usr/bin/cat'); |
| 71 | + $this->assertEquals($this->cache->get('cat'), '/usr/bin/cat'); |
| 72 | + } |
| 73 | + |
| 74 | + public function testWrongCustomPathDoesNotFindCat() { |
| 75 | + $config = $this->createMock(IConfig::class); |
| 76 | + $config |
| 77 | + ->method('getSystemValue') |
| 78 | + ->with('binary_search_paths') |
| 79 | + ->willReturn(['/wrong']); |
| 80 | + $finder = new BinaryFinder($this->cacheFactory, $config); |
| 81 | + $this->assertFalse($finder->findBinaryPath('cat')); |
| 82 | + $this->assertFalse($this->cache->get('cat')); |
| 83 | + } |
| 84 | +} |
0 commit comments