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.
5 #include "base/basictypes.h"
6 #include "base/compiler_specific.h"
7 #include "base/files/file_util.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/run_loop.h"
13 #include "base/version.h"
14 #include "components/component_updater/crx_update_item.h"
15 #include "components/component_updater/test/test_configurator.h"
16 #include "components/component_updater/test/url_request_post_interceptor.h"
17 #include "components/component_updater/update_checker.h"
18 #include "net/url_request/url_fetcher.h"
19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace component_updater
{
26 base::FilePath
test_file(const char* file
) {
28 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
29 return path
.AppendASCII("components").AppendASCII("test").AppendASCII("data")
30 .AppendASCII("component_updater").AppendASCII(file
);
35 class UpdateCheckerTest
: public testing::Test
{
38 virtual ~UpdateCheckerTest();
40 // Overrides from testing::Test.
41 virtual void SetUp() OVERRIDE
;
42 virtual void TearDown() OVERRIDE
;
44 void UpdateCheckComplete(int error
,
45 const std::string
& error_message
,
46 const UpdateResponse::Results
& results
);
51 void RunThreadsUntilIdle();
53 CrxUpdateItem
BuildCrxUpdateItem();
55 scoped_ptr
<TestConfigurator
> config_
;
57 scoped_ptr
<UpdateChecker
> update_checker_
;
59 scoped_ptr
<InterceptorFactory
> interceptor_factory_
;
60 URLRequestPostInterceptor
* post_interceptor_
; // Owned by the factory.
63 std::string error_message_
;
64 UpdateResponse::Results results_
;
67 base::MessageLoopForIO loop_
;
68 base::Closure quit_closure_
;
70 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest
);
73 UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL
), error_(0) {
74 net::URLFetcher::SetEnableInterceptionForTests(true);
77 UpdateCheckerTest::~UpdateCheckerTest() {
78 net::URLFetcher::SetEnableInterceptionForTests(false);
81 void UpdateCheckerTest::SetUp() {
82 config_
.reset(new TestConfigurator(base::MessageLoopProxy::current(),
83 base::MessageLoopProxy::current()));
84 interceptor_factory_
.reset(
85 new InterceptorFactory(base::MessageLoopProxy::current()));
86 post_interceptor_
= interceptor_factory_
->CreateInterceptor();
87 EXPECT_TRUE(post_interceptor_
);
89 update_checker_
.reset();
92 error_message_
.clear();
93 results_
= UpdateResponse::Results();
96 void UpdateCheckerTest::TearDown() {
97 update_checker_
.reset();
99 post_interceptor_
= NULL
;
100 interceptor_factory_
.reset();
104 // The PostInterceptor requires the message loop to run to destruct correctly.
105 // TODO: This is fragile and should be fixed.
106 RunThreadsUntilIdle();
109 void UpdateCheckerTest::RunThreads() {
110 base::RunLoop runloop
;
111 quit_closure_
= runloop
.QuitClosure();
114 // Since some tests need to drain currently enqueued tasks such as network
115 // 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
117 // is set and the service is started.
118 RunThreadsUntilIdle();
121 void UpdateCheckerTest::RunThreadsUntilIdle() {
122 base::RunLoop().RunUntilIdle();
125 void UpdateCheckerTest::Quit() {
126 if (!quit_closure_
.is_null())
130 void UpdateCheckerTest::UpdateCheckComplete(
132 const std::string
& error_message
,
133 const UpdateResponse::Results
& results
) {
135 error_message_
= error_message
;
140 CrxUpdateItem
UpdateCheckerTest::BuildCrxUpdateItem() {
141 CrxComponent crx_component
;
142 crx_component
.name
= "test_jebg";
143 crx_component
.pk_hash
.assign(jebg_hash
, jebg_hash
+ arraysize(jebg_hash
));
144 crx_component
.installer
= NULL
;
145 crx_component
.version
= base::Version("0.9");
146 crx_component
.fingerprint
= "fp1";
148 CrxUpdateItem crx_update_item
;
149 crx_update_item
.status
= CrxUpdateItem::kNew
;
150 crx_update_item
.id
= "jebgalgnebhfojomionfpkfelancnnkf";
151 crx_update_item
.component
= crx_component
;
153 return crx_update_item
;
156 TEST_F(UpdateCheckerTest
, UpdateCheckSuccess
) {
157 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
158 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
161 UpdateChecker::Create(*config_
,
162 base::Bind(&UpdateCheckerTest::UpdateCheckComplete
,
163 base::Unretained(this))).Pass();
165 CrxUpdateItem
item(BuildCrxUpdateItem());
166 std::vector
<CrxUpdateItem
*> items_to_check
;
167 items_to_check
.push_back(&item
);
169 update_checker_
->CheckForUpdates(items_to_check
, "extra=\"params\"");
173 EXPECT_EQ(1, post_interceptor_
->GetHitCount())
174 << post_interceptor_
->GetRequestsAsString();
175 EXPECT_EQ(1, post_interceptor_
->GetCount())
176 << post_interceptor_
->GetRequestsAsString();
178 // Sanity check the request.
181 post_interceptor_
->GetRequests()[0].find(
182 "request protocol=\"3.0\" extra=\"params\""));
185 post_interceptor_
->GetRequests()[0].find(
186 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
187 "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>"));
189 EXPECT_NE(string::npos
,
190 post_interceptor_
->GetRequests()[0].find("<hw physmemory="));
192 // Sanity check the arguments of the callback after parsing.
193 EXPECT_EQ(0, error_
);
194 EXPECT_TRUE(error_message_
.empty());
195 EXPECT_EQ(1ul, results_
.list
.size());
196 EXPECT_STREQ("jebgalgnebhfojomionfpkfelancnnkf",
197 results_
.list
[0].extension_id
.c_str());
198 EXPECT_STREQ("1.0", results_
.list
[0].manifest
.version
.c_str());
201 TEST_F(UpdateCheckerTest
, UpdateNetworkError
) {
202 // Setting this expectation simulates a network error since the
203 // file is not found. Since setting the expectation fails, this function
204 // owns |request_matcher|.
205 scoped_ptr
<PartialMatch
> request_matcher(new PartialMatch("updatecheck"));
206 EXPECT_FALSE(post_interceptor_
->ExpectRequest(request_matcher
.get(),
207 test_file("no such file")));
210 UpdateChecker::Create(*config_
,
211 base::Bind(&UpdateCheckerTest::UpdateCheckComplete
,
212 base::Unretained(this))).Pass();
214 CrxUpdateItem
item(BuildCrxUpdateItem());
215 std::vector
<CrxUpdateItem
*> items_to_check
;
216 items_to_check
.push_back(&item
);
218 update_checker_
->CheckForUpdates(items_to_check
, "");
222 EXPECT_EQ(0, post_interceptor_
->GetHitCount())
223 << post_interceptor_
->GetRequestsAsString();
224 EXPECT_EQ(1, post_interceptor_
->GetCount())
225 << post_interceptor_
->GetRequestsAsString();
227 EXPECT_NE(0, error_
);
228 EXPECT_STREQ("network error", error_message_
.c_str());
229 EXPECT_EQ(0ul, results_
.list
.size());
232 } // namespace component_updater