Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "chrome/browser/safe_browsing/chrome_cleaner/chrome_cleaner_fetcher_win .h" | 5 #include "chrome/browser/safe_browsing/chrome_cleaner/chrome_cleaner_fetcher_win .h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include "base/base_paths.h" |
| 8 | 8 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/run_loop.h" | |
| 13 #include "chrome/browser/safe_browsing/chrome_cleaner/srt_field_trial_win.h" | 14 #include "chrome/browser/safe_browsing/chrome_cleaner/srt_field_trial_win.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 16 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 17 #include "net/url_request/test_url_fetcher_factory.h" | 18 #include "net/url_request/test_url_fetcher_factory.h" |
| 18 #include "net/url_request/url_fetcher_delegate.h" | 19 #include "net/url_request/url_fetcher_delegate.h" |
| 19 #include "net/url_request/url_request_status.h" | 20 #include "net/url_request/url_request_status.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 namespace safe_browsing { | 25 namespace safe_browsing { |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 class ChromeCleanerFetcherTest : public ::testing::Test { | 28 class ChromeCleanerFetcherTest : public ::testing::Test, |
| 29 public net::TestURLFetcher::DelegateForTests { | |
| 28 public: | 30 public: |
| 29 void SetUp() override { | 31 void SetUp() override { |
| 30 FetchChromeCleaner(base::Bind(&ChromeCleanerFetcherTest::FetchedCallback, | 32 FetchChromeCleaner(base::Bind(&ChromeCleanerFetcherTest::FetchedCallback, |
| 31 base::Unretained(this))); | 33 base::Unretained(this))); |
| 32 | 34 |
| 33 // Get the TestURLFetcher instance used by FetchChromeCleaner. | 35 // Get the TestURLFetcher instance used by FetchChromeCleaner. |
| 34 fetcher_ = factory_.GetFetcherByID(0); | 36 fetcher_ = factory_.GetFetcherByID(0); |
| 37 fetcher_->SetDelegateForTests(this); | |
| 38 | |
| 35 ASSERT_TRUE(fetcher_); | 39 ASSERT_TRUE(fetcher_); |
| 40 | |
| 41 // Continue only when URLFetcher's Start() method has been called. | |
| 42 run_loop_.Run(); | |
| 36 } | 43 } |
| 37 | 44 |
| 38 void FetchedCallback(base::FilePath downloaded_path, int http_response_code) { | 45 void TearDown() override { |
| 46 if (!response_path_.empty()) { | |
| 47 base::DeleteFile(response_path_, /*recursive=*/false); | |
| 48 if (base::IsDirectoryEmpty(response_path_.DirName())) | |
| 49 base::DeleteFile(response_path_.DirName(), /*recursive=*/false); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 void FetchedCallback(base::FilePath downloaded_path, | |
| 54 ChromeCleanerFetchStatus fetch_status) { | |
| 39 callback_called_ = true; | 55 callback_called_ = true; |
| 40 downloaded_path_ = downloaded_path; | 56 downloaded_path_ = downloaded_path; |
| 41 http_response_code_ = http_response_code; | 57 fetch_status_ = fetch_status; |
| 42 } | 58 } |
| 43 | 59 |
| 60 // net::TestURLFetcher::DelegateForTests overrides. | |
| 61 | |
| 62 void OnRequestStart(int fetcher_id) override { | |
| 63 // Intercept and change the response file path so that the test can clean up | |
| 64 // any created temp files. | |
|
robertshield
2017/06/12 14:36:42
How does this intercept and change the file path?
alito
2017/06/12 19:05:36
Forgot to update the comment after some changes. U
| |
| 65 EXPECT_TRUE(fetcher_->GetResponseAsFilePath(/*take_ownership=*/false, | |
| 66 &response_path_)); | |
| 67 | |
| 68 run_loop_.QuitWhenIdle(); | |
| 69 } | |
| 70 | |
| 71 void OnChunkUpload(int fetcher_id) override {} | |
| 72 void OnRequestEnd(int fetcher_id) override {} | |
| 73 | |
| 44 protected: | 74 protected: |
| 45 // TestURLFetcher requires a MessageLoop and an IO thread to release | 75 // TestURLFetcher requires a MessageLoop and an IO thread to release |
| 46 // URLRequestContextGetter in URLFetcher::Core. | 76 // URLRequestContextGetter in URLFetcher::Core. |
| 47 content::TestBrowserThreadBundle thread_bundle_; | 77 content::TestBrowserThreadBundle thread_bundle_; |
| 78 | |
| 48 // TestURLFetcherFactory automatically sets itself as URLFetcher's factory | 79 // TestURLFetcherFactory automatically sets itself as URLFetcher's factory |
| 49 net::TestURLFetcherFactory factory_; | 80 net::TestURLFetcherFactory factory_; |
| 50 | 81 |
| 51 // The TestURLFetcher instance used by the FetchChromeCleaner. | 82 // The TestURLFetcher instance used by the FetchChromeCleaner. |
| 52 net::TestURLFetcher* fetcher_ = nullptr; | 83 net::TestURLFetcher* fetcher_ = nullptr; |
| 84 | |
| 85 base::RunLoop run_loop_; | |
| 86 | |
| 87 // File path where TestULRFetcher will save a response as intercepted by | |
| 88 // OnRequestStart(). Used to clean up during teardown. | |
| 89 base::FilePath response_path_; | |
| 90 | |
| 53 // Variables set by FetchedCallback(). | 91 // Variables set by FetchedCallback(). |
| 54 bool callback_called_ = false; | 92 bool callback_called_ = false; |
| 55 base::FilePath downloaded_path_; | 93 base::FilePath downloaded_path_; |
| 56 int http_response_code_ = -1; | 94 ChromeCleanerFetchStatus fetch_status_ = |
| 95 ChromeCleanerFetchStatus::kOtherFailure; | |
| 57 }; | 96 }; |
| 58 | 97 |
| 59 TEST_F(ChromeCleanerFetcherTest, FetchSuccess) { | 98 TEST_F(ChromeCleanerFetcherTest, FetchSuccess) { |
| 60 EXPECT_EQ(GURL(fetcher_->GetOriginalURL()), GURL(GetSRTDownloadURL())); | 99 EXPECT_EQ(GURL(fetcher_->GetOriginalURL()), GURL(GetSRTDownloadURL())); |
| 61 | 100 |
| 62 // Set up the fetcher to return success. | 101 // Set up the fetcher to return success. |
| 63 fetcher_->set_status(net::URLRequestStatus{}); | 102 fetcher_->set_status(net::URLRequestStatus{}); |
| 64 fetcher_->set_response_code(net::HTTP_OK); | 103 fetcher_->set_response_code(net::HTTP_OK); |
| 65 // We need to save the file path where the response will be saved for later | |
| 66 // because ChromeCleanerFetcher takes ownership of the file after | |
| 67 // OnURLFetchComplete() has been called and we can't call | |
| 68 // GetResponseAsFilePath() after that.. | |
| 69 base::FilePath response_file; | |
| 70 fetcher_->GetResponseAsFilePath(/*take_ownership=*/false, &response_file); | |
| 71 | 104 |
| 72 // After this call, the ChromeCleanerFetcher instance will delete itself. | |
| 73 fetcher_->delegate()->OnURLFetchComplete(fetcher_); | 105 fetcher_->delegate()->OnURLFetchComplete(fetcher_); |
| 74 | 106 |
| 75 EXPECT_TRUE(callback_called_); | 107 EXPECT_TRUE(callback_called_); |
| 76 EXPECT_EQ(downloaded_path_, response_file); | 108 EXPECT_EQ(downloaded_path_, response_path_); |
| 77 EXPECT_EQ(http_response_code_, net::HTTP_OK); | 109 EXPECT_EQ(fetch_status_, ChromeCleanerFetchStatus::kSuccess); |
| 78 } | 110 } |
| 79 | 111 |
| 80 TEST_F(ChromeCleanerFetcherTest, FetchFailure) { | 112 TEST_F(ChromeCleanerFetcherTest, NotFoundOnServer) { |
| 81 // Set up the fetcher to return failure. | 113 // Set up the fetcher to return a HTTP_NOT_FOUND failure. |
| 82 fetcher_->set_status(net::URLRequestStatus::FromError(net::ERR_FAILED)); | 114 fetcher_->set_status(net::URLRequestStatus::FromError(net::ERR_FAILED)); |
| 83 fetcher_->set_response_code(net::HTTP_NOT_FOUND); | 115 fetcher_->set_response_code(net::HTTP_NOT_FOUND); |
| 84 | 116 |
| 85 // After this call, the ChromeCleanerFetcher instance will delete itself. | |
| 86 fetcher_->delegate()->OnURLFetchComplete(fetcher_); | 117 fetcher_->delegate()->OnURLFetchComplete(fetcher_); |
| 87 | 118 |
| 88 EXPECT_TRUE(callback_called_); | 119 EXPECT_TRUE(callback_called_); |
| 89 EXPECT_TRUE(downloaded_path_.empty()); | 120 EXPECT_TRUE(downloaded_path_.empty()); |
| 90 EXPECT_EQ(http_response_code_, net::HTTP_NOT_FOUND); | 121 EXPECT_EQ(fetch_status_, ChromeCleanerFetchStatus::kNotFoundOnServer); |
| 122 } | |
| 123 | |
| 124 TEST_F(ChromeCleanerFetcherTest, OtherFailure) { | |
| 125 // Set up the fetcher to return failure other than HTTP_NOT_FOUND. | |
| 126 fetcher_->set_status(net::URLRequestStatus::FromError(net::ERR_FAILED)); | |
| 127 // For this test, just use any http response code other than net::HTTP_OK and | |
| 128 // net::HTTP_NOT_FOUND. | |
| 129 fetcher_->set_response_code(net::HTTP_INTERNAL_SERVER_ERROR); | |
| 130 | |
| 131 fetcher_->delegate()->OnURLFetchComplete(fetcher_); | |
| 132 | |
| 133 EXPECT_TRUE(callback_called_); | |
| 134 EXPECT_TRUE(downloaded_path_.empty()); | |
| 135 EXPECT_EQ(fetch_status_, ChromeCleanerFetchStatus::kOtherFailure); | |
| 91 } | 136 } |
| 92 | 137 |
| 93 } // namespace | 138 } // namespace |
| 94 } // namespace safe_browsing | 139 } // namespace safe_browsing |
| OLD | NEW |