BinaryFinderTest.php 2.6 KB

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