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

Side by Side Diff: components/safe_browsing_db/v4_update_protocol_manager_unittest.cc

Issue 2356443002: Set client_id and client_version in requests sent to SafeBrowsing (Closed)
Patch Set: Make GetBase64SerializedUpdateRequestProto non static Created 4 years, 3 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 unified diff | Download patch
« no previous file with comments | « components/safe_browsing_db/v4_update_protocol_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/safe_browsing_db/v4_update_protocol_manager.h" 5 #include "components/safe_browsing_db/v4_update_protocol_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 EXPECT_EQ(expected.platform_type(), actual->platform_type()); 58 EXPECT_EQ(expected.platform_type(), actual->platform_type());
59 EXPECT_EQ(expected.response_type(), actual->response_type()); 59 EXPECT_EQ(expected.response_type(), actual->response_type());
60 EXPECT_EQ(expected.threat_entry_type(), actual->threat_entry_type()); 60 EXPECT_EQ(expected.threat_entry_type(), actual->threat_entry_type());
61 EXPECT_EQ(expected.threat_type(), actual->threat_type()); 61 EXPECT_EQ(expected.threat_type(), actual->threat_type());
62 EXPECT_EQ(expected.new_client_state(), actual->new_client_state()); 62 EXPECT_EQ(expected.new_client_state(), actual->new_client_state());
63 63
64 // TODO(vakh): Test more fields from the proto. 64 // TODO(vakh): Test more fields from the proto.
65 } 65 }
66 } 66 }
67 67
68 std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager( 68 V4ProtocolConfig GetProtocolConfig() {
69 const std::vector<ListUpdateResponse>& expected_lurs) {
70 V4ProtocolConfig config; 69 V4ProtocolConfig config;
71 config.client_name = kClient; 70 config.client_name = kClient;
72 config.version = kAppVer; 71 config.version = kAppVer;
73 config.key_param = kKeyParam; 72 config.key_param = kKeyParam;
74 config.disable_auto_update = false; 73 config.disable_auto_update = false;
74 return config;
75 }
76
77 std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager(
78 const std::vector<ListUpdateResponse>& expected_lurs) {
75 return V4UpdateProtocolManager::Create( 79 return V4UpdateProtocolManager::Create(
76 NULL, config, 80 NULL, GetProtocolConfig(),
77 base::Bind(&V4UpdateProtocolManagerTest::ValidateGetUpdatesResults, 81 base::Bind(&V4UpdateProtocolManagerTest::ValidateGetUpdatesResults,
78 base::Unretained(this), expected_lurs)); 82 base::Unretained(this), expected_lurs));
79 } 83 }
80 84
81 void SetupStoreStates() { 85 void SetupStoreStates() {
82 store_state_map_ = base::MakeUnique<StoreStateMap>(); 86 store_state_map_ = base::MakeUnique<StoreStateMap>();
83 87
84 UpdateListIdentifier win_url_malware(WINDOWS_PLATFORM, URL, MALWARE_THREAT); 88 UpdateListIdentifier win_url_malware(WINDOWS_PLATFORM, URL, MALWARE_THREAT);
85 store_state_map_->insert({win_url_malware, "initial_state_1"}); 89 store_state_map_->insert({win_url_malware, "initial_state_1"});
86 90
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 std::string res_data; 138 std::string res_data;
135 response.SerializeToString(&res_data); 139 response.SerializeToString(&res_data);
136 140
137 return res_data; 141 return res_data;
138 } 142 }
139 143
140 bool expect_callback_to_be_called_; 144 bool expect_callback_to_be_called_;
141 std::unique_ptr<StoreStateMap> store_state_map_; 145 std::unique_ptr<StoreStateMap> store_state_map_;
142 }; 146 };
143 147
144 // TODO(vakh): Add many more tests.
145 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) { 148 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) {
146 scoped_refptr<base::TestSimpleTaskRunner> runner( 149 scoped_refptr<base::TestSimpleTaskRunner> runner(
147 new base::TestSimpleTaskRunner()); 150 new base::TestSimpleTaskRunner());
148 base::ThreadTaskRunnerHandle runner_handler(runner); 151 base::ThreadTaskRunnerHandle runner_handler(runner);
149 net::TestURLFetcherFactory factory; 152 net::TestURLFetcherFactory factory;
150 const std::vector<ListUpdateResponse> expected_lurs; 153 const std::vector<ListUpdateResponse> expected_lurs;
151 std::unique_ptr<V4UpdateProtocolManager> pm( 154 std::unique_ptr<V4UpdateProtocolManager> pm(
152 CreateProtocolManager(expected_lurs)); 155 CreateProtocolManager(expected_lurs));
153 runner->ClearPendingTasks(); 156 runner->ClearPendingTasks();
154 157
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 // No error, back off multiplier is unchanged. 298 // No error, back off multiplier is unchanged.
296 EXPECT_EQ(0ul, pm->update_error_count_); 299 EXPECT_EQ(0ul, pm->update_error_count_);
297 EXPECT_EQ(1ul, pm->update_back_off_mult_); 300 EXPECT_EQ(1ul, pm->update_back_off_mult_);
298 EXPECT_FALSE(pm->IsUpdateScheduled()); 301 EXPECT_FALSE(pm->IsUpdateScheduled());
299 } 302 }
300 303
301 TEST_F(V4UpdateProtocolManagerTest, TestBase64EncodingUsesUrlEncoding) { 304 TEST_F(V4UpdateProtocolManagerTest, TestBase64EncodingUsesUrlEncoding) {
302 // NOTE(vakh): I handpicked this value for state by generating random strings 305 // NOTE(vakh): I handpicked this value for state by generating random strings
303 // and picked the one that leads to a '-' in the base64 url encoded request 306 // and picked the one that leads to a '-' in the base64 url encoded request
304 // output. 307 // output.
305 std::string state_minus = "z-R~3ruViQH"; 308 store_state_map_->clear();
306 StoreStateMap store_state_map_minus{ 309 (*store_state_map_)[UpdateListIdentifier(LINUX_PLATFORM, URL,
307 {UpdateListIdentifier(LINUX_PLATFORM, URL, MALWARE_THREAT), state_minus}}; 310 MALWARE_THREAT)] = "h8xfYqY>:R";
311 std::unique_ptr<V4UpdateProtocolManager> pm(
312 CreateProtocolManager(std::vector<ListUpdateResponse>({})));
313 pm->store_state_map_ = std::move(store_state_map_);
314
308 std::string encoded_request_with_minus = 315 std::string encoded_request_with_minus =
309 V4UpdateProtocolManager::GetBase64SerializedUpdateRequestProto( 316 pm->GetBase64SerializedUpdateRequestProto();
310 store_state_map_minus); 317 EXPECT_EQ("Cg8KCHVuaXR0ZXN0EgMxLjAaGAgBEAIaCmg4eGZZcVk-OlIiBCABIAIoAQ==",
311 EXPECT_EQ("GhkIARACGgt6LVJ-M3J1VmlRSCIEIAEgAigB", encoded_request_with_minus); 318 encoded_request_with_minus);
312 319
313 // NOTE(vakh): Same process for chosing this string. I am representing it 320 // TODO(vakh): Add a similar test for underscore for completeness, although
314 // in base64 encoded form because the actual state value contains non-ASCII 321 // the '-' case is sufficient to prove that we are using URL encoding.
315 // characters.
316 std::string base64_encoded_state_underscore = "VTFfITBf4lBM";
317 std::string state_underscore;
318 base::Base64Decode(base64_encoded_state_underscore, &state_underscore);
319 StoreStateMap store_state_map_underscore{
320 {UpdateListIdentifier(LINUX_PLATFORM, URL, MALWARE_THREAT),
321 state_underscore}};
322 std::string encoded_request_with_underscore =
323 V4UpdateProtocolManager::GetBase64SerializedUpdateRequestProto(
324 store_state_map_underscore);
325 EXPECT_EQ("GhcIARACGglVMV8hMF_iUEwiBCABIAIoAQ==",
326 encoded_request_with_underscore);
327 } 322 }
328 323
329 } // namespace safe_browsing 324 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_update_protocol_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698