Index: content/browser/frame_host/xfo_throttle_unittest.cc |
diff --git a/content/browser/frame_host/xfo_throttle_unittest.cc b/content/browser/frame_host/xfo_throttle_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..14e93a0ccfab9ba700b286899dc5d8a2de736a65 |
--- /dev/null |
+++ b/content/browser/frame_host/xfo_throttle_unittest.cc |
@@ -0,0 +1,115 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/strings/stringprintf.h" |
+#include "content/browser/frame_host/xfo_throttle.h" |
+#include "content/public/browser/navigation_handle.h" |
+#include "content/public/browser/navigation_throttle.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/test/test_renderer_host.h" |
+#include "net/http/http_response_headers.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+net::HttpResponseHeaders* GetXFOHeader(const char* test) { |
+ std::string header_string("HTTP/1.1 200 OK\nX-Frame-Options: "); |
+ header_string += test; |
+ header_string += "\n\n"; |
+ std::replace(header_string.begin(), header_string.end(), '\n', '\0'); |
+ net::HttpResponseHeaders* headers = |
+ new net::HttpResponseHeaders(header_string); |
+ EXPECT_TRUE(headers->HasHeader("X-Frame-Options")); |
+ return headers; |
+} |
+}; |
+ |
+// XFOThrottleTest ------------------------------------------------------------- |
+ |
+using XFOThrottleTest = testing::Test; |
+ |
+TEST_F(XFOThrottleTest, Parsing) { |
+ struct TestCase { |
+ const char* header; |
+ XFOThrottle::HeaderDisposition expected; |
+ const char* value; |
+ } cases[] = { |
+ // Basic keywords |
+ {"DENY", XFOThrottle::DENY, "DENY"}, |
+ {"SAMEORIGIN", XFOThrottle::SAMEORIGIN, "SAMEORIGIN"}, |
+ {"ALLOWALL", XFOThrottle::ALLOWALL, "ALLOWALL"}, |
+ |
+ // Repeated keywords |
+ {"DENY,DENY", XFOThrottle::DENY, "DENY, DENY"}, |
+ {"SAMEORIGIN,SAMEORIGIN", XFOThrottle::SAMEORIGIN, |
+ "SAMEORIGIN, SAMEORIGIN"}, |
+ {"ALLOWALL,ALLOWALL", XFOThrottle::ALLOWALL, "ALLOWALL, ALLOWALL"}, |
+ |
+ // Case-insensitive |
+ {"deNy", XFOThrottle::DENY, "deNy"}, |
+ {"sAmEorIgIn", XFOThrottle::SAMEORIGIN, "sAmEorIgIn"}, |
+ {"AlLOWaLL", XFOThrottle::ALLOWALL, "AlLOWaLL"}, |
+ |
+ // Trim whitespace |
+ {" DENY", XFOThrottle::DENY, "DENY"}, |
+ {"SAMEORIGIN ", XFOThrottle::SAMEORIGIN, "SAMEORIGIN"}, |
+ {" ALLOWALL ", XFOThrottle::ALLOWALL, "ALLOWALL"}, |
+ {" DENY", XFOThrottle::DENY, "DENY"}, |
+ {"SAMEORIGIN ", XFOThrottle::SAMEORIGIN, "SAMEORIGIN"}, |
+ {" ALLOWALL ", XFOThrottle::ALLOWALL, "ALLOWALL"}, |
+ {" DENY , DENY ", XFOThrottle::DENY, "DENY, DENY"}, |
+ {"SAMEORIGIN, SAMEORIGIN", XFOThrottle::SAMEORIGIN, |
+ "SAMEORIGIN, SAMEORIGIN"}, |
+ {"ALLOWALL ,ALLOWALL", XFOThrottle::ALLOWALL, "ALLOWALL, ALLOWALL"}, |
+ }; |
+ |
+ for (const auto& test : cases) { |
+ SCOPED_TRACE(test.header); |
+ scoped_refptr<net::HttpResponseHeaders> headers = GetXFOHeader(test.header); |
+ std::string header_value; |
+ EXPECT_EQ(test.expected, |
+ XFOThrottle::ParseHeader(headers.get(), &header_value)); |
+ EXPECT_EQ(test.value, header_value); |
+ } |
+} |
+ |
+TEST_F(XFOThrottleTest, ParseErrors) { |
+ struct TestCase { |
+ const char* header; |
+ XFOThrottle::HeaderDisposition expected; |
+ const char* failure; |
+ } cases[] = { |
+ // Empty == Invalid. |
+ {"", XFOThrottle::INVALID, ""}, |
+ |
+ // Invalid |
+ {"INVALID", XFOThrottle::INVALID, "INVALID"}, |
+ {"INVALID DENY", XFOThrottle::INVALID, "INVALID DENY"}, |
+ {"DENY DENY", XFOThrottle::INVALID, "DENY DENY"}, |
+ {"DE NY", XFOThrottle::INVALID, "DE NY"}, |
+ |
+ // Conflicts |
+ {"INVALID,DENY", XFOThrottle::CONFLICT, "INVALID, DENY"}, |
+ {"DENY,ALLOWALL", XFOThrottle::CONFLICT, "DENY, ALLOWALL"}, |
+ {"SAMEORIGIN,DENY", XFOThrottle::CONFLICT, "SAMEORIGIN, DENY"}, |
+ {"ALLOWALL,SAMEORIGIN", XFOThrottle::CONFLICT, "ALLOWALL, SAMEORIGIN"}, |
+ {"DENY, SAMEORIGIN", XFOThrottle::CONFLICT, "DENY, SAMEORIGIN"}}; |
+ |
+ for (const auto& test : cases) { |
+ SCOPED_TRACE(test.header); |
+ scoped_refptr<net::HttpResponseHeaders> headers = GetXFOHeader(test.header); |
+ std::string header_value; |
+ EXPECT_EQ(test.expected, |
+ XFOThrottle::ParseHeader(headers.get(), &header_value)); |
+ EXPECT_EQ(test.failure, header_value); |
+ } |
+} |
+ |
+} // namespace content |