OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "public/platform/WebDocumentSubresourceFilter.h" |
| 6 |
| 7 #include "core/dom/Element.h" |
| 8 #include "core/html/HTMLImageElement.h" |
| 9 #include "platform/testing/URLTestHelpers.h" |
| 10 #include "public/platform/Platform.h" |
| 11 #include "public/platform/WebURLLoaderMockFactory.h" |
| 12 #include "public/web/WebCache.h" |
| 13 #include "public/web/WebDocument.h" |
| 14 #include "public/web/WebElement.h" |
| 15 #include "public/web/WebLocalFrame.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "web/tests/FrameTestHelpers.h" |
| 19 |
| 20 namespace blink { |
| 21 |
| 22 namespace { |
| 23 |
| 24 class TestDocumentSubresourceFilter : public WebDocumentSubresourceFilter { |
| 25 public: |
| 26 explicit TestDocumentSubresourceFilter(bool allowLoads) |
| 27 : m_allowLoads(allowLoads) |
| 28 { |
| 29 } |
| 30 |
| 31 bool allowLoad(const WebURL& resourceUrl, WebURLRequest::RequestContext) ove
rride |
| 32 { |
| 33 std::string resourcePath = WebString(KURL(resourceUrl).path()).utf8(); |
| 34 if (std::find(m_queriedSubresourcePaths.begin(), m_queriedSubresourcePat
hs.end(), resourcePath) == m_queriedSubresourcePaths.end()) |
| 35 m_queriedSubresourcePaths.push_back(resourcePath); |
| 36 return m_allowLoads; |
| 37 } |
| 38 |
| 39 const std::vector<std::string>& queriedSubresourcePaths() const { return m_q
ueriedSubresourcePaths; } |
| 40 |
| 41 private: |
| 42 std::vector<std::string> m_queriedSubresourcePaths; |
| 43 bool m_allowLoads; |
| 44 }; |
| 45 |
| 46 class SubresourceFilteringWebFrameClient : public FrameTestHelpers::TestWebFrame
Client { |
| 47 public: |
| 48 void didStartProvisionalLoad(WebLocalFrame* localFrame, double) override |
| 49 { |
| 50 // Normally, the filter should be set when the load is committed. For |
| 51 // the sake of this test, however, inject it earlier to verify that it |
| 52 // is not consulted for the main resource load. |
| 53 m_subresourceFilter = new TestDocumentSubresourceFilter(m_allowSubresour
cesFromNextLoad); |
| 54 localFrame->provisionalDataSource()->setSubresourceFilter(m_subresourceF
ilter); |
| 55 } |
| 56 |
| 57 void setAllowSubresourcesFromNextLoad(bool allow) { m_allowSubresourcesFromN
extLoad = allow; } |
| 58 const TestDocumentSubresourceFilter* subresourceFilter() const { return m_su
bresourceFilter; } |
| 59 |
| 60 private: |
| 61 // Weak, owned by WebDataSource. |
| 62 TestDocumentSubresourceFilter* m_subresourceFilter = nullptr; |
| 63 bool m_allowSubresourcesFromNextLoad = false; |
| 64 }; |
| 65 |
| 66 } // namespace |
| 67 |
| 68 class WebDocumentSubresourceFilterTest : public ::testing::Test { |
| 69 protected: |
| 70 WebDocumentSubresourceFilterTest() |
| 71 : m_baseURL("http://internal.test/") |
| 72 { |
| 73 registerMockedHttpURLLoad("white-1x1.png"); |
| 74 registerMockedHttpURLLoad("foo_with_image.html"); |
| 75 m_webViewHelper.initialize(false /* enableJavascript */, &m_client); |
| 76 } |
| 77 |
| 78 void loadDocument(bool allowSubresources) |
| 79 { |
| 80 m_client.setAllowSubresourcesFromNextLoad(allowSubresources); |
| 81 FrameTestHelpers::loadFrame(mainFrame(), baseURL() + "foo_with_image.htm
l"); |
| 82 } |
| 83 |
| 84 void expectSubresourceWasLoaded(bool loaded) |
| 85 { |
| 86 WebElement webElement = mainFrame()->document().querySelector("img"); |
| 87 ASSERT_TRUE(isHTMLImageElement(webElement)); |
| 88 HTMLImageElement* imageElement = toHTMLImageElement(webElement); |
| 89 EXPECT_EQ(loaded, !!imageElement->cachedImage()); |
| 90 } |
| 91 |
| 92 const std::string& baseURL() const { return m_baseURL; } |
| 93 WebFrame* mainFrame() { return m_webViewHelper.webViewImpl()->mainFrame(); } |
| 94 const std::vector<std::string>& queriedSubresourcePaths() const { return m_c
lient.subresourceFilter()->queriedSubresourcePaths(); } |
| 95 |
| 96 private: |
| 97 void registerMockedHttpURLLoad(const std::string& fileName) |
| 98 { |
| 99 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseU
RL.c_str()), WebString::fromUTF8(fileName.c_str())); |
| 100 } |
| 101 |
| 102 // ::testing::Test: |
| 103 void TearDown() override |
| 104 { |
| 105 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); |
| 106 WebCache::clear(); |
| 107 } |
| 108 |
| 109 SubresourceFilteringWebFrameClient m_client; |
| 110 FrameTestHelpers::WebViewHelper m_webViewHelper; |
| 111 std::string m_baseURL; |
| 112 }; |
| 113 |
| 114 TEST_F(WebDocumentSubresourceFilterTest, AllowedSubresource) |
| 115 { |
| 116 loadDocument(true /* allowSubresources */); |
| 117 expectSubresourceWasLoaded(true); |
| 118 // The filter should not be consulted for the main document resource. |
| 119 EXPECT_THAT(queriedSubresourcePaths(), testing::ElementsAre("/white-1x1.png"
)); |
| 120 } |
| 121 |
| 122 TEST_F(WebDocumentSubresourceFilterTest, DisallowedSubresource) |
| 123 { |
| 124 loadDocument(false /* allowSubresources */); |
| 125 expectSubresourceWasLoaded(false); |
| 126 } |
| 127 |
| 128 TEST_F(WebDocumentSubresourceFilterTest, FilteringDecisionIsMadeLoadByLoad) |
| 129 { |
| 130 for (const bool allowSubresources : { false, true }) { |
| 131 SCOPED_TRACE(testing::Message() << "First load allows subresources = " <
< allowSubresources); |
| 132 |
| 133 loadDocument(allowSubresources); |
| 134 expectSubresourceWasLoaded(allowSubresources); |
| 135 |
| 136 loadDocument(!allowSubresources); |
| 137 expectSubresourceWasLoaded(!allowSubresources); |
| 138 EXPECT_THAT(queriedSubresourcePaths(), testing::ElementsAre("/white-1x1.
png")); |
| 139 |
| 140 WebCache::clear(); |
| 141 } |
| 142 } |
| 143 |
| 144 } // namespace blink |
OLD | NEW |