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

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

Issue 1530393003: WIP: Move 'X-Frame-Options' checking to the browser. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Ugh. Created 4 years, 11 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.
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/xfo_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* GetXFOHeader(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
34 // XFOThrottleTest -------------------------------------------------------------
35
36 using XFOThrottleTest = testing::Test;
37
38 TEST_F(XFOThrottleTest, Parsing) {
39 struct TestCase {
40 const char* header;
41 XFOThrottle::HeaderDisposition expected;
42 const char* value;
43 } cases[] = {
44 // Basic keywords
45 {"DENY", XFOThrottle::DENY, "DENY"},
46 {"SAMEORIGIN", XFOThrottle::SAMEORIGIN, "SAMEORIGIN"},
47 {"ALLOWALL", XFOThrottle::ALLOWALL, "ALLOWALL"},
48
49 // Repeated keywords
50 {"DENY,DENY", XFOThrottle::DENY, "DENY, DENY"},
51 {"SAMEORIGIN,SAMEORIGIN", XFOThrottle::SAMEORIGIN,
52 "SAMEORIGIN, SAMEORIGIN"},
53 {"ALLOWALL,ALLOWALL", XFOThrottle::ALLOWALL, "ALLOWALL, ALLOWALL"},
54
55 // Case-insensitive
56 {"deNy", XFOThrottle::DENY, "deNy"},
57 {"sAmEorIgIn", XFOThrottle::SAMEORIGIN, "sAmEorIgIn"},
58 {"AlLOWaLL", XFOThrottle::ALLOWALL, "AlLOWaLL"},
59
60 // Trim whitespace
61 {" DENY", XFOThrottle::DENY, "DENY"},
62 {"SAMEORIGIN ", XFOThrottle::SAMEORIGIN, "SAMEORIGIN"},
63 {" ALLOWALL ", XFOThrottle::ALLOWALL, "ALLOWALL"},
64 {" DENY", XFOThrottle::DENY, "DENY"},
65 {"SAMEORIGIN ", XFOThrottle::SAMEORIGIN, "SAMEORIGIN"},
66 {" ALLOWALL ", XFOThrottle::ALLOWALL, "ALLOWALL"},
67 {" DENY , DENY ", XFOThrottle::DENY, "DENY, DENY"},
68 {"SAMEORIGIN, SAMEORIGIN", XFOThrottle::SAMEORIGIN,
69 "SAMEORIGIN, SAMEORIGIN"},
70 {"ALLOWALL ,ALLOWALL", XFOThrottle::ALLOWALL, "ALLOWALL, ALLOWALL"},
71 };
72
73 for (const auto& test : cases) {
74 SCOPED_TRACE(test.header);
75 scoped_refptr<net::HttpResponseHeaders> headers = GetXFOHeader(test.header);
76 std::string header_value;
77 EXPECT_EQ(test.expected,
78 XFOThrottle::ParseHeader(headers.get(), &header_value));
79 EXPECT_EQ(test.value, header_value);
80 }
81 }
82
83 TEST_F(XFOThrottleTest, ParseErrors) {
84 struct TestCase {
85 const char* header;
86 XFOThrottle::HeaderDisposition expected;
87 const char* failure;
88 } cases[] = {
89 // Empty == Invalid.
90 {"", XFOThrottle::INVALID, ""},
91
92 // Invalid
93 {"INVALID", XFOThrottle::INVALID, "INVALID"},
94 {"INVALID DENY", XFOThrottle::INVALID, "INVALID DENY"},
95 {"DENY DENY", XFOThrottle::INVALID, "DENY DENY"},
96 {"DE NY", XFOThrottle::INVALID, "DE NY"},
97
98 // Conflicts
99 {"INVALID,DENY", XFOThrottle::CONFLICT, "INVALID, DENY"},
100 {"DENY,ALLOWALL", XFOThrottle::CONFLICT, "DENY, ALLOWALL"},
101 {"SAMEORIGIN,DENY", XFOThrottle::CONFLICT, "SAMEORIGIN, DENY"},
102 {"ALLOWALL,SAMEORIGIN", XFOThrottle::CONFLICT, "ALLOWALL, SAMEORIGIN"},
103 {"DENY, SAMEORIGIN", XFOThrottle::CONFLICT, "DENY, SAMEORIGIN"}};
104
105 for (const auto& test : cases) {
106 SCOPED_TRACE(test.header);
107 scoped_refptr<net::HttpResponseHeaders> headers = GetXFOHeader(test.header);
108 std::string header_value;
109 EXPECT_EQ(test.expected,
110 XFOThrottle::ParseHeader(headers.get(), &header_value));
111 EXPECT_EQ(test.failure, header_value);
112 }
113 }
114
115 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698