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.
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"
26 namespace update_client
{
30 base::FilePath
test_file(const char* file
) {
32 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
33 return path
.AppendASCII("components")
36 .AppendASCII("update_client")
42 class UpdateCheckerTest
: public testing::Test
{
45 ~UpdateCheckerTest() override
;
47 // Overrides from testing::Test.
48 void SetUp() override
;
49 void TearDown() override
;
51 void UpdateCheckComplete(const GURL
& original_url
,
53 const std::string
& error_message
,
54 const UpdateResponse::Results
& results
);
59 void RunThreadsUntilIdle();
61 CrxUpdateItem
BuildCrxUpdateItem();
63 scoped_ptr
<TestConfigurator
> config_
;
65 scoped_ptr
<UpdateChecker
> update_checker_
;
67 scoped_ptr
<InterceptorFactory
> interceptor_factory_
;
68 URLRequestPostInterceptor
* post_interceptor_
; // Owned by the factory.
72 std::string error_message_
;
73 UpdateResponse::Results results_
;
76 base::MessageLoopForIO loop_
;
77 base::Closure quit_closure_
;
79 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest
);
82 UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL
), error_(0) {
85 UpdateCheckerTest::~UpdateCheckerTest() {
88 void UpdateCheckerTest::SetUp() {
89 config_
.reset(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_
);
96 update_checker_
.reset();
99 error_message_
.clear();
100 results_
= UpdateResponse::Results();
103 void UpdateCheckerTest::TearDown() {
104 update_checker_
.reset();
106 post_interceptor_
= NULL
;
107 interceptor_factory_
.reset();
111 // The PostInterceptor requires the message loop to run to destruct correctly.
112 // TODO(sorin): This is fragile and should be fixed.
113 RunThreadsUntilIdle();
116 void UpdateCheckerTest::RunThreads() {
117 base::RunLoop runloop
;
118 quit_closure_
= runloop
.QuitClosure();
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();
128 void UpdateCheckerTest::RunThreadsUntilIdle() {
129 base::RunLoop().RunUntilIdle();
132 void UpdateCheckerTest::Quit() {
133 if (!quit_closure_
.is_null())
137 void UpdateCheckerTest::UpdateCheckComplete(
138 const GURL
& original_url
,
140 const std::string
& error_message
,
141 const UpdateResponse::Results
& results
) {
142 original_url_
= original_url
;
144 error_message_
= error_message
;
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";
157 CrxUpdateItem crx_update_item
;
158 crx_update_item
.status
= CrxUpdateItem::kNew
;
159 crx_update_item
.id
= "jebgalgnebhfojomionfpkfelancnnkf";
160 crx_update_item
.component
= crx_component
;
162 return crx_update_item
;
165 TEST_F(UpdateCheckerTest
, UpdateCheckSuccess
) {
166 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
167 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
169 update_checker_
= UpdateChecker::Create(*config_
).Pass();
171 CrxUpdateItem
item(BuildCrxUpdateItem());
172 std::vector
<CrxUpdateItem
*> items_to_check
;
173 items_to_check
.push_back(&item
);
175 update_checker_
->CheckForUpdates(
176 items_to_check
, "extra=\"params\"",
177 base::Bind(&UpdateCheckerTest::UpdateCheckComplete
,
178 base::Unretained(this)));
182 EXPECT_EQ(1, post_interceptor_
->GetHitCount())
183 << post_interceptor_
->GetRequestsAsString();
184 EXPECT_EQ(1, post_interceptor_
->GetCount())
185 << post_interceptor_
->GetRequestsAsString();
187 // Sanity check the request.
188 EXPECT_NE(string::npos
, post_interceptor_
->GetRequests()[0].find(
189 "request protocol=\"3.0\" extra=\"params\""));
192 post_interceptor_
->GetRequests()[0].find(
193 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
194 "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>"));
196 EXPECT_NE(string::npos
,
197 post_interceptor_
->GetRequests()[0].find("<hw physmemory="));
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());
209 // Simulates a 403 server response error.
210 TEST_F(UpdateCheckerTest
, UpdateCheckError
) {
212 post_interceptor_
->ExpectRequest(new PartialMatch("updatecheck"), 403));
214 update_checker_
= UpdateChecker::Create(*config_
).Pass();
216 CrxUpdateItem
item(BuildCrxUpdateItem());
217 std::vector
<CrxUpdateItem
*> items_to_check
;
218 items_to_check
.push_back(&item
);
220 update_checker_
->CheckForUpdates(
221 items_to_check
, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete
,
222 base::Unretained(this)));
226 EXPECT_EQ(1, post_interceptor_
->GetHitCount())
227 << post_interceptor_
->GetRequestsAsString();
228 EXPECT_EQ(1, post_interceptor_
->GetCount())
229 << post_interceptor_
->GetRequestsAsString();
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());
237 } // namespace update_client