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/update_client/crx_update_item.h" | |
17 #include "components/update_client/test/test_configurator.h" | |
18 #include "components/update_client/test/url_request_post_interceptor.h" | |
19 #include "components/update_client/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 update_client { | |
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") | |
34 .AppendASCII("test") | |
35 .AppendASCII("data") | |
36 .AppendASCII("update_client") | |
37 .AppendASCII(file); | |
38 } | |
39 | |
40 } // namespace | |
41 | |
42 class UpdateCheckerTest : public testing::Test { | |
43 public: | |
44 UpdateCheckerTest(); | |
45 ~UpdateCheckerTest() override; | |
46 | |
47 // Overrides from testing::Test. | |
48 void SetUp() override; | |
49 void TearDown() override; | |
50 | |
51 void UpdateCheckComplete(const GURL& original_url, | |
52 int error, | |
53 const std::string& error_message, | |
54 const UpdateResponse::Results& results); | |
55 | |
56 protected: | |
57 void Quit(); | |
58 void RunThreads(); | |
59 void RunThreadsUntilIdle(); | |
60 | |
61 CrxUpdateItem BuildCrxUpdateItem(); | |
62 | |
63 scoped_refptr<TestConfigurator> config_; | |
64 | |
65 scoped_ptr<UpdateChecker> update_checker_; | |
66 | |
67 scoped_ptr<InterceptorFactory> interceptor_factory_; | |
68 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory. | |
69 | |
70 GURL original_url_; | |
71 int error_; | |
72 std::string error_message_; | |
73 UpdateResponse::Results results_; | |
74 | |
75 private: | |
76 base::MessageLoopForIO loop_; | |
77 base::Closure quit_closure_; | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest); | |
80 }; | |
81 | |
82 UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL), error_(0) { | |
83 } | |
84 | |
85 UpdateCheckerTest::~UpdateCheckerTest() { | |
86 } | |
87 | |
88 void UpdateCheckerTest::SetUp() { | |
89 config_ = new TestConfigurator(base::MessageLoopProxy::current(), | |
90 base::MessageLoopProxy::current()); | |
91 interceptor_factory_.reset( | |
92 new InterceptorFactory(base::MessageLoopProxy::current())); | |
93 post_interceptor_ = interceptor_factory_->CreateInterceptor(); | |
94 EXPECT_TRUE(post_interceptor_); | |
95 | |
96 update_checker_.reset(); | |
97 | |
98 error_ = 0; | |
99 error_message_.clear(); | |
100 results_ = UpdateResponse::Results(); | |
101 } | |
102 | |
103 void UpdateCheckerTest::TearDown() { | |
104 update_checker_.reset(); | |
105 | |
106 post_interceptor_ = NULL; | |
107 interceptor_factory_.reset(); | |
108 | |
109 config_ = nullptr; | |
110 | |
111 // The PostInterceptor requires the message loop to run to destruct correctly. | |
112 // TODO(sorin): This is fragile and should be fixed. | |
113 RunThreadsUntilIdle(); | |
114 } | |
115 | |
116 void UpdateCheckerTest::RunThreads() { | |
117 base::RunLoop runloop; | |
118 quit_closure_ = runloop.QuitClosure(); | |
119 runloop.Run(); | |
120 | |
121 // Since some tests need to drain currently enqueued tasks such as network | |
122 // intercepts on the IO thread, run the threads until they are | |
123 // idle. The component updater service won't loop again until the loop count | |
124 // is set and the service is started. | |
125 RunThreadsUntilIdle(); | |
126 } | |
127 | |
128 void UpdateCheckerTest::RunThreadsUntilIdle() { | |
129 base::RunLoop().RunUntilIdle(); | |
130 } | |
131 | |
132 void UpdateCheckerTest::Quit() { | |
133 if (!quit_closure_.is_null()) | |
134 quit_closure_.Run(); | |
135 } | |
136 | |
137 void UpdateCheckerTest::UpdateCheckComplete( | |
138 const GURL& original_url, | |
139 int error, | |
140 const std::string& error_message, | |
141 const UpdateResponse::Results& results) { | |
142 original_url_ = original_url; | |
143 error_ = error; | |
144 error_message_ = error_message; | |
145 results_ = results; | |
146 Quit(); | |
147 } | |
148 | |
149 CrxUpdateItem UpdateCheckerTest::BuildCrxUpdateItem() { | |
150 CrxComponent crx_component; | |
151 crx_component.name = "test_jebg"; | |
152 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash)); | |
153 crx_component.installer = NULL; | |
154 crx_component.version = base::Version("0.9"); | |
155 crx_component.fingerprint = "fp1"; | |
156 | |
157 CrxUpdateItem crx_update_item; | |
158 crx_update_item.state = CrxUpdateItem::State::kNew; | |
159 crx_update_item.id = "jebgalgnebhfojomionfpkfelancnnkf"; | |
160 crx_update_item.component = crx_component; | |
161 | |
162 return crx_update_item; | |
163 } | |
164 | |
165 TEST_F(UpdateCheckerTest, UpdateCheckSuccess) { | |
166 EXPECT_TRUE(post_interceptor_->ExpectRequest( | |
167 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); | |
168 | |
169 update_checker_ = UpdateChecker::Create(*config_).Pass(); | |
170 | |
171 CrxUpdateItem item(BuildCrxUpdateItem()); | |
172 std::vector<CrxUpdateItem*> items_to_check; | |
173 items_to_check.push_back(&item); | |
174 | |
175 update_checker_->CheckForUpdates( | |
176 items_to_check, "extra=\"params\"", | |
177 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, | |
178 base::Unretained(this))); | |
179 | |
180 RunThreads(); | |
181 | |
182 EXPECT_EQ(1, post_interceptor_->GetHitCount()) | |
183 << post_interceptor_->GetRequestsAsString(); | |
184 EXPECT_EQ(1, post_interceptor_->GetCount()) | |
185 << post_interceptor_->GetRequestsAsString(); | |
186 | |
187 // Sanity check the request. | |
188 EXPECT_NE(string::npos, 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, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete, | |
222 base::Unretained(this))); | |
223 | |
224 RunThreads(); | |
225 | |
226 EXPECT_EQ(1, post_interceptor_->GetHitCount()) | |
227 << post_interceptor_->GetRequestsAsString(); | |
228 EXPECT_EQ(1, post_interceptor_->GetCount()) | |
229 << post_interceptor_->GetRequestsAsString(); | |
230 | |
231 EXPECT_EQ(config_->UpdateUrl().front(), original_url_); | |
232 EXPECT_EQ(403, error_); | |
233 EXPECT_STREQ("network error", error_message_.c_str()); | |
234 EXPECT_EQ(0ul, results_.list.size()); | |
235 } | |
236 | |
237 } // namespace update_client | |
OLD | NEW |