Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 15 #include "content/public/common/mhtml_generation.h" | |
| 15 #include "content/public/test/browser_test_utils.h" | 16 #include "content/public/test/browser_test_utils.h" |
| 16 #include "content/public/test/content_browser_test.h" | 17 #include "content/public/test/content_browser_test.h" |
| 17 #include "content/public/test/content_browser_test_utils.h" | 18 #include "content/public/test/content_browser_test_utils.h" |
| 18 #include "content/public/test/test_utils.h" | 19 #include "content/public/test/test_utils.h" |
| 19 #include "content/shell/browser/shell.h" | 20 #include "content/shell/browser/shell.h" |
| 20 #include "net/dns/mock_host_resolver.h" | 21 #include "net/dns/mock_host_resolver.h" |
| 21 #include "net/test/embedded_test_server/embedded_test_server.h" | 22 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| 25 using testing::ContainsRegex; | 26 using testing::ContainsRegex; |
| 26 using testing::HasSubstr; | 27 using testing::HasSubstr; |
| 27 using testing::Not; | 28 using testing::Not; |
| 28 | 29 |
| 29 namespace content { | 30 namespace content { |
| 30 | 31 |
| 31 class MHTMLGenerationTest : public ContentBrowserTest { | 32 class MHTMLGenerationTest : public ContentBrowserTest { |
| 32 public: | 33 public: |
| 33 MHTMLGenerationTest() : has_mhtml_callback_run_(false), file_size_(0) {} | 34 MHTMLGenerationTest() : has_mhtml_callback_run_(false), file_size_(0) {} |
| 34 | 35 |
| 35 protected: | 36 protected: |
| 36 void SetUp() override { | 37 void SetUp() override { |
| 37 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 38 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 38 ASSERT_TRUE(embedded_test_server()->Start()); | 39 ASSERT_TRUE(embedded_test_server()->Start()); |
| 39 ContentBrowserTest::SetUp(); | 40 ContentBrowserTest::SetUp(); |
| 40 } | 41 } |
| 41 | 42 |
| 42 void GenerateMHTML(const base::FilePath& path, const GURL& url) { | 43 void GenerateMHTML(const base::FilePath& path, const GURL& url) { |
| 43 GenerateMHTML(path, url, false); | 44 GenerateMHTML(MHTMLGenerationParams(path), url); |
| 44 } | 45 } |
| 45 | 46 |
| 46 void GenerateMHTML(const base::FilePath& path, | 47 void GenerateMHTML(const MHTMLGenerationParams& params, const GURL& url) { |
| 47 const GURL& url, | |
| 48 bool use_binary_encoding) { | |
| 49 NavigateToURL(shell(), url); | 48 NavigateToURL(shell(), url); |
| 50 | 49 |
| 51 base::RunLoop run_loop; | 50 base::RunLoop run_loop; |
| 51 | |
| 52 shell()->web_contents()->GenerateMHTML( | 52 shell()->web_contents()->GenerateMHTML( |
| 53 path, use_binary_encoding, | 53 params, base::Bind(&MHTMLGenerationTest::MHTMLGenerated, this, |
| 54 base::Bind(&MHTMLGenerationTest::MHTMLGenerated, this, | 54 run_loop.QuitClosure())); |
| 55 run_loop.QuitClosure())); | |
| 56 | 55 |
| 57 // Block until the MHTML is generated. | 56 // Block until the MHTML is generated. |
| 58 run_loop.Run(); | 57 run_loop.Run(); |
| 59 | 58 |
| 60 EXPECT_TRUE(has_mhtml_callback_run()); | 59 EXPECT_TRUE(has_mhtml_callback_run()); |
| 61 } | 60 } |
| 62 | 61 |
| 63 int64_t ReadFileSizeFromDisk(base::FilePath path) { | 62 int64_t ReadFileSizeFromDisk(base::FilePath path) { |
| 64 int64_t file_size; | 63 int64_t file_size; |
| 65 if (!base::GetFileSize(path, &file_size)) return -1; | 64 if (!base::GetFileSize(path, &file_size)) return -1; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 } | 113 } |
| 115 | 114 |
| 116 // Tests that MHTML generated using the default 'quoted-printable' encoding does | 115 // Tests that MHTML generated using the default 'quoted-printable' encoding does |
| 117 // not contain the 'binary' Content-Transfer-Encoding header, and generates | 116 // not contain the 'binary' Content-Transfer-Encoding header, and generates |
| 118 // base64 encoding for the image part. | 117 // base64 encoding for the image part. |
| 119 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateNonBinaryMHTMLWithImage) { | 118 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateNonBinaryMHTMLWithImage) { |
| 120 base::FilePath path(temp_dir_.path()); | 119 base::FilePath path(temp_dir_.path()); |
| 121 path = path.Append(FILE_PATH_LITERAL("test_binary.mht")); | 120 path = path.Append(FILE_PATH_LITERAL("test_binary.mht")); |
| 122 | 121 |
| 123 GURL url(embedded_test_server()->GetURL("/page_with_image.html")); | 122 GURL url(embedded_test_server()->GetURL("/page_with_image.html")); |
| 124 GenerateMHTML(path, url, false); | 123 GenerateMHTML(path, url); |
| 125 ASSERT_FALSE(HasFailure()); | 124 ASSERT_FALSE(HasFailure()); |
| 126 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. | 125 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. |
| 127 EXPECT_GT(ReadFileSizeFromDisk(path), 100); // Verify the actual file size. | 126 EXPECT_GT(ReadFileSizeFromDisk(path), 100); // Verify the actual file size. |
| 128 | 127 |
| 129 std::string mhtml; | 128 std::string mhtml; |
| 130 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | 129 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); |
| 131 EXPECT_THAT(mhtml, HasSubstr("Content-Transfer-Encoding: base64")); | 130 EXPECT_THAT(mhtml, HasSubstr("Content-Transfer-Encoding: base64")); |
| 132 EXPECT_THAT(mhtml, Not(HasSubstr("Content-Transfer-Encoding: binary"))); | 131 EXPECT_THAT(mhtml, Not(HasSubstr("Content-Transfer-Encoding: binary"))); |
| 133 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*blank.jpg")); | 132 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*blank.jpg")); |
| 134 } | 133 } |
| 135 | 134 |
| 136 // Tests that MHTML generated using the binary encoding contains the 'binary' | 135 // Tests that MHTML generated using the binary encoding contains the 'binary' |
| 137 // Content-Transfer-Encoding header, and does not contain any base64 encoded | 136 // Content-Transfer-Encoding header, and does not contain any base64 encoded |
| 138 // parts. | 137 // parts. |
| 139 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateBinaryMHTMLWithImage) { | 138 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateBinaryMHTMLWithImage) { |
| 140 base::FilePath path(temp_dir_.path()); | 139 base::FilePath path(temp_dir_.path()); |
| 141 path = path.Append(FILE_PATH_LITERAL("test_binary.mht")); | 140 path = path.Append(FILE_PATH_LITERAL("test_binary.mht")); |
| 142 | 141 |
| 143 GURL url(embedded_test_server()->GetURL("/page_with_image.html")); | 142 GURL url(embedded_test_server()->GetURL("/page_with_image.html")); |
| 144 GenerateMHTML(path, url, true); | 143 MHTMLGenerationParams params(path); |
| 144 params.use_binary_encoding = true; | |
| 145 | |
| 146 GenerateMHTML(params, url); | |
| 145 ASSERT_FALSE(HasFailure()); | 147 ASSERT_FALSE(HasFailure()); |
| 146 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. | 148 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. |
| 147 EXPECT_GT(ReadFileSizeFromDisk(path), 100); // Verify the actual file size. | 149 EXPECT_GT(ReadFileSizeFromDisk(path), 100); // Verify the actual file size. |
| 148 | 150 |
| 149 std::string mhtml; | 151 std::string mhtml; |
| 150 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | 152 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); |
| 151 EXPECT_THAT(mhtml, HasSubstr("Content-Transfer-Encoding: binary")); | 153 EXPECT_THAT(mhtml, HasSubstr("Content-Transfer-Encoding: binary")); |
| 152 EXPECT_THAT(mhtml, Not(HasSubstr("Content-Transfer-Encoding: base64"))); | 154 EXPECT_THAT(mhtml, Not(HasSubstr("Content-Transfer-Encoding: base64"))); |
| 153 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*blank.jpg")); | 155 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*blank.jpg")); |
| 154 } | 156 } |
| 155 | 157 |
| 158 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateMHTMLIgnoreNoStore) { | |
| 159 base::FilePath path(temp_dir_.path()); | |
| 160 path = path.Append(FILE_PATH_LITERAL("test.mht")); | |
| 161 | |
| 162 GURL url(embedded_test_server()->GetURL("/nostore.html")); | |
| 163 | |
| 164 // First, generate MHTML without specifying the new policy. | |
|
Łukasz Anforowicz
2016/05/12 18:56:48
nit: // Generate MHTML without specifying FAIL_FOR
dewittj
2016/05/12 22:58:01
Done.
| |
| 165 GenerateMHTML(path, url); | |
| 166 // We expect that there wasn't an error (file size -1 indicates an error.) | |
| 167 ASSERT_FALSE(HasFailure()); | |
| 168 | |
| 169 std::string mhtml; | |
| 170 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | |
| 171 | |
| 172 // Make sure the contents of the body are present. | |
| 173 EXPECT_THAT(mhtml, HasSubstr("test body")); | |
| 174 | |
| 175 // Make sure that URL of the content is present. | |
| 176 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*/nostore.html")); | |
| 177 } | |
| 178 | |
| 179 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateMHTMLObeyNoStoreMainFrame) { | |
| 180 base::FilePath path(temp_dir_.path()); | |
| 181 path = path.Append(FILE_PATH_LITERAL("test.mht")); | |
| 182 | |
| 183 GURL url(embedded_test_server()->GetURL("/nostore.html")); | |
| 184 | |
| 185 // First, generate MHTML without specifying the new policy. | |
|
Łukasz Anforowicz
2016/05/12 18:56:48
nit: // Generate MHTML specifying FAIL_FOR_NO_STOR
dewittj
2016/05/12 22:58:01
Done.
| |
| 186 MHTMLGenerationParams params(path); | |
| 187 params.cache_control_policy = | |
| 188 content::MHTMLCacheControlPolicy::FAIL_FOR_NO_STORE_MAIN_FRAME; | |
| 189 | |
| 190 GenerateMHTML(params, url); | |
| 191 // We expect that there was an error (file size -1 indicates an error.) | |
| 192 EXPECT_EQ(-1, file_size()); | |
| 193 | |
| 194 std::string mhtml; | |
| 195 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | |
| 196 | |
| 197 // Make sure the contents are missing. | |
| 198 EXPECT_THAT(mhtml, Not(HasSubstr("test body"))); | |
| 199 } | |
| 200 | |
| 156 // Test suite that allows testing --site-per-process against cross-site frames. | 201 // Test suite that allows testing --site-per-process against cross-site frames. |
| 157 // See http://dev.chromium.org/developers/design-documents/site-isolation. | 202 // See http://dev.chromium.org/developers/design-documents/site-isolation. |
| 158 class MHTMLGenerationSitePerProcessTest : public MHTMLGenerationTest { | 203 class MHTMLGenerationSitePerProcessTest : public MHTMLGenerationTest { |
| 159 public: | 204 public: |
| 160 MHTMLGenerationSitePerProcessTest() {} | 205 MHTMLGenerationSitePerProcessTest() {} |
| 161 | 206 |
| 162 protected: | 207 protected: |
| 163 void SetUpCommandLine(base::CommandLine* command_line) override { | 208 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 164 MHTMLGenerationTest::SetUpCommandLine(command_line); | 209 MHTMLGenerationTest::SetUpCommandLine(command_line); |
| 165 | 210 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 | 243 |
| 199 // Make sure that URLs of both frames are present | 244 // Make sure that URLs of both frames are present |
| 200 // (note that these are single-line regexes). | 245 // (note that these are single-line regexes). |
| 201 EXPECT_THAT( | 246 EXPECT_THAT( |
| 202 mhtml, | 247 mhtml, |
| 203 ContainsRegex("Content-Location:.*/frame_tree/page_with_one_frame.html")); | 248 ContainsRegex("Content-Location:.*/frame_tree/page_with_one_frame.html")); |
| 204 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*/title1.html")); | 249 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*/title1.html")); |
| 205 } | 250 } |
| 206 | 251 |
| 207 } // namespace content | 252 } // namespace content |
| OLD | NEW |