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

Side by Side Diff: content/browser/frame_host/ancestor_throttle_unittest.cc

Issue 1617043002: Introduce AncestorThrottle, which will process 'X-Frame-Options' headers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@block-response
Patch Set: Fix. Created 4 years, 8 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 2015 The Chromium Authors. All rights reserved.
nasko 2016/04/12 22:21:19 2016? :)
Mike West 2016/04/13 13:28:08 2015! 2015 FOREVER!
nasko 2016/04/29 18:56:02 I understand you really like it, but let's ensure
Mike West 2016/05/02 09:37:51 *sigh* FINE.
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 "base/bind.h"
6 #include "base/bind_helpers.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/stringprintf.h"
9 #include "content/browser/frame_host/ancestor_throttle.h"
10 #include "content/public/browser/navigation_handle.h"
11 #include "content/public/browser/navigation_throttle.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/test/test_renderer_host.h"
14 #include "net/http/http_response_headers.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace content {
19
20 namespace {
21
22 net::HttpResponseHeaders* GetAncestorHeader(const char* test) {
23 std::string header_string("HTTP/1.1 200 OK\nX-Frame-Options: ");
24 header_string += test;
25 header_string += "\n\n";
26 std::replace(header_string.begin(), header_string.end(), '\n', '\0');
27 net::HttpResponseHeaders* headers =
28 new net::HttpResponseHeaders(header_string);
29 EXPECT_TRUE(headers->HasHeader("X-Frame-Options"));
30 return headers;
31 }
32
33 } // namespace
34
35 // AncestorThrottleTest
36 // -------------------------------------------------------------
37
38 class AncestorThrottleTest : public testing::Test {};
39
40 TEST_F(AncestorThrottleTest, Parsing) {
41 struct TestCase {
42 const char* header;
43 AncestorThrottle::HeaderDisposition expected;
44 const char* value;
45 } cases[] = {
46 // Basic keywords
47 {"DENY", AncestorThrottle::DENY, "DENY"},
48 {"SAMEORIGIN", AncestorThrottle::SAMEORIGIN, "SAMEORIGIN"},
49 {"ALLOWALL", AncestorThrottle::ALLOWALL, "ALLOWALL"},
50
51 // Repeated keywords
52 {"DENY,DENY", AncestorThrottle::DENY, "DENY, DENY"},
53 {"SAMEORIGIN,SAMEORIGIN", AncestorThrottle::SAMEORIGIN,
54 "SAMEORIGIN, SAMEORIGIN"},
55 {"ALLOWALL,ALLOWALL", AncestorThrottle::ALLOWALL, "ALLOWALL, ALLOWALL"},
56
57 // Case-insensitive
58 {"deNy", AncestorThrottle::DENY, "deNy"},
59 {"sAmEorIgIn", AncestorThrottle::SAMEORIGIN, "sAmEorIgIn"},
60 {"AlLOWaLL", AncestorThrottle::ALLOWALL, "AlLOWaLL"},
61
62 // Trim whitespace
63 {" DENY", AncestorThrottle::DENY, "DENY"},
64 {"SAMEORIGIN ", AncestorThrottle::SAMEORIGIN, "SAMEORIGIN"},
65 {" ALLOWALL ", AncestorThrottle::ALLOWALL, "ALLOWALL"},
66 {" DENY", AncestorThrottle::DENY, "DENY"},
67 {"SAMEORIGIN ", AncestorThrottle::SAMEORIGIN, "SAMEORIGIN"},
68 {" ALLOWALL ", AncestorThrottle::ALLOWALL, "ALLOWALL"},
69 {" DENY , DENY ", AncestorThrottle::DENY, "DENY, DENY"},
70 {"SAMEORIGIN, SAMEORIGIN", AncestorThrottle::SAMEORIGIN,
71 "SAMEORIGIN, SAMEORIGIN"},
72 {"ALLOWALL ,ALLOWALL", AncestorThrottle::ALLOWALL, "ALLOWALL, ALLOWALL"},
73 };
74
75 AncestorThrottle throttle(nullptr);
76 for (const auto& test : cases) {
77 SCOPED_TRACE(test.header);
78 scoped_refptr<net::HttpResponseHeaders> headers =
79 GetAncestorHeader(test.header);
80 std::string header_value;
81 EXPECT_EQ(test.expected,
82 throttle.ParseHeader(headers.get(), &header_value));
83 EXPECT_EQ(test.value, header_value);
84 }
85 }
86
87 TEST_F(AncestorThrottleTest, ParseErrors) {
88 struct TestCase {
89 const char* header;
90 AncestorThrottle::HeaderDisposition expected;
91 const char* failure;
92 } cases[] = {
93 // Empty == Invalid.
94 {"", AncestorThrottle::INVALID, ""},
95
96 // Invalid
97 {"INVALID", AncestorThrottle::INVALID, "INVALID"},
98 {"INVALID DENY", AncestorThrottle::INVALID, "INVALID DENY"},
99 {"DENY DENY", AncestorThrottle::INVALID, "DENY DENY"},
100 {"DE NY", AncestorThrottle::INVALID, "DE NY"},
101
102 // Conflicts
103 {"INVALID,DENY", AncestorThrottle::CONFLICT, "INVALID, DENY"},
104 {"DENY,ALLOWALL", AncestorThrottle::CONFLICT, "DENY, ALLOWALL"},
105 {"SAMEORIGIN,DENY", AncestorThrottle::CONFLICT, "SAMEORIGIN, DENY"},
106 {"ALLOWALL,SAMEORIGIN", AncestorThrottle::CONFLICT,
107 "ALLOWALL, SAMEORIGIN"},
108 {"DENY, SAMEORIGIN", AncestorThrottle::CONFLICT, "DENY, SAMEORIGIN"}};
109
110 AncestorThrottle throttle(nullptr);
111 for (const auto& test : cases) {
112 SCOPED_TRACE(test.header);
113 scoped_refptr<net::HttpResponseHeaders> headers =
114 GetAncestorHeader(test.header);
115 std::string header_value;
116 EXPECT_EQ(test.expected,
117 throttle.ParseHeader(headers.get(), &header_value));
118 EXPECT_EQ(test.failure, header_value);
119 }
120 }
121
122 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698