| 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 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 protected: | 95 protected: |
| 96 WebFrameSerializerSanitizationTest() { helper_.Initialize(); } | 96 WebFrameSerializerSanitizationTest() { helper_.Initialize(); } |
| 97 | 97 |
| 98 ~WebFrameSerializerSanitizationTest() override { | 98 ~WebFrameSerializerSanitizationTest() override { |
| 99 Platform::Current() | 99 Platform::Current() |
| 100 ->GetURLLoaderMockFactory() | 100 ->GetURLLoaderMockFactory() |
| 101 ->UnregisterAllURLsAndClearMemoryCache(); | 101 ->UnregisterAllURLsAndClearMemoryCache(); |
| 102 } | 102 } |
| 103 | 103 |
| 104 String GenerateMHTMLFromHtml(const String& url, const String& file_name) { | 104 String GenerateMHTMLFromHtml(const String& url, const String& file_name) { |
| 105 return GenerateMHTML(url, file_name, "text/html", false); | 105 LoadFrame(url, file_name, "text/html"); |
| 106 return GenerateMHTML(false); |
| 106 } | 107 } |
| 107 | 108 |
| 108 String GenerateMHTMLPartsFromPng(const String& url, const String& file_name) { | 109 String GenerateMHTMLPartsFromPng(const String& url, const String& file_name) { |
| 109 return GenerateMHTML(url, file_name, "image/png", true); | 110 LoadFrame(url, file_name, "image/png"); |
| 111 return GenerateMHTML(true); |
| 110 } | 112 } |
| 111 | 113 |
| 112 String GenerateMHTML(const String& url, | 114 void LoadFrame(const String& url, |
| 113 const String& file_name, | 115 const String& file_name, |
| 114 const String& mime_type, | 116 const String& mime_type) { |
| 115 const bool only_body_parts) { | |
| 116 KURL parsed_url(kParsedURLString, url); | 117 KURL parsed_url(kParsedURLString, url); |
| 117 String file_path("frameserialization/" + file_name); | 118 String file_path("frameserialization/" + file_name); |
| 118 RegisterMockedFileURLLoad(parsed_url, file_path, mime_type); | 119 RegisterMockedFileURLLoad(parsed_url, file_path, mime_type); |
| 119 FrameTestHelpers::LoadFrame(MainFrameImpl(), url.Utf8().data()); | 120 FrameTestHelpers::LoadFrame(MainFrameImpl(), url.Utf8().data()); |
| 121 MainFrameImpl()->GetFrame()->View()->UpdateAllLifecyclePhases(); |
| 122 } |
| 123 |
| 124 String GenerateMHTML(const bool only_body_parts) { |
| 120 // Boundaries are normally randomly generated but this one is predefined for | 125 // Boundaries are normally randomly generated but this one is predefined for |
| 121 // simplicity and as good as any other. Plus it gets used in almost all the | 126 // simplicity and as good as any other. Plus it gets used in almost all the |
| 122 // examples in the MHTML spec - RFC 2557. | 127 // examples in the MHTML spec - RFC 2557. |
| 123 const WebString boundary("boundary-example"); | 128 const WebString boundary("boundary-example"); |
| 124 StringBuilder mhtml; | 129 StringBuilder mhtml; |
| 125 if (!only_body_parts) { | 130 if (!only_body_parts) { |
| 126 WebThreadSafeData header_result = WebFrameSerializer::GenerateMHTMLHeader( | 131 WebThreadSafeData header_result = WebFrameSerializer::GenerateMHTMLHeader( |
| 127 boundary, MainFrameImpl(), &mhtml_delegate_); | 132 boundary, MainFrameImpl(), &mhtml_delegate_); |
| 128 mhtml.Append(header_result.Data(), header_result.size()); | 133 mhtml.Append(header_result.Data(), header_result.size()); |
| 129 } | 134 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 143 if (!only_body_parts) { | 148 if (!only_body_parts) { |
| 144 // Validate the generated MHTML. | 149 // Validate the generated MHTML. |
| 145 MHTMLParser parser(SharedBuffer::Create(mhtml_string.Characters8(), | 150 MHTMLParser parser(SharedBuffer::Create(mhtml_string.Characters8(), |
| 146 size_t(mhtml_string.length()))); | 151 size_t(mhtml_string.length()))); |
| 147 EXPECT_FALSE(parser.ParseArchive().IsEmpty()) | 152 EXPECT_FALSE(parser.ParseArchive().IsEmpty()) |
| 148 << "Generated MHTML is not well formed"; | 153 << "Generated MHTML is not well formed"; |
| 149 } | 154 } |
| 150 return mhtml_string; | 155 return mhtml_string; |
| 151 } | 156 } |
| 152 | 157 |
| 158 ShadowRoot* SetShadowContent(TreeScope& scope, |
| 159 const char* host, |
| 160 ShadowRootType shadow_type, |
| 161 const char* shadow_content, |
| 162 bool delegates_focus = false) { |
| 163 ShadowRoot* shadow_root = |
| 164 scope.getElementById(AtomicString::FromUTF8(host)) |
| 165 ->CreateShadowRootInternal(shadow_type, ASSERT_NO_EXCEPTION); |
| 166 shadow_root->SetDelegatesFocus(delegates_focus); |
| 167 shadow_root->setInnerHTML(String::FromUTF8(shadow_content), |
| 168 ASSERT_NO_EXCEPTION); |
| 169 scope.GetDocument().View()->UpdateAllLifecyclePhases(); |
| 170 return shadow_root; |
| 171 } |
| 172 |
| 153 void SetRemovePopupOverlay(bool remove_popup_overlay) { | 173 void SetRemovePopupOverlay(bool remove_popup_overlay) { |
| 154 mhtml_delegate_.SetRemovePopupOverlay(remove_popup_overlay); | 174 mhtml_delegate_.SetRemovePopupOverlay(remove_popup_overlay); |
| 155 } | 175 } |
| 156 | 176 |
| 157 void RegisterMockedFileURLLoad(const KURL& url, | 177 void RegisterMockedFileURLLoad(const KURL& url, |
| 158 const String& file_path, | 178 const String& file_path, |
| 159 const String& mime_type = "image/png") { | 179 const String& mime_type = "image/png") { |
| 160 URLTestHelpers::RegisterMockedURLLoad( | 180 URLTestHelpers::RegisterMockedURLLoad( |
| 161 url, testing::WebTestDataPath(file_path.Utf8().data()), mime_type); | 181 url, testing::WebTestDataPath(file_path.Utf8().data()), mime_type); |
| 162 } | 182 } |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 EXPECT_EQ(WTF::kNotFound, | 360 EXPECT_EQ(WTF::kNotFound, |
| 341 mhtml.Find("<meta http-equiv=3D\"Content-Security-Policy")); | 361 mhtml.Find("<meta http-equiv=3D\"Content-Security-Policy")); |
| 342 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta name=3D\"description")); | 362 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta name=3D\"description")); |
| 343 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta http-equiv=3D\"refresh")); | 363 EXPECT_NE(WTF::kNotFound, mhtml.Find("<meta http-equiv=3D\"refresh")); |
| 344 | 364 |
| 345 // If an element is removed, its children should also be skipped. | 365 // If an element is removed, its children should also be skipped. |
| 346 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<select")); | 366 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<select")); |
| 347 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<option")); | 367 EXPECT_EQ(WTF::kNotFound, mhtml.Find("<option")); |
| 348 } | 368 } |
| 349 | 369 |
| 370 TEST_F(WebFrameSerializerSanitizationTest, ShadowDOM) { |
| 371 LoadFrame("http://www.test.com", "shadow_dom.html", "text/html"); |
| 372 Document* document = MainFrameImpl()->GetFrame()->GetDocument(); |
| 373 SetShadowContent(*document, "h1", ShadowRootType::V0, "V0 shadow"); |
| 374 ShadowRoot* shadowRoot = |
| 375 SetShadowContent(*document, "h2", ShadowRootType::kOpen, |
| 376 "Parent shadow\n<p id=\"h3\">Foo</p>", true); |
| 377 SetShadowContent(*shadowRoot, "h3", ShadowRootType::kClosed, "Nested shadow"); |
| 378 String mhtml = GenerateMHTML(false); |
| 379 |
| 380 // Template with special attribute should be created for each shadow DOM tree. |
| 381 EXPECT_NE(WTF::kNotFound, mhtml.Find("<template shadowmode=3D\"v0\">")); |
| 382 EXPECT_NE(WTF::kNotFound, |
| 383 mhtml.Find("<template shadowmode=3D\"open\" shadowdelegatesfocus")); |
| 384 EXPECT_NE(WTF::kNotFound, mhtml.Find("<template shadowmode=3D\"closed\">")); |
| 385 |
| 386 // The special attribute present in the original page should be removed. |
| 387 EXPECT_EQ(WTF::kNotFound, mhtml.Find("shadowmode=3D\"foo\">")); |
| 388 EXPECT_EQ(WTF::kNotFound, mhtml.Find("shadowdelegatesfocus=3D\"bar\">")); |
| 389 } |
| 390 |
| 350 } // namespace blink | 391 } // namespace blink |
| OLD | NEW |