1
0

FeaturePolicyManagerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Test\Security\CSP;
  25. use OC\Security\CSP\ContentSecurityPolicyManager;
  26. use OC\Security\FeaturePolicy\FeaturePolicyManager;
  27. use OCP\AppFramework\Http\FeaturePolicy;
  28. use OCP\EventDispatcher\IEventDispatcher;
  29. use OCP\Security\CSP\AddContentSecurityPolicyEvent;
  30. use OCP\Security\FeaturePolicy\AddFeaturePolicyEvent;
  31. use PHPUnit\Framework\MockObject\MockObject;
  32. use Symfony\Component\EventDispatcher\EventDispatcher;
  33. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  34. use Test\TestCase;
  35. class FeaturePolicyManagerTest extends TestCase {
  36. /** @var EventDispatcherInterface */
  37. private $dispatcher;
  38. /** @var FeaturePolicyManager */
  39. private $manager;
  40. public function setUp() {
  41. parent::setUp();
  42. $this->dispatcher = \OC::$server->query(IEventDispatcher::class);
  43. $this->manager = new FeaturePolicyManager($this->dispatcher);
  44. }
  45. public function testAddDefaultPolicy() {
  46. $this->manager->addDefaultPolicy(new FeaturePolicy());
  47. $this->addToAssertionCount(1);
  48. }
  49. public function testGetDefaultPolicyWithPoliciesViaEvent() {
  50. $this->dispatcher->addListener(AddFeaturePolicyEvent::class, function(AddFeaturePolicyEvent $e) {
  51. $policy = new FeaturePolicy();
  52. $policy->addAllowedMicrophoneDomain('mydomain.com');
  53. $policy->addAllowedPaymentDomain('mypaymentdomain.com');
  54. $e->addPolicy($policy);
  55. });
  56. $this->dispatcher->addListener(AddFeaturePolicyEvent::class, function(AddFeaturePolicyEvent $e) {
  57. $policy = new FeaturePolicy();
  58. $policy->addAllowedPaymentDomain('mydomainother.com');
  59. $policy->addAllowedGeoLocationDomain('mylocation.here');
  60. $e->addPolicy($policy);
  61. });
  62. $this->dispatcher->addListener(AddFeaturePolicyEvent::class, function(AddFeaturePolicyEvent $e) {
  63. $policy = new FeaturePolicy();
  64. $policy->addAllowedAutoplayDomain('youtube.com');
  65. $e->addPolicy($policy);
  66. });
  67. $expected = new \OC\Security\FeaturePolicy\FeaturePolicy();
  68. $expected->addAllowedMicrophoneDomain('mydomain.com');
  69. $expected->addAllowedPaymentDomain('mypaymentdomain.com');
  70. $expected->addAllowedPaymentDomain('mydomainother.com');
  71. $expected->addAllowedGeoLocationDomain('mylocation.here');
  72. $expected->addAllowedAutoplayDomain('youtube.com');
  73. $expectedStringPolicy = "autoplay 'self' youtube.com;camera 'none';fullscreen 'self';geolocation mylocation.here;microphone mydomain.com;payment mypaymentdomain.com mydomainother.com";
  74. $this->assertEquals($expected, $this->manager->getDefaultPolicy());
  75. $this->assertSame($expectedStringPolicy, $this->manager->getDefaultPolicy()->buildPolicy());
  76. }
  77. }