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/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"
26 namespace component_updater
{
30 base::FilePath
test_file(const char* file
) {
32 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
33 return path
.AppendASCII("components").AppendASCII("test").AppendASCII("data")
34 .AppendASCII("component_updater").AppendASCII(file
);
39 class UpdateCheckerTest
: public testing::Test
{
42 ~UpdateCheckerTest() override
;
44 // Overrides from testing::Test.
45 void SetUp() override
;
46 void TearDown() override
;
48 void UpdateCheckComplete(const GURL
& original_url
,
50 const std::string
& error_message
,
51 const UpdateResponse::Results
& results
);
56 void RunThreadsUntilIdle();
58 CrxUpdateItem
BuildCrxUpdateItem();
60 scoped_ptr
<TestConfigurator
> config_
;
62 scoped_ptr
<UpdateChecker
> update_checker_
;
64 scoped_ptr
<InterceptorFactory
> interceptor_factory_
;
65 URLRequestPostInterceptor
* post_interceptor_
; // Owned by the factory.
69 std::string error_message_
;
70 UpdateResponse::Results results_
;
73 base::MessageLoopForIO loop_
;
74 base::Closure quit_closure_
;
76 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest
);
79 UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL
), error_(0) {
82 UpdateCheckerTest::~UpdateCheckerTest() {
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_
);
93 update_checker_
.reset();
96 error_message_
.clear();
97 results_
= UpdateResponse::Results();
100 void UpdateCheckerTest::TearDown() {
101 update_checker_
.reset();
103 post_interceptor_
= NULL
;
104 interceptor_factory_
.reset();
108 // The PostInterceptor requires the message loop to run to destruct correctly.
109 // TODO(sorin): This is fragile and should be fixed.
110 RunThreadsUntilIdle();
113 void UpdateCheckerTest::RunThreads() {
114 base::RunLoop runloop
;
115 quit_closure_
= runloop
.QuitClosure();
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();
125 void UpdateCheckerTest::RunThreadsUntilIdle() {
126 base::RunLoop().RunUntilIdle();
129 void UpdateCheckerTest::Quit() {
130 if (!quit_closure_
.is_null())
134 void UpdateCheckerTest::UpdateCheckComplete(
135 const GURL
& original_url
,
137 const std::string
& error_message
,
138 const UpdateResponse::Results
& results
) {
139 original_url_
= original_url
;
141 error_message_
= error_message
;
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";
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
;
159 return crx_update_item
;
162 TEST_F(UpdateCheckerTest
, UpdateCheckSuccess
) {
163 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
164 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
166 update_checker_
= UpdateChecker::Create(*config_
).Pass();
168 CrxUpdateItem
item(BuildCrxUpdateItem());
169 std::vector
<CrxUpdateItem
*> items_to_check
;
170 items_to_check
.push_back(&item
);
172 update_checker_
->CheckForUpdates(
175 base::Bind(&UpdateCheckerTest::UpdateCheckComplete
,
176 base::Unretained(this)));
180 EXPECT_EQ(1, post_interceptor_
->GetHitCount())
181 << post_interceptor_
->GetRequestsAsString();
182 EXPECT_EQ(1, post_interceptor_
->GetCount())
183 << post_interceptor_
->GetRequestsAsString();
185 // Sanity check the request.
188 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(
223 base::Bind(&UpdateCheckerTest::UpdateCheckComplete
,
224 base::Unretained(this)));
228 EXPECT_EQ(1, post_interceptor_
->GetHitCount())
229 << post_interceptor_
->GetRequestsAsString();
230 EXPECT_EQ(1, post_interceptor_
->GetCount())
231 << post_interceptor_
->GetRequestsAsString();
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());
239 } // namespace component_updater