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