Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(856)

Unified Diff: third_party/WebKit/Source/platform/feature_policy/FeaturePolicyTest.cpp

Issue 2254533002: [FeaturePolicy] Initial implementation of Feature Policy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fp-flag
Patch Set: Conform to JFV spec for header format Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/platform/feature_policy/FeaturePolicyTest.cpp
diff --git a/third_party/WebKit/Source/platform/feature_policy/FeaturePolicyTest.cpp b/third_party/WebKit/Source/platform/feature_policy/FeaturePolicyTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..a537f98b27acb016bb6626aa0bb3cfb4f6a7e3ca
--- /dev/null
+++ b/third_party/WebKit/Source/platform/feature_policy/FeaturePolicyTest.cpp
@@ -0,0 +1,519 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "platform/feature_policy/FeaturePolicy.h"
+
+#include "platform/RuntimeEnabledFeatures.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+// Origin strings used for tests
+#define ORIGIN_A "https://example.com/"
+#define ORIGIN_B "https://example.net/"
+#define ORIGIN_C "https://example.org/"
raymes 2016/10/19 23:47:37 nit: can these just be const char*? I haven't seen
iclelland 2016/10/21 13:38:25 I'm using the macros so that we can do easy string
+
+namespace blink {
+
+namespace {
+
+const char* kInvalidPolicies[] = {"Not A JSON literal", "\"Not a JSON object\"",
+ "[\"Also\", \"Not a JSON object\"]", "1.0"};
+
+// This is an example of a feature which should be disabled by default, both in
+// top-level and nested frames.
+const FeaturePolicyFeature kOffByDeaultFeature{"offbydefault",
+ kDisableFeatureForAllOrigins};
+
+} // namespace
+
+class FeaturePolicyTest : public ::testing::Test {
+ protected:
+ FeaturePolicyTest()
+ : m_frameworkWasEnabled(RuntimeEnabledFeatures::featurePolicyEnabled()) {
+ RuntimeEnabledFeatures::setFeaturePolicyEnabled(true);
+ // Set the list of controlled features, so that we are guaranteed to
+ // have at least one of each kind of default behaviour represented.
+ Vector<const FeaturePolicyFeature*> featureRegistry =
+ FeaturePolicy::getFeatureRegistry();
dcheng 2016/10/20 17:41:44 Doesn't this copy the feature registry? So I don't
iclelland 2016/10/21 13:38:25 I had fixed this this afternoon, but you're right
+ featureRegistry.clear();
+ featureRegistry.append(&kDocumentWrite);
+ featureRegistry.append(&kVibrateFeature);
+ featureRegistry.append(&kOffByDeaultFeature);
raymes 2016/10/19 23:47:37 nit (optional): should we just make all of these i
iclelland 2016/10/21 13:38:26 Done. I thought that using the real features would
+ }
+
+ ~FeaturePolicyTest() {
+ RuntimeEnabledFeatures::setFeaturePolicyEnabled(m_frameworkWasEnabled);
+ }
+
+ RefPtr<SecurityOrigin> originA = SecurityOrigin::createFromString(ORIGIN_A);
raymes 2016/10/19 23:47:37 nit: should these be prefixed with m_ (I don't kno
iclelland 2016/10/21 13:38:26 Done.
+ RefPtr<SecurityOrigin> originB = SecurityOrigin::createFromString(ORIGIN_B);
+ RefPtr<SecurityOrigin> originC = SecurityOrigin::createFromString(ORIGIN_C);
+
+ private:
+ const bool m_frameworkWasEnabled;
+};
+
+TEST_F(FeaturePolicyTest, ParseInvalidPolicy) {
+ for (const char* invalidPolicy : kInvalidPolicies) {
+ EXPECT_TRUE(invalidPolicy);
raymes 2016/10/19 23:47:37 Hmm, what is this testing?
iclelland 2016/10/21 13:38:26 Ahh, I had implemented that test yesterday, but di
+ }
+}
+
+TEST_F(FeaturePolicyTest, TestInitialPolicy) {
+ // +-------------+
+ // |(1)Origin A |
+ // |No Policy |
+ // +-------------+
+ // Default-on and top-level-only features should be enabled in top-level
+ // frame. Default-off features should be disabled.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy1->isFeatureEnabled(&kOffByDeaultFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestInitialSameOriginChildPolicy) {
+ // +-----------------+
+ // |(1)Origin A |
+ // |No Policy |
+ // | +-------------+ |
+ // | |(2)Origin A | |
+ // | |No Policy | |
+ // | +-------------+ |
+ // +-----------------+
+ // Default-on features should be enabled in child frame. Default-off and
+ // top-level-only features should be disabled.
raymes 2016/10/19 23:47:37 nit: I think this comment is slightly out of date?
iclelland 2016/10/21 13:38:25 Thanks, fixed.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originA);
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kOffByDeaultFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestInitialCrossOriginChildPolicy) {
+ // +-----------------+
+ // |(1)Origin A |
+ // |No Policy |
+ // | +-------------+ |
+ // | |(2)Origin B | |
+ // | |No Policy | |
+ // | +-------------+ |
+ // +-----------------+
+ // Default-on features should be enabled in child frame. Default-off and
+ // top-level-only features should be disabled.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kOffByDeaultFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestCrossOriginChildCannotEnableFeature) {
+ // +-------------------------------------+
+ // |(1) Origin A |
+ // |No Policy |
+ // | +---------------------------------+ |
+ // | |(2) Origin B | |
+ // | |Policy: {"vibrate": ["self"]} | |
+ // | +---------------------------------+ |
+ // +-------------------------------------+
+ // Feature should be disabled in cross origin frame, even if no policy was
+ // specified in the parent frame.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ policy2->setHeaderPolicy("{\"vibrate\": [\"self\"]}");
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kVibrateFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestFrameSelfInheritance) {
+ // +------------------------------------------+
+ // |(1) Origin A |
+ // |Policy: {"vibrate": ["self"]} |
+ // | +-----------------+ +-----------------+ |
+ // | |(2) Origin A | |(4) Origin B | |
+ // | |No Policy | |No Policy | |
+ // | | +-------------+ | | +-------------+ | |
+ // | | |(3)Origin A | | | |(5)Origin B | | |
+ // | | |No Policy | | | |No Policy | | |
+ // | | +-------------+ | | +-------------+ | |
+ // | +-----------------+ +-----------------+ |
+ // +------------------------------------------+
+ // Feature should be enabled at the top-level, and through the chain of
+ // same-origin frames 2 and 3. It should be disabled in frames 4 and 5, as
+ // they are at a different origin.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"vibrate\": [\"self\"]}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originA);
+ FeaturePolicy* policy3 =
+ FeaturePolicy::createFromParentPolicy(policy2, originA);
+ FeaturePolicy* policy4 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ FeaturePolicy* policy5 =
+ FeaturePolicy::createFromParentPolicy(policy4, originB);
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_TRUE(policy3->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy4->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy5->isFeatureEnabled(&kVibrateFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestReflexiveFrameSelfInheritance) {
+ // +---------------------------------+
+ // |(1) Origin A |
+ // |Policy: {"vibrate": ["self"]} |
+ // | +-----------------+ |
+ // | |(2) Origin B | |
+ // | |No Policy | |
+ // | | +-------------+ | |
+ // | | |(3)Origin A | | |
+ // | | |No Policy | | |
+ // | | +-------------+ | |
+ // | +-----------------+ |
+ // +---------------------------------+
+ // Feature which is enabled at top-level should be disabled in frame 3, as
+ // it is embedded by frame 2, for which the feature is not enabled.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"vibrate\": [\"self\"]}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ FeaturePolicy* policy3 =
+ FeaturePolicy::createFromParentPolicy(policy2, originA);
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kVibrateFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestSelectiveFrameInheritance) {
+ // +------------------------------------------+
+ // |(1) Origin A |
+ // |Policy: {"vibrate": ["Origin B"]} |
+ // | +-----------------+ +-----------------+ |
+ // | |(2) Origin B | |(3) Origin C | |
+ // | |No Policy | |No Policy | |
+ // | | | | +-------------+ | |
+ // | | | | |(4)Origin B | | |
+ // | | | | |No Policy | | |
+ // | | | | +-------------+ | |
+ // | +-----------------+ +-----------------+ |
+ // +------------------------------------------+
+ // Feature should be enabled in second level Origin B frame, but disabled in
+ // Frame 4, because it is embedded by frame 3, where the feature is not
+ // enabled.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"vibrate\": [\"" ORIGIN_B "\"]}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ FeaturePolicy* policy3 =
+ FeaturePolicy::createFromParentPolicy(policy1, originC);
+ FeaturePolicy* policy4 =
+ FeaturePolicy::createFromParentPolicy(policy3, originB);
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy4->isFeatureEnabled(&kVibrateFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestPolicyCanBlockSelf) {
+ // +----------------------------+
+ // |(1)Origin A |
+ // |Policy: {"docwrite": []} |
+ // +----------------------------+
+ // Default-on feature should be disabled in top-level frame.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"docwrite\": []}");
+ EXPECT_FALSE(policy1->isFeatureEnabled(&kDocumentWrite));
+}
+
+TEST_F(FeaturePolicyTest, TestParentPolicyBlocksSameOriginChildPolicy) {
+ // +----------------------------+
+ // |(1)Origin A |
+ // |Policy: {"docwrite": []} |
+ // | +-------------+ |
+ // | |(2)Origin A | |
+ // | |No Policy | |
+ // | +-------------+ |
+ // +----------------------------+
+ // Feature should be disabled in child frame.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"docwrite\": []}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originA);
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kDocumentWrite));
+}
+
+TEST_F(FeaturePolicyTest, TestChildPolicyCanBlockSelf) {
+ // +--------------------------------+
+ // |(1)Origin A |
+ // |No Policy |
+ // | +----------------------------+ |
+ // | |(2)Origin B | |
+ // | |Policy: {"docwrite": []} | |
+ // | +----------------------------+ |
+ // +--------------------------------+
+ // Default-on feature should be disabled by cross-origin child frame.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ policy2->setHeaderPolicy("{\"docwrite\": []}");
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kDocumentWrite));
+}
+
+TEST_F(FeaturePolicyTest, TestChildPolicyCanBlockChildren) {
+ // +--------------------------------------+
+ // |(1)Origin A |
+ // |No Policy |
+ // | +----------------------------------+ |
+ // | |(2)Origin B | |
+ // | |Policy: {"docwrite": ["self"]} | |
+ // | | +-------------+ | |
+ // | | |(3)Origin C | | |
+ // | | |No Policy | | |
+ // | | +-------------+ | |
+ // | +----------------------------------+ |
+ // +--------------------------------------+
+ // Default-on feature should be enabled in frames 1 and 2; disabled in frame
+ // 3 by child frame policy.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ policy2->setHeaderPolicy("{\"docwrite\": [\"self\"]}");
+ FeaturePolicy* policy3 =
+ FeaturePolicy::createFromParentPolicy(policy2, originC);
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kDocumentWrite));
+}
+
+TEST_F(FeaturePolicyTest, TestParentPolicyBlocksCrossOriginChildPolicy) {
+ // +----------------------------+
+ // |(1)Origin A |
+ // |Policy: {"docwrite": []} |
+ // | +-------------+ |
+ // | |(2)Origin B | |
+ // | |No Policy | |
+ // | +-------------+ |
+ // +----------------------------+
+ // Default-on feature should be disabled in cross-origin child frame.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"docwrite\": []}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kDocumentWrite));
+}
+
+TEST_F(FeaturePolicyTest, TestEnableForAllOrigins) {
+ // +------------------------------+
+ // |(1) Origin A |
+ // |Policy: {"vibrate": ["*"]} |
+ // | +-----------------+ |
+ // | |(2) Origin B | |
+ // | |No Policy | |
+ // | | +-------------+ | |
+ // | | |(3)Origin A | | |
+ // | | |No Policy | | |
+ // | | +-------------+ | |
+ // | +-----------------+ |
+ // +------------------------------+
+ // Feature should be enabled in top and second level; disabled in frame 3.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"vibrate\": [\"*\"]}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ FeaturePolicy* policy3 =
+ FeaturePolicy::createFromParentPolicy(policy2, originA);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kVibrateFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestReenableForAllOrigins) {
+ // +----------------------------------+
+ // |(1) Origin A |
+ // |Policy: {"vibrate": ["*"]} |
+ // | +------------------------------+ |
+ // | |(2) Origin B | |
+ // | |Policy: {"vibrate": ["*"]} | |
+ // | | +-------------+ | |
+ // | | |(3)Origin A | | |
+ // | | |No Policy | | |
+ // | | +-------------+ | |
+ // | +------------------------------+ |
+ // +----------------------------------+
+ // Feature should be enabled in all frames.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"vibrate\": [\"*\"]}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ policy2->setHeaderPolicy("{\"vibrate\": [\"*\"]}");
+ FeaturePolicy* policy3 =
+ FeaturePolicy::createFromParentPolicy(policy2, originA);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_TRUE(policy3->isFeatureEnabled(&kVibrateFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestBlockedFrameCannotReenable) {
+ // +--------------------------------------+
+ // |(1)Origin A |
+ // |Policy: {"vibrate": ["self"]} |
+ // | +----------------------------------+ |
+ // | |(2)Origin B | |
+ // | |Policy: {"vibrate": ["*"]} | |
+ // | | +-------------+ +-------------+ | |
+ // | | |(3)Origin A | |(4)Origin C | | |
+ // | | |No Policy | |No Policy | | |
+ // | | +-------------+ +-------------+ | |
+ // | +----------------------------------+ |
+ // +--------------------------------------+
+ // Feature should be enabled at the top level; disabled in all other frames.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"vibrate\": [\"self\"]}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ policy2->setHeaderPolicy("{\"vibrate\": [\"*\"]}");
+ FeaturePolicy* policy3 =
+ FeaturePolicy::createFromParentPolicy(policy2, originA);
+ FeaturePolicy* policy4 =
+ FeaturePolicy::createFromParentPolicy(policy2, originC);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy4->isFeatureEnabled(&kVibrateFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestEnabledFrameCanDelegate) {
+ // +-------------------------------------------------+
+ // |(1) Origin A |
+ // |Policy: {"vibrate": ["self", "Origin B"]} |
+ // | +---------------------------------------------+ |
+ // | |(2) Origin B | |
+ // | |Policy: {"vibrate": ["self", "Origin C"]} | |
+ // | | +-------------+ | |
+ // | | |(3)Origin C | | |
+ // | | |No Policy | | |
+ // | | +-------------+ | |
+ // | +---------------------------------------------+ |
+ // +-------------------------------------------------+
+ // Feature should be enabled in all frames.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"vibrate\": [\"self\", \"" ORIGIN_B "\"]}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ policy2->setHeaderPolicy("{\"vibrate\": [\"self\", \"" ORIGIN_C "\"]}");
+ FeaturePolicy* policy3 =
+ FeaturePolicy::createFromParentPolicy(policy2, originC);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_TRUE(policy3->isFeatureEnabled(&kVibrateFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestEnabledFrameCanDelegateByDefault) {
+ // +-----------------------------------------------+
+ // |(1) Origin A |
+ // |Policy: {"docwrite": ["self", "Origin B"]} |
+ // | +--------------------+ +--------------------+ |
+ // | |(2) Origin B | | (4) Origin C | |
+ // | |No Policy | | No Policy | |
+ // | | +-------------+ | | | |
+ // | | |(3)Origin C | | | | |
+ // | | |No Policy | | | | |
+ // | | +-------------+ | | | |
+ // | +--------------------+ +--------------------+ |
+ // +-----------------------------------------------+
+ // Feature should be enabled in frames 1, 2, and 3, and disabled in frame 4.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"docwrite\": [\"self\", \"" ORIGIN_B "\"]}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ FeaturePolicy* policy3 =
+ FeaturePolicy::createFromParentPolicy(policy2, originC);
+ FeaturePolicy* policy4 =
+ FeaturePolicy::createFromParentPolicy(policy1, originC);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_TRUE(policy3->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_FALSE(policy4->isFeatureEnabled(&kDocumentWrite));
+}
+
+TEST_F(FeaturePolicyTest, TestNonNestedFeaturesDontDelegateByDefault) {
+ // +-----------------------------------------------+
+ // |(1) Origin A |
+ // |Policy: {"vibrate": ["self", "Origin B"]} |
+ // | +--------------------+ +--------------------+ |
+ // | |(2) Origin B | | (4) Origin C | |
+ // | |No Policy | | No Policy | |
+ // | | +-------------+ | | | |
+ // | | |(3)Origin C | | | | |
+ // | | |No Policy | | | | |
+ // | | +-------------+ | | | |
+ // | +--------------------+ +--------------------+ |
+ // +-----------------------------------------------+
+ // Feature should be enabled in frames 1 and 2, and disabled in frames 3 and
+ // 4.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"vibrate\": [\"self\", \"" ORIGIN_B "\"]}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ FeaturePolicy* policy3 =
+ FeaturePolicy::createFromParentPolicy(policy2, originC);
+ FeaturePolicy* policy4 =
+ FeaturePolicy::createFromParentPolicy(policy1, originC);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy4->isFeatureEnabled(&kVibrateFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestFeaturesAreIndependent) {
+ // +---------------------------------------------+
+ // |(1) Origin A |
+ // |Policy: {"vibrate": ["self", "Origin B"], |
+ // | "docwrite": ["self"]} |
+ // | +-----------------------------------------+ |
+ // | |(2) Origin B | |
+ // | |Policy: {"vibrate": ["*"], | |
+ // | | "docwrite": ["*"]} | |
+ // | | +-------------+ | |
+ // | | |(3)Origin C | | |
+ // | | |No Policy | | |
+ // | | +-------------+ | |
+ // | +-----------------------------------------+ |
+ // +---------------------------------------------+
+ // Vibrate feature should be enabled in all frames; Docwrite feature
+ // should be enabled in frame 1, and disabled in frames 2 and 3.
+ FeaturePolicy* policy1 =
+ FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->setHeaderPolicy("{\"vibrate\": [\"self\", \"" ORIGIN_B
+ "\"], \"docwrite\": [\"self\"]}");
+ FeaturePolicy* policy2 =
+ FeaturePolicy::createFromParentPolicy(policy1, originB);
+ policy2->setHeaderPolicy("{\"vibrate\": [\"*\"], \"docwrite\": [\"*\"]}");
+ FeaturePolicy* policy3 =
+ FeaturePolicy::createFromParentPolicy(policy2, originC);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_TRUE(policy3->isFeatureEnabled(&kVibrateFeature));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kDocumentWrite));
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698