Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / chromeos / app_mode / fake_cws.cc
blobe8c798da2f2d1c0cf2f425607a7fd39a58ebaf21
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 "chrome/browser/chromeos/app_mode/fake_cws.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/files/file_util.h"
10 #include "base/path_service.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_util.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "crypto/sha2.h"
17 #include "net/base/url_util.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
20 using net::test_server::BasicHttpResponse;
21 using net::test_server::EmbeddedTestServer;
22 using net::test_server::HttpRequest;
23 using net::test_server::HttpResponse;
25 namespace chromeos {
27 namespace {
29 const char kWebstoreDomain[] = "cws.com";
30 // Kiosk app crx file download path under web store site.
31 const char kCrxDownloadPath[] = "/chromeos/app_mode/webstore/downloads/";
33 const char kAppNoUpdateTemplate[] =
34 "<app appid=\"$AppId\" status=\"ok\">"
35 "<updatecheck status=\"noupdate\"/>"
36 "</app>";
38 const char kAppHasUpdateTemplate[] =
39 "<app appid=\"$AppId\" status=\"ok\">"
40 "<updatecheck codebase=\"$CrxDownloadUrl\" fp=\"1.$FP\" "
41 "hash=\"\" hash_sha256=\"$FP\" size=\"$Size\" status=\"ok\" "
42 "version=\"$Version\"/>"
43 "</app>";
45 const char kPrivateStoreAppHasUpdateTemplate[] =
46 "<app appid=\"$AppId\">"
47 "<updatecheck codebase=\"$CrxDownloadUrl\" version=\"$Version\"/>"
48 "</app>";
50 const char kUpdateContentTemplate[] =
51 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
52 "<gupdate xmlns=\"http://www.google.com/update2/response\" "
53 "protocol=\"2.0\" server=\"prod\">"
54 "<daystart elapsed_days=\"2569\" elapsed_seconds=\"36478\"/>"
55 "$APPS"
56 "</gupdate>";
58 bool GetAppIdsFromUpdateUrl(const GURL& update_url,
59 std::vector<std::string>* ids) {
60 for (net::QueryIterator it(update_url); !it.IsAtEnd(); it.Advance()) {
61 if (it.GetKey() != "x")
62 continue;
63 std::string id;
64 net::GetValueForKeyInQuery(GURL("http://dummy?" + it.GetUnescapedValue()),
65 "id", &id);
66 ids->push_back(id);
68 return !ids->empty();
71 } // namespace
73 FakeCWS::FakeCWS() : update_check_count_(0) {
76 FakeCWS::~FakeCWS() {
79 void FakeCWS::Init(EmbeddedTestServer* embedded_test_server) {
80 has_update_template_ = kAppHasUpdateTemplate;
81 no_update_template_ = kAppNoUpdateTemplate;
82 update_check_end_point_ = "/update_check.xml";
84 SetupWebStoreURL(embedded_test_server->base_url());
85 OverrideGalleryCommandlineSwitches();
86 embedded_test_server->RegisterRequestHandler(
87 base::Bind(&FakeCWS::HandleRequest, base::Unretained(this)));
90 void FakeCWS::InitAsPrivateStore(EmbeddedTestServer* embedded_test_server,
91 const std::string& update_check_end_point) {
92 has_update_template_ = kPrivateStoreAppHasUpdateTemplate;
93 no_update_template_ = kAppNoUpdateTemplate;
94 update_check_end_point_ = update_check_end_point;
96 SetupWebStoreURL(embedded_test_server->base_url());
97 embedded_test_server->RegisterRequestHandler(
98 base::Bind(&FakeCWS::HandleRequest, base::Unretained(this)));
101 void FakeCWS::SetUpdateCrx(const std::string& app_id,
102 const std::string& crx_file,
103 const std::string& version) {
104 GURL crx_download_url = web_store_url_.Resolve(kCrxDownloadPath + crx_file);
106 base::FilePath test_data_dir;
107 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
108 base::FilePath crx_file_path =
109 test_data_dir.AppendASCII("chromeos/app_mode/webstore/downloads")
110 .AppendASCII(crx_file);
111 std::string crx_content;
112 ASSERT_TRUE(base::ReadFileToString(crx_file_path, &crx_content));
114 const std::string sha256 = crypto::SHA256HashString(crx_content);
115 const std::string sha256_hex = base::HexEncode(sha256.c_str(), sha256.size());
117 std::string update_check_content(has_update_template_);
118 base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$AppId",
119 app_id);
120 base::ReplaceSubstringsAfterOffset(
121 &update_check_content, 0, "$CrxDownloadUrl", crx_download_url.spec());
122 base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$FP",
123 sha256_hex);
124 base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$Size",
125 base::UintToString(crx_content.size()));
126 base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$Version",
127 version);
128 id_to_update_check_content_map_[app_id] = update_check_content;
131 void FakeCWS::SetNoUpdate(const std::string& app_id) {
132 std::string app_update_check_content(no_update_template_);
133 base::ReplaceSubstringsAfterOffset(&app_update_check_content, 0, "$AppId",
134 app_id);
135 id_to_update_check_content_map_[app_id] = app_update_check_content;
138 int FakeCWS::GetUpdateCheckCountAndReset() {
139 int current_count = update_check_count_;
140 update_check_count_ = 0;
141 return current_count;
144 void FakeCWS::SetupWebStoreURL(const GURL& test_server_url) {
145 GURL::Replacements replace_webstore_host;
146 replace_webstore_host.SetHostStr(kWebstoreDomain);
147 web_store_url_ = test_server_url.ReplaceComponents(replace_webstore_host);
150 void FakeCWS::OverrideGalleryCommandlineSwitches() {
151 DCHECK(web_store_url_.is_valid());
153 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
155 command_line->AppendSwitchASCII(
156 ::switches::kAppsGalleryURL,
157 web_store_url_.Resolve("/chromeos/app_mode/webstore").spec());
159 std::string downloads_path = std::string(kCrxDownloadPath).append("%s.crx");
160 GURL downloads_url = web_store_url_.Resolve(downloads_path);
161 command_line->AppendSwitchASCII(::switches::kAppsGalleryDownloadURL,
162 downloads_url.spec());
164 GURL update_url = web_store_url_.Resolve(update_check_end_point_);
165 command_line->AppendSwitchASCII(::switches::kAppsGalleryUpdateURL,
166 update_url.spec());
169 bool FakeCWS::GetUpdateCheckContent(const std::vector<std::string>& ids,
170 std::string* update_check_content) {
171 std::string apps_content;
172 for (const std::string& id : ids) {
173 std::string app_update_content;
174 auto it = id_to_update_check_content_map_.find(id);
175 if (it == id_to_update_check_content_map_.end())
176 return false;
177 apps_content.append(it->second);
179 if (apps_content.empty())
180 return false;
182 *update_check_content = kUpdateContentTemplate;
183 base::ReplaceSubstringsAfterOffset(update_check_content, 0, "$APPS",
184 apps_content);
185 return true;
188 scoped_ptr<HttpResponse> FakeCWS::HandleRequest(const HttpRequest& request) {
189 GURL request_url = GURL("http://localhost").Resolve(request.relative_url);
190 std::string request_path = request_url.path();
191 if (request_path.find(update_check_end_point_) != std::string::npos &&
192 !id_to_update_check_content_map_.empty()) {
193 std::vector<std::string> ids;
194 if (GetAppIdsFromUpdateUrl(request_url, &ids)) {
195 std::string update_check_content;
196 if (GetUpdateCheckContent(ids, &update_check_content)) {
197 ++update_check_count_;
198 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse());
199 http_response->set_code(net::HTTP_OK);
200 http_response->set_content_type("text/xml");
201 http_response->set_content(update_check_content);
202 return http_response.Pass();
207 return scoped_ptr<HttpResponse>();
210 } // namespace chromeos