| 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/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 class UpdateCheckerTest : public testing::Test { | 35 class UpdateCheckerTest : public testing::Test { |
| 36 public: | 36 public: |
| 37 UpdateCheckerTest(); | 37 UpdateCheckerTest(); |
| 38 virtual ~UpdateCheckerTest(); | 38 virtual ~UpdateCheckerTest(); |
| 39 | 39 |
| 40 // Overrides from testing::Test. | 40 // Overrides from testing::Test. |
| 41 virtual void SetUp() OVERRIDE; | 41 virtual void SetUp() OVERRIDE; |
| 42 virtual void TearDown() OVERRIDE; | 42 virtual void TearDown() OVERRIDE; |
| 43 | 43 |
| 44 void UpdateCheckComplete(int error, | 44 void UpdateCheckComplete(const GURL& original_url, |
| 45 int error, |
| 45 const std::string& error_message, | 46 const std::string& error_message, |
| 46 const UpdateResponse::Results& results); | 47 const UpdateResponse::Results& results); |
| 47 | 48 |
| 48 protected: | 49 protected: |
| 49 void Quit(); | 50 void Quit(); |
| 50 void RunThreads(); | 51 void RunThreads(); |
| 51 void RunThreadsUntilIdle(); | 52 void RunThreadsUntilIdle(); |
| 52 | 53 |
| 53 CrxUpdateItem BuildCrxUpdateItem(); | 54 CrxUpdateItem BuildCrxUpdateItem(); |
| 54 | 55 |
| 55 scoped_ptr<TestConfigurator> config_; | 56 scoped_ptr<TestConfigurator> config_; |
| 56 | 57 |
| 57 scoped_ptr<UpdateChecker> update_checker_; | 58 scoped_ptr<UpdateChecker> update_checker_; |
| 58 | 59 |
| 59 scoped_ptr<InterceptorFactory> interceptor_factory_; | 60 scoped_ptr<InterceptorFactory> interceptor_factory_; |
| 60 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory. | 61 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory. |
| 61 | 62 |
| 63 GURL original_url_; |
| 62 int error_; | 64 int error_; |
| 63 std::string error_message_; | 65 std::string error_message_; |
| 64 UpdateResponse::Results results_; | 66 UpdateResponse::Results results_; |
| 65 | 67 |
| 66 private: | 68 private: |
| 67 base::MessageLoopForIO loop_; | 69 base::MessageLoopForIO loop_; |
| 68 base::Closure quit_closure_; | 70 base::Closure quit_closure_; |
| 69 | 71 |
| 70 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest); | 72 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest); |
| 71 }; | 73 }; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 95 | 97 |
| 96 void UpdateCheckerTest::TearDown() { | 98 void UpdateCheckerTest::TearDown() { |
| 97 update_checker_.reset(); | 99 update_checker_.reset(); |
| 98 | 100 |
| 99 post_interceptor_ = NULL; | 101 post_interceptor_ = NULL; |
| 100 interceptor_factory_.reset(); | 102 interceptor_factory_.reset(); |
| 101 | 103 |
| 102 config_.reset(); | 104 config_.reset(); |
| 103 | 105 |
| 104 // The PostInterceptor requires the message loop to run to destruct correctly. | 106 // The PostInterceptor requires the message loop to run to destruct correctly. |
| 105 // TODO: This is fragile and should be fixed. | 107 // TODO(sorin): This is fragile and should be fixed. |
| 106 RunThreadsUntilIdle(); | 108 RunThreadsUntilIdle(); |
| 107 } | 109 } |
| 108 | 110 |
| 109 void UpdateCheckerTest::RunThreads() { | 111 void UpdateCheckerTest::RunThreads() { |
| 110 base::RunLoop runloop; | 112 base::RunLoop runloop; |
| 111 quit_closure_ = runloop.QuitClosure(); | 113 quit_closure_ = runloop.QuitClosure(); |
| 112 runloop.Run(); | 114 runloop.Run(); |
| 113 | 115 |
| 114 // Since some tests need to drain currently enqueued tasks such as network | 116 // Since some tests need to drain currently enqueued tasks such as network |
| 115 // intercepts on the IO thread, run the threads until they are | 117 // intercepts on the IO thread, run the threads until they are |
| 116 // idle. The component updater service won't loop again until the loop count | 118 // idle. The component updater service won't loop again until the loop count |
| 117 // is set and the service is started. | 119 // is set and the service is started. |
| 118 RunThreadsUntilIdle(); | 120 RunThreadsUntilIdle(); |
| 119 } | 121 } |
| 120 | 122 |
| 121 void UpdateCheckerTest::RunThreadsUntilIdle() { | 123 void UpdateCheckerTest::RunThreadsUntilIdle() { |
| 122 base::RunLoop().RunUntilIdle(); | 124 base::RunLoop().RunUntilIdle(); |
| 123 } | 125 } |
| 124 | 126 |
| 125 void UpdateCheckerTest::Quit() { | 127 void UpdateCheckerTest::Quit() { |
| 126 if (!quit_closure_.is_null()) | 128 if (!quit_closure_.is_null()) |
| 127 quit_closure_.Run(); | 129 quit_closure_.Run(); |
| 128 } | 130 } |
| 129 | 131 |
| 130 void UpdateCheckerTest::UpdateCheckComplete( | 132 void UpdateCheckerTest::UpdateCheckComplete( |
| 133 const GURL& original_url, |
| 131 int error, | 134 int error, |
| 132 const std::string& error_message, | 135 const std::string& error_message, |
| 133 const UpdateResponse::Results& results) { | 136 const UpdateResponse::Results& results) { |
| 137 original_url_ = original_url; |
| 134 error_ = error; | 138 error_ = error; |
| 135 error_message_ = error_message; | 139 error_message_ = error_message; |
| 136 results_ = results; | 140 results_ = results; |
| 137 Quit(); | 141 Quit(); |
| 138 } | 142 } |
| 139 | 143 |
| 140 CrxUpdateItem UpdateCheckerTest::BuildCrxUpdateItem() { | 144 CrxUpdateItem UpdateCheckerTest::BuildCrxUpdateItem() { |
| 141 CrxComponent crx_component; | 145 CrxComponent crx_component; |
| 142 crx_component.name = "test_jebg"; | 146 crx_component.name = "test_jebg"; |
| 143 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); | 147 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); |
| 144 crx_component.installer = NULL; | 148 crx_component.installer = NULL; |
| 145 crx_component.version = base::Version("0.9"); | 149 crx_component.version = base::Version("0.9"); |
| 146 crx_component.fingerprint = "fp1"; | 150 crx_component.fingerprint = "fp1"; |
| 147 | 151 |
| 148 CrxUpdateItem crx_update_item; | 152 CrxUpdateItem crx_update_item; |
| 149 crx_update_item.status = CrxUpdateItem::kNew; | 153 crx_update_item.status = CrxUpdateItem::kNew; |
| 150 crx_update_item.id = "jebgalgnebhfojomionfpkfelancnnkf"; | 154 crx_update_item.id = "jebgalgnebhfojomionfpkfelancnnkf"; |
| 151 crx_update_item.component = crx_component; | 155 crx_update_item.component = crx_component; |
| 152 | 156 |
| 153 return crx_update_item; | 157 return crx_update_item; |
| 154 } | 158 } |
| 155 | 159 |
| 156 TEST_F(UpdateCheckerTest, UpdateCheckSuccess) { | 160 TEST_F(UpdateCheckerTest, UpdateCheckSuccess) { |
| 157 EXPECT_TRUE(post_interceptor_->ExpectRequest( | 161 EXPECT_TRUE(post_interceptor_->ExpectRequest( |
| 158 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); | 162 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); |
| 159 | 163 |
| 160 update_checker_ = | 164 update_checker_ = UpdateChecker::Create(*config_).Pass(); |
| 161 UpdateChecker::Create(*config_, | |
| 162 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, | |
| 163 base::Unretained(this))).Pass(); | |
| 164 | 165 |
| 165 CrxUpdateItem item(BuildCrxUpdateItem()); | 166 CrxUpdateItem item(BuildCrxUpdateItem()); |
| 166 std::vector<CrxUpdateItem*> items_to_check; | 167 std::vector<CrxUpdateItem*> items_to_check; |
| 167 items_to_check.push_back(&item); | 168 items_to_check.push_back(&item); |
| 168 | 169 |
| 169 update_checker_->CheckForUpdates(items_to_check, "extra=\"params\""); | 170 update_checker_->CheckForUpdates( |
| 171 items_to_check, |
| 172 "extra=\"params\"", |
| 173 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, |
| 174 base::Unretained(this))); |
| 170 | 175 |
| 171 RunThreads(); | 176 RunThreads(); |
| 172 | 177 |
| 173 EXPECT_EQ(1, post_interceptor_->GetHitCount()) | 178 EXPECT_EQ(1, post_interceptor_->GetHitCount()) |
| 174 << post_interceptor_->GetRequestsAsString(); | 179 << post_interceptor_->GetRequestsAsString(); |
| 175 EXPECT_EQ(1, post_interceptor_->GetCount()) | 180 EXPECT_EQ(1, post_interceptor_->GetCount()) |
| 176 << post_interceptor_->GetRequestsAsString(); | 181 << post_interceptor_->GetRequestsAsString(); |
| 177 | 182 |
| 178 // Sanity check the request. | 183 // Sanity check the request. |
| 179 EXPECT_NE( | 184 EXPECT_NE( |
| 180 string::npos, | 185 string::npos, |
| 181 post_interceptor_->GetRequests()[0].find( | 186 post_interceptor_->GetRequests()[0].find( |
| 182 "request protocol=\"3.0\" extra=\"params\"")); | 187 "request protocol=\"3.0\" extra=\"params\"")); |
| 183 EXPECT_NE( | 188 EXPECT_NE( |
| 184 string::npos, | 189 string::npos, |
| 185 post_interceptor_->GetRequests()[0].find( | 190 post_interceptor_->GetRequests()[0].find( |
| 186 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">" | 191 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">" |
| 187 "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>")); | 192 "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>")); |
| 188 | 193 |
| 189 EXPECT_NE(string::npos, | 194 EXPECT_NE(string::npos, |
| 190 post_interceptor_->GetRequests()[0].find("<hw physmemory=")); | 195 post_interceptor_->GetRequests()[0].find("<hw physmemory=")); |
| 191 | 196 |
| 192 // Sanity check the arguments of the callback after parsing. | 197 // Sanity check the arguments of the callback after parsing. |
| 198 EXPECT_EQ(config_->UpdateUrl().front(), original_url_); |
| 193 EXPECT_EQ(0, error_); | 199 EXPECT_EQ(0, error_); |
| 194 EXPECT_TRUE(error_message_.empty()); | 200 EXPECT_TRUE(error_message_.empty()); |
| 195 EXPECT_EQ(1ul, results_.list.size()); | 201 EXPECT_EQ(1ul, results_.list.size()); |
| 196 EXPECT_STREQ("jebgalgnebhfojomionfpkfelancnnkf", | 202 EXPECT_STREQ("jebgalgnebhfojomionfpkfelancnnkf", |
| 197 results_.list[0].extension_id.c_str()); | 203 results_.list[0].extension_id.c_str()); |
| 198 EXPECT_STREQ("1.0", results_.list[0].manifest.version.c_str()); | 204 EXPECT_STREQ("1.0", results_.list[0].manifest.version.c_str()); |
| 199 } | 205 } |
| 200 | 206 |
| 201 TEST_F(UpdateCheckerTest, UpdateNetworkError) { | 207 // Simulates a 403 server response error. |
| 202 // Setting this expectation simulates a network error since the | 208 TEST_F(UpdateCheckerTest, UpdateCheckError) { |
| 203 // file is not found. Since setting the expectation fails, this function | 209 EXPECT_TRUE( |
| 204 // owns |request_matcher|. | 210 post_interceptor_->ExpectRequest(new PartialMatch("updatecheck"), 403)); |
| 205 scoped_ptr<PartialMatch> request_matcher(new PartialMatch("updatecheck")); | |
| 206 EXPECT_FALSE(post_interceptor_->ExpectRequest(request_matcher.get(), | |
| 207 test_file("no such file"))); | |
| 208 | 211 |
| 209 update_checker_ = | 212 update_checker_ = UpdateChecker::Create(*config_).Pass(); |
| 210 UpdateChecker::Create(*config_, | |
| 211 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, | |
| 212 base::Unretained(this))).Pass(); | |
| 213 | 213 |
| 214 CrxUpdateItem item(BuildCrxUpdateItem()); | 214 CrxUpdateItem item(BuildCrxUpdateItem()); |
| 215 std::vector<CrxUpdateItem*> items_to_check; | 215 std::vector<CrxUpdateItem*> items_to_check; |
| 216 items_to_check.push_back(&item); | 216 items_to_check.push_back(&item); |
| 217 | 217 |
| 218 update_checker_->CheckForUpdates(items_to_check, ""); | 218 update_checker_->CheckForUpdates( |
| 219 items_to_check, |
| 220 "", |
| 221 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, |
| 222 base::Unretained(this))); |
| 219 | 223 |
| 220 RunThreads(); | 224 RunThreads(); |
| 221 | 225 |
| 222 EXPECT_EQ(0, post_interceptor_->GetHitCount()) | 226 EXPECT_EQ(1, post_interceptor_->GetHitCount()) |
| 223 << post_interceptor_->GetRequestsAsString(); | 227 << post_interceptor_->GetRequestsAsString(); |
| 224 EXPECT_EQ(1, post_interceptor_->GetCount()) | 228 EXPECT_EQ(1, post_interceptor_->GetCount()) |
| 225 << post_interceptor_->GetRequestsAsString(); | 229 << post_interceptor_->GetRequestsAsString(); |
| 226 | 230 |
| 227 EXPECT_NE(0, error_); | 231 EXPECT_EQ(config_->UpdateUrl().front(), original_url_); |
| 232 EXPECT_EQ(403, error_); |
| 228 EXPECT_STREQ("network error", error_message_.c_str()); | 233 EXPECT_STREQ("network error", error_message_.c_str()); |
| 229 EXPECT_EQ(0ul, results_.list.size()); | 234 EXPECT_EQ(0ul, results_.list.size()); |
| 230 } | 235 } |
| 231 | 236 |
| 232 } // namespace component_updater | 237 } // namespace component_updater |
| OLD | NEW |