1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/files/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/version.h"
#include "chrome/browser/component_updater/test/component_updater_service_unittest.h"
#include "chrome/browser/component_updater/test/url_request_post_interceptor.h"
#include "chrome/common/chrome_paths.h"
#include "components/component_updater/crx_update_item.h"
#include "components/component_updater/update_checker.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
namespace component_updater {
namespace {
base::FilePath test_file(const char* file) {
base::FilePath path;
PathService::Get(chrome::DIR_TEST_DATA, &path);
return path.AppendASCII("components").AppendASCII(file);
}
} // namespace
class UpdateCheckerTest : public testing::Test {
public:
UpdateCheckerTest();
virtual ~UpdateCheckerTest();
// Overrides from testing::Test.
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
void UpdateCheckComplete(int error,
const std::string& error_message,
const UpdateResponse::Results& results);
protected:
void Quit();
void RunThreads();
void RunThreadsUntilIdle();
CrxUpdateItem BuildCrxUpdateItem();
scoped_ptr<TestConfigurator> config_;
scoped_ptr<UpdateChecker> update_checker_;
scoped_ptr<InterceptorFactory> interceptor_factory_;
URLRequestPostInterceptor* post_interceptor_; // Owned by the factory.
int error_;
std::string error_message_;
UpdateResponse::Results results_;
private:
content::TestBrowserThreadBundle thread_bundle_;
base::FilePath test_data_dir_;
base::Closure quit_closure_;
DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest);
};
UpdateCheckerTest::UpdateCheckerTest()
: config_(new TestConfigurator),
error_(0),
thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
// The test directory is chrome/test/data/components.
PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
test_data_dir_ = test_data_dir_.AppendASCII("components");
net::URLFetcher::SetEnableInterceptionForTests(true);
}
UpdateCheckerTest::~UpdateCheckerTest() {
net::URLFetcher::SetEnableInterceptionForTests(false);
}
void UpdateCheckerTest::SetUp() {
interceptor_factory_.reset(new InterceptorFactory);
post_interceptor_ = interceptor_factory_->CreateInterceptor();
EXPECT_TRUE(post_interceptor_);
update_checker_.reset();
error_ = 0;
error_message_.clear();
results_ = UpdateResponse::Results();
}
void UpdateCheckerTest::TearDown() {
update_checker_.reset();
post_interceptor_ = NULL;
interceptor_factory_.reset();
config_.reset();
}
void UpdateCheckerTest::RunThreads() {
base::RunLoop runloop;
quit_closure_ = runloop.QuitClosure();
runloop.Run();
// Since some tests need to drain currently enqueued tasks such as network
// intercepts on the IO thread, run the threads until they are
// idle. The component updater service won't loop again until the loop count
// is set and the service is started.
RunThreadsUntilIdle();
}
void UpdateCheckerTest::RunThreadsUntilIdle() {
base::RunLoop().RunUntilIdle();
}
void UpdateCheckerTest::Quit() {
if (!quit_closure_.is_null())
quit_closure_.Run();
}
void UpdateCheckerTest::UpdateCheckComplete(
int error,
const std::string& error_message,
const UpdateResponse::Results& results) {
error_ = error;
error_message_ = error_message;
results_ = results;
Quit();
}
CrxUpdateItem UpdateCheckerTest::BuildCrxUpdateItem() {
CrxComponent crx_component;
crx_component.name = "test_jebg";
crx_component.pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
crx_component.installer = NULL;
crx_component.version = base::Version("0.9");
crx_component.fingerprint = "fp1";
CrxUpdateItem crx_update_item;
crx_update_item.status = CrxUpdateItem::kNew;
crx_update_item.id = "jebgalgnebhfojomionfpkfelancnnkf";
crx_update_item.component = crx_component;
return crx_update_item;
}
TEST_F(UpdateCheckerTest, UpdateCheckSuccess) {
EXPECT_TRUE(post_interceptor_->ExpectRequest(
new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
update_checker_ =
UpdateChecker::Create(*config_,
base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
base::Unretained(this))).Pass();
CrxUpdateItem item(BuildCrxUpdateItem());
std::vector<CrxUpdateItem*> items_to_check;
items_to_check.push_back(&item);
update_checker_->CheckForUpdates(items_to_check, "extra=\"params\"");
RunThreads();
EXPECT_EQ(1, post_interceptor_->GetHitCount())
<< post_interceptor_->GetRequestsAsString();
EXPECT_EQ(1, post_interceptor_->GetCount())
<< post_interceptor_->GetRequestsAsString();
// Sanity check the request.
EXPECT_NE(
string::npos,
post_interceptor_->GetRequests()[0].find(
"request protocol=\"3.0\" extra=\"params\""));
EXPECT_NE(
string::npos,
post_interceptor_->GetRequests()[0].find(
"app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
"<updatecheck /><packages><package fp=\"fp1\"/></packages></app>"));
EXPECT_NE(string::npos,
post_interceptor_->GetRequests()[0].find("<hw physmemory="));
// Sanity check the arguments of the callback after parsing.
EXPECT_EQ(0, error_);
EXPECT_TRUE(error_message_.empty());
EXPECT_EQ(1ul, results_.list.size());
EXPECT_STREQ("jebgalgnebhfojomionfpkfelancnnkf",
results_.list[0].extension_id.c_str());
EXPECT_STREQ("1.0", results_.list[0].manifest.version.c_str());
}
TEST_F(UpdateCheckerTest, UpdateNetworkError) {
// Setting this expectation simulates a network error since the
// file is not found. Since setting the expectation fails, this function
// owns |request_matcher|.
scoped_ptr<PartialMatch> request_matcher(new PartialMatch("updatecheck"));
EXPECT_FALSE(post_interceptor_->ExpectRequest(request_matcher.get(),
test_file("no such file")));
update_checker_ =
UpdateChecker::Create(*config_,
base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
base::Unretained(this))).Pass();
CrxUpdateItem item(BuildCrxUpdateItem());
std::vector<CrxUpdateItem*> items_to_check;
items_to_check.push_back(&item);
update_checker_->CheckForUpdates(items_to_check, "");
RunThreads();
EXPECT_EQ(0, post_interceptor_->GetHitCount())
<< post_interceptor_->GetRequestsAsString();
EXPECT_EQ(1, post_interceptor_->GetCount())
<< post_interceptor_->GetRequestsAsString();
EXPECT_NE(0, error_);
EXPECT_STREQ("network error", error_message_.c_str());
EXPECT_EQ(0ul, results_.list.size());
}
} // namespace component_updater
|