| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/update_client/ping_manager.h" | 5 #include "components/update_client/ping_manager.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> |
| 9 | 10 |
| 11 #include "base/memory/ptr_util.h" |
| 10 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 11 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "base/version.h" | 15 #include "base/version.h" |
| 14 #include "components/update_client/crx_update_item.h" | 16 #include "components/update_client/component.h" |
| 15 #include "components/update_client/test_configurator.h" | 17 #include "components/update_client/test_configurator.h" |
| 18 #include "components/update_client/update_engine.h" |
| 16 #include "components/update_client/url_request_post_interceptor.h" | 19 #include "components/update_client/url_request_post_interceptor.h" |
| 17 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 22 |
| 20 using std::string; | 23 using std::string; |
| 21 | 24 |
| 22 namespace update_client { | 25 namespace update_client { |
| 23 | 26 |
| 24 class ComponentUpdaterPingManagerTest : public testing::Test { | 27 class PingManagerTest : public testing::Test { |
| 25 public: | 28 public: |
| 26 ComponentUpdaterPingManagerTest(); | 29 PingManagerTest(); |
| 27 ~ComponentUpdaterPingManagerTest() override {} | 30 ~PingManagerTest() override {} |
| 28 | 31 |
| 29 void RunThreadsUntilIdle(); | 32 void RunThreadsUntilIdle(); |
| 30 | 33 |
| 34 std::unique_ptr<UpdateContext> MakeFakeUpdateContext() const; |
| 35 |
| 31 // Overrides from testing::Test. | 36 // Overrides from testing::Test. |
| 32 void SetUp() override; | 37 void SetUp() override; |
| 33 void TearDown() override; | 38 void TearDown() override; |
| 34 | 39 |
| 35 protected: | 40 protected: |
| 36 scoped_refptr<TestConfigurator> config_; | 41 scoped_refptr<TestConfigurator> config_; |
| 37 std::unique_ptr<PingManager> ping_manager_; | 42 std::unique_ptr<PingManager> ping_manager_; |
| 38 | 43 |
| 39 private: | 44 private: |
| 40 base::MessageLoopForIO loop_; | 45 base::MessageLoopForIO loop_; |
| 41 }; | 46 }; |
| 42 | 47 |
| 43 ComponentUpdaterPingManagerTest::ComponentUpdaterPingManagerTest() { | 48 PingManagerTest::PingManagerTest() {} |
| 44 } | |
| 45 | 49 |
| 46 void ComponentUpdaterPingManagerTest::SetUp() { | 50 void PingManagerTest::SetUp() { |
| 47 config_ = new TestConfigurator(base::ThreadTaskRunnerHandle::Get(), | 51 config_ = new TestConfigurator(base::ThreadTaskRunnerHandle::Get(), |
| 48 base::ThreadTaskRunnerHandle::Get()); | 52 base::ThreadTaskRunnerHandle::Get()); |
| 49 ping_manager_.reset(new PingManager(config_)); | 53 ping_manager_.reset(new PingManager(config_)); |
| 50 } | 54 } |
| 51 | 55 |
| 52 void ComponentUpdaterPingManagerTest::TearDown() { | 56 void PingManagerTest::TearDown() { |
| 53 ping_manager_.reset(); | 57 ping_manager_.reset(); |
| 54 config_ = nullptr; | 58 config_ = nullptr; |
| 55 } | 59 } |
| 56 | 60 |
| 57 void ComponentUpdaterPingManagerTest::RunThreadsUntilIdle() { | 61 void PingManagerTest::RunThreadsUntilIdle() { |
| 58 base::RunLoop().RunUntilIdle(); | 62 base::RunLoop().RunUntilIdle(); |
| 59 } | 63 } |
| 60 | 64 |
| 61 TEST_F(ComponentUpdaterPingManagerTest, PingManagerTest) { | 65 std::unique_ptr<UpdateContext> PingManagerTest::MakeFakeUpdateContext() const { |
| 66 return base::MakeUnique<UpdateContext>( |
| 67 config_, false, std::vector<std::string>(), |
| 68 UpdateClient::CrxDataCallback(), UpdateEngine::NotifyObserversCallback(), |
| 69 UpdateEngine::Callback(), nullptr); |
| 70 } |
| 71 |
| 72 TEST_F(PingManagerTest, SendPing) { |
| 62 std::unique_ptr<InterceptorFactory> interceptor_factory( | 73 std::unique_ptr<InterceptorFactory> interceptor_factory( |
| 63 new InterceptorFactory(base::ThreadTaskRunnerHandle::Get())); | 74 new InterceptorFactory(base::ThreadTaskRunnerHandle::Get())); |
| 64 URLRequestPostInterceptor* interceptor = | 75 URLRequestPostInterceptor* interceptor = |
| 65 interceptor_factory->CreateInterceptor(); | 76 interceptor_factory->CreateInterceptor(); |
| 66 EXPECT_TRUE(interceptor); | 77 EXPECT_TRUE(interceptor); |
| 67 | 78 |
| 68 // Test eventresult="1" is sent for successful updates. | 79 // Test eventresult="1" is sent for successful updates. |
| 69 CrxUpdateItem item; | 80 const auto update_context = MakeFakeUpdateContext(); |
| 70 item.id = "abc"; | |
| 71 item.state = CrxUpdateItem::State::kUpdated; | |
| 72 item.previous_version = base::Version("1.0"); | |
| 73 item.next_version = base::Version("2.0"); | |
| 74 | 81 |
| 75 ping_manager_->SendPing(&item); | 82 { |
| 76 base::RunLoop().RunUntilIdle(); | 83 Component component(*update_context, "abc"); |
| 77 | 84 |
| 78 EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString(); | 85 component.state_ = base::MakeUnique<Component::StateUpdated>(&component); |
| 79 EXPECT_NE(string::npos, | 86 component.previous_version_ = base::Version("1.0"); |
| 80 interceptor->GetRequests()[0].find( | 87 component.next_version_ = base::Version("2.0"); |
| 81 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">" | |
| 82 "<event eventtype=\"3\" eventresult=\"1\"/></app>")) | |
| 83 << interceptor->GetRequestsAsString(); | |
| 84 interceptor->Reset(); | |
| 85 | 88 |
| 86 // Test eventresult="0" is sent for failed updates. | 89 ping_manager_->SendPing(component); |
| 87 item = CrxUpdateItem(); | 90 base::RunLoop().RunUntilIdle(); |
| 88 item.id = "abc"; | |
| 89 item.state = CrxUpdateItem::State::kNoUpdate; | |
| 90 item.previous_version = base::Version("1.0"); | |
| 91 item.next_version = base::Version("2.0"); | |
| 92 | 91 |
| 93 ping_manager_->SendPing(&item); | 92 EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString(); |
| 94 base::RunLoop().RunUntilIdle(); | 93 EXPECT_NE(string::npos, |
| 94 interceptor->GetRequests()[0].find( |
| 95 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">" |
| 96 "<event eventtype=\"3\" eventresult=\"1\"/></app>")) |
| 97 << interceptor->GetRequestsAsString(); |
| 98 interceptor->Reset(); |
| 99 } |
| 95 | 100 |
| 96 EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString(); | 101 { |
| 97 EXPECT_NE(string::npos, | 102 // Test eventresult="0" is sent for failed updates. |
| 98 interceptor->GetRequests()[0].find( | 103 Component component(*update_context, "abc"); |
| 99 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">" | 104 component.state_ = |
| 100 "<event eventtype=\"3\" eventresult=\"0\"/></app>")) | 105 base::MakeUnique<Component::StateUpdateError>(&component); |
| 101 << interceptor->GetRequestsAsString(); | 106 component.previous_version_ = base::Version("1.0"); |
| 102 interceptor->Reset(); | 107 component.next_version_ = base::Version("2.0"); |
| 103 | 108 |
| 104 // Test the error values and the fingerprints. | 109 ping_manager_->SendPing(component); |
| 105 item = CrxUpdateItem(); | 110 base::RunLoop().RunUntilIdle(); |
| 106 item.id = "abc"; | |
| 107 item.state = CrxUpdateItem::State::kNoUpdate; | |
| 108 item.previous_version = base::Version("1.0"); | |
| 109 item.next_version = base::Version("2.0"); | |
| 110 item.previous_fp = "prev fp"; | |
| 111 item.next_fp = "next fp"; | |
| 112 item.error_category = 1; | |
| 113 item.error_code = 2; | |
| 114 item.extra_code1 = -1; | |
| 115 item.diff_error_category = 10; | |
| 116 item.diff_error_code = 20; | |
| 117 item.diff_extra_code1 = -10; | |
| 118 item.diff_update_failed = true; | |
| 119 item.crx_diffurls.push_back(GURL("http://host/path")); | |
| 120 | 111 |
| 121 ping_manager_->SendPing(&item); | 112 EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString(); |
| 122 base::RunLoop().RunUntilIdle(); | 113 EXPECT_NE(string::npos, |
| 114 interceptor->GetRequests()[0].find( |
| 115 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">" |
| 116 "<event eventtype=\"3\" eventresult=\"0\"/></app>")) |
| 117 << interceptor->GetRequestsAsString(); |
| 118 interceptor->Reset(); |
| 119 } |
| 123 | 120 |
| 124 EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString(); | 121 { |
| 125 EXPECT_NE(string::npos, | 122 // Test the error values and the fingerprints. |
| 126 interceptor->GetRequests()[0].find( | 123 Component component(*update_context, "abc"); |
| 127 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">" | 124 component.state_ = |
| 128 "<event eventtype=\"3\" eventresult=\"0\" errorcat=\"1\" " | 125 base::MakeUnique<Component::StateUpdateError>(&component); |
| 129 "errorcode=\"2\" extracode1=\"-1\" diffresult=\"0\" " | 126 component.previous_version_ = base::Version("1.0"); |
| 130 "differrorcat=\"10\" " | 127 component.next_version_ = base::Version("2.0"); |
| 131 "differrorcode=\"20\" diffextracode1=\"-10\" " | 128 component.previous_fp_ = "prev fp"; |
| 132 "previousfp=\"prev fp\" nextfp=\"next fp\"/></app>")) | 129 component.next_fp_ = "next fp"; |
| 133 << interceptor->GetRequestsAsString(); | 130 component.error_category_ = 1; |
| 134 interceptor->Reset(); | 131 component.error_code_ = 2; |
| 132 component.extra_code1_ = -1; |
| 133 component.diff_error_category_ = 10; |
| 134 component.diff_error_code_ = 20; |
| 135 component.diff_extra_code1_ = -10; |
| 136 component.crx_diffurls_.push_back(GURL("http://host/path")); |
| 135 | 137 |
| 136 // Test the download metrics. | 138 ping_manager_->SendPing(component); |
| 137 item = CrxUpdateItem(); | 139 base::RunLoop().RunUntilIdle(); |
| 138 item.id = "abc"; | |
| 139 item.state = CrxUpdateItem::State::kUpdated; | |
| 140 item.previous_version = base::Version("1.0"); | |
| 141 item.next_version = base::Version("2.0"); | |
| 142 | 140 |
| 143 CrxDownloader::DownloadMetrics download_metrics; | 141 EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString(); |
| 144 download_metrics.url = GURL("http://host1/path1"); | 142 EXPECT_NE(string::npos, |
| 145 download_metrics.downloader = CrxDownloader::DownloadMetrics::kUrlFetcher; | 143 interceptor->GetRequests()[0].find( |
| 146 download_metrics.error = -1; | 144 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">" |
| 147 download_metrics.downloaded_bytes = 123; | 145 "<event eventtype=\"3\" eventresult=\"0\" errorcat=\"1\" " |
| 148 download_metrics.total_bytes = 456; | 146 "errorcode=\"2\" extracode1=\"-1\" diffresult=\"0\" " |
| 149 download_metrics.download_time_ms = 987; | 147 "differrorcat=\"10\" " |
| 150 item.download_metrics.push_back(download_metrics); | 148 "differrorcode=\"20\" diffextracode1=\"-10\" " |
| 149 "previousfp=\"prev fp\" nextfp=\"next fp\"/></app>")) |
| 150 << interceptor->GetRequestsAsString(); |
| 151 interceptor->Reset(); |
| 152 } |
| 151 | 153 |
| 152 download_metrics = CrxDownloader::DownloadMetrics(); | 154 { |
| 153 download_metrics.url = GURL("http://host2/path2"); | 155 // Test the download metrics. |
| 154 download_metrics.downloader = CrxDownloader::DownloadMetrics::kBits; | 156 Component component(*update_context, "abc"); |
| 155 download_metrics.error = 0; | 157 component.state_ = base::MakeUnique<Component::StateUpdated>(&component); |
| 156 download_metrics.downloaded_bytes = 1230; | 158 component.previous_version_ = base::Version("1.0"); |
| 157 download_metrics.total_bytes = 4560; | 159 component.next_version_ = base::Version("2.0"); |
| 158 download_metrics.download_time_ms = 9870; | |
| 159 item.download_metrics.push_back(download_metrics); | |
| 160 | 160 |
| 161 ping_manager_->SendPing(&item); | 161 CrxDownloader::DownloadMetrics download_metrics; |
| 162 base::RunLoop().RunUntilIdle(); | 162 download_metrics.url = GURL("http://host1/path1"); |
| 163 download_metrics.downloader = CrxDownloader::DownloadMetrics::kUrlFetcher; |
| 164 download_metrics.error = -1; |
| 165 download_metrics.downloaded_bytes = 123; |
| 166 download_metrics.total_bytes = 456; |
| 167 download_metrics.download_time_ms = 987; |
| 168 component.download_metrics_.push_back(download_metrics); |
| 163 | 169 |
| 164 EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString(); | 170 download_metrics = CrxDownloader::DownloadMetrics(); |
| 165 EXPECT_NE( | 171 download_metrics.url = GURL("http://host2/path2"); |
| 166 string::npos, | 172 download_metrics.downloader = CrxDownloader::DownloadMetrics::kBits; |
| 167 interceptor->GetRequests()[0].find( | 173 download_metrics.error = 0; |
| 168 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">" | 174 download_metrics.downloaded_bytes = 1230; |
| 169 "<event eventtype=\"3\" eventresult=\"1\"/>" | 175 download_metrics.total_bytes = 4560; |
| 170 "<event eventtype=\"14\" eventresult=\"0\" downloader=\"direct\" " | 176 download_metrics.download_time_ms = 9870; |
| 171 "errorcode=\"-1\" url=\"http://host1/path1\" downloaded=\"123\" " | 177 component.download_metrics_.push_back(download_metrics); |
| 172 "total=\"456\" download_time_ms=\"987\"/>" | 178 |
| 173 "<event eventtype=\"14\" eventresult=\"1\" downloader=\"bits\" " | 179 ping_manager_->SendPing(component); |
| 174 "url=\"http://host2/path2\" downloaded=\"1230\" total=\"4560\" " | 180 base::RunLoop().RunUntilIdle(); |
| 175 "download_time_ms=\"9870\"/></app>")) | 181 |
| 176 << interceptor->GetRequestsAsString(); | 182 EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString(); |
| 177 interceptor->Reset(); | 183 EXPECT_NE( |
| 184 string::npos, |
| 185 interceptor->GetRequests()[0].find( |
| 186 "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">" |
| 187 "<event eventtype=\"3\" eventresult=\"1\"/>" |
| 188 "<event eventtype=\"14\" eventresult=\"0\" downloader=\"direct\" " |
| 189 "errorcode=\"-1\" url=\"http://host1/path1\" downloaded=\"123\" " |
| 190 "total=\"456\" download_time_ms=\"987\"/>" |
| 191 "<event eventtype=\"14\" eventresult=\"1\" downloader=\"bits\" " |
| 192 "url=\"http://host2/path2\" downloaded=\"1230\" total=\"4560\" " |
| 193 "download_time_ms=\"9870\"/></app>")) |
| 194 << interceptor->GetRequestsAsString(); |
| 195 interceptor->Reset(); |
| 196 } |
| 178 | 197 |
| 179 interceptor_factory.reset(); | 198 interceptor_factory.reset(); |
| 180 base::RunLoop().RunUntilIdle(); | 199 base::RunLoop().RunUntilIdle(); |
| 181 } | 200 } |
| 182 | 201 |
| 183 // Tests that sending the ping fails when the component requires encryption but | 202 // Tests that sending the ping fails when the component requires encryption but |
| 184 // the ping URL is unsecure. | 203 // the ping URL is unsecure. |
| 185 TEST_F(ComponentUpdaterPingManagerTest, PingManagerRequiresEncryptionTest) { | 204 TEST_F(PingManagerTest, RequiresEncryption) { |
| 186 config_->SetPingUrl(GURL("http:\\foo\bar")); | 205 config_->SetPingUrl(GURL("http:\\foo\bar")); |
| 187 | 206 |
| 207 const auto update_context = MakeFakeUpdateContext(); |
| 208 |
| 188 { | 209 { |
| 189 CrxUpdateItem item; | 210 Component component(*update_context, "abc"); |
| 190 item.component.requires_network_encryption = true; | 211 component.crx_component_.requires_network_encryption = true; |
| 191 | 212 |
| 192 EXPECT_FALSE(ping_manager_->SendPing(&item)); | 213 EXPECT_FALSE(ping_manager_->SendPing(component)); |
| 193 } | 214 } |
| 194 | 215 |
| 195 { | 216 { |
| 196 // Tests that the default for |requires_network_encryption| is true. | 217 // Tests that the default for |requires_network_encryption| is true. |
| 197 CrxUpdateItem item; | 218 Component component(*update_context, "abc"); |
| 198 EXPECT_FALSE(ping_manager_->SendPing(&item)); | 219 EXPECT_FALSE(ping_manager_->SendPing(component)); |
| 199 } | 220 } |
| 200 } | 221 } |
| 201 | 222 |
| 202 } // namespace update_client | 223 } // namespace update_client |
| OLD | NEW |