OLD | NEW |
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* const kValidPolicies[] = { | 19 const char* const kValidPolicies[] = { |
20 "{\"vibrate\": []}", | 20 "{\"payment\": []}", |
21 "{\"vibrate\": [\"self\"]}", | 21 "{\"payment\": [\"self\"]}", |
22 "{\"vibrate\": [\"*\"]}", | 22 "{\"payment\": [\"*\"]}", |
23 "{\"vibrate\": [\"" ORIGIN_A "\"]}", | 23 "{\"payment\": [\"" ORIGIN_A "\"]}", |
24 "{\"vibrate\": [\"" ORIGIN_B "\"]}", | 24 "{\"payment\": [\"" ORIGIN_B "\"]}", |
25 "{\"vibrate\": [\"" ORIGIN_A "\", \"" ORIGIN_B "\"]}", | 25 "{\"payment\": [\"" ORIGIN_A "\", \"" ORIGIN_B "\"]}", |
26 "{\"fullscreen\": [\"" ORIGIN_A "\"], \"payment\": [\"self\"]}", | 26 "{\"fullscreen\": [\"" ORIGIN_A "\"], \"payment\": [\"self\"]}", |
27 "{\"fullscreen\": [\"" ORIGIN_A "\"]}, {\"payment\": [\"self\"]}"}; | 27 "{\"fullscreen\": [\"" ORIGIN_A "\"]}, {\"payment\": [\"self\"]}"}; |
28 | 28 |
29 const char* g_k_invalid_policies[] = { | 29 const char* g_k_invalid_policies[] = { |
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 "{\"vibrate\": \"Not a JSON array\"}", | 34 "{\"payment\": \"Not a JSON array\"}", |
35 "{\"vibrate\": [\"*\"], \"payment\": \"Not a JSON array\"}"}; | 35 "{\"payment\": [\"*\"], \"payment\": \"Not a JSON 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 Loading... |
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 WebParsedFeaturePolicy parsed_policy = | 73 WebParsedFeaturePolicy parsed_policy = |
74 ParseFeaturePolicy("{}", origin_a_.Get(), &messages); | 74 ParseFeaturePolicy("{}", origin_a_.Get(), &messages); |
75 EXPECT_EQ(0UL, parsed_policy.size()); | 75 EXPECT_EQ(0UL, parsed_policy.size()); |
76 | 76 |
77 // Simple policy with "self". | 77 // Simple policy with "self". |
78 parsed_policy = ParseFeaturePolicy("{\"vibrate\": [\"self\"]}", | 78 parsed_policy = ParseFeaturePolicy("{\"payment\": [\"self\"]}", |
79 origin_a_.Get(), &messages); | 79 origin_a_.Get(), &messages); |
80 EXPECT_EQ(1UL, parsed_policy.size()); | 80 EXPECT_EQ(1UL, parsed_policy.size()); |
81 EXPECT_EQ(WebFeaturePolicyFeature::kVibrate, parsed_policy[0].feature); | 81 EXPECT_EQ(WebFeaturePolicyFeature::kPayment, parsed_policy[0].feature); |
82 EXPECT_FALSE(parsed_policy[0].matches_all_origins); | 82 EXPECT_FALSE(parsed_policy[0].matches_all_origins); |
83 EXPECT_EQ(1UL, parsed_policy[0].origins.size()); | 83 EXPECT_EQ(1UL, parsed_policy[0].origins.size()); |
84 EXPECT_TRUE(origin_a_->IsSameSchemeHostPortAndSuborigin( | 84 EXPECT_TRUE(origin_a_->IsSameSchemeHostPortAndSuborigin( |
85 parsed_policy[0].origins[0].Get())); | 85 parsed_policy[0].origins[0].Get())); |
86 | 86 |
87 // Simple policy with *. | 87 // Simple policy with *. |
88 parsed_policy = | 88 parsed_policy = |
89 ParseFeaturePolicy("{\"vibrate\": [\"*\"]}", origin_a_.Get(), &messages); | 89 ParseFeaturePolicy("{\"payment\": [\"*\"]}", origin_a_.Get(), &messages); |
90 EXPECT_EQ(1UL, parsed_policy.size()); | 90 EXPECT_EQ(1UL, parsed_policy.size()); |
91 EXPECT_EQ(WebFeaturePolicyFeature::kVibrate, parsed_policy[0].feature); | 91 EXPECT_EQ(WebFeaturePolicyFeature::kPayment, parsed_policy[0].feature); |
92 EXPECT_TRUE(parsed_policy[0].matches_all_origins); | 92 EXPECT_TRUE(parsed_policy[0].matches_all_origins); |
93 EXPECT_EQ(0UL, parsed_policy[0].origins.size()); | 93 EXPECT_EQ(0UL, parsed_policy[0].origins.size()); |
94 | 94 |
95 // Complicated policy. | 95 // Complicated policy. |
96 parsed_policy = ParseFeaturePolicy( | 96 parsed_policy = ParseFeaturePolicy( |
97 "{\"vibrate\": [\"*\"], " | 97 "{\"payment\": [\"*\"], " |
98 "\"fullscreen\": [\"https://example.net\", \"https://example.org\"], " | 98 "\"fullscreen\": [\"https://example.net\", \"https://example.org\"], " |
99 "\"payment\": [\"self\"]}", | 99 "\"payment\": [\"self\"]}", |
100 origin_a_.Get(), &messages); | 100 origin_a_.Get(), &messages); |
101 EXPECT_EQ(3UL, parsed_policy.size()); | 101 EXPECT_EQ(3UL, parsed_policy.size()); |
102 EXPECT_EQ(WebFeaturePolicyFeature::kVibrate, parsed_policy[0].feature); | 102 EXPECT_EQ(WebFeaturePolicyFeature::kPayment, parsed_policy[0].feature); |
103 EXPECT_TRUE(parsed_policy[0].matches_all_origins); | 103 EXPECT_TRUE(parsed_policy[0].matches_all_origins); |
104 EXPECT_EQ(0UL, parsed_policy[0].origins.size()); | 104 EXPECT_EQ(0UL, parsed_policy[0].origins.size()); |
105 EXPECT_EQ(WebFeaturePolicyFeature::kFullscreen, parsed_policy[1].feature); | 105 EXPECT_EQ(WebFeaturePolicyFeature::kFullscreen, parsed_policy[1].feature); |
106 EXPECT_FALSE(parsed_policy[1].matches_all_origins); | 106 EXPECT_FALSE(parsed_policy[1].matches_all_origins); |
107 EXPECT_EQ(2UL, parsed_policy[1].origins.size()); | 107 EXPECT_EQ(2UL, parsed_policy[1].origins.size()); |
108 EXPECT_TRUE(origin_b_->IsSameSchemeHostPortAndSuborigin( | 108 EXPECT_TRUE(origin_b_->IsSameSchemeHostPortAndSuborigin( |
109 parsed_policy[1].origins[0].Get())); | 109 parsed_policy[1].origins[0].Get())); |
110 EXPECT_TRUE(origin_c_->IsSameSchemeHostPortAndSuborigin( | 110 EXPECT_TRUE(origin_c_->IsSameSchemeHostPortAndSuborigin( |
111 parsed_policy[1].origins[1].Get())); | 111 parsed_policy[1].origins[1].Get())); |
112 EXPECT_EQ(WebFeaturePolicyFeature::kPayment, parsed_policy[2].feature); | 112 EXPECT_EQ(WebFeaturePolicyFeature::kPayment, parsed_policy[2].feature); |
113 EXPECT_FALSE(parsed_policy[2].matches_all_origins); | 113 EXPECT_FALSE(parsed_policy[2].matches_all_origins); |
114 EXPECT_EQ(1UL, parsed_policy[2].origins.size()); | 114 EXPECT_EQ(1UL, parsed_policy[2].origins.size()); |
115 EXPECT_TRUE(origin_a_->IsSameSchemeHostPortAndSuborigin( | 115 EXPECT_TRUE(origin_a_->IsSameSchemeHostPortAndSuborigin( |
116 parsed_policy[2].origins[0].Get())); | 116 parsed_policy[2].origins[0].Get())); |
117 } | 117 } |
118 | 118 |
119 TEST_F(FeaturePolicyTest, ParseEmptyContainerPolicy) { | 119 TEST_F(FeaturePolicyTest, ParseEmptyContainerPolicy) { |
120 WebParsedFeaturePolicy container_policy = | 120 WebParsedFeaturePolicy container_policy = |
121 GetContainerPolicyFromAllowedFeatures( | 121 GetContainerPolicyFromAllowedFeatures( |
122 std::vector<WebFeaturePolicyFeature>({}), origin_a_.Get()); | 122 std::vector<WebFeaturePolicyFeature>({}), origin_a_.Get()); |
123 EXPECT_EQ(0UL, container_policy.size()); | 123 EXPECT_EQ(0UL, container_policy.size()); |
124 } | 124 } |
125 | 125 |
126 TEST_F(FeaturePolicyTest, ParseContainerPolicy) { | 126 TEST_F(FeaturePolicyTest, ParseContainerPolicy) { |
127 WebParsedFeaturePolicy container_policy = | 127 WebParsedFeaturePolicy container_policy = |
128 GetContainerPolicyFromAllowedFeatures( | 128 GetContainerPolicyFromAllowedFeatures( |
129 std::vector<WebFeaturePolicyFeature>( | 129 std::vector<WebFeaturePolicyFeature>( |
130 {WebFeaturePolicyFeature::kVibrate, | 130 {WebFeaturePolicyFeature::kPayment, |
131 WebFeaturePolicyFeature::kPayment}), | 131 WebFeaturePolicyFeature::kPayment}), |
132 origin_a_.Get()); | 132 origin_a_.Get()); |
133 EXPECT_EQ(2UL, container_policy.size()); | 133 EXPECT_EQ(2UL, container_policy.size()); |
134 EXPECT_EQ(WebFeaturePolicyFeature::kVibrate, container_policy[0].feature); | 134 EXPECT_EQ(WebFeaturePolicyFeature::kPayment, container_policy[0].feature); |
135 EXPECT_FALSE(container_policy[0].matches_all_origins); | 135 EXPECT_FALSE(container_policy[0].matches_all_origins); |
136 EXPECT_EQ(1UL, container_policy[0].origins.size()); | 136 EXPECT_EQ(1UL, container_policy[0].origins.size()); |
137 EXPECT_TRUE(origin_a_->IsSameSchemeHostPortAndSuborigin( | 137 EXPECT_TRUE(origin_a_->IsSameSchemeHostPortAndSuborigin( |
138 container_policy[0].origins[0].Get())); | 138 container_policy[0].origins[0].Get())); |
139 EXPECT_EQ(WebFeaturePolicyFeature::kPayment, container_policy[1].feature); | 139 EXPECT_EQ(WebFeaturePolicyFeature::kPayment, container_policy[1].feature); |
140 EXPECT_FALSE(container_policy[1].matches_all_origins); | 140 EXPECT_FALSE(container_policy[1].matches_all_origins); |
141 EXPECT_EQ(1UL, container_policy[1].origins.size()); | 141 EXPECT_EQ(1UL, container_policy[1].origins.size()); |
142 EXPECT_TRUE(origin_a_->IsSameSchemeHostPortAndSuborigin( | 142 EXPECT_TRUE(origin_a_->IsSameSchemeHostPortAndSuborigin( |
143 container_policy[1].origins[0].Get())); | 143 container_policy[1].origins[0].Get())); |
144 } | 144 } |
145 | 145 |
146 } // namespace blink | 146 } // namespace blink |
OLD | NEW |