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 "chrome/browser/chromeos/app_mode/fake_cws.h"
6
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/test/embedded_test_server/embedded_test_server.h"
18
19using net::test_server::BasicHttpResponse;
20using net::test_server::EmbeddedTestServer;
21using net::test_server::HttpRequest;
22using net::test_server::HttpResponse;
23
24namespace chromeos {
25
26namespace {
27
28const char kWebstoreDomain[] = "cws.com";
29// Kiosk app crx file download path under web store site.
30const char kCrxDownloadPath[] = "/chromeos/app_mode/webstore/downloads/";
31
32}  // namespace
33
34void FakeCWS::Init(EmbeddedTestServer* embedded_test_server) {
35  SetupWebStore(embedded_test_server->base_url());
36  SetupWebStoreGalleryUrl();
37  SetupCrxDownloadAndUpdateUrls(embedded_test_server);
38}
39
40void FakeCWS::SetUpdateCrx(const std::string& app_id,
41                           const std::string& crx_file,
42                           const std::string& version) {
43  GURL crx_download_url = web_store_url_.Resolve(kCrxDownloadPath + crx_file);
44
45  base::FilePath test_data_dir;
46  PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
47  base::FilePath crx_file_path =
48      test_data_dir.AppendASCII("chromeos/app_mode/webstore/downloads")
49          .AppendASCII(crx_file);
50  std::string crx_content;
51  ASSERT_TRUE(base::ReadFileToString(crx_file_path, &crx_content));
52
53  const std::string sha256 = crypto::SHA256HashString(crx_content);
54  const std::string sha256_hex = base::HexEncode(sha256.c_str(), sha256.size());
55
56  SetUpdateCheckContent(
57      "chromeos/app_mode/webstore/update_check/has_update.xml",
58      crx_download_url,
59      app_id,
60      sha256_hex,
61      base::UintToString(crx_content.size()),
62      version,
63      &update_check_content_);
64}
65
66void FakeCWS::SetNoUpdate(const std::string& app_id) {
67  SetUpdateCheckContent("chromeos/app_mode/webstore/update_check/no_update.xml",
68                        GURL(),
69                        app_id,
70                        "",
71                        "",
72                        "",
73                        &update_check_content_);
74}
75
76void FakeCWS::SetupWebStore(const GURL& test_server_url) {
77  std::string webstore_host(kWebstoreDomain);
78  GURL::Replacements replace_webstore_host;
79  replace_webstore_host.SetHostStr(webstore_host);
80  web_store_url_ = test_server_url.ReplaceComponents(replace_webstore_host);
81}
82
83void FakeCWS::SetupWebStoreGalleryUrl() {
84  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
85      ::switches::kAppsGalleryURL,
86      web_store_url_.Resolve("/chromeos/app_mode/webstore").spec());
87}
88
89void FakeCWS::SetupCrxDownloadAndUpdateUrls(
90    EmbeddedTestServer* embedded_test_server) {
91  SetupCrxDownloadUrl();
92  SetupCrxUpdateUrl(embedded_test_server);
93}
94
95void FakeCWS::SetupCrxDownloadUrl() {
96  std::string downloads_path = std::string(kCrxDownloadPath).append("%s.crx");
97  GURL downloads_url = web_store_url_.Resolve(downloads_path);
98  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
99      ::switches::kAppsGalleryDownloadURL, downloads_url.spec());
100}
101
102void FakeCWS::SetupCrxUpdateUrl(EmbeddedTestServer* embedded_test_server) {
103  GURL update_url = web_store_url_.Resolve("/update_check.xml");
104  CommandLine::ForCurrentProcess()->AppendSwitchASCII(
105      ::switches::kAppsGalleryUpdateURL, update_url.spec());
106
107  embedded_test_server->RegisterRequestHandler(
108      base::Bind(&FakeCWS::HandleRequest, base::Unretained(this)));
109}
110
111void FakeCWS::SetUpdateCheckContent(const std::string& update_check_file,
112                                    const GURL& crx_download_url,
113                                    const std::string& app_id,
114                                    const std::string& crx_fp,
115                                    const std::string& crx_size,
116                                    const std::string& version,
117                                    std::string* update_check_content) {
118  base::FilePath test_data_dir;
119  PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
120  base::FilePath update_file =
121      test_data_dir.AppendASCII(update_check_file.c_str());
122  ASSERT_TRUE(base::ReadFileToString(update_file, update_check_content));
123
124  ReplaceSubstringsAfterOffset(update_check_content, 0, "$AppId", app_id);
125  ReplaceSubstringsAfterOffset(
126      update_check_content, 0, "$CrxDownloadUrl", crx_download_url.spec());
127  ReplaceSubstringsAfterOffset(update_check_content, 0, "$FP", crx_fp);
128  ReplaceSubstringsAfterOffset(update_check_content, 0, "$Size", crx_size);
129  ReplaceSubstringsAfterOffset(update_check_content, 0, "$Version", version);
130}
131
132scoped_ptr<HttpResponse> FakeCWS::HandleRequest(const HttpRequest& request) {
133  GURL request_url = GURL("http://localhost").Resolve(request.relative_url);
134  std::string request_path = request_url.path();
135  if (!update_check_content_.empty() &&
136      request_path.find("/update_check.xml") != std::string::npos) {
137    scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse());
138    http_response->set_code(net::HTTP_OK);
139    http_response->set_content_type("text/xml");
140    http_response->set_content(update_check_content_);
141    return http_response.PassAs<HttpResponse>();
142  }
143
144  return scoped_ptr<HttpResponse>();
145}
146
147}  // namespace chromeos
148