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\"/></app>"; | |
xiyuan
2015/08/26 18:16:16
nit: indent 2 more spaces and split </app> to the
jennyz
2015/08/28 18:24:08
Done.
| |
36 | |
37 const char kAppHasUpdateTemplate[] = | |
38 "<app appid=\"$AppId\" status=\"ok\">" | |
39 "<updatecheck codebase=\"$CrxDownloadUrl\" fp=\"1.$FP\" " | |
40 "hash=\"\" hash_sha256=\"$FP\" size=\"$Size\" status=\"ok\" " | |
41 "version=\"$Version\"/></app>"; | |
42 | |
43 const char kPrivateStoreAppHasUpdateTemplate[] = | |
44 "<app appid=\"$AppId\">" | |
45 "<updatecheck codebase=\"$CrxDownloadUrl\" version=\"$Version\"/></app>"; | |
46 | |
47 const char kUpdateContentTemplate[] = | |
48 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
49 "<gupdate xmlns=\"http://www.google.com/update2/response\" " | |
50 "protocol=\"2.0\" server=\"prod\">" | |
51 "<daystart elapsed_days=\"2569\" " | |
52 "elapsed_seconds=\"36478\"/>$APPS</gupdate>"; | |
53 | |
54 bool GetAppIdsFromUpdateUrl(const GURL& update_url, | |
55 std::vector<std::string>* ids) { | |
56 for (net::QueryIterator it(update_url); !it.IsAtEnd(); it.Advance()) { | |
57 if (it.GetKey() != "x") | |
58 continue; | |
59 std::string id; | |
60 net::GetValueForKeyInQuery(GURL("http://dummy?" + it.GetUnescapedValue()), | |
61 "id", &id); | |
62 ids->push_back(id); | |
63 } | |
64 return !ids->empty(); | |
65 } | |
66 | |
32 } // namespace | 67 } // namespace |
33 | 68 |
34 FakeCWS::FakeCWS() : update_check_count_(0) { | 69 FakeCWS::FakeCWS() : update_check_count_(0) { |
not at google - send to devlin
2015/08/26 17:53:58
Is somebody reviewing this file for correctness?
jennyz
2015/08/28 18:24:08
xiyuan is reviewing this one.
| |
35 } | 70 } |
36 | 71 |
37 FakeCWS::~FakeCWS() { | 72 FakeCWS::~FakeCWS() { |
38 } | 73 } |
39 | 74 |
40 void FakeCWS::Init(EmbeddedTestServer* embedded_test_server) { | 75 void FakeCWS::Init(EmbeddedTestServer* embedded_test_server) { |
41 has_update_template_ = | 76 has_update_template_ = std::string(kAppHasUpdateTemplate); |
not at google - send to devlin
2015/08/26 17:53:58
std::string not needed, it will implicitly convert
jennyz
2015/08/28 18:24:08
Done.
| |
42 "chromeos/app_mode/webstore/update_check/has_update.xml"; | 77 no_update_template_ = std::string(kAppNoUpdateTemplate); |
43 no_update_template_ = "chromeos/app_mode/webstore/update_check/no_update.xml"; | |
xiyuan
2015/08/26 18:16:16
nit: remove the two files since we have moved the
| |
44 update_check_end_point_ = "/update_check.xml"; | 78 update_check_end_point_ = "/update_check.xml"; |
45 | 79 |
46 SetupWebStoreURL(embedded_test_server->base_url()); | 80 SetupWebStoreURL(embedded_test_server->base_url()); |
47 OverrideGalleryCommandlineSwitches(); | 81 OverrideGalleryCommandlineSwitches(); |
48 embedded_test_server->RegisterRequestHandler( | 82 embedded_test_server->RegisterRequestHandler( |
49 base::Bind(&FakeCWS::HandleRequest, base::Unretained(this))); | 83 base::Bind(&FakeCWS::HandleRequest, base::Unretained(this))); |
50 } | 84 } |
51 | 85 |
52 void FakeCWS::InitAsPrivateStore(EmbeddedTestServer* embedded_test_server, | 86 void FakeCWS::InitAsPrivateStore(EmbeddedTestServer* embedded_test_server, |
53 const std::string& update_check_end_point) { | 87 const std::string& update_check_end_point) { |
54 has_update_template_ = | 88 has_update_template_ = std::string(kPrivateStoreAppHasUpdateTemplate); |
55 "chromeos/app_mode/webstore/update_check/has_update_private_store.xml"; | 89 no_update_template_ = std::string(kAppNoUpdateTemplate); |
xiyuan
2015/08/26 18:16:16
nit: remove this file too.
| |
56 no_update_template_ = "chromeos/app_mode/webstore/update_check/no_update.xml"; | |
57 update_check_end_point_ = update_check_end_point; | 90 update_check_end_point_ = update_check_end_point; |
58 | 91 |
59 SetupWebStoreURL(embedded_test_server->base_url()); | 92 SetupWebStoreURL(embedded_test_server->base_url()); |
60 embedded_test_server->RegisterRequestHandler( | 93 embedded_test_server->RegisterRequestHandler( |
61 base::Bind(&FakeCWS::HandleRequest, base::Unretained(this))); | 94 base::Bind(&FakeCWS::HandleRequest, base::Unretained(this))); |
62 } | 95 } |
63 | 96 |
64 void FakeCWS::SetUpdateCrx(const std::string& app_id, | 97 void FakeCWS::SetUpdateCrx(const std::string& app_id, |
65 const std::string& crx_file, | 98 const std::string& crx_file, |
66 const std::string& version) { | 99 const std::string& version) { |
67 GURL crx_download_url = web_store_url_.Resolve(kCrxDownloadPath + crx_file); | 100 GURL crx_download_url = web_store_url_.Resolve(kCrxDownloadPath + crx_file); |
68 | 101 |
69 base::FilePath test_data_dir; | 102 base::FilePath test_data_dir; |
70 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | 103 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); |
71 base::FilePath crx_file_path = | 104 base::FilePath crx_file_path = |
72 test_data_dir.AppendASCII("chromeos/app_mode/webstore/downloads") | 105 test_data_dir.AppendASCII("chromeos/app_mode/webstore/downloads") |
73 .AppendASCII(crx_file); | 106 .AppendASCII(crx_file); |
74 std::string crx_content; | 107 std::string crx_content; |
75 ASSERT_TRUE(base::ReadFileToString(crx_file_path, &crx_content)); | 108 ASSERT_TRUE(base::ReadFileToString(crx_file_path, &crx_content)); |
76 | 109 |
77 const std::string sha256 = crypto::SHA256HashString(crx_content); | 110 const std::string sha256 = crypto::SHA256HashString(crx_content); |
78 const std::string sha256_hex = base::HexEncode(sha256.c_str(), sha256.size()); | 111 const std::string sha256_hex = base::HexEncode(sha256.c_str(), sha256.size()); |
79 | 112 |
80 SetUpdateCheckContent( | 113 std::string update_check_content(has_update_template_); |
81 has_update_template_, | 114 base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$AppId", |
82 crx_download_url, | 115 app_id); |
83 app_id, | 116 base::ReplaceSubstringsAfterOffset( |
84 sha256_hex, | 117 &update_check_content, 0, "$CrxDownloadUrl", crx_download_url.spec()); |
85 base::UintToString(crx_content.size()), | 118 base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$FP", |
86 version, | 119 sha256_hex); |
87 &update_check_content_); | 120 base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$Size", |
121 base::UintToString(crx_content.size())); | |
122 base::ReplaceSubstringsAfterOffset(&update_check_content, 0, "$Version", | |
123 version); | |
124 id_to_update_check_content_map_[app_id] = update_check_content; | |
88 } | 125 } |
89 | 126 |
90 void FakeCWS::SetNoUpdate(const std::string& app_id) { | 127 void FakeCWS::SetNoUpdate(const std::string& app_id) { |
91 SetUpdateCheckContent(no_update_template_, | 128 std::string app_update_check_content(no_update_template_); |
92 GURL(), | 129 base::ReplaceSubstringsAfterOffset(&app_update_check_content, 0, "$AppId", |
93 app_id, | 130 app_id); |
94 "", | 131 id_to_update_check_content_map_[app_id] = app_update_check_content; |
95 "", | |
96 "", | |
97 &update_check_content_); | |
98 } | 132 } |
99 | 133 |
100 int FakeCWS::GetUpdateCheckCountAndReset() { | 134 int FakeCWS::GetUpdateCheckCountAndReset() { |
101 int current_count = update_check_count_; | 135 int current_count = update_check_count_; |
102 update_check_count_ = 0; | 136 update_check_count_ = 0; |
103 return current_count; | 137 return current_count; |
104 } | 138 } |
105 | 139 |
106 void FakeCWS::SetupWebStoreURL(const GURL& test_server_url) { | 140 void FakeCWS::SetupWebStoreURL(const GURL& test_server_url) { |
107 GURL::Replacements replace_webstore_host; | 141 GURL::Replacements replace_webstore_host; |
(...skipping 13 matching lines...) Expand all Loading... | |
121 std::string downloads_path = std::string(kCrxDownloadPath).append("%s.crx"); | 155 std::string downloads_path = std::string(kCrxDownloadPath).append("%s.crx"); |
122 GURL downloads_url = web_store_url_.Resolve(downloads_path); | 156 GURL downloads_url = web_store_url_.Resolve(downloads_path); |
123 command_line->AppendSwitchASCII(::switches::kAppsGalleryDownloadURL, | 157 command_line->AppendSwitchASCII(::switches::kAppsGalleryDownloadURL, |
124 downloads_url.spec()); | 158 downloads_url.spec()); |
125 | 159 |
126 GURL update_url = web_store_url_.Resolve(update_check_end_point_); | 160 GURL update_url = web_store_url_.Resolve(update_check_end_point_); |
127 command_line->AppendSwitchASCII(::switches::kAppsGalleryUpdateURL, | 161 command_line->AppendSwitchASCII(::switches::kAppsGalleryUpdateURL, |
128 update_url.spec()); | 162 update_url.spec()); |
129 } | 163 } |
130 | 164 |
131 void FakeCWS::SetUpdateCheckContent(const std::string& update_check_file, | 165 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) { | 166 std::string* update_check_content) { |
138 base::FilePath test_data_dir; | 167 std::string apps_content; |
139 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | 168 for (size_t i = 0; i < ids.size(); ++i) { |
140 base::FilePath update_file = | 169 std::string app_update_content; |
141 test_data_dir.AppendASCII(update_check_file.c_str()); | 170 if (id_to_update_check_content_map_.find(ids[i]) == |
142 ASSERT_TRUE(base::ReadFileToString(update_file, update_check_content)); | 171 id_to_update_check_content_map_.end()) |
172 return false; | |
173 apps_content.append(id_to_update_check_content_map_[ids[i]]); | |
xiyuan
2015/08/26 18:16:16
nit: use iterate to save a bit
auto it = id_to_up
jennyz
2015/08/28 18:24:08
Done.
| |
174 } | |
175 if (apps_content.empty()) | |
176 return false; | |
143 | 177 |
144 base::ReplaceSubstringsAfterOffset(update_check_content, 0, "$AppId", app_id); | 178 *update_check_content = std::string(kUpdateContentTemplate); |
145 base::ReplaceSubstringsAfterOffset( | 179 base::ReplaceSubstringsAfterOffset(update_check_content, 0, "$APPS", |
146 update_check_content, 0, "$CrxDownloadUrl", crx_download_url.spec()); | 180 apps_content); |
147 base::ReplaceSubstringsAfterOffset(update_check_content, 0, "$FP", crx_fp); | 181 return true; |
148 base::ReplaceSubstringsAfterOffset(update_check_content, 0, | |
149 "$Size", crx_size); | |
150 base::ReplaceSubstringsAfterOffset(update_check_content, 0, | |
151 "$Version", version); | |
152 } | 182 } |
153 | 183 |
154 scoped_ptr<HttpResponse> FakeCWS::HandleRequest(const HttpRequest& request) { | 184 scoped_ptr<HttpResponse> FakeCWS::HandleRequest(const HttpRequest& request) { |
155 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); | 185 GURL request_url = GURL("http://localhost").Resolve(request.relative_url); |
156 std::string request_path = request_url.path(); | 186 std::string request_path = request_url.path(); |
157 if (!update_check_content_.empty() && | 187 if (request_path.find(update_check_end_point_) != std::string::npos && |
158 request_path.find(update_check_end_point_) != std::string::npos) { | 188 !id_to_update_check_content_map_.empty()) { |
159 ++update_check_count_; | 189 std::vector<std::string> ids; |
160 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); | 190 if (GetAppIdsFromUpdateUrl(request_url, &ids)) { |
161 http_response->set_code(net::HTTP_OK); | 191 std::string update_check_content; |
162 http_response->set_content_type("text/xml"); | 192 if (GetUpdateCheckContent(ids, &update_check_content)) { |
163 http_response->set_content(update_check_content_); | 193 ++update_check_count_; |
164 return http_response.Pass(); | 194 scoped_ptr<BasicHttpResponse> http_response(new BasicHttpResponse()); |
195 http_response->set_code(net::HTTP_OK); | |
196 http_response->set_content_type("text/xml"); | |
197 http_response->set_content(update_check_content); | |
198 return http_response.Pass(); | |
199 } | |
200 } | |
165 } | 201 } |
166 | 202 |
167 return scoped_ptr<HttpResponse>(); | 203 return scoped_ptr<HttpResponse>(); |
168 } | 204 } |
169 | 205 |
170 } // namespace chromeos | 206 } // namespace chromeos |
OLD | NEW |