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