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