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

Side by Side Diff: third_party/WebKit/Source/core/frame/csp/CSPSourceTest.cpp

Issue 2452903004: Part 2.2: Is policy list subsumed under subsuming policy? (Closed)
Patch Set: Rebasing Created 4 years, 1 month 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "core/frame/csp/CSPSource.h" 5 #include "core/frame/csp/CSPSource.h"
6 6
7 #include "core/dom/Document.h" 7 #include "core/dom/Document.h"
8 #include "core/frame/csp/ContentSecurityPolicy.h" 8 #include "core/frame/csp/ContentSecurityPolicy.h"
9 #include "platform/network/ResourceRequest.h" 9 #include "platform/network/ResourceRequest.h"
10 #include "platform/weborigin/KURL.h" 10 #include "platform/weborigin/KURL.h"
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 CSPSource* required = new CSPSource( 438 CSPSource* required = new CSPSource(
439 csp.get(), test.b.scheme, test.b.host, test.b.port, test.b.path, 439 csp.get(), test.b.scheme, test.b.host, test.b.port, test.b.path,
440 CSPSource::NoWildcard, CSPSource::NoWildcard); 440 CSPSource::NoWildcard, CSPSource::NoWildcard);
441 441
442 EXPECT_EQ(returned->isSimilar(required), test.isSimilar); 442 EXPECT_EQ(returned->isSimilar(required), test.isSimilar);
443 // Verify the same test with a and b swapped. 443 // Verify the same test with a and b swapped.
444 EXPECT_EQ(required->isSimilar(returned), test.isSimilar); 444 EXPECT_EQ(required->isSimilar(returned), test.isSimilar);
445 } 445 }
446 } 446 }
447 447
448 TEST_F(CSPSourceTest, FirstSubsumesSecond) {
449 struct Source {
450 const char* scheme;
451 const char* host;
452 const int port;
453 const char* path;
454 };
455 struct TestCase {
456 const Source sourceB;
457 String schemeA;
458 bool expected;
459 } cases[] = {
460 // Subsumed.
461 {{"http", "example.com", 0, "/"}, "http", true},
462 {{"http", "example.com", 0, "/page.html"}, "http", true},
463 {{"http", "second-example.com", 80, "/"}, "http", true},
464 {{"https", "second-example.com", 0, "/"}, "http", true},
465 {{"http", "second-example.com", 0, "/page.html"}, "http", true},
466 {{"https", "second-example.com", 80, "/page.html"}, "http", true},
467 {{"https", "second-example.com", 0, "/"}, "https", true},
468 {{"https", "second-example.com", 0, "/page.html"}, "https", true},
469 {{"http", "example.com", 900, "/"}, "http", true},
470 // NOT subsumed.
471 {{"http", "second-example.com", 0, "/"}, "wss", false},
472 {{"http", "non-example.com", 900, "/"}, "http", false},
473 {{"http", "second-example.com", 0, "/"}, "https", false},
474 };
475
476 CSPSource* noWildcards =
477 new CSPSource(csp.get(), "http", "example.com", 0, "/",
478 CSPSource::NoWildcard, CSPSource::NoWildcard);
479 CSPSource* hostWildcard =
480 new CSPSource(csp.get(), "http", "third-example.com", 0, "/",
481 CSPSource::HasWildcard, CSPSource::NoWildcard);
482 CSPSource* portWildcard =
483 new CSPSource(csp.get(), "http", "third-example.com", 0, "/",
484 CSPSource::NoWildcard, CSPSource::HasWildcard);
485 CSPSource* bothWildcards =
486 new CSPSource(csp.get(), "http", "third-example.com", 0, "/",
487 CSPSource::HasWildcard, CSPSource::HasWildcard);
488 CSPSource* httpOnly =
489 new CSPSource(csp.get(), "http", "", 0, "", CSPSource::NoWildcard,
490 CSPSource::NoWildcard);
491 CSPSource* httpsOnly =
492 new CSPSource(csp.get(), "https", "", 0, "", CSPSource::NoWildcard,
493 CSPSource::NoWildcard);
494
495 for (const auto& test : cases) {
496 // Setup default vectors.
497 HeapVector<Member<CSPSource>> listA;
498 HeapVector<Member<CSPSource>> listB;
499 listB.append(noWildcards);
500 // Empty `listA` implies `none` is allowed.
501 EXPECT_FALSE(CSPSource::firstSubsumesSecond(listA, listB));
502
503 listA.append(noWildcards);
504 // Add CSPSources based on the current test.
505 listB.append(new CSPSource(csp.get(), test.sourceB.scheme,
506 test.sourceB.host, 0, test.sourceB.path,
507 CSPSource::NoWildcard, CSPSource::NoWildcard));
508 listA.append(new CSPSource(csp.get(), test.schemeA, "second-example.com", 0,
509 "/", CSPSource::NoWildcard,
510 CSPSource::NoWildcard));
511 // listB contains: ["http://example.com/", test.listB]
512 // listA contains: ["http://example.com/",
513 // test.schemeA + "://second-example.com/"]
514 EXPECT_EQ(test.expected, CSPSource::firstSubsumesSecond(listA, listB));
515
516 // If we add another source to `listB` with a host wildcard,
517 // then the result should definitely be false.
518 listB.append(hostWildcard);
519
520 // If we add another source to `listA` with a port wildcard,
521 // it does not make `listB` to be subsumed under `listA`.
522 listB.append(portWildcard);
523 EXPECT_FALSE(CSPSource::firstSubsumesSecond(listA, listB));
524
525 // If however we add another source to `listA` with both wildcards,
526 // that CSPSource is subsumed, so the answer should be as expected
527 // before.
528 listA.append(bothWildcards);
529 EXPECT_EQ(test.expected, CSPSource::firstSubsumesSecond(listA, listB));
530
531 // If we add a scheme-source expression of 'https' to `listB`, then it
532 // should not be subsumed.
533 listB.append(httpsOnly);
534 EXPECT_FALSE(CSPSource::firstSubsumesSecond(listA, listB));
535
536 // If we add a scheme-source expression of 'http' to `listA`, then it should
537 // subsume all current epxression in `listB`.
538 listA.append(httpOnly);
539 EXPECT_TRUE(CSPSource::firstSubsumesSecond(listA, listB));
540 }
541 }
542
448 } // namespace blink 543 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/frame/csp/CSPSource.cpp ('k') | third_party/WebKit/Source/core/frame/csp/SourceListDirective.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698