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

Side by Side Diff: Source/web/tests/WebFrameTest.cpp

Issue 570563003: Implement CSP check for manifest fetching (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebased Created 6 years, 2 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 30 matching lines...) Expand all
41 #include "core/css/resolver/ViewportStyleResolver.h" 41 #include "core/css/resolver/ViewportStyleResolver.h"
42 #include "core/dom/DocumentMarkerController.h" 42 #include "core/dom/DocumentMarkerController.h"
43 #include "core/dom/Fullscreen.h" 43 #include "core/dom/Fullscreen.h"
44 #include "core/dom/NodeRenderStyle.h" 44 #include "core/dom/NodeRenderStyle.h"
45 #include "core/dom/Range.h" 45 #include "core/dom/Range.h"
46 #include "core/editing/Editor.h" 46 #include "core/editing/Editor.h"
47 #include "core/editing/FrameSelection.h" 47 #include "core/editing/FrameSelection.h"
48 #include "core/editing/SpellChecker.h" 48 #include "core/editing/SpellChecker.h"
49 #include "core/editing/VisiblePosition.h" 49 #include "core/editing/VisiblePosition.h"
50 #include "core/events/MouseEvent.h" 50 #include "core/events/MouseEvent.h"
51 #include "core/fetch/FetchRequest.h"
51 #include "core/fetch/MemoryCache.h" 52 #include "core/fetch/MemoryCache.h"
53 #include "core/fetch/ResourceFetcher.h"
52 #include "core/frame/FrameView.h" 54 #include "core/frame/FrameView.h"
53 #include "core/frame/LocalFrame.h" 55 #include "core/frame/LocalFrame.h"
54 #include "core/frame/Settings.h" 56 #include "core/frame/Settings.h"
55 #include "core/html/HTMLDocument.h" 57 #include "core/html/HTMLDocument.h"
56 #include "core/html/HTMLFormElement.h" 58 #include "core/html/HTMLFormElement.h"
57 #include "core/loader/DocumentThreadableLoader.h" 59 #include "core/loader/DocumentThreadableLoader.h"
58 #include "core/loader/DocumentThreadableLoaderClient.h" 60 #include "core/loader/DocumentThreadableLoaderClient.h"
59 #include "core/loader/FrameLoadRequest.h" 61 #include "core/loader/FrameLoadRequest.h"
60 #include "core/loader/ThreadableLoader.h" 62 #include "core/loader/ThreadableLoader.h"
61 #include "core/page/EventHandler.h" 63 #include "core/page/EventHandler.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 125
124 class FakeCompositingWebViewClient : public FrameTestHelpers::TestWebViewClient { 126 class FakeCompositingWebViewClient : public FrameTestHelpers::TestWebViewClient {
125 public: 127 public:
126 virtual bool enterFullScreen() OVERRIDE { return true; } 128 virtual bool enterFullScreen() OVERRIDE { return true; }
127 }; 129 };
128 130
129 class WebFrameTest : public testing::Test { 131 class WebFrameTest : public testing::Test {
130 protected: 132 protected:
131 WebFrameTest() 133 WebFrameTest()
132 : m_baseURL("http://www.test.com/") 134 : m_baseURL("http://www.test.com/")
135 , m_notBaseURL("http://www.nottest.com/")
Mike West 2014/09/29 11:00:46 Nit: These shouldn't be real URLs. Would you mind
133 , m_chromeURL("chrome://") 136 , m_chromeURL("chrome://")
134 { 137 {
135 } 138 }
136 139
137 virtual ~WebFrameTest() 140 virtual ~WebFrameTest()
138 { 141 {
139 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); 142 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
140 } 143 }
141 144
142 void registerMockedHttpURLLoad(const std::string& fileName) 145 void registerMockedHttpURLLoad(const std::string& fileName)
143 { 146 {
144 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseU RL.c_str()), WebString::fromUTF8(fileName.c_str())); 147 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_baseU RL.c_str()), WebString::fromUTF8(fileName.c_str()));
145 } 148 }
146 149
147 void registerMockedChromeURLLoad(const std::string& fileName) 150 void registerMockedChromeURLLoad(const std::string& fileName)
148 { 151 {
149 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_chrom eURL.c_str()), WebString::fromUTF8(fileName.c_str())); 152 URLTestHelpers::registerMockedURLFromBaseURL(WebString::fromUTF8(m_chrom eURL.c_str()), WebString::fromUTF8(fileName.c_str()));
150 } 153 }
151 154
155
156 void registerMockedHttpURLLoadWithCSP(const std::string& fileName, const std ::string& csp)
157 {
158 WebURLResponse response;
159 response.initialize();
160 response.setMIMEType("text/html");
161 response.addHTTPHeaderField("Content-Security-Policy", WebString::fromUT F8(csp));
Mike West 2014/09/29 11:00:46 It would be good to add a test which verified that
162 std::string fullString = m_baseURL + fileName;
163 URLTestHelpers::registerMockedURLLoadWithCustomResponse(toKURL(fullStrin g.c_str()), WebString::fromUTF8(fileName.c_str()), WebString::fromUTF8(""), resp onse);
164 }
165
152 void applyViewportStyleOverride(FrameTestHelpers::WebViewHelper* webViewHelp er) 166 void applyViewportStyleOverride(FrameTestHelpers::WebViewHelper* webViewHelp er)
153 { 167 {
154 RefPtrWillBeRawPtr<StyleSheetContents> styleSheet = StyleSheetContents:: create(CSSParserContext(UASheetMode, 0)); 168 RefPtrWillBeRawPtr<StyleSheetContents> styleSheet = StyleSheetContents:: create(CSSParserContext(UASheetMode, 0));
155 styleSheet->parseString(String(blink::viewportAndroidCss, sizeof(blink:: viewportAndroidCss))); 169 styleSheet->parseString(String(blink::viewportAndroidCss, sizeof(blink:: viewportAndroidCss)));
156 OwnPtrWillBeRawPtr<RuleSet> ruleSet = RuleSet::create(); 170 OwnPtrWillBeRawPtr<RuleSet> ruleSet = RuleSet::create();
157 ruleSet->addRulesFromSheet(styleSheet.get(), MediaQueryEvaluator("screen ")); 171 ruleSet->addRulesFromSheet(styleSheet.get(), MediaQueryEvaluator("screen "));
158 172
159 Document* document = toLocalFrame(webViewHelper->webViewImpl()->page()-> mainFrame())->document(); 173 Document* document = toLocalFrame(webViewHelper->webViewImpl()->page()-> mainFrame())->document();
160 document->ensureStyleResolver().viewportStyleResolver()->collectViewport Rules(ruleSet.get(), ViewportStyleResolver::UserAgentOrigin); 174 document->ensureStyleResolver().viewportStyleResolver()->collectViewport Rules(ruleSet.get(), ViewportStyleResolver::UserAgentOrigin);
161 document->ensureStyleResolver().viewportStyleResolver()->resolve(); 175 document->ensureStyleResolver().viewportStyleResolver()->resolve();
(...skipping 22 matching lines...) Expand all
184 registerMockedHttpURLLoad("nodeimage.html"); 198 registerMockedHttpURLLoad("nodeimage.html");
185 webViewHelper->initializeAndLoad(m_baseURL + "nodeimage.html"); 199 webViewHelper->initializeAndLoad(m_baseURL + "nodeimage.html");
186 webViewHelper->webView()->resize(WebSize(640, 480)); 200 webViewHelper->webView()->resize(WebSize(640, 480));
187 webViewHelper->webView()->layout(); 201 webViewHelper->webView()->layout();
188 RefPtr<LocalFrame> frame = toLocalFrame(webViewHelper->webViewImpl()->pa ge()->mainFrame()); 202 RefPtr<LocalFrame> frame = toLocalFrame(webViewHelper->webViewImpl()->pa ge()->mainFrame());
189 Element* element = frame->document()->getElementById(testcase.c_str()); 203 Element* element = frame->document()->getElementById(testcase.c_str());
190 return frame->nodeImage(*element); 204 return frame->nodeImage(*element);
191 } 205 }
192 206
193 std::string m_baseURL; 207 std::string m_baseURL;
208 std::string m_notBaseURL;
194 std::string m_chromeURL; 209 std::string m_chromeURL;
195 }; 210 };
196 211
197 class UseMockScrollbarSettings { 212 class UseMockScrollbarSettings {
198 public: 213 public:
199 UseMockScrollbarSettings() 214 UseMockScrollbarSettings()
200 { 215 {
201 Settings::setMockScrollbarsEnabled(true); 216 Settings::setMockScrollbarsEnabled(true);
202 RuntimeEnabledFeatures::setOverlayScrollbarsEnabled(true); 217 RuntimeEnabledFeatures::setOverlayScrollbarsEnabled(true);
203 EXPECT_TRUE(ScrollbarTheme::theme()->usesOverlayScrollbars()); 218 EXPECT_TRUE(ScrollbarTheme::theme()->usesOverlayScrollbars());
(...skipping 5647 matching lines...) Expand 10 before | Expand all | Expand 10 after
5851 { 5866 {
5852 registerMockedHttpURLLoad("link-manifest-change.html"); 5867 registerMockedHttpURLLoad("link-manifest-change.html");
5853 5868
5854 ManifestChangeWebFrameClient webFrameClient; 5869 ManifestChangeWebFrameClient webFrameClient;
5855 FrameTestHelpers::WebViewHelper webViewHelper; 5870 FrameTestHelpers::WebViewHelper webViewHelper;
5856 webViewHelper.initializeAndLoad(m_baseURL + "link-manifest-change.html", tru e, &webFrameClient); 5871 webViewHelper.initializeAndLoad(m_baseURL + "link-manifest-change.html", tru e, &webFrameClient);
5857 5872
5858 EXPECT_EQ(14, webFrameClient.manifestChangeCount()); 5873 EXPECT_EQ(14, webFrameClient.manifestChangeCount());
5859 } 5874 }
5860 5875
5876 static ResourcePtr<Resource> fetchManifest(Document* document, const KURL& url)
5877 {
5878 FetchRequest fetchRequest = FetchRequest(ResourceRequest(url), FetchInitiato rInfo());
5879 fetchRequest.mutableResourceRequest().setRequestContext(WebURLRequest::Reque stContextManifest);
5880
5881 return document->fetcher()->fetchSynchronously(fetchRequest);
5882 }
5883
5884 TEST_F(WebFrameTest, ManifestFetch)
5885 {
5886 registerMockedHttpURLLoad("foo.html");
5887 registerMockedHttpURLLoad("link-manifest-fetch.json");
5888
5889 FrameTestHelpers::WebViewHelper webViewHelper;
5890 webViewHelper.initializeAndLoad(m_baseURL + "foo.html");
5891 Document* document = toWebLocalFrameImpl(webViewHelper.webViewImpl()->mainFr ame())->frame()->document();
5892
5893 ResourcePtr<Resource> resource = fetchManifest(document, toKURL(m_baseURL + "link-manifest-fetch.json"));
5894
5895 EXPECT_TRUE(resource->isLoaded());
5896 }
5897
5898 TEST_F(WebFrameTest, ManifestCSPFetchAllow)
5899 {
5900 URLTestHelpers::registerMockedURLLoad(toKURL(m_notBaseURL + "link-manifest-f etch.json"), "link-manifest-fetch.json");
5901 registerMockedHttpURLLoadWithCSP("foo.html", "manifest-src *");
5902
5903 FrameTestHelpers::WebViewHelper webViewHelper;
5904 webViewHelper.initializeAndLoad(m_baseURL + "foo.html");
5905 Document* document = toWebLocalFrameImpl(webViewHelper.webViewImpl()->mainFr ame())->frame()->document();
5906
5907 ResourcePtr<Resource> resource = fetchManifest(document, toKURL(m_notBaseURL + "link-manifest-fetch.json"));
5908
5909 EXPECT_TRUE(resource->isLoaded());
5910 }
5911
5912 TEST_F(WebFrameTest, ManifestCSPFetchSelf)
5913 {
5914 URLTestHelpers::registerMockedURLLoad(toKURL(m_notBaseURL + "link-manifest-f etch.json"), "link-manifest-fetch.json");
5915 registerMockedHttpURLLoadWithCSP("foo.html", "manifest-src 'self'");
5916
5917 FrameTestHelpers::WebViewHelper webViewHelper;
5918 webViewHelper.initializeAndLoad(m_baseURL + "foo.html");
5919 Document* document = toWebLocalFrameImpl(webViewHelper.webViewImpl()->mainFr ame())->frame()->document();
5920
5921 ResourcePtr<Resource> resource = fetchManifest(document, toKURL(m_notBaseURL + "link-manifest-fetch.json"));
5922
5923 EXPECT_EQ(0, resource.get()); // Fetching resource wasn't allowed.
5924 }
5925
5861 TEST_F(WebFrameTest, ReloadBypassingCache) 5926 TEST_F(WebFrameTest, ReloadBypassingCache)
5862 { 5927 {
5863 // Check that a reload ignoring cache on a frame will result in the cache 5928 // Check that a reload ignoring cache on a frame will result in the cache
5864 // policy of the request being set to ReloadBypassingCache. 5929 // policy of the request being set to ReloadBypassingCache.
5865 registerMockedHttpURLLoad("foo.html"); 5930 registerMockedHttpURLLoad("foo.html");
5866 FrameTestHelpers::WebViewHelper webViewHelper; 5931 FrameTestHelpers::WebViewHelper webViewHelper;
5867 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true); 5932 webViewHelper.initializeAndLoad(m_baseURL + "foo.html", true);
5868 WebFrame* frame = webViewHelper.webView()->mainFrame(); 5933 WebFrame* frame = webViewHelper.webView()->mainFrame();
5869 FrameTestHelpers::reloadFrameIgnoringCache(frame); 5934 FrameTestHelpers::reloadFrameIgnoringCache(frame);
5870 EXPECT_EQ(WebURLRequest::ReloadBypassingCache, frame->dataSource()->request( ).cachePolicy()); 5935 EXPECT_EQ(WebURLRequest::ReloadBypassingCache, frame->dataSource()->request( ).cachePolicy());
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
6272 EXPECT_EQ(1u, frameClient.navigationalDataReceivedCount()); 6337 EXPECT_EQ(1u, frameClient.navigationalDataReceivedCount());
6273 6338
6274 // Neither should a page reload. 6339 // Neither should a page reload.
6275 localFrame->reload(); 6340 localFrame->reload();
6276 EXPECT_EQ(4u, frameClient.provisionalLoadCount()); 6341 EXPECT_EQ(4u, frameClient.provisionalLoadCount());
6277 EXPECT_FALSE(frameClient.wasLastProvisionalLoadATransition()); 6342 EXPECT_FALSE(frameClient.wasLastProvisionalLoadATransition());
6278 EXPECT_EQ(1u, frameClient.navigationalDataReceivedCount()); 6343 EXPECT_EQ(1u, frameClient.navigationalDataReceivedCount());
6279 } 6344 }
6280 6345
6281 } // namespace 6346 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698