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"
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/test/embedded_test_server/embedded_test_server.h"
19 using net::test_server::BasicHttpResponse
;
20 using net::test_server::EmbeddedTestServer
;
21 using net::test_server::HttpRequest
;
22 using net::test_server::HttpResponse
;
28 const char kWebstoreDomain
[] = "cws.com";
29 // Kiosk app crx file download path under web store site.
30 const char kCrxDownloadPath
[] = "/chromeos/app_mode/webstore/downloads/";
34 FakeCWS::FakeCWS() : update_check_count_(0) {
40 void FakeCWS::Init(EmbeddedTestServer
* embedded_test_server
) {
41 has_update_template_
=
42 "chromeos/app_mode/webstore/update_check/has_update.xml";
43 no_update_template_
= "chromeos/app_mode/webstore/update_check/no_update.xml";
44 update_check_end_point_
= "/update_check.xml";
46 SetupWebStoreURL(embedded_test_server
->base_url());
47 OverrideGalleryCommandlineSwitches();
48 embedded_test_server
->RegisterRequestHandler(
49 base::Bind(&FakeCWS::HandleRequest
, base::Unretained(this)));
52 void FakeCWS::InitAsPrivateStore(EmbeddedTestServer
* embedded_test_server
,
53 const std::string
& update_check_end_point
) {
54 has_update_template_
=
55 "chromeos/app_mode/webstore/update_check/has_update_private_store.xml";
56 no_update_template_
= "chromeos/app_mode/webstore/update_check/no_update.xml";
57 update_check_end_point_
= update_check_end_point
;
59 SetupWebStoreURL(embedded_test_server
->base_url());
60 embedded_test_server
->RegisterRequestHandler(
61 base::Bind(&FakeCWS::HandleRequest
, base::Unretained(this)));
64 void FakeCWS::SetUpdateCrx(const std::string
& app_id
,
65 const std::string
& crx_file
,
66 const std::string
& version
) {
67 GURL crx_download_url
= web_store_url_
.Resolve(kCrxDownloadPath
+ crx_file
);
69 base::FilePath test_data_dir
;
70 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
71 base::FilePath crx_file_path
=
72 test_data_dir
.AppendASCII("chromeos/app_mode/webstore/downloads")
73 .AppendASCII(crx_file
);
74 std::string crx_content
;
75 ASSERT_TRUE(base::ReadFileToString(crx_file_path
, &crx_content
));
77 const std::string sha256
= crypto::SHA256HashString(crx_content
);
78 const std::string sha256_hex
= base::HexEncode(sha256
.c_str(), sha256
.size());
80 SetUpdateCheckContent(
85 base::UintToString(crx_content
.size()),
87 &update_check_content_
);
90 void FakeCWS::SetNoUpdate(const std::string
& app_id
) {
91 SetUpdateCheckContent(no_update_template_
,
97 &update_check_content_
);
100 int FakeCWS::GetUpdateCheckCountAndReset() {
101 int current_count
= update_check_count_
;
102 update_check_count_
= 0;
103 return current_count
;
106 void FakeCWS::SetupWebStoreURL(const GURL
& test_server_url
) {
107 GURL::Replacements replace_webstore_host
;
108 replace_webstore_host
.SetHostStr(kWebstoreDomain
);
109 web_store_url_
= test_server_url
.ReplaceComponents(replace_webstore_host
);
112 void FakeCWS::OverrideGalleryCommandlineSwitches() {
113 DCHECK(web_store_url_
.is_valid());
115 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
117 command_line
->AppendSwitchASCII(
118 ::switches::kAppsGalleryURL
,
119 web_store_url_
.Resolve("/chromeos/app_mode/webstore").spec());
121 std::string downloads_path
= std::string(kCrxDownloadPath
).append("%s.crx");
122 GURL downloads_url
= web_store_url_
.Resolve(downloads_path
);
123 command_line
->AppendSwitchASCII(::switches::kAppsGalleryDownloadURL
,
124 downloads_url
.spec());
126 GURL update_url
= web_store_url_
.Resolve(update_check_end_point_
);
127 command_line
->AppendSwitchASCII(::switches::kAppsGalleryUpdateURL
,
131 void FakeCWS::SetUpdateCheckContent(const std::string
& update_check_file
,
132 const GURL
& crx_download_url
,
133 const std::string
& app_id
,
134 const std::string
& crx_fp
,
135 const std::string
& crx_size
,
136 const std::string
& version
,
137 std::string
* update_check_content
) {
138 base::FilePath test_data_dir
;
139 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
);
140 base::FilePath update_file
=
141 test_data_dir
.AppendASCII(update_check_file
.c_str());
142 ASSERT_TRUE(base::ReadFileToString(update_file
, update_check_content
));
144 ReplaceSubstringsAfterOffset(update_check_content
, 0, "$AppId", app_id
);
145 ReplaceSubstringsAfterOffset(
146 update_check_content
, 0, "$CrxDownloadUrl", crx_download_url
.spec());
147 ReplaceSubstringsAfterOffset(update_check_content
, 0, "$FP", crx_fp
);
148 ReplaceSubstringsAfterOffset(update_check_content
, 0, "$Size", crx_size
);
149 ReplaceSubstringsAfterOffset(update_check_content
, 0, "$Version", version
);
152 scoped_ptr
<HttpResponse
> FakeCWS::HandleRequest(const HttpRequest
& request
) {
153 GURL request_url
= GURL("http://localhost").Resolve(request
.relative_url
);
154 std::string request_path
= request_url
.path();
155 if (!update_check_content_
.empty() &&
156 request_path
.find(update_check_end_point_
) != std::string::npos
) {
157 ++update_check_count_
;
158 scoped_ptr
<BasicHttpResponse
> http_response(new BasicHttpResponse());
159 http_response
->set_code(net::HTTP_OK
);
160 http_response
->set_content_type("text/xml");
161 http_response
->set_content(update_check_content_
);
162 return http_response
.Pass();
165 return scoped_ptr
<HttpResponse
>();
168 } // namespace chromeos