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

Side by Side Diff: content/common/feature_policy/feature_policy_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698