trusteddomainhelper.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Lukas Reschke <lukas@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. use \OC\Security\TrustedDomainHelper;
  9. use OCP\IConfig;
  10. /**
  11. * Class TrustedDomainHelperTest
  12. */
  13. class TrustedDomainHelperTest extends \Test\TestCase {
  14. /** @var IConfig */
  15. protected $config;
  16. protected function setUp() {
  17. parent::setUp();
  18. $this->config = $this->getMockBuilder('\OCP\IConfig')->getMock();
  19. }
  20. /**
  21. * @dataProvider trustedDomainDataProvider
  22. * @param string $trustedDomains
  23. * @param string $testDomain
  24. * @param bool $result
  25. */
  26. public function testIsTrustedDomain($trustedDomains, $testDomain, $result) {
  27. $this->config->expects($this->once())
  28. ->method('getSystemValue')
  29. ->with('trusted_domains')
  30. ->will($this->returnValue($trustedDomains));
  31. $trustedDomainHelper = new TrustedDomainHelper($this->config);
  32. $this->assertEquals($result, $trustedDomainHelper->isTrustedDomain($testDomain));
  33. }
  34. /**
  35. * @return array
  36. */
  37. public function trustedDomainDataProvider() {
  38. $trustedHostTestList = ['host.one.test', 'host.two.test', '[1fff:0:a88:85a3::ac1f]'];
  39. return [
  40. // empty defaults to false with 8.1
  41. [null, 'host.one.test:8080', false],
  42. ['', 'host.one.test:8080', false],
  43. [[], 'host.one.test:8080', false],
  44. // trust list when defined
  45. [$trustedHostTestList, 'host.two.test:8080', true],
  46. [$trustedHostTestList, 'host.two.test:9999', true],
  47. [$trustedHostTestList, 'host.three.test:8080', false],
  48. [$trustedHostTestList, 'host.two.test:8080:aa:222', false],
  49. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]', true],
  50. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801', true],
  51. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801:34', false],
  52. // trust localhost regardless of trust list
  53. [$trustedHostTestList, 'localhost', true],
  54. [$trustedHostTestList, 'localhost:8080', true],
  55. [$trustedHostTestList, '127.0.0.1', true],
  56. [$trustedHostTestList, '127.0.0.1:8080', true],
  57. // do not trust invalid localhosts
  58. [$trustedHostTestList, 'localhost:1:2', false],
  59. [$trustedHostTestList, 'localhost: evil.host', false],
  60. ];
  61. }
  62. }