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

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: Remove overaggressive bool transform Created 4 years, 3 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..7df08f31c92a3d19d61e94893fd7af0ab368addc
--- /dev/null
+++ b/third_party/WebKit/Source/platform/feature_policy/FeaturePolicyTest.cpp
@@ -0,0 +1,392 @@
+// 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/"
+
+namespace blink {
+
+namespace {
+
+const char* kInvalidPolicies[] = {
+ "Not A JSON literal",
+ "\"Not a JSON array\"",
+ "{\"Also\": \"Not a JSON array\"}",
+ "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", false, false };
+
+} // namespace
+
+class FeaturePolicyTest : public ::testing::Test {
+protected:
+ FeaturePolicyTest()
+ : m_frameworkWasEnabled(RuntimeEnabledFeatures::featurePolicyEnabled())
+ {
+ RuntimeEnabledFeatures::setFeaturePolicyEnabled(true);
+ }
+
+ ~FeaturePolicyTest()
+ {
+ RuntimeEnabledFeatures::setFeaturePolicyEnabled(m_frameworkWasEnabled);
+ }
+
+ RefPtr<SecurityOrigin> originA = SecurityOrigin::createFromString(ORIGIN_A);
+ 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);
+ }
+}
+
+TEST_F(FeaturePolicyTest, TestInitialPolicy)
+{
+ // +-------------+
+ // |(1)Origin A |
+ // |No Policy |
+ // +-------------+
raymes 2016/09/19 09:19:05 +1 thanks for these awesome diagrams!
+ // 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(&kVibrate));
+ EXPECT_FALSE(policy1->isFeatureEnabled(&kOffByDeaultFeature));
+}
+
+TEST_F(FeaturePolicyTest, TestInitialChildPolicy)
+{
+ // +-----------------+
+ // |(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.
+ FeaturePolicy* policy1 = FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ FeaturePolicy* policy2 = FeaturePolicy::createFromParentPolicy(policy1, originA);
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kVibrate));
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kOffByDeaultFeature));
+}
+
+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->addPolicyFromString("[{\"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(&kVibrate));
+ EXPECT_TRUE(policy3->isFeatureEnabled(&kVibrate));
+ EXPECT_FALSE(policy4->isFeatureEnabled(&kVibrate));
+ EXPECT_FALSE(policy5->isFeatureEnabled(&kVibrate));
+}
+
+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->addPolicyFromString("[{\"vibrate\": [\"self\"]}]");
+ FeaturePolicy* policy2 = FeaturePolicy::createFromParentPolicy(policy1, originB);
+ FeaturePolicy* policy3 = FeaturePolicy::createFromParentPolicy(policy2, originA);
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kVibrate));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kVibrate));
+}
+
+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->addPolicyFromString("[{\"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(&kVibrate));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kVibrate));
+ EXPECT_FALSE(policy4->isFeatureEnabled(&kVibrate));
+}
+
+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->addPolicyFromString("[{\"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->addPolicyFromString("[{\"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->addPolicyFromString("[{\"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->addPolicyFromString("[{\"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->addPolicyFromString("[{\"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->addPolicyFromString("[{\"vibrate\": [\"*\"]}]");
+ FeaturePolicy* policy2 = FeaturePolicy::createFromParentPolicy(policy1, originB);
+ FeaturePolicy* policy3 = FeaturePolicy::createFromParentPolicy(policy2, originA);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrate));
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrate));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kVibrate));
+}
+
+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->addPolicyFromString("[{\"vibrate\": [\"*\"]}]");
+ FeaturePolicy* policy2 = FeaturePolicy::createFromParentPolicy(policy1, originB);
+ policy2->addPolicyFromString("[{\"vibrate\": [\"*\"]}]");
+ FeaturePolicy* policy3 = FeaturePolicy::createFromParentPolicy(policy2, originA);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrate));
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrate));
+ EXPECT_TRUE(policy3->isFeatureEnabled(&kVibrate));
+}
+
+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->addPolicyFromString("[{\"vibrate\": [\"self\"]}]");
+ FeaturePolicy* policy2 = FeaturePolicy::createFromParentPolicy(policy1, originB);
+ policy2->addPolicyFromString("[{\"vibrate\": [\"*\"]}]");
+ FeaturePolicy* policy3 = FeaturePolicy::createFromParentPolicy(policy2, originA);
+ FeaturePolicy* policy4 = FeaturePolicy::createFromParentPolicy(policy2, originC);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrate));
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kVibrate));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kVibrate));
+ EXPECT_FALSE(policy4->isFeatureEnabled(&kVibrate));
+}
+
+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->addPolicyFromString("[{\"vibrate\": [\"self\", \"" ORIGIN_B "\"]}]");
+ FeaturePolicy* policy2 = FeaturePolicy::createFromParentPolicy(policy1, originB);
+ policy2->addPolicyFromString("[{\"vibrate\": [\"self\", \"" ORIGIN_C "\"]}]");
+ FeaturePolicy* policy3 = FeaturePolicy::createFromParentPolicy(policy2, originC);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrate));
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrate));
+ EXPECT_TRUE(policy3->isFeatureEnabled(&kVibrate));
+}
+
+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 features should
+ // be enabled in frame 1, and disabled in frames 2 and 3.
+ FeaturePolicy* policy1 = FeaturePolicy::createFromParentPolicy(nullptr, originA);
+ policy1->addPolicyFromString("[{\"vibrate\": [\"self\", \"" ORIGIN_B "\"]}, {\"docwrite\": [\"self\"]}]");
+ FeaturePolicy* policy2 = FeaturePolicy::createFromParentPolicy(policy1, originB);
+ policy2->addPolicyFromString("[{\"vibrate\": [\"*\"]}, {\"docwrite\": [\"*\"]}]");
+ FeaturePolicy* policy3 = FeaturePolicy::createFromParentPolicy(policy2, originC);
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kVibrate));
+ EXPECT_TRUE(policy1->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_TRUE(policy2->isFeatureEnabled(&kVibrate));
+ EXPECT_FALSE(policy2->isFeatureEnabled(&kDocumentWrite));
+ EXPECT_TRUE(policy3->isFeatureEnabled(&kVibrate));
+ EXPECT_FALSE(policy3->isFeatureEnabled(&kDocumentWrite));
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698