| 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 "chrome/browser/safe_browsing/client_side_model_loader.h" |
| 6 |
| 5 #include <stdint.h> | 7 #include <stdint.h> |
| 6 | 8 |
| 7 #include <map> | 9 #include <map> |
| 10 #include <memory> |
| 8 #include <string> | 11 #include <string> |
| 9 | 12 |
| 10 #include "base/callback.h" | 13 #include "base/callback.h" |
| 11 #include "base/macros.h" | 14 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/metrics/field_trial.h" | 15 #include "base/metrics/field_trial.h" |
| 14 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 15 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 17 #include "chrome/browser/safe_browsing/client_side_model_loader.h" | |
| 18 #include "chrome/common/safe_browsing/client_model.pb.h" | 19 #include "chrome/common/safe_browsing/client_model.pb.h" |
| 19 #include "chrome/common/safe_browsing/csd.pb.h" | 20 #include "chrome/common/safe_browsing/csd.pb.h" |
| 20 #include "components/variations/variations_associated_data.h" | 21 #include "components/variations/variations_associated_data.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 #include "net/http/http_status_code.h" | 23 #include "net/http/http_status_code.h" |
| 23 #include "net/url_request/test_url_fetcher_factory.h" | 24 #include "net/url_request/test_url_fetcher_factory.h" |
| 24 #include "net/url_request/url_request_status.h" | 25 #include "net/url_request/url_request_status.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 89 |
| 89 void SetModelFetchResponse(std::string response_data, | 90 void SetModelFetchResponse(std::string response_data, |
| 90 net::HttpStatusCode response_code, | 91 net::HttpStatusCode response_code, |
| 91 net::URLRequestStatus::Status status) { | 92 net::URLRequestStatus::Status status) { |
| 92 CHECK(model_url_.is_valid()); | 93 CHECK(model_url_.is_valid()); |
| 93 factory_->SetFakeResponse(model_url_, response_data, response_code, status); | 94 factory_->SetFakeResponse(model_url_, response_data, response_code, status); |
| 94 } | 95 } |
| 95 | 96 |
| 96 private: | 97 private: |
| 97 content::TestBrowserThreadBundle thread_bundle_; | 98 content::TestBrowserThreadBundle thread_bundle_; |
| 98 scoped_ptr<net::FakeURLFetcherFactory> factory_; | 99 std::unique_ptr<net::FakeURLFetcherFactory> factory_; |
| 99 scoped_ptr<base::FieldTrialList> field_trials_; | 100 std::unique_ptr<base::FieldTrialList> field_trials_; |
| 100 GURL model_url_; | 101 GURL model_url_; |
| 101 }; | 102 }; |
| 102 | 103 |
| 103 ACTION_P(InvokeClosure, closure) { | 104 ACTION_P(InvokeClosure, closure) { |
| 104 closure.Run(); | 105 closure.Run(); |
| 105 } | 106 } |
| 106 | 107 |
| 107 // Test the reponse to many variations of model responses. | 108 // Test the reponse to many variations of model responses. |
| 108 TEST_F(ModelLoaderTest, FetchModelTest) { | 109 TEST_F(ModelLoaderTest, FetchModelTest) { |
| 109 StrictMock<MockModelLoader> loader(base::Closure(), "top_model.pb"); | 110 StrictMock<MockModelLoader> loader(base::Closure(), "top_model.pb"); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 | 280 |
| 280 // Test that Finch params control the model names. | 281 // Test that Finch params control the model names. |
| 281 TEST_F(ModelLoaderTest, ModelNamesTest) { | 282 TEST_F(ModelLoaderTest, ModelNamesTest) { |
| 282 // Test the name-templating. | 283 // Test the name-templating. |
| 283 EXPECT_EQ(ModelLoader::FillInModelName(true, 3), | 284 EXPECT_EQ(ModelLoader::FillInModelName(true, 3), |
| 284 "client_model_v5_ext_variation_3.pb"); | 285 "client_model_v5_ext_variation_3.pb"); |
| 285 EXPECT_EQ(ModelLoader::FillInModelName(false, 5), | 286 EXPECT_EQ(ModelLoader::FillInModelName(false, 5), |
| 286 "client_model_v5_variation_5.pb"); | 287 "client_model_v5_variation_5.pb"); |
| 287 | 288 |
| 288 // No Finch setup. Should default to 0. | 289 // No Finch setup. Should default to 0. |
| 289 scoped_ptr<ModelLoader> loader; | 290 std::unique_ptr<ModelLoader> loader; |
| 290 loader.reset(new ModelLoader(base::Closure(), NULL, | 291 loader.reset(new ModelLoader(base::Closure(), NULL, |
| 291 false /* is_extended_reporting */)); | 292 false /* is_extended_reporting */)); |
| 292 EXPECT_EQ(loader->name(), "client_model_v5_variation_0.pb"); | 293 EXPECT_EQ(loader->name(), "client_model_v5_variation_0.pb"); |
| 293 EXPECT_EQ(loader->url_.spec(), | 294 EXPECT_EQ(loader->url_.spec(), |
| 294 "https://ssl.gstatic.com/safebrowsing/csd/" | 295 "https://ssl.gstatic.com/safebrowsing/csd/" |
| 295 "client_model_v5_variation_0.pb"); | 296 "client_model_v5_variation_0.pb"); |
| 296 | 297 |
| 297 // Model 1, no extended reporting. | 298 // Model 1, no extended reporting. |
| 298 SetFinchModelNumber(1); | 299 SetFinchModelNumber(1); |
| 299 loader.reset(new ModelLoader(base::Closure(), NULL, false)); | 300 loader.reset(new ModelLoader(base::Closure(), NULL, false)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 EXPECT_FALSE(ModelLoader::ModelHasValidHashIds(model)); | 337 EXPECT_FALSE(ModelLoader::ModelHasValidHashIds(model)); |
| 337 | 338 |
| 338 rule->set_feature(2, 2); | 339 rule->set_feature(2, 2); |
| 339 EXPECT_FALSE(ModelLoader::ModelHasValidHashIds(model)); | 340 EXPECT_FALSE(ModelLoader::ModelHasValidHashIds(model)); |
| 340 | 341 |
| 341 rule->set_feature(2, 1); | 342 rule->set_feature(2, 1); |
| 342 EXPECT_TRUE(ModelLoader::ModelHasValidHashIds(model)); | 343 EXPECT_TRUE(ModelLoader::ModelHasValidHashIds(model)); |
| 343 } | 344 } |
| 344 | 345 |
| 345 } // namespace safe_browsing | 346 } // namespace safe_browsing |
| OLD | NEW |