Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2282)

Unified Diff: chrome/browser/safe_browsing/download_protection_service_unittest.cc

Issue 8345033: Collect some histograms about signed binary downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/safe_browsing/download_protection_service_unittest.cc
===================================================================
--- chrome/browser/safe_browsing/download_protection_service_unittest.cc (revision 107249)
+++ chrome/browser/safe_browsing/download_protection_service_unittest.cc (working copy)
@@ -9,13 +9,16 @@
#include "base/bind.h"
#include "base/callback.h"
+#include "base/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "chrome/common/safe_browsing/csd.pb.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "content/browser/browser_thread.h"
+#include "content/browser/download/download_item.h"
#include "content/common/net/url_fetcher.h"
+#include "content/public/common/url_fetcher_delegate.h"
#include "content/test/test_url_fetcher_factory.h"
#include "googleurl/src/gurl.h"
#include "testing/gmock/include/gmock/gmock.h"
@@ -42,19 +45,25 @@
protected:
virtual void SetUp() {
ui_thread_.reset(new BrowserThread(BrowserThread::UI, &msg_loop_));
- io_thread_.reset(new BrowserThread(BrowserThread::IO, &msg_loop_));
+ // Start real threads for the IO and File threads so that the DCHECKs
+ // to test that we're on the correct thread work.
+ io_thread_.reset(new BrowserThread(BrowserThread::IO));
+ ASSERT_TRUE(io_thread_->Start());
+ file_thread_.reset(new BrowserThread(BrowserThread::FILE));
+ ASSERT_TRUE(file_thread_->Start());
sb_service_ = new MockSafeBrowsingService();
- download_service_ = new DownloadProtectionService(sb_service_.get(),
- NULL);
+ download_service_ = sb_service_->download_protection_service();
download_service_->SetEnabled(true);
msg_loop_.RunAllPending();
}
virtual void TearDown() {
- msg_loop_.RunAllPending();
- download_service_ = NULL;
+ // Flush all of the thread message loops to ensure that there are no
+ // tasks currently running.
+ FlushThreadMessageLoops();
sb_service_ = NULL;
io_thread_.reset();
+ file_thread_.reset();
ui_thread_.reset();
}
@@ -72,6 +81,45 @@
return false;
}
+ // Flushes any pending tasks in the message loops of all threads.
+ void FlushThreadMessageLoops() {
+ FlushMessageLoop(BrowserThread::FILE);
+ FlushMessageLoop(BrowserThread::IO);
+ msg_loop_.RunAllPending();
+ }
+
+ private:
+ // Helper functions for FlushThreadMessageLoops.
+ void RunAllPendingAndQuitUI() {
+ MessageLoop::current()->RunAllPending();
+ BrowserThread::PostTask(
+ BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&DownloadProtectionServiceTest::QuitMessageLoop,
+ base::Unretained(this)));
+ }
+
+ void QuitMessageLoop() {
+ MessageLoop::current()->Quit();
+ }
+
+ void PostRunMessageLoopTask(BrowserThread::ID thread) {
+ BrowserThread::PostTask(
+ thread,
+ FROM_HERE,
+ base::Bind(&DownloadProtectionServiceTest::RunAllPendingAndQuitUI,
+ base::Unretained(this)));
+ }
+
+ void FlushMessageLoop(BrowserThread::ID thread) {
+ BrowserThread::PostTask(
+ BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(&DownloadProtectionServiceTest::PostRunMessageLoopTask,
+ base::Unretained(this), thread));
+ msg_loop_.Run();
+ }
+
public:
void CheckDoneCallback(
DownloadProtectionService::DownloadCheckResult result) {
@@ -79,36 +127,51 @@
msg_loop_.Quit();
}
+ void SendURLFetchComplete(TestURLFetcher* fetcher) {
+ fetcher->delegate()->OnURLFetchComplete(fetcher);
+ }
+
protected:
scoped_refptr<MockSafeBrowsingService> sb_service_;
- scoped_refptr<DownloadProtectionService> download_service_;
+ DownloadProtectionService* download_service_;
MessageLoop msg_loop_;
DownloadProtectionService::DownloadCheckResult result_;
scoped_ptr<BrowserThread> io_thread_;
+ scoped_ptr<BrowserThread> file_thread_;
scoped_ptr<BrowserThread> ui_thread_;
};
TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) {
DownloadProtectionService::DownloadInfo info;
- EXPECT_TRUE(download_service_->CheckClientDownload(
+ download_service_->CheckClientDownload(
info,
base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
- base::Unretained(this))));
+ base::Unretained(this)));
+ msg_loop_.Run();
+ EXPECT_EQ(DownloadProtectionService::SAFE, result_);
+
// Only http is supported for now.
+ info.local_file = FilePath(FILE_PATH_LITERAL("a.exe"));
info.download_url_chain.push_back(GURL("https://www.google.com/"));
- EXPECT_TRUE(download_service_->CheckClientDownload(
+ download_service_->CheckClientDownload(
info,
base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
- base::Unretained(this))));
+ base::Unretained(this)));
+ msg_loop_.Run();
+ EXPECT_EQ(DownloadProtectionService::SAFE, result_);
+
info.download_url_chain[0] = GURL("ftp://www.google.com/");
- EXPECT_TRUE(download_service_->CheckClientDownload(
+ download_service_->CheckClientDownload(
info,
base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
- base::Unretained(this))));
+ base::Unretained(this)));
+ msg_loop_.Run();
+ EXPECT_EQ(DownloadProtectionService::SAFE, result_);
}
TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) {
DownloadProtectionService::DownloadInfo info;
+ info.local_file = FilePath(FILE_PATH_LITERAL("a.exe"));
info.download_url_chain.push_back(GURL("http://www.evil.com/bla.exe"));
info.download_url_chain.push_back(GURL("http://www.google.com/a.exe"));
info.referrer_url = GURL("http://www.google.com/");
@@ -119,20 +182,20 @@
MatchDownloadWhitelistUrl(GURL("http://www.google.com/a.exe")))
.WillRepeatedly(Return(true));
- EXPECT_FALSE(download_service_->CheckClientDownload(
+ download_service_->CheckClientDownload(
info,
base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
- base::Unretained(this))));
+ base::Unretained(this)));
msg_loop_.Run();
EXPECT_EQ(DownloadProtectionService::SAFE, result_);
// Check that the referrer is matched against the whitelist.
info.download_url_chain.pop_back();
info.referrer_url = GURL("http://www.google.com/a.exe");
- EXPECT_FALSE(download_service_->CheckClientDownload(
+ download_service_->CheckClientDownload(
info,
base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
- base::Unretained(this))));
+ base::Unretained(this)));
msg_loop_.Run();
EXPECT_EQ(DownloadProtectionService::SAFE, result_);
}
@@ -147,12 +210,13 @@
.WillRepeatedly(Return(false));
DownloadProtectionService::DownloadInfo info;
+ info.local_file = FilePath(FILE_PATH_LITERAL("a.exe"));
info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe"));
info.referrer_url = GURL("http://www.google.com/");
- EXPECT_FALSE(download_service_->CheckClientDownload(
+ download_service_->CheckClientDownload(
info,
base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
- base::Unretained(this))));
+ base::Unretained(this)));
msg_loop_.Run();
EXPECT_EQ(DownloadProtectionService::SAFE, result_);
}
@@ -167,12 +231,13 @@
.WillRepeatedly(Return(false));
DownloadProtectionService::DownloadInfo info;
+ info.local_file = FilePath(FILE_PATH_LITERAL("a.exe"));
info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe"));
info.referrer_url = GURL("http://www.google.com/");
- EXPECT_FALSE(download_service_->CheckClientDownload(
+ download_service_->CheckClientDownload(
info,
base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
- base::Unretained(this))));
+ base::Unretained(this)));
msg_loop_.Run();
EXPECT_EQ(DownloadProtectionService::SAFE, result_);
@@ -180,10 +245,10 @@
factory.SetFakeResponse(
DownloadProtectionService::kDownloadRequestUrl, "bla", true);
- EXPECT_FALSE(download_service_->CheckClientDownload(
+ download_service_->CheckClientDownload(
info,
base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
- base::Unretained(this))));
+ base::Unretained(this)));
msg_loop_.Run();
EXPECT_EQ(DownloadProtectionService::SAFE, result_);
}
@@ -192,6 +257,7 @@
TestURLFetcherFactory factory;
DownloadProtectionService::DownloadInfo info;
+ info.local_file = FilePath(FILE_PATH_LITERAL("bla.exe"));
info.download_url_chain.push_back(GURL("http://www.google.com/"));
info.download_url_chain.push_back(GURL("http://www.google.com/bla.exe"));
info.referrer_url = GURL("http://www.google.com/");
@@ -201,16 +267,17 @@
EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_))
.WillRepeatedly(Return(false));
- EXPECT_FALSE(download_service_->CheckClientDownload(
+ download_service_->CheckClientDownload(
info,
base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
- base::Unretained(this))));
- msg_loop_.RunAllPending(); // Wait until StartCheckClientDownload is called.
+ base::Unretained(this)));
+ // Run the message loop(s) until SendRequest is called.
+ FlushThreadMessageLoops();
- ASSERT_TRUE(factory.GetFetcherByID(0));
+ TestURLFetcher* fetcher = factory.GetFetcherByID(0);
+ ASSERT_TRUE(fetcher);
ClientDownloadRequest request;
- EXPECT_TRUE(request.ParseFromString(
- factory.GetFetcherByID(0)->upload_data()));
+ EXPECT_TRUE(request.ParseFromString(fetcher->upload_data()));
EXPECT_EQ("http://www.google.com/bla.exe", request.url());
EXPECT_EQ(info.sha256_hash, request.digests().sha256());
EXPECT_EQ(info.total_bytes, request.length());
@@ -223,5 +290,12 @@
ClientDownloadRequest::DOWNLOAD_URL,
"http://www.google.com/bla.exe",
info.referrer_url.spec()));
+
+ // Simulate the request finishing.
+ MessageLoop::current()->PostTask(
+ FROM_HERE,
+ base::Bind(&DownloadProtectionServiceTest::SendURLFetchComplete,
+ base::Unretained(this), fetcher));
+ msg_loop_.Run();
}
} // namespace safe_browsing

Powered by Google App Engine
This is Rietveld 408576698