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

Side by Side 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: Addressing review comments from PS#13 and #15 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 unified diff | Download patch
OLDNEW
(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 "platform/RuntimeEnabledFeatures.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 // Origin strings used for tests
11 #define ORIGIN_A "https://example.com/"
12 #define ORIGIN_B "https://example.net/"
13 #define ORIGIN_C "https://example.org/"
14
15 namespace blink {
16
17 namespace {
18
19 const char* kValidPolicies[] = {
20 "{\"feature\": []}",
21 "{\"feature\": [\"self\"]}",
22 "{\"feature\": [\"*\"]}",
23 "{\"feature\": [\"" ORIGIN_A "\"]}",
24 "{\"feature\": [\"" ORIGIN_B "\"]}",
25 "{\"feature\": [\"" ORIGIN_A "\", \"" ORIGIN_B "\"]}",
26 "{\"feature1\": [\"" ORIGIN_A "\"], \"feature2\": [\"self\"]}",
27 "{\"feature1\": [\"" ORIGIN_A "\"]}, {\"feature2\": [\"self\"]}"};
28
29 const char* kInvalidPolicies[] = {
30 "Not A JSON literal",
31 "\"Not a JSON object\"",
32 "[\"Also\", \"Not a JSON object\"]",
33 "1.0",
34 "{\"Whitelist\": \"Not a JSON array\"}",
35 "{\"feature1\": [\"*\"], \"feature2\": \"Not an array\"}"};
36
37 // This is an example of a feature which should be enabled by default in all
38 // frames.
39 const FeaturePolicyFeature kDefaultOnFeature{
40 "default-on", FeaturePolicyFeatureDefault::EnableForAll};
41
42 // This is an example of a feature which should be enabled in top-level frames,
43 // and same-origin child-frames, but must be delegated to all cross-origin
44 // frames explicitly.
45 const FeaturePolicyFeature kDefaultSelfFeature{
46 "default-self", FeaturePolicyFeatureDefault::EnableForSelf};
47
48 // This is an example of a feature which should be disabled by default, both in
49 // top-level and nested frames.
50 const FeaturePolicyFeature kDefaultOffFeature{
51 "default-off", FeaturePolicyFeatureDefault::DisableForAll};
52
53 } // namespace
54
55 class FeaturePolicyTest : public ::testing::Test {
56 protected:
57 FeaturePolicyTest()
58 : m_frameworkWasEnabled(RuntimeEnabledFeatures::featurePolicyEnabled()),
59 m_featureList(
60 {&kDefaultOnFeature, &kDefaultSelfFeature, &kDefaultOffFeature}) {
61 RuntimeEnabledFeatures::setFeaturePolicyEnabled(true);
62 }
63
64 ~FeaturePolicyTest() {
65 RuntimeEnabledFeatures::setFeaturePolicyEnabled(m_frameworkWasEnabled);
66 }
67
68 FeaturePolicy* createFromParentPolicy(const FeaturePolicy* parent,
69 RefPtr<SecurityOrigin> origin) {
70 return FeaturePolicy::createFromParentPolicy(parent, origin, m_featureList);
71 }
72
73 RefPtr<SecurityOrigin> m_originA = SecurityOrigin::createFromString(ORIGIN_A);
74 RefPtr<SecurityOrigin> m_originB = SecurityOrigin::createFromString(ORIGIN_B);
75 RefPtr<SecurityOrigin> m_originC = SecurityOrigin::createFromString(ORIGIN_C);
76
77 private:
78 const bool m_frameworkWasEnabled;
79
80 // Contains the list of controlled features, so that we are guaranteed to
81 // have at least one of each kind of default behaviour represented.
82 FeatureList m_featureList;
83 };
84
85 TEST_F(FeaturePolicyTest, ParseValidPolicy) {
86 Vector<String> messages;
87 for (const char* policyString : kValidPolicies) {
88 messages.clear();
89 FeaturePolicy* policy = createFromParentPolicy(nullptr, m_originA);
90 policy->setHeaderPolicy(policyString, messages);
91 EXPECT_EQ(0UL, messages.size());
92 }
93 }
94
95 TEST_F(FeaturePolicyTest, ParseInvalidPolicy) {
96 Vector<String> messages;
97 for (const char* policyString : kInvalidPolicies) {
98 messages.clear();
99 FeaturePolicy* policy = createFromParentPolicy(nullptr, m_originA);
100 policy->setHeaderPolicy(policyString, messages);
101 EXPECT_NE(0UL, messages.size());
102 }
103 }
104
105 TEST_F(FeaturePolicyTest, TestInitialPolicy) {
106 // +-------------+
107 // |(1)Origin A |
108 // |No Policy |
109 // +-------------+
110 // Default-on and top-level-only features should be enabled in top-level
111 // frame. Default-off features should be disabled.
112 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
113 EXPECT_TRUE(policy1->isFeatureEnabled(&kDefaultOnFeature));
114 EXPECT_TRUE(policy1->isFeatureEnabled(&kDefaultSelfFeature));
115 EXPECT_FALSE(policy1->isFeatureEnabled(&kDefaultOffFeature));
116 }
117
118 TEST_F(FeaturePolicyTest, TestInitialSameOriginChildPolicy) {
119 // +-----------------+
120 // |(1)Origin A |
121 // |No Policy |
122 // | +-------------+ |
123 // | |(2)Origin A | |
124 // | |No Policy | |
125 // | +-------------+ |
126 // +-----------------+
127 // Default-on and Default-self features should be enabled in a same-origin
128 // child frame. Default-off features should be disabled.
129 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
130 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originA);
131 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultOnFeature));
132 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
133 EXPECT_FALSE(policy2->isFeatureEnabled(&kDefaultOffFeature));
134 }
135
136 TEST_F(FeaturePolicyTest, TestInitialCrossOriginChildPolicy) {
137 // +-----------------+
138 // |(1)Origin A |
139 // |No Policy |
140 // | +-------------+ |
141 // | |(2)Origin B | |
142 // | |No Policy | |
143 // | +-------------+ |
144 // +-----------------+
145 // Default-on features should be enabled in child frame. Default-self and
146 // Default-off features should be disabled.
147 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
148 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
149 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultOnFeature));
150 EXPECT_FALSE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
151 EXPECT_FALSE(policy2->isFeatureEnabled(&kDefaultOffFeature));
152 }
153
154 TEST_F(FeaturePolicyTest, TestCrossOriginChildCannotEnableFeature) {
155 // +---------------------------------------+
156 // |(1) Origin A |
157 // |No Policy |
158 // | +-----------------------------------+ |
159 // | |(2) Origin B | |
160 // | |Policy: {"default-self": ["self"]} | |
161 // | +-----------------------------------+ |
162 // +---------------------------------------+
163 // Default-self feature should be disabled in cross origin frame, even if no
164 // policy was specified in the parent frame.
165 Vector<String> messages;
166 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
167 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
168 policy2->setHeaderPolicy("{\"default-self\": [\"self\"]}", messages);
169 EXPECT_EQ(0UL, messages.size());
170 EXPECT_FALSE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
171 }
172
173 TEST_F(FeaturePolicyTest, TestFrameSelfInheritance) {
174 // +------------------------------------------+
175 // |(1) Origin A |
176 // |Policy: {"default-self": ["self"]} |
177 // | +-----------------+ +-----------------+ |
178 // | |(2) Origin A | |(4) Origin B | |
179 // | |No Policy | |No Policy | |
180 // | | +-------------+ | | +-------------+ | |
181 // | | |(3)Origin A | | | |(5)Origin B | | |
182 // | | |No Policy | | | |No Policy | | |
183 // | | +-------------+ | | +-------------+ | |
184 // | +-----------------+ +-----------------+ |
185 // +------------------------------------------+
186 // Feature should be enabled at the top-level, and through the chain of
187 // same-origin frames 2 and 3. It should be disabled in frames 4 and 5, as
188 // they are at a different origin.
189 Vector<String> messages;
190 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
191 policy1->setHeaderPolicy("{\"default-self\": [\"self\"]}", messages);
192 EXPECT_EQ(0UL, messages.size());
193 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originA);
194 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originA);
195 FeaturePolicy* policy4 = createFromParentPolicy(policy1, m_originB);
196 FeaturePolicy* policy5 = createFromParentPolicy(policy4, m_originB);
197 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
198 EXPECT_TRUE(policy3->isFeatureEnabled(&kDefaultSelfFeature));
199 EXPECT_FALSE(policy4->isFeatureEnabled(&kDefaultSelfFeature));
200 EXPECT_FALSE(policy5->isFeatureEnabled(&kDefaultSelfFeature));
201 }
202
203 TEST_F(FeaturePolicyTest, TestReflexiveFrameSelfInheritance) {
204 // +-----------------------------------+
205 // |(1) Origin A |
206 // |Policy: {"default-self": ["self"]} |
207 // | +-----------------+ |
208 // | |(2) Origin B | |
209 // | |No Policy | |
210 // | | +-------------+ | |
211 // | | |(3)Origin A | | |
212 // | | |No Policy | | |
213 // | | +-------------+ | |
214 // | +-----------------+ |
215 // +-----------------------------------+
216 // Feature which is enabled at top-level should be disabled in frame 3, as
217 // it is embedded by frame 2, for which the feature is not enabled.
218 Vector<String> messages;
219 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
220 policy1->setHeaderPolicy("{\"default-self\": [\"self\"]}", messages);
221 EXPECT_EQ(0UL, messages.size());
222 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
223 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originA);
224 EXPECT_FALSE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
225 EXPECT_FALSE(policy3->isFeatureEnabled(&kDefaultSelfFeature));
226 }
227
228 TEST_F(FeaturePolicyTest, TestSelectiveFrameInheritance) {
229 // +------------------------------------------+
230 // |(1) Origin A |
231 // |Policy: {"default-self": ["Origin B"]} |
232 // | +-----------------+ +-----------------+ |
233 // | |(2) Origin B | |(3) Origin C | |
234 // | |No Policy | |No Policy | |
235 // | | | | +-------------+ | |
236 // | | | | |(4)Origin B | | |
237 // | | | | |No Policy | | |
238 // | | | | +-------------+ | |
239 // | +-----------------+ +-----------------+ |
240 // +------------------------------------------+
241 // Feature should be enabled in second level Origin B frame, but disabled in
242 // Frame 4, because it is embedded by frame 3, where the feature is not
243 // enabled.
244 Vector<String> messages;
245 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
246 policy1->setHeaderPolicy("{\"default-self\": [\"" ORIGIN_B "\"]}", messages);
247 EXPECT_EQ(0UL, messages.size());
248 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
249 FeaturePolicy* policy3 = createFromParentPolicy(policy1, m_originC);
250 FeaturePolicy* policy4 = createFromParentPolicy(policy3, m_originB);
251 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
252 EXPECT_FALSE(policy3->isFeatureEnabled(&kDefaultSelfFeature));
253 EXPECT_FALSE(policy4->isFeatureEnabled(&kDefaultSelfFeature));
254 }
255
256 TEST_F(FeaturePolicyTest, TestPolicyCanBlockSelf) {
257 // +----------------------------+
258 // |(1)Origin A |
259 // |Policy: {"default-on": []} |
260 // +----------------------------+
261 // Default-on feature should be disabled in top-level frame.
262 Vector<String> messages;
263 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
264 policy1->setHeaderPolicy("{\"default-on\": []}", messages);
265 EXPECT_EQ(0UL, messages.size());
266 EXPECT_FALSE(policy1->isFeatureEnabled(&kDefaultOnFeature));
267 }
268
269 TEST_F(FeaturePolicyTest, TestParentPolicyBlocksSameOriginChildPolicy) {
270 // +----------------------------+
271 // |(1)Origin A |
272 // |Policy: {"default-on": []} |
273 // | +-------------+ |
274 // | |(2)Origin A | |
275 // | |No Policy | |
276 // | +-------------+ |
277 // +----------------------------+
278 // Feature should be disabled in child frame.
279 Vector<String> messages;
280 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
281 policy1->setHeaderPolicy("{\"default-on\": []}", messages);
282 EXPECT_EQ(0UL, messages.size());
283 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originA);
284 EXPECT_FALSE(policy2->isFeatureEnabled(&kDefaultOnFeature));
285 }
286
287 TEST_F(FeaturePolicyTest, TestChildPolicyCanBlockSelf) {
288 // +--------------------------------+
289 // |(1)Origin A |
290 // |No Policy |
291 // | +----------------------------+ |
292 // | |(2)Origin B | |
293 // | |Policy: {"default-on": []} | |
294 // | +----------------------------+ |
295 // +--------------------------------+
296 // Default-on feature should be disabled by cross-origin child frame.
297 Vector<String> messages;
298 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
299 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
300 policy2->setHeaderPolicy("{\"default-on\": []}", messages);
301 EXPECT_EQ(0UL, messages.size());
302 EXPECT_FALSE(policy2->isFeatureEnabled(&kDefaultOnFeature));
303 }
304
305 TEST_F(FeaturePolicyTest, TestChildPolicyCanBlockChildren) {
306 // +--------------------------------------+
307 // |(1)Origin A |
308 // |No Policy |
309 // | +----------------------------------+ |
310 // | |(2)Origin B | |
311 // | |Policy: {"default-on": ["self"]} | |
312 // | | +-------------+ | |
313 // | | |(3)Origin C | | |
314 // | | |No Policy | | |
315 // | | +-------------+ | |
316 // | +----------------------------------+ |
317 // +--------------------------------------+
318 // Default-on feature should be enabled in frames 1 and 2; disabled in frame
319 // 3 by child frame policy.
320 Vector<String> messages;
321 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
322 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
323 policy2->setHeaderPolicy("{\"default-on\": [\"self\"]}", messages);
324 EXPECT_EQ(0UL, messages.size());
325 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originC);
326 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultOnFeature));
327 EXPECT_FALSE(policy3->isFeatureEnabled(&kDefaultOnFeature));
328 }
329
330 TEST_F(FeaturePolicyTest, TestParentPolicyBlocksCrossOriginChildPolicy) {
331 // +----------------------------+
332 // |(1)Origin A |
333 // |Policy: {"default-on": []} |
334 // | +-------------+ |
335 // | |(2)Origin B | |
336 // | |No Policy | |
337 // | +-------------+ |
338 // +----------------------------+
339 // Default-on feature should be disabled in cross-origin child frame.
340 Vector<String> messages;
341 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
342 policy1->setHeaderPolicy("{\"default-on\": []}", messages);
343 EXPECT_EQ(0UL, messages.size());
344 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
345 EXPECT_FALSE(policy2->isFeatureEnabled(&kDefaultOnFeature));
346 }
347
348 TEST_F(FeaturePolicyTest, TestEnableForAllOrigins) {
349 // +--------------------------------+
350 // |(1) Origin A |
351 // |Policy: {"default-self": ["*"]} |
352 // | +-----------------+ |
353 // | |(2) Origin B | |
354 // | |No Policy | |
355 // | | +-------------+ | |
356 // | | |(3)Origin A | | |
357 // | | |No Policy | | |
358 // | | +-------------+ | |
359 // | +-----------------+ |
360 // +--------------------------------+
361 // Feature should be enabled in top and second level; disabled in frame 3.
362 Vector<String> messages;
363 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
364 policy1->setHeaderPolicy("{\"default-self\": [\"*\"]}", messages);
365 EXPECT_EQ(0UL, messages.size());
366 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
367 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originA);
368 EXPECT_TRUE(policy1->isFeatureEnabled(&kDefaultSelfFeature));
369 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
370 EXPECT_FALSE(policy3->isFeatureEnabled(&kDefaultSelfFeature));
371 }
372
373 TEST_F(FeaturePolicyTest, TestDefaultOnEnablesForAllAncestors) {
374 // +---------------------------------------+
375 // |(1) Origin A |
376 // |Policy: {"default-on": ["Origin B"]} |
377 // | +-----------------------------------+ |
378 // | |(2) Origin B | |
379 // | |No Policy | |
380 // | | +-------------+ +-------------+ | |
381 // | | |(3)Origin B | |(4)Origin C | | |
382 // | | |No Policy | |No Policy | | |
383 // | | +-------------+ +-------------+ | |
384 // | +-----------------------------------+ |
385 // +---------------------------------------+
386 // Feature should be disabled in frame 1; enabled in frames 2, 3 and 4.
387 Vector<String> messages;
388 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
389 policy1->setHeaderPolicy("{\"default-on\": [\"" ORIGIN_B "\"]}", messages);
390 EXPECT_EQ(0UL, messages.size());
391 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
392 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originB);
393 FeaturePolicy* policy4 = createFromParentPolicy(policy2, m_originC);
394 EXPECT_FALSE(policy1->isFeatureEnabled(&kDefaultOnFeature));
395 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultOnFeature));
396 EXPECT_TRUE(policy3->isFeatureEnabled(&kDefaultOnFeature));
397 EXPECT_TRUE(policy4->isFeatureEnabled(&kDefaultOnFeature));
398 }
399
400 TEST_F(FeaturePolicyTest, TestDefaultSelfRespectsSameOriginEmbedding) {
401 // +---------------------------------------+
402 // |(1) Origin A |
403 // |Policy: {"default-self": ["Origin B"]} |
404 // | +-----------------------------------+ |
405 // | |(2) Origin B | |
406 // | |No Policy | |
407 // | | +-------------+ +-------------+ | |
408 // | | |(3)Origin B | |(4)Origin C | | |
409 // | | |No Policy | |No Policy | | |
410 // | | +-------------+ +-------------+ | |
411 // | +-----------------------------------+ |
412 // +---------------------------------------+
413 // Feature should be disabled in frames 1 and 4; enabled in frames 2 and 3.
414 Vector<String> messages;
415 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
416 policy1->setHeaderPolicy("{\"default-self\": [\"" ORIGIN_B "\"]}", messages);
417 EXPECT_EQ(0UL, messages.size());
418 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
419 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originB);
420 FeaturePolicy* policy4 = createFromParentPolicy(policy2, m_originC);
421 EXPECT_FALSE(policy1->isFeatureEnabled(&kDefaultSelfFeature));
422 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
423 EXPECT_TRUE(policy3->isFeatureEnabled(&kDefaultSelfFeature));
424 EXPECT_FALSE(policy4->isFeatureEnabled(&kDefaultSelfFeature));
425 }
426
427 TEST_F(FeaturePolicyTest, TestDefaultOffMustBeDelegatedToAllCrossOriginFrames) {
428 // +---------------------------------------+
429 // |(1) Origin A |
430 // |Policy: {"default-off": ["Origin B"]} |
431 // | +-----------------------------------+ |
432 // | |(2) Origin B | |
433 // | |No Policy | |
434 // | | +-------------+ +-------------+ | |
435 // | | |(3)Origin B | |(4)Origin C | | |
436 // | | |No Policy | |No Policy | | |
437 // | | +-------------+ +-------------+ | |
438 // | +-----------------------------------+ |
439 // +---------------------------------------+
440 // Feature should be disabled in frames 1, 3 and 4; enabled in frame 2 only.
441 Vector<String> messages;
442 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
443 policy1->setHeaderPolicy("{\"default-off\": [\"" ORIGIN_B "\"]}", messages);
444 EXPECT_EQ(0UL, messages.size());
445 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
446 policy2->setHeaderPolicy("{\"default-off\": [\"self\"]}", messages);
447 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originB);
448 FeaturePolicy* policy4 = createFromParentPolicy(policy2, m_originC);
449 policy4->setHeaderPolicy("{\"default-off\": [\"self\"]}", messages);
450 EXPECT_FALSE(policy1->isFeatureEnabled(&kDefaultOffFeature));
451 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultOffFeature));
raymes 2016/10/23 23:31:05 Hmm, the code here doesn't seem to match what's in
iclelland 2016/10/24 05:14:32 Thanks, you're right -- I forgot to update the dia
452 EXPECT_FALSE(policy3->isFeatureEnabled(&kDefaultOffFeature));
453 EXPECT_FALSE(policy4->isFeatureEnabled(&kDefaultOffFeature));
454 }
455
456 TEST_F(FeaturePolicyTest, TestReenableForAllOrigins) {
457 // +------------------------------------+
458 // |(1) Origin A |
459 // |Policy: {"default-self": ["*"]} |
460 // | +--------------------------------+ |
461 // | |(2) Origin B | |
462 // | |Policy: {"default-self": ["*"]} | |
463 // | | +-------------+ | |
464 // | | |(3)Origin A | | |
465 // | | |No Policy | | |
466 // | | +-------------+ | |
467 // | +--------------------------------+ |
468 // +------------------------------------+
469 // Feature should be enabled in all frames.
470 Vector<String> messages;
471 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
472 policy1->setHeaderPolicy("{\"default-self\": [\"*\"]}", messages);
473 EXPECT_EQ(0UL, messages.size());
474 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
475 policy2->setHeaderPolicy("{\"default-self\": [\"*\"]}", messages);
476 EXPECT_EQ(0UL, messages.size());
477 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originA);
478 EXPECT_TRUE(policy1->isFeatureEnabled(&kDefaultSelfFeature));
479 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
480 EXPECT_TRUE(policy3->isFeatureEnabled(&kDefaultSelfFeature));
481 }
482
483 TEST_F(FeaturePolicyTest, TestBlockedFrameCannotReenable) {
484 // +--------------------------------------+
485 // |(1)Origin A |
486 // |Policy: {"default-self": ["self"]} |
487 // | +----------------------------------+ |
488 // | |(2)Origin B | |
489 // | |Policy: {"default-self": ["*"]} | |
490 // | | +-------------+ +-------------+ | |
491 // | | |(3)Origin A | |(4)Origin C | | |
492 // | | |No Policy | |No Policy | | |
493 // | | +-------------+ +-------------+ | |
494 // | +----------------------------------+ |
495 // +--------------------------------------+
496 // Feature should be enabled at the top level; disabled in all other frames.
497 Vector<String> messages;
498 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
499 policy1->setHeaderPolicy("{\"default-self\": [\"self\"]}", messages);
500 EXPECT_EQ(0UL, messages.size());
501 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
502 policy2->setHeaderPolicy("{\"default-self\": [\"*\"]}", messages);
503 EXPECT_EQ(0UL, messages.size());
504 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originA);
505 FeaturePolicy* policy4 = createFromParentPolicy(policy2, m_originC);
506 EXPECT_TRUE(policy1->isFeatureEnabled(&kDefaultSelfFeature));
507 EXPECT_FALSE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
508 EXPECT_FALSE(policy3->isFeatureEnabled(&kDefaultSelfFeature));
509 EXPECT_FALSE(policy4->isFeatureEnabled(&kDefaultSelfFeature));
510 }
511
512 TEST_F(FeaturePolicyTest, TestEnabledFrameCanDelegate) {
513 // +---------------------------------------------------+
514 // |(1) Origin A |
515 // |Policy: {"default-self": ["self", "Origin B"]} |
516 // | +-----------------------------------------------+ |
517 // | |(2) Origin B | |
518 // | |Policy: {"default-self": ["self", "Origin C"]} | |
519 // | | +-------------+ | |
520 // | | |(3)Origin C | | |
521 // | | |No Policy | | |
522 // | | +-------------+ | |
523 // | +-----------------------------------------------+ |
524 // +---------------------------------------------------+
525 // Feature should be enabled in all frames.
526 Vector<String> messages;
527 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
528 policy1->setHeaderPolicy("{\"default-self\": [\"self\", \"" ORIGIN_B "\"]}",
529 messages);
530 EXPECT_EQ(0UL, messages.size());
531 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
532 policy2->setHeaderPolicy("{\"default-self\": [\"self\", \"" ORIGIN_C "\"]}",
533 messages);
534 EXPECT_EQ(0UL, messages.size());
535 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originC);
536 EXPECT_TRUE(policy1->isFeatureEnabled(&kDefaultSelfFeature));
537 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
538 EXPECT_TRUE(policy3->isFeatureEnabled(&kDefaultSelfFeature));
539 }
540
541 TEST_F(FeaturePolicyTest, TestEnabledFrameCanDelegateByDefault) {
542 // +-----------------------------------------------+
543 // |(1) Origin A |
544 // |Policy: {"default-on": ["self", "Origin B"]} |
545 // | +--------------------+ +--------------------+ |
546 // | |(2) Origin B | | (4) Origin C | |
547 // | |No Policy | | No Policy | |
548 // | | +-------------+ | | | |
549 // | | |(3)Origin C | | | | |
550 // | | |No Policy | | | | |
551 // | | +-------------+ | | | |
552 // | +--------------------+ +--------------------+ |
553 // +-----------------------------------------------+
554 // Feature should be enabled in frames 1, 2, and 3, and disabled in frame 4.
555 Vector<String> messages;
556 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
557 policy1->setHeaderPolicy("{\"default-on\": [\"self\", \"" ORIGIN_B "\"]}",
558 messages);
559 EXPECT_EQ(0UL, messages.size());
560 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
561 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originC);
562 FeaturePolicy* policy4 = createFromParentPolicy(policy1, m_originC);
563 EXPECT_TRUE(policy1->isFeatureEnabled(&kDefaultOnFeature));
564 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultOnFeature));
565 EXPECT_TRUE(policy3->isFeatureEnabled(&kDefaultOnFeature));
566 EXPECT_FALSE(policy4->isFeatureEnabled(&kDefaultOnFeature));
567 }
568
569 TEST_F(FeaturePolicyTest, TestNonNestedFeaturesDontDelegateByDefault) {
570 // +-----------------------------------------------+
571 // |(1) Origin A |
572 // |Policy: {"default-self": ["self", "Origin B"]} |
573 // | +--------------------+ +--------------------+ |
574 // | |(2) Origin B | | (4) Origin C | |
575 // | |No Policy | | No Policy | |
576 // | | +-------------+ | | | |
577 // | | |(3)Origin C | | | | |
578 // | | |No Policy | | | | |
579 // | | +-------------+ | | | |
580 // | +--------------------+ +--------------------+ |
581 // +-----------------------------------------------+
582 // Feature should be enabled in frames 1 and 2, and disabled in frames 3 and
583 // 4.
584 Vector<String> messages;
585 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
586 policy1->setHeaderPolicy("{\"default-self\": [\"self\", \"" ORIGIN_B "\"]}",
587 messages);
588 EXPECT_EQ(0UL, messages.size());
589 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
590 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originC);
591 FeaturePolicy* policy4 = createFromParentPolicy(policy1, m_originC);
592 EXPECT_TRUE(policy1->isFeatureEnabled(&kDefaultSelfFeature));
593 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
594 EXPECT_FALSE(policy3->isFeatureEnabled(&kDefaultSelfFeature));
595 EXPECT_FALSE(policy4->isFeatureEnabled(&kDefaultSelfFeature));
596 }
597
598 TEST_F(FeaturePolicyTest, TestFeaturesAreIndependent) {
599 // +-----------------------------------------------+
600 // |(1) Origin A |
601 // |Policy: {"default-self": ["self", "Origin B"], |
602 // | "default-on": ["self"]} |
603 // | +-------------------------------------------+ |
604 // | |(2) Origin B | |
605 // | |Policy: {"default-self": ["*"], | |
606 // | | "default-on": ["*"]} | |
607 // | | +-------------+ | |
608 // | | |(3)Origin C | | |
609 // | | |No Policy | | |
610 // | | +-------------+ | |
611 // | +-------------------------------------------+ |
612 // +-----------------------------------------------+
613 // Vibrate feature should be enabled in all frames; Docwrite feature
614 // should be enabled in frame 1, and disabled in frames 2 and 3.
615 Vector<String> messages;
616 FeaturePolicy* policy1 = createFromParentPolicy(nullptr, m_originA);
617 policy1->setHeaderPolicy("{\"default-self\": [\"self\", \"" ORIGIN_B
618 "\"], \"default-on\": [\"self\"]}",
619 messages);
620 EXPECT_EQ(0UL, messages.size());
621 FeaturePolicy* policy2 = createFromParentPolicy(policy1, m_originB);
622 policy2->setHeaderPolicy(
623 "{\"default-self\": [\"*\"], \"default-on\": [\"*\"]}", messages);
624 EXPECT_EQ(0UL, messages.size());
625 FeaturePolicy* policy3 = createFromParentPolicy(policy2, m_originC);
626 EXPECT_TRUE(policy1->isFeatureEnabled(&kDefaultSelfFeature));
627 EXPECT_TRUE(policy1->isFeatureEnabled(&kDefaultOnFeature));
628 EXPECT_TRUE(policy2->isFeatureEnabled(&kDefaultSelfFeature));
629 EXPECT_FALSE(policy2->isFeatureEnabled(&kDefaultOnFeature));
630 EXPECT_TRUE(policy3->isFeatureEnabled(&kDefaultSelfFeature));
631 EXPECT_FALSE(policy3->isFeatureEnabled(&kDefaultOnFeature));
632 }
633
634 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698