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

Side by Side Diff: third_party/WebKit/Source/platform/feature_policy/FeaturePolicyTest.cpp

Issue 2727803004: Replace string by enum in WebParsedFeaturePolicyDeclaration#feature (Closed)
Patch Set: Set upstream Created 3 years, 9 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "platform/feature_policy/FeaturePolicy.h" 5 #include "platform/feature_policy/FeaturePolicy.h"
6 6
7 #include "platform/RuntimeEnabledFeatures.h" 7 #include "platform/RuntimeEnabledFeatures.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 // Origin strings used for tests 10 // Origin strings used for tests
11 #define ORIGIN_A "https://example.com/" 11 #define ORIGIN_A "https://example.com/"
12 #define ORIGIN_B "https://example.net/" 12 #define ORIGIN_B "https://example.net/"
13 #define ORIGIN_C "https://example.org/" 13 #define ORIGIN_C "https://example.org/"
14 14
15 namespace blink { 15 namespace blink {
16 16
17 namespace { 17 namespace {
18 18
19 const char* kValidPolicies[] = { 19 const char* kValidPolicies[] = {
20 "{\"feature\": []}", 20 "{\"vibrate\": []}",
21 "{\"feature\": [\"self\"]}", 21 "{\"vibrate\": [\"self\"]}",
22 "{\"feature\": [\"*\"]}", 22 "{\"vibrate\": [\"*\"]}",
23 "{\"feature\": [\"" ORIGIN_A "\"]}", 23 "{\"vibrate\": [\"" ORIGIN_A "\"]}",
24 "{\"feature\": [\"" ORIGIN_B "\"]}", 24 "{\"vibrate\": [\"" ORIGIN_B "\"]}",
25 "{\"feature\": [\"" ORIGIN_A "\", \"" ORIGIN_B "\"]}", 25 "{\"vibrate\": [\"" ORIGIN_A "\", \"" ORIGIN_B "\"]}",
26 "{\"feature1\": [\"" ORIGIN_A "\"], \"feature2\": [\"self\"]}", 26 "{\"fullscreen\": [\"" ORIGIN_A "\"], \"payment\": [\"self\"]}",
27 "{\"feature1\": [\"" ORIGIN_A "\"]}, {\"feature2\": [\"self\"]}"}; 27 "{\"fullscreen\": [\"" ORIGIN_A "\"]}, {\"payment\": [\"self\"]}"};
28 28
29 const char* kInvalidPolicies[] = { 29 const char* kInvalidPolicies[] = {
30 "Not A JSON literal", 30 "Not A JSON literal",
31 "\"Not a JSON object\"", 31 "\"Not a JSON object\"",
32 "[\"Also\", \"Not a JSON object\"]", 32 "[\"Also\", \"Not a JSON object\"]",
33 "1.0", 33 "1.0",
34 "{\"Whitelist\": \"Not a JSON array\"}", 34 "{\"Whitelist\": \"Not a JSON array\"}",
35 "{\"feature1\": [\"*\"], \"feature2\": \"Not an array\"}"}; 35 "{\"vibrate\": [\"*\"], \"payment\": \"Not an array\"}"};
36 36
37 } // namespace 37 } // namespace
38 38
39 class FeaturePolicyTest : public ::testing::Test { 39 class FeaturePolicyTest : public ::testing::Test {
40 protected: 40 protected:
41 FeaturePolicyTest() {} 41 FeaturePolicyTest() {}
42 42
43 ~FeaturePolicyTest() { 43 ~FeaturePolicyTest() {
44 } 44 }
45 45
(...skipping 22 matching lines...) Expand all
68 68
69 TEST_F(FeaturePolicyTest, PolicyParsedCorrectly) { 69 TEST_F(FeaturePolicyTest, PolicyParsedCorrectly) {
70 Vector<String> messages; 70 Vector<String> messages;
71 71
72 // Empty policy. 72 // Empty policy.
73 WebParsedFeaturePolicyHeader parsedPolicy = 73 WebParsedFeaturePolicyHeader parsedPolicy =
74 parseFeaturePolicy("{}", m_originA.get(), &messages); 74 parseFeaturePolicy("{}", m_originA.get(), &messages);
75 EXPECT_EQ(0UL, parsedPolicy.size()); 75 EXPECT_EQ(0UL, parsedPolicy.size());
76 76
77 // Simple policy with "self". 77 // Simple policy with "self".
78 parsedPolicy = parseFeaturePolicy("{\"default-self\": [\"self\"]}", 78 parsedPolicy = parseFeaturePolicy("{\"vibrate\": [\"self\"]}",
79 m_originA.get(), &messages); 79 m_originA.get(), &messages);
80 EXPECT_EQ(1UL, parsedPolicy.size()); 80 EXPECT_EQ(1UL, parsedPolicy.size());
81 EXPECT_EQ("default-self", parsedPolicy[0].featureName); 81 EXPECT_EQ(WebFeaturePolicyFeature::Vibrate, parsedPolicy[0].feature);
82 EXPECT_FALSE(parsedPolicy[0].matchesAllOrigins); 82 EXPECT_FALSE(parsedPolicy[0].matchesAllOrigins);
83 EXPECT_EQ(1UL, parsedPolicy[0].origins.size()); 83 EXPECT_EQ(1UL, parsedPolicy[0].origins.size());
84 EXPECT_TRUE(m_originA->isSameSchemeHostPortAndSuborigin( 84 EXPECT_TRUE(m_originA->isSameSchemeHostPortAndSuborigin(
85 parsedPolicy[0].origins[0].get())); 85 parsedPolicy[0].origins[0].get()));
86 86
87 // Simple policy with *. 87 // Simple policy with *.
88 parsedPolicy = parseFeaturePolicy("{\"default-self\": [\"*\"]}", 88 parsedPolicy =
89 m_originA.get(), &messages); 89 parseFeaturePolicy("{\"vibrate\": [\"*\"]}", m_originA.get(), &messages);
90 EXPECT_EQ(1UL, parsedPolicy.size()); 90 EXPECT_EQ(1UL, parsedPolicy.size());
91 EXPECT_EQ("default-self", parsedPolicy[0].featureName); 91 EXPECT_EQ(WebFeaturePolicyFeature::Vibrate, parsedPolicy[0].feature);
92 EXPECT_TRUE(parsedPolicy[0].matchesAllOrigins); 92 EXPECT_TRUE(parsedPolicy[0].matchesAllOrigins);
93 EXPECT_EQ(0UL, parsedPolicy[0].origins.size()); 93 EXPECT_EQ(0UL, parsedPolicy[0].origins.size());
94 94
95 // Complicated policy. 95 // Complicated policy.
96 parsedPolicy = parseFeaturePolicy( 96 parsedPolicy = parseFeaturePolicy(
97 "{\"default-self\": [\"*\"], " 97 "{\"vibrate\": [\"*\"], "
98 "\"default-on\": [\"https://example.net\", \"https://example.org\"], " 98 "\"fullscreen\": [\"https://example.net\", \"https://example.org\"], "
99 "\"default-off\": [\"self\"]}", 99 "\"payment\": [\"self\"]}",
100 m_originA.get(), &messages); 100 m_originA.get(), &messages);
101 EXPECT_EQ(3UL, parsedPolicy.size()); 101 EXPECT_EQ(3UL, parsedPolicy.size());
102 EXPECT_EQ("default-self", parsedPolicy[0].featureName); 102 EXPECT_EQ(WebFeaturePolicyFeature::Vibrate, parsedPolicy[0].feature);
103 EXPECT_TRUE(parsedPolicy[0].matchesAllOrigins); 103 EXPECT_TRUE(parsedPolicy[0].matchesAllOrigins);
104 EXPECT_EQ(0UL, parsedPolicy[0].origins.size()); 104 EXPECT_EQ(0UL, parsedPolicy[0].origins.size());
105 EXPECT_EQ("default-on", parsedPolicy[1].featureName); 105 EXPECT_EQ(WebFeaturePolicyFeature::Fullscreen, parsedPolicy[1].feature);
106 EXPECT_FALSE(parsedPolicy[1].matchesAllOrigins); 106 EXPECT_FALSE(parsedPolicy[1].matchesAllOrigins);
107 EXPECT_EQ(2UL, parsedPolicy[1].origins.size()); 107 EXPECT_EQ(2UL, parsedPolicy[1].origins.size());
108 EXPECT_TRUE(m_originB->isSameSchemeHostPortAndSuborigin( 108 EXPECT_TRUE(m_originB->isSameSchemeHostPortAndSuborigin(
109 parsedPolicy[1].origins[0].get())); 109 parsedPolicy[1].origins[0].get()));
110 EXPECT_TRUE(m_originC->isSameSchemeHostPortAndSuborigin( 110 EXPECT_TRUE(m_originC->isSameSchemeHostPortAndSuborigin(
111 parsedPolicy[1].origins[1].get())); 111 parsedPolicy[1].origins[1].get()));
112 EXPECT_EQ("default-off", parsedPolicy[2].featureName); 112 EXPECT_EQ(WebFeaturePolicyFeature::Payment, parsedPolicy[2].feature);
113 EXPECT_FALSE(parsedPolicy[2].matchesAllOrigins); 113 EXPECT_FALSE(parsedPolicy[2].matchesAllOrigins);
114 EXPECT_EQ(1UL, parsedPolicy[2].origins.size()); 114 EXPECT_EQ(1UL, parsedPolicy[2].origins.size());
115 EXPECT_TRUE(m_originA->isSameSchemeHostPortAndSuborigin( 115 EXPECT_TRUE(m_originA->isSameSchemeHostPortAndSuborigin(
116 parsedPolicy[2].origins[0].get())); 116 parsedPolicy[2].origins[0].get()));
117 } 117 }
118 118
119 } // namespace blink 119 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698