Chromium Code Reviews| Index: content/browser/frame_host/ancestor_throttle_unittest.cc |
| diff --git a/content/browser/frame_host/ancestor_throttle_unittest.cc b/content/browser/frame_host/ancestor_throttle_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2c18f3f3552215942f28e7bd47d72d936b467bd9 |
| --- /dev/null |
| +++ b/content/browser/frame_host/ancestor_throttle_unittest.cc |
| @@ -0,0 +1,122 @@ |
| +// 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.
|
| +// 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/ancestor_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* GetAncestorHeader(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; |
| +} |
| + |
| +} // namespace |
| + |
| +// AncestorThrottleTest |
| +// ------------------------------------------------------------- |
| + |
| +class AncestorThrottleTest : public testing::Test {}; |
| + |
| +TEST_F(AncestorThrottleTest, Parsing) { |
| + struct TestCase { |
| + const char* header; |
| + AncestorThrottle::HeaderDisposition expected; |
| + const char* value; |
| + } cases[] = { |
| + // Basic keywords |
| + {"DENY", AncestorThrottle::DENY, "DENY"}, |
| + {"SAMEORIGIN", AncestorThrottle::SAMEORIGIN, "SAMEORIGIN"}, |
| + {"ALLOWALL", AncestorThrottle::ALLOWALL, "ALLOWALL"}, |
| + |
| + // Repeated keywords |
| + {"DENY,DENY", AncestorThrottle::DENY, "DENY, DENY"}, |
| + {"SAMEORIGIN,SAMEORIGIN", AncestorThrottle::SAMEORIGIN, |
| + "SAMEORIGIN, SAMEORIGIN"}, |
| + {"ALLOWALL,ALLOWALL", AncestorThrottle::ALLOWALL, "ALLOWALL, ALLOWALL"}, |
| + |
| + // Case-insensitive |
| + {"deNy", AncestorThrottle::DENY, "deNy"}, |
| + {"sAmEorIgIn", AncestorThrottle::SAMEORIGIN, "sAmEorIgIn"}, |
| + {"AlLOWaLL", AncestorThrottle::ALLOWALL, "AlLOWaLL"}, |
| + |
| + // Trim whitespace |
| + {" DENY", AncestorThrottle::DENY, "DENY"}, |
| + {"SAMEORIGIN ", AncestorThrottle::SAMEORIGIN, "SAMEORIGIN"}, |
| + {" ALLOWALL ", AncestorThrottle::ALLOWALL, "ALLOWALL"}, |
| + {" DENY", AncestorThrottle::DENY, "DENY"}, |
| + {"SAMEORIGIN ", AncestorThrottle::SAMEORIGIN, "SAMEORIGIN"}, |
| + {" ALLOWALL ", AncestorThrottle::ALLOWALL, "ALLOWALL"}, |
| + {" DENY , DENY ", AncestorThrottle::DENY, "DENY, DENY"}, |
| + {"SAMEORIGIN, SAMEORIGIN", AncestorThrottle::SAMEORIGIN, |
| + "SAMEORIGIN, SAMEORIGIN"}, |
| + {"ALLOWALL ,ALLOWALL", AncestorThrottle::ALLOWALL, "ALLOWALL, ALLOWALL"}, |
| + }; |
| + |
| + AncestorThrottle throttle(nullptr); |
| + for (const auto& test : cases) { |
| + SCOPED_TRACE(test.header); |
| + scoped_refptr<net::HttpResponseHeaders> headers = |
| + GetAncestorHeader(test.header); |
| + std::string header_value; |
| + EXPECT_EQ(test.expected, |
| + throttle.ParseHeader(headers.get(), &header_value)); |
| + EXPECT_EQ(test.value, header_value); |
| + } |
| +} |
| + |
| +TEST_F(AncestorThrottleTest, ParseErrors) { |
| + struct TestCase { |
| + const char* header; |
| + AncestorThrottle::HeaderDisposition expected; |
| + const char* failure; |
| + } cases[] = { |
| + // Empty == Invalid. |
| + {"", AncestorThrottle::INVALID, ""}, |
| + |
| + // Invalid |
| + {"INVALID", AncestorThrottle::INVALID, "INVALID"}, |
| + {"INVALID DENY", AncestorThrottle::INVALID, "INVALID DENY"}, |
| + {"DENY DENY", AncestorThrottle::INVALID, "DENY DENY"}, |
| + {"DE NY", AncestorThrottle::INVALID, "DE NY"}, |
| + |
| + // Conflicts |
| + {"INVALID,DENY", AncestorThrottle::CONFLICT, "INVALID, DENY"}, |
| + {"DENY,ALLOWALL", AncestorThrottle::CONFLICT, "DENY, ALLOWALL"}, |
| + {"SAMEORIGIN,DENY", AncestorThrottle::CONFLICT, "SAMEORIGIN, DENY"}, |
| + {"ALLOWALL,SAMEORIGIN", AncestorThrottle::CONFLICT, |
| + "ALLOWALL, SAMEORIGIN"}, |
| + {"DENY, SAMEORIGIN", AncestorThrottle::CONFLICT, "DENY, SAMEORIGIN"}}; |
| + |
| + AncestorThrottle throttle(nullptr); |
| + for (const auto& test : cases) { |
| + SCOPED_TRACE(test.header); |
| + scoped_refptr<net::HttpResponseHeaders> headers = |
| + GetAncestorHeader(test.header); |
| + std::string header_value; |
| + EXPECT_EQ(test.expected, |
| + throttle.ParseHeader(headers.get(), &header_value)); |
| + EXPECT_EQ(test.failure, header_value); |
| + } |
| +} |
| + |
| +} // namespace content |