| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "platform/feature_policy/FeaturePolicy.h" | |
| 6 | |
| 7 #include "bindings/core/v8/ConditionalFeaturesForCore.h" | |
| 8 #include "core/frame/LocalFrame.h" | |
| 9 #include "core/testing/DummyPageHolder.h" | |
| 10 #include "platform/RuntimeEnabledFeatures.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 // Origin strings used for tests | |
| 14 #define ORIGIN_A "https://example.com/" | |
| 15 #define ORIGIN_B "https://example.net/" | |
| 16 | |
| 17 namespace blink { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // This is an example of a feature which should be enabled by default in all | |
| 22 // frames. | |
| 23 const FeaturePolicy::Feature kDefaultOnFeature{ | |
| 24 "default-on", FeaturePolicy::FeatureDefault::EnableForAll}; | |
| 25 | |
| 26 // This is an example of a feature which should be enabled in top-level frames, | |
| 27 // and same-origin child-frames, but must be delegated to all cross-origin | |
| 28 // frames explicitly. | |
| 29 const FeaturePolicy::Feature kDefaultSelfFeature{ | |
| 30 "default-self", FeaturePolicy::FeatureDefault::EnableForSelf}; | |
| 31 | |
| 32 // This is an example of a feature which should be disabled by default, both in | |
| 33 // top-level and nested frames. | |
| 34 const FeaturePolicy::Feature kDefaultOffFeature{ | |
| 35 "default-off", FeaturePolicy::FeatureDefault::DisableForAll}; | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 class FeaturePolicyInFrameTest : public ::testing::Test { | |
| 40 protected: | |
| 41 FeaturePolicyInFrameTest() | |
| 42 : m_frameworkWasEnabled(RuntimeEnabledFeatures::featurePolicyEnabled()), | |
| 43 m_featureList( | |
| 44 {&kDefaultOnFeature, &kDefaultSelfFeature, &kDefaultOffFeature}) { | |
| 45 RuntimeEnabledFeatures::setFeaturePolicyEnabled(true); | |
| 46 m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600)); | |
| 47 m_dummyPageHolder->document().setSecurityOrigin(m_originA); | |
| 48 } | |
| 49 | |
| 50 ~FeaturePolicyInFrameTest() { | |
| 51 RuntimeEnabledFeatures::setFeaturePolicyEnabled(m_frameworkWasEnabled); | |
| 52 } | |
| 53 | |
| 54 std::unique_ptr<FeaturePolicy> createFromParentPolicy( | |
| 55 const FeaturePolicy* parent, | |
| 56 RefPtr<SecurityOrigin> origin) { | |
| 57 return FeaturePolicy::createFromParentPolicy(parent, nullptr, origin, | |
| 58 m_featureList); | |
| 59 } | |
| 60 | |
| 61 Document& document() { return m_dummyPageHolder->document(); } | |
| 62 | |
| 63 LocalFrame* frame() { return m_dummyPageHolder->document().frame(); } | |
| 64 | |
| 65 RefPtr<SecurityOrigin> m_originA = SecurityOrigin::createFromString(ORIGIN_A); | |
| 66 RefPtr<SecurityOrigin> m_originB = SecurityOrigin::createFromString(ORIGIN_B); | |
| 67 | |
| 68 private: | |
| 69 const bool m_frameworkWasEnabled; | |
| 70 | |
| 71 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; | |
| 72 | |
| 73 // Contains the list of controlled features, so that we are guaranteed to | |
| 74 // have at least one of each kind of default behaviour represented. | |
| 75 FeaturePolicy::FeatureList m_featureList; | |
| 76 }; | |
| 77 | |
| 78 TEST_F(FeaturePolicyInFrameTest, TestFeatureDefaultsInTopLevelFrame) { | |
| 79 std::unique_ptr<FeaturePolicy> policy1 = | |
| 80 createFromParentPolicy(nullptr, m_originA); | |
| 81 document().setFeaturePolicyForTesting(std::move(policy1)); | |
| 82 EXPECT_TRUE(isFeatureEnabledInFrame(kDefaultOnFeature, frame())); | |
| 83 EXPECT_TRUE(isFeatureEnabledInFrame(kDefaultSelfFeature, frame())); | |
| 84 EXPECT_FALSE(isFeatureEnabledInFrame(kDefaultOffFeature, frame())); | |
| 85 } | |
| 86 | |
| 87 TEST_F(FeaturePolicyInFrameTest, TestFeatureDefaultsInCrossOriginFrame) { | |
| 88 std::unique_ptr<FeaturePolicy> policy1 = | |
| 89 createFromParentPolicy(nullptr, m_originB); | |
| 90 std::unique_ptr<FeaturePolicy> policy2 = | |
| 91 createFromParentPolicy(policy1.get(), m_originA); | |
| 92 document().setFeaturePolicyForTesting(std::move(policy2)); | |
| 93 EXPECT_TRUE(isFeatureEnabledInFrame(kDefaultOnFeature, frame())); | |
| 94 EXPECT_FALSE(isFeatureEnabledInFrame(kDefaultSelfFeature, frame())); | |
| 95 EXPECT_FALSE(isFeatureEnabledInFrame(kDefaultOffFeature, frame())); | |
| 96 } | |
| 97 | |
| 98 TEST_F(FeaturePolicyInFrameTest, TestFeatureOverriddenInFrame) { | |
| 99 Vector<String> messages; | |
| 100 std::unique_ptr<FeaturePolicy> policy1 = | |
| 101 createFromParentPolicy(nullptr, m_originA); | |
| 102 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy( | |
| 103 "{\"default-off\": [\"self\"], \"default-on\": []}", m_originA.get(), | |
| 104 &messages)); | |
| 105 EXPECT_EQ(0UL, messages.size()); | |
| 106 document().setFeaturePolicyForTesting(std::move(policy1)); | |
| 107 EXPECT_TRUE(isFeatureEnabledInFrame(kDefaultOffFeature, frame())); | |
| 108 EXPECT_FALSE(isFeatureEnabledInFrame(kDefaultOnFeature, frame())); | |
| 109 } | |
| 110 | |
| 111 // Ensure that features are enabled / disabled correctly when the FP runtime | |
| 112 // flag is turned off. | |
| 113 TEST_F(FeaturePolicyInFrameTest, TestFeatureDefaultsWhenFPDisabled) { | |
| 114 RuntimeEnabledFeatures::setFeaturePolicyEnabled(false); | |
| 115 EXPECT_TRUE(isFeatureEnabledInFrame(kDefaultOnFeature, frame())); | |
| 116 EXPECT_TRUE(isFeatureEnabledInFrame(kDefaultSelfFeature, frame())); | |
| 117 EXPECT_FALSE(isFeatureEnabledInFrame(kDefaultOffFeature, frame())); | |
| 118 } | |
| 119 | |
| 120 // Ensure that feature policy has no effect when the FP runtime flag is turned | |
| 121 // off. | |
| 122 TEST_F(FeaturePolicyInFrameTest, TestPolicyInactiveWhenFPDisabled) { | |
| 123 RuntimeEnabledFeatures::setFeaturePolicyEnabled(false); | |
| 124 Vector<String> messages; | |
| 125 std::unique_ptr<FeaturePolicy> policy1 = | |
| 126 createFromParentPolicy(nullptr, m_originA); | |
| 127 policy1->setHeaderPolicy(FeaturePolicy::parseFeaturePolicy( | |
| 128 "{\"default-on\": []}", m_originA.get(), &messages)); | |
| 129 EXPECT_EQ(0UL, messages.size()); | |
| 130 document().setFeaturePolicyForTesting(std::move(policy1)); | |
| 131 EXPECT_TRUE(isFeatureEnabledInFrame(kDefaultOnFeature, frame())); | |
| 132 } | |
| 133 | |
| 134 } // namespace blink | |
| OLD | NEW |