OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 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 21 matching lines...) Expand all Loading... |
32 | 32 |
33 #include "core/exported/WebViewBase.h" | 33 #include "core/exported/WebViewBase.h" |
34 #include "platform/mhtml/MHTMLArchive.h" | 34 #include "platform/mhtml/MHTMLArchive.h" |
35 #include "platform/mhtml/MHTMLParser.h" | 35 #include "platform/mhtml/MHTMLParser.h" |
36 #include "platform/testing/HistogramTester.h" | 36 #include "platform/testing/HistogramTester.h" |
37 #include "platform/testing/URLTestHelpers.h" | 37 #include "platform/testing/URLTestHelpers.h" |
38 #include "platform/testing/UnitTestHelpers.h" | 38 #include "platform/testing/UnitTestHelpers.h" |
39 #include "platform/weborigin/KURL.h" | 39 #include "platform/weborigin/KURL.h" |
40 #include "platform/wtf/text/StringBuilder.h" | 40 #include "platform/wtf/text/StringBuilder.h" |
41 #include "public/platform/Platform.h" | 41 #include "public/platform/Platform.h" |
42 #include "public/platform/WebCString.h" | |
43 #include "public/platform/WebCache.h" | |
44 #include "public/platform/WebString.h" | 42 #include "public/platform/WebString.h" |
45 #include "public/platform/WebURL.h" | 43 #include "public/platform/WebURL.h" |
46 #include "public/platform/WebURLLoaderMockFactory.h" | 44 #include "public/platform/WebURLLoaderMockFactory.h" |
47 #include "public/web/WebFrameSerializerClient.h" | |
48 #include "testing/gtest/include/gtest/gtest.h" | 45 #include "testing/gtest/include/gtest/gtest.h" |
49 #include "web/WebLocalFrameImpl.h" | 46 #include "web/WebLocalFrameImpl.h" |
50 #include "web/tests/FrameTestHelpers.h" | 47 #include "web/tests/FrameTestHelpers.h" |
51 | 48 |
52 namespace blink { | 49 namespace blink { |
53 | 50 |
54 namespace { | 51 namespace { |
55 class SimpleWebFrameSerializerClient final : public WebFrameSerializerClient { | |
56 public: | |
57 String ToString() { return builder_.ToString(); } | |
58 | |
59 private: | |
60 void DidSerializeDataForFrame(const WebCString& data, | |
61 FrameSerializationStatus) final { | |
62 builder_.Append(data.Data(), data.length()); | |
63 } | |
64 | |
65 StringBuilder builder_; | |
66 }; | |
67 | 52 |
68 class SimpleMHTMLPartsGenerationDelegate | 53 class SimpleMHTMLPartsGenerationDelegate |
69 : public WebFrameSerializer::MHTMLPartsGenerationDelegate { | 54 : public WebFrameSerializer::MHTMLPartsGenerationDelegate { |
70 public: | 55 public: |
71 SimpleMHTMLPartsGenerationDelegate() : remove_popup_overlay_(false) {} | 56 SimpleMHTMLPartsGenerationDelegate() : remove_popup_overlay_(false) {} |
72 | 57 |
73 void SetRemovePopupOverlay(bool remove_popup_overlay) { | 58 void SetRemovePopupOverlay(bool remove_popup_overlay) { |
74 remove_popup_overlay_ = remove_popup_overlay; | 59 remove_popup_overlay_ = remove_popup_overlay; |
75 } | 60 } |
76 | 61 |
(...skipping 21 matching lines...) Expand all Loading... |
98 if (pos == WTF::kNotFound) | 83 if (pos == WTF::kNotFound) |
99 break; | 84 break; |
100 matches++; | 85 matches++; |
101 start = pos + size; | 86 start = pos + size; |
102 } | 87 } |
103 return matches; | 88 return matches; |
104 } | 89 } |
105 | 90 |
106 } // namespace | 91 } // namespace |
107 | 92 |
108 class WebFrameSerializerTest : public ::testing::Test { | 93 class WebFrameSerializerSanitizationTest : public ::testing::Test { |
109 protected: | 94 protected: |
110 WebFrameSerializerTest() { helper_.Initialize(); } | 95 WebFrameSerializerSanitizationTest() { helper_.Initialize(); } |
111 | 96 |
112 ~WebFrameSerializerTest() override { | 97 ~WebFrameSerializerSanitizationTest() override { |
113 Platform::Current() | 98 Platform::Current() |
114 ->GetURLLoaderMockFactory() | 99 ->GetURLLoaderMockFactory() |
115 ->UnregisterAllURLsAndClearMemoryCache(); | 100 ->UnregisterAllURLsAndClearMemoryCache(); |
116 } | 101 } |
117 | 102 |
118 void RegisterMockedImageURLLoad(const String& url) { | |
119 // Image resources need to be mocked, but irrelevant here what image they | |
120 // map to. | |
121 RegisterMockedFileURLLoad(URLTestHelpers::ToKURL(url.Utf8().data()), | |
122 "frameserialization/awesome.png"); | |
123 } | |
124 void RegisterMockedFileURLLoad(const KURL& url, | |
125 const String& file_path, | |
126 const String& mime_type = "image/png") { | |
127 URLTestHelpers::RegisterMockedURLLoad( | |
128 url, testing::WebTestDataPath(file_path.Utf8().data()), mime_type); | |
129 } | |
130 | |
131 class SingleLinkRewritingDelegate | |
132 : public WebFrameSerializer::LinkRewritingDelegate { | |
133 public: | |
134 SingleLinkRewritingDelegate(const WebURL& url, const WebString& local_path) | |
135 : url_(url), local_path_(local_path) {} | |
136 | |
137 bool RewriteFrameSource(WebFrame* frame, | |
138 WebString* rewritten_link) override { | |
139 return false; | |
140 } | |
141 | |
142 bool RewriteLink(const WebURL& url, WebString* rewritten_link) override { | |
143 if (url != url_) | |
144 return false; | |
145 | |
146 *rewritten_link = local_path_; | |
147 return true; | |
148 } | |
149 | |
150 private: | |
151 const WebURL url_; | |
152 const WebString local_path_; | |
153 }; | |
154 | |
155 String SerializeFile(const String& url, const String& file_name) { | |
156 KURL parsed_url(kParsedURLString, url); | |
157 String file_path("frameserialization/" + file_name); | |
158 RegisterMockedFileURLLoad(parsed_url, file_path, "text/html"); | |
159 FrameTestHelpers::LoadFrame(MainFrameImpl(), url.Utf8().data()); | |
160 SingleLinkRewritingDelegate delegate(parsed_url, WebString("local")); | |
161 SimpleWebFrameSerializerClient serializer_client; | |
162 WebFrameSerializer::Serialize(MainFrameImpl(), &serializer_client, | |
163 &delegate); | |
164 return serializer_client.ToString(); | |
165 } | |
166 | |
167 WebViewBase* WebView() { return helper_.WebView(); } | |
168 | |
169 WebLocalFrameImpl* MainFrameImpl() { | |
170 return helper_.WebView()->MainFrameImpl(); | |
171 } | |
172 | |
173 private: | |
174 FrameTestHelpers::WebViewHelper helper_; | |
175 }; | |
176 | |
177 TEST_F(WebFrameSerializerTest, URLAttributeValues) { | |
178 RegisterMockedImageURLLoad("javascript:\""); | |
179 | |
180 const char* expected_html = | |
181 "\n<!-- saved from url=(0020)http://www.test.com/ -->\n" | |
182 "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; " | |
183 "charset=UTF-8\">\n" | |
184 "</head><body><img src=\"javascript:"\">\n" | |
185 "<a href=\"http://www.test.com/local#"\">local</a>\n" | |
186 "<a " | |
187 "href=\"http://www.example.com/#"><script>alert(0)</" | |
188 "script>\">external</a>\n" | |
189 "</body></html>"; | |
190 String actual_html = | |
191 SerializeFile("http://www.test.com", "url_attribute_values.html"); | |
192 EXPECT_EQ(expected_html, actual_html); | |
193 } | |
194 | |
195 TEST_F(WebFrameSerializerTest, EncodingAndNormalization) { | |
196 const char* expected_html = | |
197 "<!DOCTYPE html>\n" | |
198 "<!-- saved from url=(0020)http://www.test.com/ -->\n" | |
199 "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; " | |
200 "charset=EUC-KR\">\n" | |
201 "<title>Ensure NFC normalization is not performed by frame " | |
202 "serializer</title>\n" | |
203 "</head><body>\n" | |
204 "\xe4\xc5\xd1\xe2\n" | |
205 "\n</body></html>"; | |
206 String actual_html = | |
207 SerializeFile("http://www.test.com", "encoding_normalization.html"); | |
208 EXPECT_EQ(expected_html, actual_html); | |
209 } | |
210 | |
211 TEST_F(WebFrameSerializerTest, FromUrlWithMinusMinus) { | |
212 String actual_html = | |
213 SerializeFile("http://www.test.com?--x--", "text_only_page.html"); | |
214 EXPECT_EQ("<!-- saved from url=(0030)http://www.test.com/?-%2Dx-%2D -->", | |
215 actual_html.Substring(1, 60)); | |
216 } | |
217 | |
218 class WebFrameSerializerSanitizationTest : public WebFrameSerializerTest { | |
219 protected: | |
220 WebFrameSerializerSanitizationTest() {} | |
221 | |
222 ~WebFrameSerializerSanitizationTest() override {} | |
223 | |
224 String GenerateMHTMLFromHtml(const String& url, const String& file_name) { | 103 String GenerateMHTMLFromHtml(const String& url, const String& file_name) { |
225 return GenerateMHTML(url, file_name, "text/html", false); | 104 return GenerateMHTML(url, file_name, "text/html", false); |
226 } | 105 } |
227 | 106 |
228 String GenerateMHTMLPartsFromPng(const String& url, const String& file_name) { | 107 String GenerateMHTMLPartsFromPng(const String& url, const String& file_name) { |
229 return GenerateMHTML(url, file_name, "image/png", true); | 108 return GenerateMHTML(url, file_name, "image/png", true); |
230 } | 109 } |
231 | 110 |
232 String GenerateMHTML(const String& url, | 111 String GenerateMHTML(const String& url, |
233 const String& file_name, | 112 const String& file_name, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 EXPECT_FALSE(parser.ParseArchive().IsEmpty()) | 146 EXPECT_FALSE(parser.ParseArchive().IsEmpty()) |
268 << "Generated MHTML is not well formed"; | 147 << "Generated MHTML is not well formed"; |
269 } | 148 } |
270 return mhtml_string; | 149 return mhtml_string; |
271 } | 150 } |
272 | 151 |
273 void SetRemovePopupOverlay(bool remove_popup_overlay) { | 152 void SetRemovePopupOverlay(bool remove_popup_overlay) { |
274 mhtml_delegate_.SetRemovePopupOverlay(remove_popup_overlay); | 153 mhtml_delegate_.SetRemovePopupOverlay(remove_popup_overlay); |
275 } | 154 } |
276 | 155 |
277 protected: | 156 void RegisterMockedFileURLLoad(const KURL& url, |
| 157 const String& file_path, |
| 158 const String& mime_type = "image/png") { |
| 159 URLTestHelpers::RegisterMockedURLLoad( |
| 160 url, testing::WebTestDataPath(file_path.Utf8().data()), mime_type); |
| 161 } |
| 162 |
| 163 WebViewBase* WebView() { return helper_.WebView(); } |
| 164 |
| 165 WebLocalFrameImpl* MainFrameImpl() { |
| 166 return helper_.WebView()->MainFrameImpl(); |
| 167 } |
| 168 |
278 HistogramTester histogram_tester_; | 169 HistogramTester histogram_tester_; |
279 | 170 |
280 private: | 171 private: |
| 172 FrameTestHelpers::WebViewHelper helper_; |
281 SimpleMHTMLPartsGenerationDelegate mhtml_delegate_; | 173 SimpleMHTMLPartsGenerationDelegate mhtml_delegate_; |
282 }; | 174 }; |
283 | 175 |
284 TEST_F(WebFrameSerializerSanitizationTest, RemoveInlineScriptInAttributes) { | 176 TEST_F(WebFrameSerializerSanitizationTest, RemoveInlineScriptInAttributes) { |
285 String mhtml = | 177 String mhtml = |
286 GenerateMHTMLFromHtml("http://www.test.com", "script_in_attributes.html"); | 178 GenerateMHTMLFromHtml("http://www.test.com", "script_in_attributes.html"); |
287 | 179 |
288 // These scripting attributes should be removed. | 180 // These scripting attributes should be removed. |
289 EXPECT_EQ(WTF::kNotFound, mhtml.Find("onload=")); | 181 EXPECT_EQ(WTF::kNotFound, mhtml.Find("onload=")); |
290 EXPECT_EQ(WTF::kNotFound, mhtml.Find("ONLOAD=")); | 182 EXPECT_EQ(WTF::kNotFound, mhtml.Find("ONLOAD=")); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 mhtml.Find("<meta http-equiv=3D\"Content-Security-Policy")); | 340 mhtml.Find("<meta http-equiv=3D\"Content-Security-Policy")); |
449 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta name=3D\"description")); | 341 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta name=3D\"description")); |
450 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta http-equiv=3D\"refresh")); | 342 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta http-equiv=3D\"refresh")); |
451 | 343 |
452 // If an element is removed, its children should also be skipped. | 344 // If an element is removed, its children should also be skipped. |
453 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<select")); | 345 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<select")); |
454 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<option")); | 346 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<option")); |
455 } | 347 } |
456 | 348 |
457 } // namespace blink | 349 } // namespace blink |
OLD | NEW |