| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/app_mode/fake_cws.h" | 5 #include "chrome/browser/chromeos/app_mode/fake_cws.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
| 16 #include "crypto/sha2.h" | 16 #include "crypto/sha2.h" |
| 17 #include "net/base/url_util.h" |
| 17 #include "net/test/embedded_test_server/embedded_test_server.h" | 18 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 18 | 19 |
| 19 using net::test_server::BasicHttpResponse; | 20 using net::test_server::BasicHttpResponse; |
| 20 using net::test_server::EmbeddedTestServer; | 21 using net::test_server::EmbeddedTestServer; |
| 21 using net::test_server::HttpRequest; | 22 using net::test_server::HttpRequest; |
| 22 using net::test_server::HttpResponse; | 23 using net::test_server::HttpResponse; |
| 23 | 24 |
| 24 namespace chromeos { | 25 namespace chromeos { |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 const char kWebstoreDomain[] = "cws.com"; | 29 const char kWebstoreDomain[] = "cws.com"; |
| 29 // Kiosk app crx file download path under web store site. | 30 // Kiosk app crx file download path under web store site. |
| 30 const char kCrxDownloadPath[] = "/chromeos/app_mode/webstore/downloads/"; | 31 const char kCrxDownloadPath[] = "/chromeos/app_mode/webstore/downloads/"; |
| 31 | 32 |
| 33 const char kAppNoUpdateTemplate[] = |
| 34 "<app appid=\"$AppId\" status=\"ok\">" |
| 35 "<updatecheck status=\"noupdate\"/>" |
| 36 "</app>"; |
| 37 |
| 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>"; |
| 44 |
| 45 const char kPrivateStoreAppHasUpdateTemplate[] = |
| 46 "<app appid=\"$AppId\">" |
| 47 "<updatecheck codebase=\"$CrxDownloadUrl\" version=\"$Version\"/>" |
| 48 "</app>"; |
| 49 |
| 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>"; |
| 57 |
| 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); |
| 67 } |
| 68 return !ids->empty(); |
| 69 } |
| 70 |
| 32 } // namespace | 71 } // namespace |
| 33 | 72 |
| 34 FakeCWS::FakeCWS() : update_check_count_(0) { | 73 FakeCWS::FakeCWS() : update_check_count_(0) { |
| 35 } | 74 } |
| 36 | 75 |
| 37 FakeCWS::~FakeCWS() { | 76 FakeCWS::~FakeCWS() { |
| 38 } | 77 } |
| 39 | 78 |
| 40 void FakeCWS::Init(EmbeddedTestServer* embedded_test_server) { | 79 void FakeCWS::Init(EmbeddedTestServer* embedded_test_server) { |
| 41 has_update_template_ = | 80 has_update_template_ = kAppHasUpdateTemplate; |
| 42 "chromeos/app_mode/webstore/update_check/has_update.xml"; | 81 no_update_template_ = kAppNoUpdateTemplate; |
| 43 no_update_template_ = "chromeos/app_mode/webstore/update_check/no_update.xml"; | |
| 44 update_check_end_point_ = "/update_check.xml"; | 82 update_check_end_point_ = "/update_check.xml"; |
| 45 | 83 |
| 46 SetupWebStoreURL(embedded_test_server->base_url()); | 84 SetupWebStoreURL(embedded_test_server->base_url()); |
| 47 OverrideGalleryCommandlineSwitches(); | 85 OverrideGalleryCommandlineSwitches(); |
| 48 embedded_test_server->RegisterRequestHandler( | 86 embedded_test_server->RegisterRequestHandler( |
| 49 base::Bind(&FakeCWS::HandleRequest, base::Unretained(this))); | 87 base::Bind(&FakeCWS::HandleRequest, base::Unretained(this))); |
| 50 } | 88 } |
| 51 | 89 |
| 52 void FakeCWS::InitAsPrivateStore(EmbeddedTestServer* embedded_test_server, | 90 void FakeCWS::InitAsPrivateStore(EmbeddedTestServer* embedded_test_server, |
| 53 const std::string& update_check_end_point) { | 91 const std::string& update_check_end_point) { |
| 54 has_update_template_ = | 92 has_update_template_ = kPrivateStoreAppHasUpdateTemplate; |
| 55 "chromeos/app_mode/webstore/update_check/has_update_private_store.xml"; | 93 no_update_template_ = kAppNoUpdateTemplate; |
| 56 no_update_template_ = "chromeos/app_mode/webstore/update_check/no_update.xml"; | |
| 57 update_check_end_point_ = update_check_end_point; | 94 update_check_end_point_ = update_check_end_point; |
| 58 | 95 |
| 59 SetupWebStoreURL(embedded_test_server->base_url()); | 96 SetupWebStoreURL(embedded_test_server->base_url()); |
| 60 embedded_test_server->RegisterRequestHandler( | 97 embedded_test_server->RegisterRequestHandler( |
| 61 base::Bind(&FakeCWS::HandleRequest, base::Unretained(this))); | 98 base::Bind(&FakeCWS::HandleRequest, base::Unretained(this))); |
| 62 } | 99 } |
| 63 | 100 |
| 64 void FakeCWS::SetUpdateCrx(const std::string& app_id, | 101 void FakeCWS::SetUpdateCrx(const std::string& app_id, |
| 65 const std::string& crx_file, | 102 const std::string& crx_file, |
| 66 const std::string& version) { | 103 const std::string& version) { |
| 67 GURL crx_download_url = web_store_url_.Resolve(kCrxDownloadPath + crx_file); | 104 GURL crx_download_url = web_store_url_.Resolve(kCrxDownloadPath + crx_file); |
| 68 | 105 |
| 69 base::FilePath test_data_dir; | 106 base::FilePath test_data_dir; |
| 70 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | 107 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
| 71 base::FilePath crx_file_path = | 108 base::FilePath crx_file_path = |
| 72 test_data_dir.AppendASCII("chromeos/app_mode/webstore/downloads") | 109 test_data_dir.AppendASCII("chromeos/app_mode/webstore/downloads") |
| 73 .AppendASCII(crx_file); | 110 .AppendASCII(crx_file); |
| 74 std::string crx_content; | 111 std::string crx_content; |
| 75 ASSERT_TRUE(base::ReadFileToString(crx_file_path, &crx_content)); | 112 ASSERT_TRUE(base::ReadFileToString(crx_file_path, &crx_content)); |
| 76 | 113 |
| 77 const std::string sha256 = crypto::SHA256HashString(crx_content); | 114 const std::string sha256 = crypto::SHA256HashString(crx_content); |
| 78 const std::string sha256_hex = base::HexEncode(sha256.c_str(), sha256.size()); | 115 const std::string sha256_hex = base::HexEncode(sha256.c_str(), sha256.size()); |
| 79 | 116 |
| 80 SetUpdateCheckContent( | 117 std::string update_check_content(has_update_template_); |
| 81 has_update_template_, | 118 base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$AppId", |
| 82 crx_download_url, | 119 app_id); |
| 83 app_id, | 120 base::ReplaceSubstringsAfterOffset( |
| 84 sha256_hex, | 121 &update_check_content, 0, "$CrxDownloadUrl", crx_download_url.spec()); |
| 85 base::UintToString(crx_content.size()), | 122 base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$FP", |
| 86 version, | 123 sha256_hex); |
| 87 &update_check_content_); | 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; |
| 88 } | 129 } |
| 89 | 130 |
| 90 void FakeCWS::SetNoUpdate(const std::string& app_id) { | 131 void FakeCWS::SetNoUpdate(const std::string& app_id) { |
| 91 SetUpdateCheckContent(no_update_template_, | 132 std::string app_update_check_content(no_update_template_); |
| 92 GURL(), | 133 base::ReplaceSubstringsAfterOffset(&app_update_check_content, 0, "$AppId", |
| 93 app_id, | 134 app_id); |
| 94 "", | 135 id_to_update_check_content_map_[app_id] = app_update_check_content; |
| 95 "", | |
| 96 "", | |
| 97 &update_check_content_); | |
| 98 } | 136 } |
| 99 | 137 |
| 100 int FakeCWS::GetUpdateCheckCountAndReset() { | 138 int FakeCWS::GetUpdateCheckCountAndReset() { |
| 101 int current_count = update_check_count_; | 139 int current_count = update_check_count_; |
| 102 update_check_count_ = 0; | 140 update_check_count_ = 0; |
| 103 return current_count; | 141 return current_count; |
| 104 } | 142 } |
| 105 | 143 |
| 106 void FakeCWS::SetupWebStoreURL(const GURL& test_server_url) { | 144 void FakeCWS::SetupWebStoreURL(const GURL& test_server_url) { |
| 107 GURL::Replacements replace_webstore_host; | 145 GURL::Replacements replace_webstore_host; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 121 std::string downloads_path = std::string(kCrxDownloadPath).append("%s.crx"); | 159 std::string downloads_path = std::string(kCrxDownloadPath).append("%s.crx"); |
| 122 GURL downloads_url = web_store_url_.Resolve(downloads_path); | 160 GURL downloads_url = web_store_url_.Resolve(downloads_path); |
| 123 command_line->AppendSwitchASCII(::switches::kAppsGalleryDownloadURL, | 161 command_line->AppendSwitchASCII(::switches::kAppsGalleryDownloadURL, |
| 124 downloads_url.spec()); | 162 downloads_url.spec()); |
| 125 | 163 |
| 126 GURL update_url = web_store_url_.Resolve(update_check_end_point_); | 164 GURL update_url = web_store_url_.Resolve(update_check_end_point_); |
| 127 command_line->AppendSwitchASCII(::switches::kAppsGalleryUpdateURL, | 165 command_line->AppendSwitchASCII(::switches::kAppsGalleryUpdateURL, |
| 128 update_url.spec()); | 166 update_url.spec()); |
| 129 } | 167 } |
| 130 | 168 |
| 131 void FakeCWS::SetUpdateCheckContent(const std::string& update_check_file, | 169 bool FakeCWS::GetUpdateCheckContent(const std::vector<std::string>& ids, |
| 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) { | 170 std::string* update_check_content) { |
| 138 base::FilePath test_data_dir; | 171 std::string apps_content; |
| 139 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | 172 for (const std::string& id : ids) { |
| 140 base::FilePath update_file = | 173 std::string app_update_content; |
| 141 test_data_dir.AppendASCII(update_check_file.c_str()); | 174 auto it = id_to_update_check_content_map_.find(id); |
| 142 ASSERT_TRUE(base::ReadFileToString(update_file, update_check_content)); | 175 if (it == id_to_update_check_content_map_.end()) |
| 176 return false; |
| 177 apps_content.append(it->second); |
| 178 } |
| 179 if (apps_content.empty()) |
| 180 return false; |
| 143 | 181 |
| 144 base::ReplaceSubstringsAfterOffset(update_check_content, 0, "$AppId", app_id); | 182 *update_check_content = kUpdateContentTemplate; |
| 145 base::ReplaceSubstringsAfterOffset( | 183 base::ReplaceSubstringsAfterOffset(update_check_content, 0, "$APPS", |
| 146 update_check_content, 0, "$CrxDownloadUrl", crx_download_url.spec()); | 184 apps_content); |
| 147 base::ReplaceSubstringsAfterOffset(update_check_content, 0, "$FP", crx_fp); | 185 return true; |
| 148 base::ReplaceSubstringsAfterOffset(update_check_content, 0, | |
| 149 "$Size", crx_size); | |
| 150 base::ReplaceSubstringsAfterOffset(update_check_content, 0, | |
| 151 "$Version", version); | |
| 152 } | 186 } |
| 153 | 187 |
| 154 scoped_ptr<HttpResponse> FakeCWS::HandleRequest(const HttpRequest& request) { | 188 scoped_ptr<HttpResponse> FakeCWS::HandleRequest(const HttpRequest& request) { |
| 155 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); | 189 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); |
| 156 std::string request_path = request_url.path(); | 190 std::string request_path = request_url.path(); |
| 157 if (!update_check_content_.empty() && | 191 if (request_path.find(update_check_end_point_) != std::string::npos && |
| 158 request_path.find(update_check_end_point_) != std::string::npos) { | 192 !id_to_update_check_content_map_.empty()) { |
| 159 ++update_check_count_; | 193 std::vector<std::string> ids; |
| 160 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); | 194 if (GetAppIdsFromUpdateUrl(request_url, &ids)) { |
| 161 http_response->set_code(net::HTTP_OK); | 195 std::string update_check_content; |
| 162 http_response->set_content_type("text/xml"); | 196 if (GetUpdateCheckContent(ids, &update_check_content)) { |
| 163 http_response->set_content(update_check_content_); | 197 ++update_check_count_; |
| 164 return http_response.Pass(); | 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(); |
| 203 } |
| 204 } |
| 165 } | 205 } |
| 166 | 206 |
| 167 return scoped_ptr<HttpResponse>(); | 207 return scoped_ptr<HttpResponse>(); |
| 168 } | 208 } |
| 169 | 209 |
| 170 } // namespace chromeos | 210 } // namespace chromeos |
| OLD | NEW |