trusteddomainhelper.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 = [
  39. 'host.one.test',
  40. 'host.two.test',
  41. '[1fff:0:a88:85a3::ac1f]',
  42. 'host.three.test:443',
  43. ];
  44. return [
  45. // empty defaults to false with 8.1
  46. [null, 'host.one.test:8080', false],
  47. ['', 'host.one.test:8080', false],
  48. [[], 'host.one.test:8080', false],
  49. // trust list when defined
  50. [$trustedHostTestList, 'host.two.test:8080', true],
  51. [$trustedHostTestList, 'host.two.test:9999', true],
  52. [$trustedHostTestList, 'host.three.test:8080', false],
  53. [$trustedHostTestList, 'host.two.test:8080:aa:222', false],
  54. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]', true],
  55. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801', true],
  56. [$trustedHostTestList, '[1fff:0:a88:85a3::ac1f]:801:34', false],
  57. [$trustedHostTestList, 'host.three.test:443', true],
  58. [$trustedHostTestList, 'host.three.test:80', false],
  59. [$trustedHostTestList, 'host.three.test', false],
  60. // trust localhost regardless of trust list
  61. [$trustedHostTestList, 'localhost', true],
  62. [$trustedHostTestList, 'localhost:8080', true],
  63. [$trustedHostTestList, '127.0.0.1', true],
  64. [$trustedHostTestList, '127.0.0.1:8080', true],
  65. // do not trust invalid localhosts
  66. [$trustedHostTestList, 'localhost:1:2', false],
  67. [$trustedHostTestList, 'localhost: evil.host', false],
  68. // do not trust casting
  69. [[1], '1', false],
  70. ];
  71. }
  72. }