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/file_util.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/path_service.h"
11 #include "base/run_loop.h"
12 #include "base/version.h"
13 #include "chrome/browser/component_updater/crx_update_item.h"
14 #include "chrome/browser/component_updater/test/component_updater_service_unittest.h"
15 #include "chrome/browser/component_updater/test/url_request_post_interceptor.h"
16 #include "chrome/browser/component_updater/update_checker.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "net/url_request/url_fetcher.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using content::BrowserThread;
26 namespace component_updater {
30 base::FilePath test_file(const char* file) {
32 PathService::Get(chrome::DIR_TEST_DATA, &path);
33 return path.AppendASCII("components").AppendASCII(file);
38 class UpdateCheckerTest : public testing::Test {
41 virtual ~UpdateCheckerTest();
43 // Overrides from testing::Test.
44 virtual void SetUp() OVERRIDE;
45 virtual void TearDown() OVERRIDE;
47 void UpdateCheckComplete(int error,
48 const std::string& error_message,
49 const UpdateResponse::Results& results);
54 void RunThreadsUntilIdle();
56 CrxUpdateItem BuildCrxUpdateItem();
58 scoped_ptr<TestConfigurator> config_;
60 scoped_ptr<UpdateChecker> update_checker_;
62 scoped_ptr<InterceptorFactory> interceptor_factory_;
63 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory.
66 std::string error_message_;
67 UpdateResponse::Results results_;
70 content::TestBrowserThreadBundle thread_bundle_;
71 base::FilePath test_data_dir_;
72 base::Closure quit_closure_;
74 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest);
77 UpdateCheckerTest::UpdateCheckerTest()
78 : config_(new TestConfigurator),
80 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
81 // The test directory is chrome/test/data/components.
82 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
83 test_data_dir_ = test_data_dir_.AppendASCII("components");
85 net::URLFetcher::SetEnableInterceptionForTests(true);
88 UpdateCheckerTest::~UpdateCheckerTest() {
89 net::URLFetcher::SetEnableInterceptionForTests(false);
92 void UpdateCheckerTest::SetUp() {
93 interceptor_factory_.reset(new InterceptorFactory);
94 post_interceptor_ = interceptor_factory_->CreateInterceptor();
95 EXPECT_TRUE(post_interceptor_);
97 update_checker_.reset();
100 error_message_.clear();
101 results_ = UpdateResponse::Results();
104 void UpdateCheckerTest::TearDown() {
105 update_checker_.reset();
107 post_interceptor_ = NULL;
108 interceptor_factory_.reset();
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(
136 const std::string& error_message,
137 const UpdateResponse::Results& results) {
139 error_message_ = error_message;
144 CrxUpdateItem UpdateCheckerTest::BuildCrxUpdateItem() {
145 CrxComponent crx_component;
146 crx_component.name = "test_jebg";
147 crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
148 crx_component.installer = NULL;
149 crx_component.version = base::Version("0.9");
150 crx_component.fingerprint = "fp1";
152 CrxUpdateItem crx_update_item;
153 crx_update_item.status = CrxUpdateItem::kNew;
154 crx_update_item.id = "jebgalgnebhfojomionfpkfelancnnkf";
155 crx_update_item.component = crx_component;
157 return crx_update_item;
160 TEST_F(UpdateCheckerTest, UpdateCheckSuccess) {
161 EXPECT_TRUE(post_interceptor_->ExpectRequest(
162 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
165 UpdateChecker::Create(*config_,
166 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
167 base::Unretained(this))).Pass();
169 CrxUpdateItem item(BuildCrxUpdateItem());
170 std::vector<CrxUpdateItem*> items_to_check;
171 items_to_check.push_back(&item);
173 update_checker_->CheckForUpdates(items_to_check, "extra=\"params\"");
177 EXPECT_EQ(1, post_interceptor_->GetHitCount())
178 << post_interceptor_->GetRequestsAsString();
179 EXPECT_EQ(1, post_interceptor_->GetCount())
180 << post_interceptor_->GetRequestsAsString();
182 // Sanity check the request.
185 post_interceptor_->GetRequests()[0].find(
186 "request protocol=\"3.0\" extra=\"params\""));
189 post_interceptor_->GetRequests()[0].find(
190 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
191 "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>"));
193 EXPECT_NE(string::npos,
194 post_interceptor_->GetRequests()[0].find("<hw physmemory="));
196 // Sanity check the arguments of the callback after parsing.
197 EXPECT_EQ(0, error_);
198 EXPECT_TRUE(error_message_.empty());
199 EXPECT_EQ(1ul, results_.list.size());
200 EXPECT_STREQ("jebgalgnebhfojomionfpkfelancnnkf",
201 results_.list[0].extension_id.c_str());
202 EXPECT_STREQ("1.0", results_.list[0].manifest.version.c_str());
205 TEST_F(UpdateCheckerTest, UpdateNetworkError) {
206 // Setting this expectation simulates a network error since the
207 // file is not found. Since setting the expectation fails, this function
208 // owns |request_matcher|.
209 scoped_ptr<PartialMatch> request_matcher(new PartialMatch("updatecheck"));
210 EXPECT_FALSE(post_interceptor_->ExpectRequest(request_matcher.get(),
211 test_file("no such file")));
214 UpdateChecker::Create(*config_,
215 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
216 base::Unretained(this))).Pass();
218 CrxUpdateItem item(BuildCrxUpdateItem());
219 std::vector<CrxUpdateItem*> items_to_check;
220 items_to_check.push_back(&item);
222 update_checker_->CheckForUpdates(items_to_check, "");
226 EXPECT_EQ(0, post_interceptor_->GetHitCount())
227 << post_interceptor_->GetRequestsAsString();
228 EXPECT_EQ(1, post_interceptor_->GetCount())
229 << post_interceptor_->GetRequestsAsString();
231 EXPECT_NE(0, error_);
232 EXPECT_STREQ("network error", error_message_.c_str());
233 EXPECT_EQ(0ul, results_.list.size());
236 } // namespace component_updater