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

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: Moar. 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, "SAMEORIGIN, SAMEORIGIN "},
52 {"ALLOWALL,ALLOWALL", XFOThrottle::ALLOWALL, "ALLOWALL, ALLOWALL"},
53
54 // Case-insensitive
55 {"deNy", XFOThrottle::DENY, "deNy"},
56 {"sAmEorIgIn", XFOThrottle::SAMEORIGIN, "sAmEorIgIn"},
57 {"AlLOWaLL", XFOThrottle::ALLOWALL, "AlLOWaLL"},
58
59 // Trim whitespace
60 {" DENY", XFOThrottle::DENY, "DENY"},
61 {"SAMEORIGIN ", XFOThrottle::SAMEORIGIN, "SAMEORIGIN"},
62 {" ALLOWALL ", XFOThrottle::ALLOWALL, "ALLOWALL"},
63 {" DENY", XFOThrottle::DENY, "DENY"},
64 {"SAMEORIGIN ", XFOThrottle::SAMEORIGIN, "SAMEORIGIN"},
65 {" ALLOWALL ", XFOThrottle::ALLOWALL, "ALLOWALL"},
66 {" DENY , DENY ", XFOThrottle::DENY, "DENY, DENY"},
67 {"SAMEORIGIN, SAMEORIGIN", XFOThrottle::SAMEORIGIN, "SAMEORIGIN, SAMEORIG IN"},
68 {"ALLOWALL ,ALLOWALL", XFOThrottle::ALLOWALL, "ALLOWALL, ALLOWALL"},
69 };
70
71 for (const auto& test : cases) {
72 SCOPED_TRACE(test.header);
73 scoped_refptr<net::HttpResponseHeaders> headers = GetXFOHeader(test.header);
74 std::string header_value;
75 EXPECT_EQ(test.expected,
76 XFOThrottle::ParseHeader(headers.get(), &header_value));
77 EXPECT_EQ(test.value, header_value);
78 }
79 }
80
81 TEST_F(XFOThrottleTest, ParseErrors) {
82 struct TestCase {
83 const char* header;
84 XFOThrottle::HeaderDisposition expected;
85 const char* failure;
86 } cases[] = {// Empty == Invalid.
87 {"", XFOThrottle::INVALID, ""},
88
89 // Invalid
90 {"INVALID", XFOThrottle::INVALID, "INVALID"},
91 {"INVALID DENY", XFOThrottle::INVALID, "INVALID DENY"},
92 {"DENY DENY", XFOThrottle::INVALID, "DENY DENY"},
93 {"DE NY", XFOThrottle::INVALID, "DE NY"},
94
95 // Conflicts
96 {"INVALID,DENY", XFOThrottle::CONFLICT, "INVALID, DENY"},
97 {"DENY,ALLOWALL", XFOThrottle::CONFLICT, "DENY, ALLOWALL"},
98 {"SAMEORIGIN,DENY", XFOThrottle::CONFLICT, "SAMEORIGIN, DENY"},
99 {"ALLOWALL,SAMEORIGIN", XFOThrottle::CONFLICT, "ALLOWALL, SAMEORI GIN"},
100 {"DENY, SAMEORIGIN", XFOThrottle::CONFLICT, "DENY, SAMEORIGIN"}} ;
101
102 for (const auto& test : cases) {
103 SCOPED_TRACE(test.header);
104 scoped_refptr<net::HttpResponseHeaders> headers = GetXFOHeader(test.header);
105 std::string header_value;
106 EXPECT_EQ(test.expected,
107 XFOThrottle::ParseHeader(headers.get(), &header_value));
108 EXPECT_EQ(test.failure, header_value);
109 }
110 }
111
112 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698