| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 class UpdateCheckerTest : public testing::Test { | 41 class UpdateCheckerTest : public testing::Test { |
| 42 public: | 42 public: |
| 43 UpdateCheckerTest(); | 43 UpdateCheckerTest(); |
| 44 ~UpdateCheckerTest() override; | 44 ~UpdateCheckerTest() override; |
| 45 | 45 |
| 46 // Overrides from testing::Test. | 46 // Overrides from testing::Test. |
| 47 void SetUp() override; | 47 void SetUp() override; |
| 48 void TearDown() override; | 48 void TearDown() override; |
| 49 | 49 |
| 50 void UpdateCheckComplete(const GURL& original_url, | 50 void UpdateCheckComplete(int error, const UpdateResponse::Results& results); |
| 51 int error, | |
| 52 const std::string& error_message, | |
| 53 const UpdateResponse::Results& results); | |
| 54 | 51 |
| 55 protected: | 52 protected: |
| 56 void Quit(); | 53 void Quit(); |
| 57 void RunThreads(); | 54 void RunThreads(); |
| 58 void RunThreadsUntilIdle(); | 55 void RunThreadsUntilIdle(); |
| 59 | 56 |
| 60 CrxUpdateItem BuildCrxUpdateItem(); | 57 CrxUpdateItem BuildCrxUpdateItem(); |
| 61 | 58 |
| 62 scoped_refptr<TestConfigurator> config_; | 59 scoped_refptr<TestConfigurator> config_; |
| 63 | 60 |
| 64 scoped_ptr<UpdateChecker> update_checker_; | 61 scoped_ptr<UpdateChecker> update_checker_; |
| 65 | 62 |
| 66 scoped_ptr<InterceptorFactory> interceptor_factory_; | 63 scoped_ptr<InterceptorFactory> interceptor_factory_; |
| 67 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory. | 64 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory. |
| 68 | 65 |
| 69 GURL original_url_; | |
| 70 int error_; | 66 int error_; |
| 71 std::string error_message_; | |
| 72 UpdateResponse::Results results_; | 67 UpdateResponse::Results results_; |
| 73 | 68 |
| 74 private: | 69 private: |
| 75 base::MessageLoopForIO loop_; | 70 base::MessageLoopForIO loop_; |
| 76 base::Closure quit_closure_; | 71 base::Closure quit_closure_; |
| 77 | 72 |
| 78 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest); | 73 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest); |
| 79 }; | 74 }; |
| 80 | 75 |
| 81 UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL), error_(0) { | 76 UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL), error_(0) { |
| 82 } | 77 } |
| 83 | 78 |
| 84 UpdateCheckerTest::~UpdateCheckerTest() { | 79 UpdateCheckerTest::~UpdateCheckerTest() { |
| 85 } | 80 } |
| 86 | 81 |
| 87 void UpdateCheckerTest::SetUp() { | 82 void UpdateCheckerTest::SetUp() { |
| 88 config_ = new TestConfigurator(base::ThreadTaskRunnerHandle::Get(), | 83 config_ = new TestConfigurator(base::ThreadTaskRunnerHandle::Get(), |
| 89 base::ThreadTaskRunnerHandle::Get()); | 84 base::ThreadTaskRunnerHandle::Get()); |
| 90 interceptor_factory_.reset( | 85 interceptor_factory_.reset( |
| 91 new InterceptorFactory(base::ThreadTaskRunnerHandle::Get())); | 86 new InterceptorFactory(base::ThreadTaskRunnerHandle::Get())); |
| 92 post_interceptor_ = interceptor_factory_->CreateInterceptor(); | 87 post_interceptor_ = interceptor_factory_->CreateInterceptor(); |
| 93 EXPECT_TRUE(post_interceptor_); | 88 EXPECT_TRUE(post_interceptor_); |
| 94 | 89 |
| 95 update_checker_.reset(); | 90 update_checker_.reset(); |
| 96 | 91 |
| 97 error_ = 0; | 92 error_ = 0; |
| 98 error_message_.clear(); | |
| 99 results_ = UpdateResponse::Results(); | 93 results_ = UpdateResponse::Results(); |
| 100 } | 94 } |
| 101 | 95 |
| 102 void UpdateCheckerTest::TearDown() { | 96 void UpdateCheckerTest::TearDown() { |
| 103 update_checker_.reset(); | 97 update_checker_.reset(); |
| 104 | 98 |
| 105 post_interceptor_ = NULL; | 99 post_interceptor_ = NULL; |
| 106 interceptor_factory_.reset(); | 100 interceptor_factory_.reset(); |
| 107 | 101 |
| 108 config_ = nullptr; | 102 config_ = nullptr; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 127 void UpdateCheckerTest::RunThreadsUntilIdle() { | 121 void UpdateCheckerTest::RunThreadsUntilIdle() { |
| 128 base::RunLoop().RunUntilIdle(); | 122 base::RunLoop().RunUntilIdle(); |
| 129 } | 123 } |
| 130 | 124 |
| 131 void UpdateCheckerTest::Quit() { | 125 void UpdateCheckerTest::Quit() { |
| 132 if (!quit_closure_.is_null()) | 126 if (!quit_closure_.is_null()) |
| 133 quit_closure_.Run(); | 127 quit_closure_.Run(); |
| 134 } | 128 } |
| 135 | 129 |
| 136 void UpdateCheckerTest::UpdateCheckComplete( | 130 void UpdateCheckerTest::UpdateCheckComplete( |
| 137 const GURL& original_url, | |
| 138 int error, | 131 int error, |
| 139 const std::string& error_message, | |
| 140 const UpdateResponse::Results& results) { | 132 const UpdateResponse::Results& results) { |
| 141 original_url_ = original_url; | |
| 142 error_ = error; | 133 error_ = error; |
| 143 error_message_ = error_message; | |
| 144 results_ = results; | 134 results_ = results; |
| 145 Quit(); | 135 Quit(); |
| 146 } | 136 } |
| 147 | 137 |
| 148 CrxUpdateItem UpdateCheckerTest::BuildCrxUpdateItem() { | 138 CrxUpdateItem UpdateCheckerTest::BuildCrxUpdateItem() { |
| 149 CrxComponent crx_component; | 139 CrxComponent crx_component; |
| 150 crx_component.name = "test_jebg"; | 140 crx_component.name = "test_jebg"; |
| 151 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); | 141 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
| 152 crx_component.installer = NULL; | 142 crx_component.installer = NULL; |
| 153 crx_component.version = base::Version("0.9"); | 143 crx_component.version = base::Version("0.9"); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 EXPECT_NE( | 182 EXPECT_NE( |
| 193 string::npos, | 183 string::npos, |
| 194 post_interceptor_->GetRequests()[0].find( | 184 post_interceptor_->GetRequests()[0].find( |
| 195 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">" | 185 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">" |
| 196 "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>")); | 186 "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>")); |
| 197 | 187 |
| 198 EXPECT_NE(string::npos, | 188 EXPECT_NE(string::npos, |
| 199 post_interceptor_->GetRequests()[0].find("<hw physmemory=")); | 189 post_interceptor_->GetRequests()[0].find("<hw physmemory=")); |
| 200 | 190 |
| 201 // Sanity check the arguments of the callback after parsing. | 191 // Sanity check the arguments of the callback after parsing. |
| 202 ASSERT_FALSE(config_->UpdateUrl().empty()); | |
| 203 EXPECT_EQ(config_->UpdateUrl().front(), original_url_); | |
| 204 EXPECT_EQ(0, error_); | 192 EXPECT_EQ(0, error_); |
| 205 EXPECT_TRUE(error_message_.empty()); | |
| 206 EXPECT_EQ(1ul, results_.list.size()); | 193 EXPECT_EQ(1ul, results_.list.size()); |
| 207 EXPECT_STREQ("jebgalgnebhfojomionfpkfelancnnkf", | 194 EXPECT_STREQ("jebgalgnebhfojomionfpkfelancnnkf", |
| 208 results_.list[0].extension_id.c_str()); | 195 results_.list[0].extension_id.c_str()); |
| 209 EXPECT_STREQ("1.0", results_.list[0].manifest.version.c_str()); | 196 EXPECT_STREQ("1.0", results_.list[0].manifest.version.c_str()); |
| 210 } | 197 } |
| 211 | 198 |
| 212 // Simulates a 403 server response error. | 199 // Simulates a 403 server response error. |
| 213 TEST_F(UpdateCheckerTest, UpdateCheckError) { | 200 TEST_F(UpdateCheckerTest, UpdateCheckError) { |
| 214 EXPECT_TRUE( | 201 EXPECT_TRUE( |
| 215 post_interceptor_->ExpectRequest(new PartialMatch("updatecheck"), 403)); | 202 post_interceptor_->ExpectRequest(new PartialMatch("updatecheck"), 403)); |
| 216 | 203 |
| 217 update_checker_ = UpdateChecker::Create(config_); | 204 update_checker_ = UpdateChecker::Create(config_); |
| 218 | 205 |
| 219 CrxUpdateItem item(BuildCrxUpdateItem()); | 206 CrxUpdateItem item(BuildCrxUpdateItem()); |
| 220 std::vector<CrxUpdateItem*> items_to_check; | 207 std::vector<CrxUpdateItem*> items_to_check; |
| 221 items_to_check.push_back(&item); | 208 items_to_check.push_back(&item); |
| 222 | 209 |
| 223 update_checker_->CheckForUpdates( | 210 update_checker_->CheckForUpdates( |
| 224 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete, | 211 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete, |
| 225 base::Unretained(this))); | 212 base::Unretained(this))); |
| 226 | |
| 227 RunThreads(); | 213 RunThreads(); |
| 228 | 214 |
| 229 EXPECT_EQ(1, post_interceptor_->GetHitCount()) | 215 EXPECT_EQ(1, post_interceptor_->GetHitCount()) |
| 230 << post_interceptor_->GetRequestsAsString(); | 216 << post_interceptor_->GetRequestsAsString(); |
| 231 EXPECT_EQ(1, post_interceptor_->GetCount()) | 217 EXPECT_EQ(1, post_interceptor_->GetCount()) |
| 232 << post_interceptor_->GetRequestsAsString(); | 218 << post_interceptor_->GetRequestsAsString(); |
| 233 | 219 |
| 234 ASSERT_FALSE(config_->UpdateUrl().empty()); | |
| 235 EXPECT_EQ(config_->UpdateUrl().front(), original_url_); | |
| 236 EXPECT_EQ(403, error_); | 220 EXPECT_EQ(403, error_); |
| 237 EXPECT_STREQ("network error", error_message_.c_str()); | |
| 238 EXPECT_EQ(0ul, results_.list.size()); | 221 EXPECT_EQ(0ul, results_.list.size()); |
| 239 } | 222 } |
| 240 | 223 |
| 241 TEST_F(UpdateCheckerTest, UpdateCheckDownloadPreference) { | 224 TEST_F(UpdateCheckerTest, UpdateCheckDownloadPreference) { |
| 242 EXPECT_TRUE(post_interceptor_->ExpectRequest( | 225 EXPECT_TRUE(post_interceptor_->ExpectRequest( |
| 243 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); | 226 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); |
| 244 | 227 |
| 245 config_->SetDownloadPreference(string("cacheable")); | 228 config_->SetDownloadPreference(string("cacheable")); |
| 246 | 229 |
| 247 update_checker_ = UpdateChecker::Create(config_); | 230 update_checker_ = UpdateChecker::Create(config_); |
| 248 | 231 |
| 249 CrxUpdateItem item(BuildCrxUpdateItem()); | 232 CrxUpdateItem item(BuildCrxUpdateItem()); |
| 250 std::vector<CrxUpdateItem*> items_to_check; | 233 std::vector<CrxUpdateItem*> items_to_check; |
| 251 items_to_check.push_back(&item); | 234 items_to_check.push_back(&item); |
| 252 | 235 |
| 253 update_checker_->CheckForUpdates( | 236 update_checker_->CheckForUpdates( |
| 254 items_to_check, "extra=\"params\"", | 237 items_to_check, "extra=\"params\"", |
| 255 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, | 238 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, |
| 256 base::Unretained(this))); | 239 base::Unretained(this))); |
| 257 | 240 |
| 258 RunThreads(); | 241 RunThreads(); |
| 259 | 242 |
| 260 // The request must contain dlpref="cacheable". | 243 // The request must contain dlpref="cacheable". |
| 261 EXPECT_NE(string::npos, | 244 EXPECT_NE(string::npos, |
| 262 post_interceptor_->GetRequests()[0].find(" dlpref=\"cacheable\"")); | 245 post_interceptor_->GetRequests()[0].find(" dlpref=\"cacheable\"")); |
| 263 } | 246 } |
| 264 | 247 |
| 248 // This test is checking that an update check signed with CUP fails, since there |
| 249 // is currently no entity that can respond with a valid signed response. |
| 250 // A proper CUP test requires network mocks, which are not available now. |
| 251 TEST_F(UpdateCheckerTest, UpdateCheckCupError) { |
| 252 EXPECT_TRUE(post_interceptor_->ExpectRequest( |
| 253 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); |
| 254 |
| 255 config_->SetUseCupSigning(true); |
| 256 update_checker_ = UpdateChecker::Create(config_); |
| 257 |
| 258 CrxUpdateItem item(BuildCrxUpdateItem()); |
| 259 std::vector<CrxUpdateItem*> items_to_check; |
| 260 items_to_check.push_back(&item); |
| 261 |
| 262 update_checker_->CheckForUpdates( |
| 263 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete, |
| 264 base::Unretained(this))); |
| 265 |
| 266 RunThreads(); |
| 267 |
| 268 EXPECT_EQ(1, post_interceptor_->GetHitCount()) |
| 269 << post_interceptor_->GetRequestsAsString(); |
| 270 ASSERT_EQ(1, post_interceptor_->GetCount()) |
| 271 << post_interceptor_->GetRequestsAsString(); |
| 272 |
| 273 // Sanity check the request. |
| 274 EXPECT_NE( |
| 275 string::npos, |
| 276 post_interceptor_->GetRequests()[0].find( |
| 277 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">" |
| 278 "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>")); |
| 279 |
| 280 // Expect an error since the response is not trusted. |
| 281 EXPECT_EQ(-10000, error_); |
| 282 EXPECT_EQ(0ul, results_.list.size()); |
| 283 } |
| 284 |
| 265 } // namespace update_client | 285 } // namespace update_client |
| OLD | NEW |