Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/test/test_simple_task_runner.h" | |
| 11 #include "base/thread_task_runner_handle.h" | |
| 10 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 11 #include "components/safe_browsing_db/safebrowsing.pb.h" | 13 #include "components/safe_browsing_db/safebrowsing.pb.h" |
| 12 #include "components/safe_browsing_db/util.h" | 14 #include "components/safe_browsing_db/util.h" |
| 13 #include "components/safe_browsing_db/v4_update_protocol_manager.h" | 15 #include "components/safe_browsing_db/v4_update_protocol_manager.h" |
| 14 #include "net/base/escape.h" | 16 #include "net/base/escape.h" |
| 15 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
| 16 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 17 #include "net/url_request/test_url_fetcher_factory.h" | 19 #include "net/url_request/test_url_fetcher_factory.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 using base::Time; | 22 using base::Time; |
| 21 using base::TimeDelta; | 23 using base::TimeDelta; |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 25 const char kClient[] = "unittest"; | 27 const char kClient[] = "unittest"; |
| 26 const char kAppVer[] = "1.0"; | 28 const char kAppVer[] = "1.0"; |
| 27 const char kKeyParam[] = "test_key_param"; | 29 const char kKeyParam[] = "test_key_param"; |
| 28 | 30 |
| 29 } // namespace | 31 } // namespace |
| 30 | 32 |
| 31 namespace safe_browsing { | 33 namespace safe_browsing { |
| 32 | 34 |
| 33 typedef V4UpdateProtocolManager::ListUpdateRequest ListUpdateRequest; | |
| 34 typedef V4UpdateProtocolManager::ListUpdateResponse ListUpdateResponse; | |
| 35 | |
| 36 class V4UpdateProtocolManagerTest : public testing::Test { | 35 class V4UpdateProtocolManagerTest : public testing::Test { |
| 37 protected: | 36 protected: |
| 38 scoped_ptr<V4UpdateProtocolManager> CreateProtocolManager() { | 37 static void ValidateGetUpdatesResults( |
|
Nathan Parker
2016/04/01 01:04:52
(I ran out of time -- will look at tests tomorrow)
vakh (use Gerrit instead)
2016/04/01 02:23:02
Acknowledged.
| |
| 38 const std::vector<ListUpdateResponse>& expected_lurs, | |
| 39 const std::vector<ListUpdateResponse>& list_update_responses) { | |
| 40 ASSERT_EQ(expected_lurs.size(), list_update_responses.size()); | |
| 41 | |
| 42 for (unsigned int i = 0; i < list_update_responses.size(); ++i) { | |
| 43 const ListUpdateResponse& expected = expected_lurs[i]; | |
| 44 const ListUpdateResponse& actual = list_update_responses[i]; | |
| 45 | |
| 46 EXPECT_EQ(expected.platform_type(), actual.platform_type()); | |
| 47 EXPECT_EQ(expected.response_type(), actual.response_type()); | |
| 48 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type()); | |
| 49 EXPECT_EQ(expected.threat_type(), actual.threat_type()); | |
| 50 EXPECT_EQ(expected.new_client_state(), actual.new_client_state()); | |
| 51 | |
| 52 // TODO(vakh): Test more fields from the proto. | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 scoped_ptr<V4UpdateProtocolManager> CreateProtocolManager( | |
| 57 const base::hash_map<UpdateListIdentifier, std::string> | |
| 58 current_list_states, | |
| 59 const std::vector<ListUpdateResponse>& expected_lurs) { | |
| 39 V4ProtocolConfig config; | 60 V4ProtocolConfig config; |
| 40 config.client_name = kClient; | 61 config.client_name = kClient; |
| 41 config.version = kAppVer; | 62 config.version = kAppVer; |
| 42 config.key_param = kKeyParam; | 63 config.key_param = kKeyParam; |
| 43 return scoped_ptr<V4UpdateProtocolManager>( | 64 config.disable_auto_update = false; |
| 44 V4UpdateProtocolManager::Create(NULL, config)); | 65 return scoped_ptr<V4UpdateProtocolManager>(V4UpdateProtocolManager::Create( |
| 66 NULL, config, current_list_states, | |
| 67 base::Bind(ValidateGetUpdatesResults, expected_lurs), true)); | |
| 45 } | 68 } |
| 46 | 69 |
| 47 void SetupListsToUpdate( | 70 void SetupCurrentListStates( |
| 48 base::hash_set<UpdateListIdentifier>* lists_to_update) { | 71 base::hash_map<UpdateListIdentifier, std::string>* current_list_states) { |
| 49 UpdateListIdentifier list_identifier; | 72 UpdateListIdentifier list_identifier; |
| 50 list_identifier.platform_type = WINDOWS_PLATFORM; | 73 list_identifier.platform_type = WINDOWS_PLATFORM; |
| 51 list_identifier.threat_entry_type = URL_EXPRESSION; | 74 list_identifier.threat_entry_type = URL_EXPRESSION; |
| 52 list_identifier.threat_type = MALWARE_THREAT; | 75 list_identifier.threat_type = MALWARE_THREAT; |
| 53 lists_to_update->insert(list_identifier); | 76 current_list_states->insert({list_identifier, "initial_state_1"}); |
| 54 | 77 |
| 55 list_identifier.platform_type = WINDOWS_PLATFORM; | 78 list_identifier.platform_type = WINDOWS_PLATFORM; |
| 56 list_identifier.threat_entry_type = URL_EXPRESSION; | 79 list_identifier.threat_entry_type = URL_EXPRESSION; |
| 57 list_identifier.threat_type = UNWANTED_SOFTWARE; | 80 list_identifier.threat_type = UNWANTED_SOFTWARE; |
| 58 lists_to_update->insert(list_identifier); | 81 current_list_states->insert({list_identifier, "initial_state_2"}); |
| 59 | 82 |
| 60 list_identifier.platform_type = WINDOWS_PLATFORM; | 83 list_identifier.platform_type = WINDOWS_PLATFORM; |
| 61 list_identifier.threat_entry_type = BINARY_DIGEST; | 84 list_identifier.threat_entry_type = BINARY_DIGEST; |
| 62 list_identifier.threat_type = MALWARE_THREAT; | 85 list_identifier.threat_type = MALWARE_THREAT; |
| 63 lists_to_update->insert(list_identifier); | 86 current_list_states->insert({list_identifier, "initial_state_3"}); |
| 64 } | 87 } |
| 65 | 88 |
| 66 void ClearListsToUpdate( | 89 void SetupExpectedListUpdateResponse( |
| 67 base::hash_set<UpdateListIdentifier>* lists_to_update) { | 90 std::vector<ListUpdateResponse>* expected_lurs) { |
| 68 lists_to_update->clear(); | 91 ListUpdateResponse lur; |
| 92 lur.set_platform_type(WINDOWS_PLATFORM); | |
| 93 lur.set_response_type(ListUpdateResponse::PARTIAL_UPDATE); | |
| 94 lur.set_threat_entry_type(URL_EXPRESSION); | |
| 95 lur.set_threat_type(MALWARE_THREAT); | |
| 96 lur.set_new_client_state("new_state_1"); | |
| 97 expected_lurs->push_back(lur); | |
| 98 | |
| 99 lur.set_platform_type(WINDOWS_PLATFORM); | |
| 100 lur.set_response_type(ListUpdateResponse::PARTIAL_UPDATE); | |
| 101 lur.set_threat_entry_type(URL_EXPRESSION); | |
| 102 lur.set_threat_type(UNWANTED_SOFTWARE); | |
| 103 lur.set_new_client_state("new_state_2"); | |
| 104 expected_lurs->push_back(lur); | |
| 105 | |
| 106 lur.set_platform_type(WINDOWS_PLATFORM); | |
| 107 lur.set_response_type(ListUpdateResponse::FULL_UPDATE); | |
| 108 lur.set_threat_entry_type(BINARY_DIGEST); | |
| 109 lur.set_threat_type(MALWARE_THREAT); | |
| 110 lur.set_new_client_state("new_state_3"); | |
| 111 expected_lurs->push_back(lur); | |
| 69 } | 112 } |
| 70 | 113 |
| 71 void SetupCurrentListStates( | 114 std::string GetExpectedV4UpdateResponse( |
| 72 const base::hash_set<UpdateListIdentifier>& lists_to_update, | 115 std::vector<ListUpdateResponse>& expected_lurs) const { |
| 73 base::hash_map<UpdateListIdentifier, std::string>* current_list_states) { | |
| 74 // TODO(vakh): Implement this to test the cases when we have an existing | |
| 75 // state for some of the lists. | |
| 76 } | |
| 77 | |
| 78 std::string GetStockV4UpdateResponse() { | |
| 79 FetchThreatListUpdatesResponse response; | 116 FetchThreatListUpdatesResponse response; |
| 80 | 117 |
| 81 ListUpdateResponse* lur = response.add_list_update_responses(); | 118 for (const auto& expected_lur : expected_lurs) { |
| 82 lur->set_platform_type(WINDOWS_PLATFORM); | 119 ListUpdateResponse* lur = response.add_list_update_responses(); |
| 83 lur->set_response_type(ListUpdateResponse::PARTIAL_UPDATE); | 120 lur->set_new_client_state(expected_lur.new_client_state()); |
| 84 lur->set_threat_entry_type(URL_EXPRESSION); | 121 lur->set_platform_type(expected_lur.platform_type()); |
| 85 lur->set_threat_type(MALWARE_THREAT); | 122 lur->set_response_type(expected_lur.response_type()); |
| 86 | 123 lur->set_threat_entry_type(expected_lur.threat_entry_type()); |
| 87 lur = response.add_list_update_responses(); | 124 lur->set_threat_type(expected_lur.threat_type()); |
| 88 lur->set_platform_type(WINDOWS_PLATFORM); | 125 } |
| 89 lur->set_response_type(ListUpdateResponse::PARTIAL_UPDATE); | |
| 90 lur->set_threat_entry_type(URL_EXPRESSION); | |
| 91 lur->set_threat_type(UNWANTED_SOFTWARE); | |
| 92 | |
| 93 lur = response.add_list_update_responses(); | |
| 94 lur->set_platform_type(WINDOWS_PLATFORM); | |
| 95 lur->set_response_type(ListUpdateResponse::FULL_UPDATE); | |
| 96 lur->set_threat_entry_type(BINARY_DIGEST); | |
| 97 lur->set_threat_type(MALWARE_THREAT); | |
| 98 | 126 |
| 99 // Serialize. | 127 // Serialize. |
| 100 std::string res_data; | 128 std::string res_data; |
| 101 response.SerializeToString(&res_data); | 129 response.SerializeToString(&res_data); |
| 102 | 130 |
| 103 return res_data; | 131 return res_data; |
| 104 } | 132 } |
| 105 }; | 133 }; |
| 106 | 134 |
| 107 void ValidateGetUpdatesResults( | 135 // TODO(vakh): Add many more tests. |
| 108 const std::vector<ListUpdateResponse>& expected_lurs, | 136 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) { |
| 109 const std::vector<ListUpdateResponse>& list_update_responses) { | 137 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 110 ASSERT_EQ(expected_lurs.size(), list_update_responses.size()); | 138 new base::TestSimpleTaskRunner()); |
| 139 base::ThreadTaskRunnerHandle runner_handler(runner); | |
| 140 net::TestURLFetcherFactory factory; | |
| 141 const base::hash_map<UpdateListIdentifier, std::string> current_list_states; | |
| 142 const std::vector<ListUpdateResponse> expected_lurs; | |
| 143 scoped_ptr<V4UpdateProtocolManager> pm( | |
| 144 CreateProtocolManager(current_list_states, expected_lurs)); | |
| 111 | 145 |
| 112 for (unsigned int i = 0; i < list_update_responses.size(); ++i) { | 146 // Initial state. No errors. |
| 113 const ListUpdateResponse& expected = expected_lurs[i]; | 147 EXPECT_EQ(0ul, pm->update_error_count_); |
| 114 const ListUpdateResponse& actual = list_update_responses[i]; | 148 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 115 | 149 |
| 116 EXPECT_EQ(expected.platform_type(), actual.platform_type()); | 150 pm->IssueUpdateRequest(); |
| 117 EXPECT_EQ(expected.response_type(), actual.response_type()); | 151 EXPECT_FALSE(pm->IsUpdateScheduled()); |
| 118 EXPECT_EQ(expected.threat_entry_type(), actual.threat_entry_type()); | |
| 119 EXPECT_EQ(expected.threat_type(), actual.threat_type()); | |
| 120 | 152 |
| 121 // TODO(vakh): Test more fields from the proto. | 153 runner->RunPendingTasks(); |
| 122 } | |
| 123 } | |
| 124 | |
| 125 // TODO(vakh): Add many more tests. | |
| 126 | |
| 127 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingNetwork) { | |
| 128 net::TestURLFetcherFactory factory; | |
| 129 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager()); | |
| 130 | |
| 131 const std::vector<ListUpdateResponse> expected_lurs; | |
| 132 const base::hash_set<UpdateListIdentifier> lists_to_update; | |
| 133 const base::hash_map<UpdateListIdentifier, std::string> current_list_states; | |
| 134 pm->GetUpdates(lists_to_update, current_list_states, | |
| 135 base::Bind(&ValidateGetUpdatesResults, expected_lurs)); | |
| 136 | 154 |
| 137 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 155 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 138 DCHECK(fetcher); | 156 DCHECK(fetcher); |
| 139 // Failed request status should result in error. | 157 // Failed request status should result in error. |
| 140 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 158 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 141 net::ERR_CONNECTION_RESET)); | 159 net::ERR_CONNECTION_RESET)); |
| 142 fetcher->delegate()->OnURLFetchComplete(fetcher); | 160 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 143 | 161 |
| 144 // Should have recorded one error, but back off multiplier is unchanged. | 162 // Should have recorded one error, but back off multiplier is unchanged. |
| 145 EXPECT_EQ(1ul, pm->update_error_count_); | 163 EXPECT_EQ(1ul, pm->update_error_count_); |
| 146 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 164 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 165 EXPECT_TRUE(pm->IsUpdateScheduled()); | |
| 147 } | 166 } |
| 148 | 167 |
| 149 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) { | 168 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesErrorHandlingResponseCode) { |
| 169 scoped_refptr<base::TestSimpleTaskRunner> runner( | |
| 170 new base::TestSimpleTaskRunner()); | |
| 171 base::ThreadTaskRunnerHandle runner_handler(runner); | |
| 150 net::TestURLFetcherFactory factory; | 172 net::TestURLFetcherFactory factory; |
| 151 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager()); | 173 const std::vector<ListUpdateResponse> expected_lurs; |
| 174 const base::hash_map<UpdateListIdentifier, std::string> current_list_states; | |
| 175 scoped_ptr<V4UpdateProtocolManager> pm( | |
| 176 CreateProtocolManager(current_list_states, expected_lurs)); | |
| 152 | 177 |
| 153 const std::vector<ListUpdateResponse> expected_lurs; | 178 pm->IssueUpdateRequest(); |
| 154 const base::hash_set<UpdateListIdentifier> lists_to_update; | 179 EXPECT_FALSE(pm->IsUpdateScheduled()); |
| 155 const base::hash_map<UpdateListIdentifier, std::string> current_list_states; | |
| 156 pm->GetUpdates(lists_to_update, current_list_states, | |
| 157 base::Bind(&ValidateGetUpdatesResults, expected_lurs)); | |
| 158 | 180 |
| 181 runner->RunPendingTasks(); | |
| 159 | 182 |
| 160 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 183 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 161 DCHECK(fetcher); | 184 DCHECK(fetcher); |
| 162 fetcher->set_status(net::URLRequestStatus()); | 185 fetcher->set_status(net::URLRequestStatus()); |
| 163 // Response code of anything other than 200 should result in error. | 186 // Response code of anything other than 200 should result in error. |
| 164 fetcher->set_response_code(204); | 187 fetcher->set_response_code(net::HTTP_NO_CONTENT); |
| 165 fetcher->SetResponseString(GetStockV4UpdateResponse()); | 188 fetcher->SetResponseString(""); |
| 166 fetcher->delegate()->OnURLFetchComplete(fetcher); | 189 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 167 | 190 |
| 168 // Should have recorded one error, but back off multiplier is unchanged. | 191 // Should have recorded one error, but back off multiplier is unchanged. |
| 169 EXPECT_EQ(1ul, pm->update_error_count_); | 192 EXPECT_EQ(1ul, pm->update_error_count_); |
| 170 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 193 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 194 EXPECT_TRUE(pm->IsUpdateScheduled()); | |
| 171 } | 195 } |
| 172 | 196 |
| 173 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesNoError) { | 197 TEST_F(V4UpdateProtocolManagerTest, TestGetUpdatesNoError) { |
| 198 scoped_refptr<base::TestSimpleTaskRunner> runner( | |
| 199 new base::TestSimpleTaskRunner()); | |
| 200 base::ThreadTaskRunnerHandle runner_handler(runner); | |
| 174 net::TestURLFetcherFactory factory; | 201 net::TestURLFetcherFactory factory; |
| 175 scoped_ptr<V4UpdateProtocolManager> pm(CreateProtocolManager()); | 202 std::vector<ListUpdateResponse> expected_lurs; |
| 203 SetupExpectedListUpdateResponse(&expected_lurs); | |
| 204 base::hash_map<UpdateListIdentifier, std::string> current_list_states; | |
| 205 SetupCurrentListStates(¤t_list_states); | |
| 206 scoped_ptr<V4UpdateProtocolManager> pm( | |
| 207 CreateProtocolManager(current_list_states, expected_lurs)); | |
| 176 | 208 |
| 209 pm->IssueUpdateRequest(); | |
| 210 EXPECT_FALSE(pm->IsUpdateScheduled()); | |
| 177 | 211 |
| 178 const std::vector<ListUpdateResponse> expected_lurs; | 212 runner->RunPendingTasks(); |
| 179 base::hash_set<UpdateListIdentifier> lists_to_update; | |
| 180 SetupListsToUpdate(&lists_to_update); | |
| 181 base::hash_map<UpdateListIdentifier, std::string> current_list_states; | |
| 182 SetupCurrentListStates(lists_to_update, ¤t_list_states); | |
| 183 pm->GetUpdates(lists_to_update, current_list_states, | |
| 184 base::Bind(&ValidateGetUpdatesResults, expected_lurs)); | |
| 185 ClearListsToUpdate(&lists_to_update); | |
| 186 | 213 |
| 187 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 214 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 188 DCHECK(fetcher); | 215 DCHECK(fetcher); |
| 189 fetcher->set_status(net::URLRequestStatus()); | 216 fetcher->set_status(net::URLRequestStatus()); |
| 190 fetcher->set_response_code(200); | 217 fetcher->set_response_code(net::HTTP_OK); |
| 191 fetcher->SetResponseString(GetStockV4UpdateResponse()); | 218 fetcher->SetResponseString(GetExpectedV4UpdateResponse(expected_lurs)); |
| 192 fetcher->delegate()->OnURLFetchComplete(fetcher); | 219 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 193 | 220 |
| 194 // No error, back off multiplier is unchanged. | 221 // No error, back off multiplier is unchanged. |
| 195 EXPECT_EQ(0ul, pm->update_error_count_); | 222 EXPECT_EQ(0ul, pm->update_error_count_); |
| 196 EXPECT_EQ(1ul, pm->update_back_off_mult_); | 223 EXPECT_EQ(1ul, pm->update_back_off_mult_); |
| 224 EXPECT_TRUE(pm->IsUpdateScheduled()); | |
| 197 } | 225 } |
| 198 | 226 |
| 199 } // namespace safe_browsing | 227 } // namespace safe_browsing |
| OLD | NEW |