Suppress a Valgrind report.
[chromium-blink-merge.git] / extensions / browser / updater / update_service_browsertest.cc
blobc4ff0ef0cb56fe50262a9f5dc74628a54fb4a366
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 <map>
6 #include <string>
7 #include <utility>
9 #include "base/bind.h"
10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "content/public/test/test_utils.h"
14 #include "extensions/browser/api/extensions_api_client.h"
15 #include "extensions/browser/extensions_browser_client.h"
16 #include "extensions/browser/updater/update_service.h"
17 #include "extensions/common/extension_urls.h"
18 #include "extensions/shell/test/shell_test.h"
19 #include "net/base/escape.h"
20 #include "net/http/http_status_code.h"
21 #include "net/url_request/test_url_fetcher_factory.h"
23 namespace extensions {
25 namespace {
27 using FakeResponse = std::pair<std::string, net::HttpStatusCode>;
29 // TODO(rockot): In general there's enough mock-Omaha-noise that this might be
30 // better placed into some test library code in //components/update_client.
31 FakeResponse CreateFakeUpdateResponse(const std::string& id,
32 size_t crx_length) {
33 std::string response_text = base::StringPrintf(
34 "<gupdate xmlns=\"http://www.google.com/update2/response\" "
35 " protocol=\"2.0\" server=\"prod\">\n"
36 " <daystart elapsed_days=\"2860\" elapsed_seconds=\"42042\"/>\n"
37 " <app appid=\"%s\" status=\"ok\">\n"
38 " <updatecheck codebase=\"%s\" fp=\"0\" hash=\"\" hash_sha256=\"\" "
39 " size=\"%d\" status=\"ok\" version=\"1\"/>\n"
40 " </app>\n"
41 "</gupdate>\n",
42 id.c_str(),
43 base::StringPrintf("https://fake-omaha-hostname/%s.crx",
44 id.c_str()).c_str(),
45 static_cast<int>(crx_length));
46 return std::make_pair(response_text, net::HTTP_OK);
49 FakeResponse CreateFakeUpdateNotFoundResponse() {
50 return std::make_pair(
51 std::string(
52 "<gupdate xmlns=\"http://www.google.com/update2/response\" "
53 " protocol=\"2.0\" server=\"prod\">\n"
54 " <daystart elapsed_days=\"4242\" elapsed_seconds=\"42042\"/>\n"
55 " <app appid=\"\" status=\"error-invalidAppId\">\n"
56 "</gupdate>"),
57 net::HTTP_OK);
60 bool ExtractKeyValueFromComponent(const std::string& component_str,
61 const std::string& target_key,
62 std::string* extracted_value) {
63 url::Component component(0, static_cast<int>(component_str.length()));
64 url::Component key, value;
65 while (url::ExtractQueryKeyValue(component_str.c_str(), &component, &key,
66 &value)) {
67 if (target_key == component_str.substr(key.begin, key.len)) {
68 *extracted_value = component_str.substr(value.begin, value.len);
69 return true;
72 return false;
75 // This extracts an extension ID from an Omaha update query. Queries have the
76 // form https://foo/bar/update?x=id%3Dabcdefghijklmnopqrstuvwxyzaaaaaa%26...
77 // This function extracts the 'x' query parameter (e.g. "id%3Dabcdef...."),
78 // unescapes its value (to become e.g., "id=abcdef...", and then extracts the
79 // 'id' value from the result (e.g. "abcdef...").
80 bool ExtractIdFromUpdateQuery(const std::string& query_str, std::string* id) {
81 std::string data_string;
82 if (!ExtractKeyValueFromComponent(query_str, "x", &data_string))
83 return false;
84 data_string = net::UnescapeURLComponent(data_string,
85 net::UnescapeRule::URL_SPECIAL_CHARS);
86 if (!ExtractKeyValueFromComponent(data_string, "id", id))
87 return false;
88 EXPECT_EQ(32u, id->size());
89 return true;
92 void ExpectDownloadSuccess(const base::Closure& continuation, bool success) {
93 EXPECT_TRUE(success) << "Download failed.";
94 continuation.Run();
97 class FakeUpdateURLFetcherFactory : public net::URLFetcherFactory {
98 public:
99 ~FakeUpdateURLFetcherFactory() override {}
101 void RegisterFakeExtension(const std::string& id,
102 const std::string& contents) {
103 CHECK_EQ(32u, id.size());
104 fake_extensions_.insert(std::make_pair(id, contents));
107 // net::URLFetcherFactory:
108 net::URLFetcher* CreateURLFetcher(
109 int id,
110 const GURL& url,
111 net::URLFetcher::RequestType request_type,
112 net::URLFetcherDelegate* delegate) override {
113 if (url.spec().find(extension_urls::GetWebstoreUpdateUrl().spec()) == 0) {
114 // Handle fake Omaha requests.
115 return CreateUpdateManifestFetcher(url, delegate);
116 } else if (url.spec().find("https://fake-omaha-hostname") == 0) {
117 // Handle a fake CRX request.
118 return CreateCrxFetcher(url, delegate);
120 NOTREACHED();
121 return nullptr;
124 private:
125 net::URLFetcher* CreateUpdateManifestFetcher(
126 const GURL& url,
127 net::URLFetcherDelegate* delegate) {
128 // If we have a fake CRX for the ID, return a fake update blob for it.
129 // Otherwise return an invalid-ID response.
130 FakeResponse response;
131 std::string extension_id;
132 if (!ExtractIdFromUpdateQuery(url.query(), &extension_id)) {
133 response = CreateFakeUpdateNotFoundResponse();
134 } else {
135 const auto& iter = fake_extensions_.find(extension_id);
136 if (iter == fake_extensions_.end())
137 response = CreateFakeUpdateNotFoundResponse();
138 else
139 response = CreateFakeUpdateResponse(extension_id, iter->second.size());
141 return new net::FakeURLFetcher(url, delegate, response.first,
142 response.second,
143 net::URLRequestStatus::SUCCESS);
146 net::URLFetcher* CreateCrxFetcher(const GURL& url,
147 net::URLFetcherDelegate* delegate) {
148 FakeResponse response;
149 std::string extension_id = url.path().substr(1, 32);
150 const auto& iter = fake_extensions_.find(extension_id);
151 if (iter == fake_extensions_.end())
152 response = std::make_pair(std::string(), net::HTTP_NOT_FOUND);
153 else
154 response = std::make_pair(iter->second, net::HTTP_OK);
155 net::TestURLFetcher* fetcher =
156 new net::FakeURLFetcher(url, delegate, response.first, response.second,
157 net::URLRequestStatus::SUCCESS);
158 fetcher->SetResponseFilePath(base::FilePath::FromUTF8Unsafe(url.path()));
159 return fetcher;
162 std::map<std::string, std::string> fake_extensions_;
165 } // namespace
167 class UpdateServiceTest : public AppShellTest {
168 public:
169 UpdateServiceTest() {}
170 ~UpdateServiceTest() override {}
172 void SetUpOnMainThread() override {
173 AppShellTest::SetUpOnMainThread();
175 update_service_ = UpdateService::Get(browser_context());
177 default_url_fetcher_factory_.reset(new FakeUpdateURLFetcherFactory());
178 url_fetcher_factory_.reset(
179 new net::FakeURLFetcherFactory(default_url_fetcher_factory_.get()));
182 protected:
183 void RegisterFakeExtension(const std::string& id,
184 const std::string& crx_contents) {
185 default_url_fetcher_factory_->RegisterFakeExtension(id, crx_contents);
188 UpdateService* update_service() const { return update_service_; }
190 private:
191 scoped_ptr<FakeUpdateURLFetcherFactory> default_url_fetcher_factory_;
192 scoped_ptr<net::FakeURLFetcherFactory> url_fetcher_factory_;
194 UpdateService* update_service_;
197 IN_PROC_BROWSER_TEST_F(UpdateServiceTest, DownloadAndInstall) {
198 const char kCrxId[] = "abcdefghijklmnopqrstuvwxyzaaaaaa";
199 const char kCrxContents[] = "Hello, world!";
200 RegisterFakeExtension(kCrxId, kCrxContents);
202 base::RunLoop run_loop;
203 update_service()->DownloadAndInstall(
204 kCrxId, base::Bind(ExpectDownloadSuccess, run_loop.QuitClosure()));
205 run_loop.Run();
208 } // namespace extensions