Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 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 17 matching lines...) Expand all Loading... | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "core/dom/Document.h" | 31 #include "core/dom/Document.h" |
| 32 #include "core/frame/LocalFrame.h" | 32 #include "core/frame/LocalFrame.h" |
| 33 #include "core/frame/Location.h" | 33 #include "core/frame/Location.h" |
| 34 #include "core/page/Page.h" | 34 #include "core/page/Page.h" |
| 35 #include "platform/SerializedResource.h" | 35 #include "platform/SerializedResource.h" |
| 36 #include "platform/SharedBuffer.h" | 36 #include "platform/SharedBuffer.h" |
| 37 #include "platform/mhtml/MHTMLArchive.h" | 37 #include "platform/mhtml/MHTMLArchive.h" |
| 38 #include "platform/mhtml/MHTMLParser.h" | |
| 38 #include "platform/testing/URLTestHelpers.h" | 39 #include "platform/testing/URLTestHelpers.h" |
| 39 #include "platform/testing/UnitTestHelpers.h" | 40 #include "platform/testing/UnitTestHelpers.h" |
| 40 #include "platform/weborigin/KURL.h" | 41 #include "platform/weborigin/KURL.h" |
| 41 #include "platform/weborigin/SchemeRegistry.h" | 42 #include "platform/weborigin/SchemeRegistry.h" |
| 42 #include "public/platform/Platform.h" | 43 #include "public/platform/Platform.h" |
| 43 #include "public/platform/WebString.h" | 44 #include "public/platform/WebString.h" |
| 44 #include "public/platform/WebURL.h" | 45 #include "public/platform/WebURL.h" |
| 45 #include "public/platform/WebURLLoaderMockFactory.h" | 46 #include "public/platform/WebURLLoaderMockFactory.h" |
| 46 #include "public/platform/WebURLRequest.h" | 47 #include "public/platform/WebURLRequest.h" |
| 47 #include "public/platform/WebURLResponse.h" | 48 #include "public/platform/WebURLResponse.h" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 | 155 |
| 155 RefPtr<RawData> mhtml_data = RawData::Create(); | 156 RefPtr<RawData> mhtml_data = RawData::Create(); |
| 156 MHTMLArchive::GenerateMHTMLHeader(boundary, title, mime_type, | 157 MHTMLArchive::GenerateMHTMLHeader(boundary, title, mime_type, |
| 157 *mhtml_data->MutableData()); | 158 *mhtml_data->MutableData()); |
| 158 for (const auto& resource : resources) { | 159 for (const auto& resource : resources) { |
| 159 MHTMLArchive::GenerateMHTMLPart(boundary, String(), encoding_policy, | 160 MHTMLArchive::GenerateMHTMLPart(boundary, String(), encoding_policy, |
| 160 resource, *mhtml_data->MutableData()); | 161 resource, *mhtml_data->MutableData()); |
| 161 } | 162 } |
| 162 MHTMLArchive::GenerateMHTMLFooterForTesting(boundary, | 163 MHTMLArchive::GenerateMHTMLFooterForTesting(boundary, |
| 163 *mhtml_data->MutableData()); | 164 *mhtml_data->MutableData()); |
| 165 | |
| 166 // Validate the generated MHTML. | |
| 167 MHTMLParser parser( | |
| 168 SharedBuffer::Create(mhtml_data->data(), mhtml_data->length())); | |
| 169 if (parser.ParseArchive().IsEmpty()) { | |
| 170 ADD_FAILURE() << "Invalid MHTML"; | |
| 171 return RawData::Create(); | |
| 172 } | |
| 173 | |
| 164 return mhtml_data.Release(); | 174 return mhtml_data.Release(); |
| 165 } | 175 } |
| 166 | 176 |
| 167 PassRefPtr<RawData> Serialize(const char* title, | 177 PassRefPtr<RawData> Serialize(const char* title, |
| 168 const char* mime, | 178 const char* mime, |
| 169 MHTMLArchive::EncodingPolicy encoding_policy) { | 179 MHTMLArchive::EncodingPolicy encoding_policy) { |
| 170 return GenerateMHTMLData(resources_, encoding_policy, title, mime); | 180 return GenerateMHTMLData(resources_, encoding_policy, title, mime); |
| 171 } | 181 } |
| 172 | 182 |
| 173 private: | 183 private: |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 199 EXPECT_STREQ(kFileURL, frame->DomWindow()->location()->href().Ascii().data()); | 209 EXPECT_STREQ(kFileURL, frame->DomWindow()->location()->href().Ascii().data()); |
| 200 | 210 |
| 201 SecurityOrigin* origin = document->GetSecurityOrigin(); | 211 SecurityOrigin* origin = document->GetSecurityOrigin(); |
| 202 EXPECT_STRNE("localhost", origin->Domain().Ascii().data()); | 212 EXPECT_STRNE("localhost", origin->Domain().Ascii().data()); |
| 203 } | 213 } |
| 204 | 214 |
| 205 TEST_F(MHTMLTest, TestMHTMLEncoding) { | 215 TEST_F(MHTMLTest, TestMHTMLEncoding) { |
| 206 AddTestResources(); | 216 AddTestResources(); |
| 207 RefPtr<RawData> data = Serialize("Test Serialization", "text/html", | 217 RefPtr<RawData> data = Serialize("Test Serialization", "text/html", |
| 208 MHTMLArchive::kUseDefaultEncoding); | 218 MHTMLArchive::kUseDefaultEncoding); |
| 219 ASSERT_FALSE(HasFailure()); | |
|
dcheng
2017/04/26 08:52:39
Per https://github.com/google/googletest/blob/mast
| |
| 209 | 220 |
| 210 // Read the MHTML data line per line and do some pseudo-parsing to make sure | 221 // Read the MHTML data line per line and do some pseudo-parsing to make sure |
| 211 // the right encoding is used for the different sections. | 222 // the right encoding is used for the different sections. |
| 212 LineReader line_reader(std::string(data->data(), data->length())); | 223 LineReader line_reader(std::string(data->data(), data->length())); |
| 213 int section_checked_count = 0; | 224 int section_checked_count = 0; |
| 214 const char* expected_encoding = 0; | 225 const char* expected_encoding = 0; |
| 215 std::string line; | 226 std::string line; |
| 216 while (line_reader.GetNextLine(&line)) { | 227 while (line_reader.GetNextLine(&line)) { |
| 217 if (line.compare(0, 13, "Content-Type:") == 0) { | 228 if (line.compare(0, 13, "Content-Type:") == 0) { |
| 218 ASSERT_FALSE(expected_encoding); | 229 ASSERT_FALSE(expected_encoding); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 235 section_checked_count++; | 246 section_checked_count++; |
| 236 } | 247 } |
| 237 } | 248 } |
| 238 EXPECT_EQ(12, section_checked_count); | 249 EXPECT_EQ(12, section_checked_count); |
| 239 } | 250 } |
| 240 | 251 |
| 241 TEST_F(MHTMLTest, MHTMLFromScheme) { | 252 TEST_F(MHTMLTest, MHTMLFromScheme) { |
| 242 AddTestResources(); | 253 AddTestResources(); |
| 243 RefPtr<RawData> raw_data = Serialize("Test Serialization", "text/html", | 254 RefPtr<RawData> raw_data = Serialize("Test Serialization", "text/html", |
| 244 MHTMLArchive::kUseDefaultEncoding); | 255 MHTMLArchive::kUseDefaultEncoding); |
| 256 ASSERT_FALSE(HasFailure()); | |
| 257 | |
| 245 RefPtr<SharedBuffer> data = | 258 RefPtr<SharedBuffer> data = |
| 246 SharedBuffer::Create(raw_data->data(), raw_data->length()); | 259 SharedBuffer::Create(raw_data->data(), raw_data->length()); |
| 247 KURL http_url = ToKURL("http://www.example.com"); | 260 KURL http_url = ToKURL("http://www.example.com"); |
| 248 KURL content_url = ToKURL("content://foo"); | 261 KURL content_url = ToKURL("content://foo"); |
| 249 KURL file_url = ToKURL("file://foo"); | 262 KURL file_url = ToKURL("file://foo"); |
| 250 KURL special_scheme_url = ToKURL("fooscheme://bar"); | 263 KURL special_scheme_url = ToKURL("fooscheme://bar"); |
| 251 | 264 |
| 252 // MHTMLArchives can only be initialized from local schemes, http/https | 265 // MHTMLArchives can only be initialized from local schemes, http/https |
| 253 // schemes, and content scheme(Android specific). | 266 // schemes, and content scheme(Android specific). |
| 254 EXPECT_NE(nullptr, MHTMLArchive::Create(http_url, data.Get())); | 267 EXPECT_NE(nullptr, MHTMLArchive::Create(http_url, data.Get())); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 279 // Full sandboxing should be turned on. | 292 // Full sandboxing should be turned on. |
| 280 EXPECT_TRUE(document->IsSandboxed(kSandboxAll)); | 293 EXPECT_TRUE(document->IsSandboxed(kSandboxAll)); |
| 281 | 294 |
| 282 // MHTML document should be loaded into unique origin. | 295 // MHTML document should be loaded into unique origin. |
| 283 EXPECT_TRUE(document->GetSecurityOrigin()->IsUnique()); | 296 EXPECT_TRUE(document->GetSecurityOrigin()->IsUnique()); |
| 284 // Script execution should be disabled. | 297 // Script execution should be disabled. |
| 285 EXPECT_FALSE(document->CanExecuteScripts(kNotAboutToExecuteScript)); | 298 EXPECT_FALSE(document->CanExecuteScripts(kNotAboutToExecuteScript)); |
| 286 } | 299 } |
| 287 | 300 |
| 288 } // namespace blink | 301 } // namespace blink |
| OLD | NEW |