Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / component_updater / test / update_checker_unittest.cc
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/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"
23
24 using content::BrowserThread;
25
26 namespace component_updater {
27
28 namespace {
29
30 base::FilePath test_file(const char* file) {
31   base::FilePath path;
32   PathService::Get(chrome::DIR_TEST_DATA, &path);
33   return path.AppendASCII("components").AppendASCII(file);
34 }
35
36 }  // namespace
37
38 class UpdateCheckerTest : public testing::Test {
39  public:
40   UpdateCheckerTest();
41   virtual ~UpdateCheckerTest();
42
43   // Overrides from testing::Test.
44   virtual void SetUp() OVERRIDE;
45   virtual void TearDown() OVERRIDE;
46
47   void UpdateCheckComplete(int error,
48                            const std::string& error_message,
49                            const UpdateResponse::Results& results);
50
51  protected:
52   void Quit();
53   void RunThreads();
54   void RunThreadsUntilIdle();
55
56   CrxUpdateItem BuildCrxUpdateItem();
57
58   scoped_ptr<TestConfigurator> config_;
59
60   scoped_ptr<UpdateChecker> update_checker_;
61
62   scoped_ptr<InterceptorFactory> interceptor_factory_;
63   URLRequestPostInterceptor* post_interceptor_;  // Owned by the factory.
64
65   int error_;
66   std::string error_message_;
67   UpdateResponse::Results results_;
68
69  private:
70   content::TestBrowserThreadBundle thread_bundle_;
71   base::FilePath test_data_dir_;
72   base::Closure quit_closure_;
73
74   DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest);
75 };
76
77 UpdateCheckerTest::UpdateCheckerTest()
78     : config_(new TestConfigurator),
79       error_(0),
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");
84
85   net::URLFetcher::SetEnableInterceptionForTests(true);
86 }
87
88 UpdateCheckerTest::~UpdateCheckerTest() {
89   net::URLFetcher::SetEnableInterceptionForTests(false);
90 }
91
92 void UpdateCheckerTest::SetUp() {
93   interceptor_factory_.reset(new InterceptorFactory);
94   post_interceptor_ = interceptor_factory_->CreateInterceptor();
95   EXPECT_TRUE(post_interceptor_);
96
97   update_checker_.reset();
98
99   error_ = 0;
100   error_message_.clear();
101   results_ = UpdateResponse::Results();
102 }
103
104 void UpdateCheckerTest::TearDown() {
105   update_checker_.reset();
106
107   post_interceptor_ = NULL;
108   interceptor_factory_.reset();
109
110   config_.reset();
111 }
112
113 void UpdateCheckerTest::RunThreads() {
114   base::RunLoop runloop;
115   quit_closure_ = runloop.QuitClosure();
116   runloop.Run();
117
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();
123 }
124
125 void UpdateCheckerTest::RunThreadsUntilIdle() {
126   base::RunLoop().RunUntilIdle();
127 }
128
129 void UpdateCheckerTest::Quit() {
130   if (!quit_closure_.is_null())
131     quit_closure_.Run();
132 }
133
134 void UpdateCheckerTest::UpdateCheckComplete(
135     int error,
136     const std::string& error_message,
137     const UpdateResponse::Results& results) {
138   error_ = error;
139   error_message_ = error_message;
140   results_ = results;
141   Quit();
142 }
143
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";
151
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;
156
157   return crx_update_item;
158 }
159
160 TEST_F(UpdateCheckerTest, UpdateCheckSuccess) {
161   EXPECT_TRUE(post_interceptor_->ExpectRequest(
162       new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
163
164   update_checker_ =
165       UpdateChecker::Create(*config_,
166                             base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
167                                        base::Unretained(this))).Pass();
168
169   CrxUpdateItem item(BuildCrxUpdateItem());
170   std::vector<CrxUpdateItem*> items_to_check;
171   items_to_check.push_back(&item);
172
173   update_checker_->CheckForUpdates(items_to_check, "extra=\"params\"");
174
175   RunThreads();
176
177   EXPECT_EQ(1, post_interceptor_->GetHitCount())
178       << post_interceptor_->GetRequestsAsString();
179   EXPECT_EQ(1, post_interceptor_->GetCount())
180       << post_interceptor_->GetRequestsAsString();
181
182   // Sanity check the request.
183   EXPECT_NE(
184       string::npos,
185       post_interceptor_->GetRequests()[0].find(
186           "request protocol=\"3.0\" extra=\"params\""));
187   EXPECT_NE(
188       string::npos,
189       post_interceptor_->GetRequests()[0].find(
190           "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
191           "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>"));
192
193   EXPECT_NE(string::npos,
194             post_interceptor_->GetRequests()[0].find("<hw physmemory="));
195
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());
203 }
204
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")));
212
213   update_checker_ =
214       UpdateChecker::Create(*config_,
215                             base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
216                                        base::Unretained(this))).Pass();
217
218   CrxUpdateItem item(BuildCrxUpdateItem());
219   std::vector<CrxUpdateItem*> items_to_check;
220   items_to_check.push_back(&item);
221
222   update_checker_->CheckForUpdates(items_to_check, "");
223
224   RunThreads();
225
226   EXPECT_EQ(0, post_interceptor_->GetHitCount())
227       << post_interceptor_->GetRequestsAsString();
228   EXPECT_EQ(1, post_interceptor_->GetCount())
229       << post_interceptor_->GetRequestsAsString();
230
231   EXPECT_NE(0, error_);
232   EXPECT_STREQ("network error", error_message_.c_str());
233   EXPECT_EQ(0ul, results_.list.size());
234 }
235
236 }  // namespace component_updater