OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <map> | 5 #include <map> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/file_util_proxy.h" | 11 #include "base/file_util_proxy.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
15 #include "base/scoped_ptr.h" | 15 #include "base/scoped_ptr.h" |
16 #include "base/scoped_temp_dir.h" | 16 #include "base/scoped_temp_dir.h" |
17 #include "base/task.h" | 17 #include "base/task.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "chrome/browser/browser_thread.h" | 19 #include "chrome/browser/browser_thread.h" |
20 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 20 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
21 #include "chrome/browser/safe_browsing/csd.pb.h" | 21 #include "chrome/browser/safe_browsing/csd.pb.h" |
22 #include "chrome/common/net/test_url_fetcher_factory.h" | 22 #include "chrome/common/net/test_url_fetcher_factory.h" |
23 #include "chrome/common/net/url_fetcher.h" | 23 #include "chrome/common/net/url_fetcher.h" |
24 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
25 #include "net/url_request/url_request_status.h" | 25 #include "net/url_request/url_request_status.h" |
26 #include "third_party/skia/include/core/SkBitmap.h" | |
27 | 26 |
28 namespace safe_browsing { | 27 namespace safe_browsing { |
29 | 28 |
30 class ClientSideDetectionServiceTest : public testing::Test { | 29 class ClientSideDetectionServiceTest : public testing::Test { |
31 protected: | 30 protected: |
32 virtual void SetUp() { | 31 virtual void SetUp() { |
33 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &msg_loop_)); | 32 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &msg_loop_)); |
34 | 33 |
35 factory_.reset(new FakeURLFetcherFactory()); | 34 factory_.reset(new FakeURLFetcherFactory()); |
36 URLFetcher::set_factory(factory_.get()); | 35 URLFetcher::set_factory(factory_.get()); |
(...skipping 19 matching lines...) Loading... |
56 } | 55 } |
57 | 56 |
58 std::string ReadModelFile(base::PlatformFile model_file) { | 57 std::string ReadModelFile(base::PlatformFile model_file) { |
59 char buf[1024]; | 58 char buf[1024]; |
60 int n = base::ReadPlatformFile(model_file, 0, buf, 1024); | 59 int n = base::ReadPlatformFile(model_file, 0, buf, 1024); |
61 EXPECT_LE(0, n); | 60 EXPECT_LE(0, n); |
62 return (n < 0) ? "" : std::string(buf, n); | 61 return (n < 0) ? "" : std::string(buf, n); |
63 } | 62 } |
64 | 63 |
65 bool SendClientReportPhishingRequest(const GURL& phishing_url, | 64 bool SendClientReportPhishingRequest(const GURL& phishing_url, |
66 double score, | 65 double score) { |
67 SkBitmap thumbnail) { | |
68 csd_service_->SendClientReportPhishingRequest( | 66 csd_service_->SendClientReportPhishingRequest( |
69 phishing_url, | 67 phishing_url, |
70 score, | 68 score, |
71 thumbnail, | |
72 NewCallback(this, &ClientSideDetectionServiceTest::SendRequestDone)); | 69 NewCallback(this, &ClientSideDetectionServiceTest::SendRequestDone)); |
73 phishing_url_ = phishing_url; | 70 phishing_url_ = phishing_url; |
74 msg_loop_.Run(); // Waits until callback is called. | 71 msg_loop_.Run(); // Waits until callback is called. |
75 return is_phishing_; | 72 return is_phishing_; |
76 } | 73 } |
77 | 74 |
78 void SetModelFetchResponse(std::string response_data, bool success) { | 75 void SetModelFetchResponse(std::string response_data, bool success) { |
79 factory_->SetFakeResponse(ClientSideDetectionService::kClientModelUrl, | 76 factory_->SetFakeResponse(ClientSideDetectionService::kClientModelUrl, |
80 response_data, success); | 77 response_data, success); |
81 } | 78 } |
(...skipping 77 matching lines...) Loading... |
159 msg_loop_.RunAllPending(); | 156 msg_loop_.RunAllPending(); |
160 } | 157 } |
161 | 158 |
162 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) { | 159 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) { |
163 SetModelFetchResponse("bogus model", true /* success */); | 160 SetModelFetchResponse("bogus model", true /* success */); |
164 ScopedTempDir tmp_dir; | 161 ScopedTempDir tmp_dir; |
165 ASSERT_TRUE(tmp_dir.CreateUniqueTempDir()); | 162 ASSERT_TRUE(tmp_dir.CreateUniqueTempDir()); |
166 csd_service_.reset(ClientSideDetectionService::Create( | 163 csd_service_.reset(ClientSideDetectionService::Create( |
167 tmp_dir.path().AppendASCII("model"), NULL)); | 164 tmp_dir.path().AppendASCII("model"), NULL)); |
168 | 165 |
169 // Invalid thumbnail. | |
170 SkBitmap thumbnail; | |
171 GURL url("http://a.com/"); | 166 GURL url("http://a.com/"); |
172 double score = 0.4; // Some random client score. | 167 double score = 0.4; // Some random client score. |
173 EXPECT_FALSE(SendClientReportPhishingRequest(url, score, thumbnail)); | |
174 | |
175 // Valid thumbnail but the server returns an error. | |
176 thumbnail.setConfig(SkBitmap::kARGB_8888_Config, 100, 100); | |
177 ASSERT_TRUE(thumbnail.allocPixels()); | |
178 thumbnail.eraseRGB(255, 0, 0); | |
179 SetClientReportPhishingResponse("", false /* fail */); | |
180 EXPECT_FALSE(SendClientReportPhishingRequest(url, score, thumbnail)); | |
181 | 168 |
182 // Invalid response body from the server. | 169 // Invalid response body from the server. |
183 SetClientReportPhishingResponse("invalid proto response", true /* success */); | 170 SetClientReportPhishingResponse("invalid proto response", true /* success */); |
184 EXPECT_FALSE(SendClientReportPhishingRequest(url, score, thumbnail)); | 171 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); |
185 | 172 |
186 // Normal behavior. | 173 // Normal behavior. |
187 ClientPhishingResponse response; | 174 ClientPhishingResponse response; |
188 response.set_phishy(true); | 175 response.set_phishy(true); |
189 SetClientReportPhishingResponse(response.SerializeAsString(), | 176 SetClientReportPhishingResponse(response.SerializeAsString(), |
190 true /* success */); | 177 true /* success */); |
191 EXPECT_TRUE(SendClientReportPhishingRequest(url, score, thumbnail)); | 178 EXPECT_TRUE(SendClientReportPhishingRequest(url, score)); |
192 response.set_phishy(false); | 179 response.set_phishy(false); |
193 SetClientReportPhishingResponse(response.SerializeAsString(), | 180 SetClientReportPhishingResponse(response.SerializeAsString(), |
194 true /* success */); | 181 true /* success */); |
195 EXPECT_FALSE(SendClientReportPhishingRequest(url, score, thumbnail)); | 182 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); |
196 } | 183 } |
197 | 184 |
198 } // namespace safe_browsing | 185 } // namespace safe_browsing |
OLD | NEW |