blob: 8771b4ae7c1d15b781b70ac09f56cca328f231fc [file] [log] [blame]
Nikita Podguzov77f7c832019-09-16 18:48:091// Copyright 2019 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/extensions/policy_test_utils.h"
6
7#include "base/files/file_util.h"
8#include "base/path_service.h"
9#include "base/values.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/common/chrome_paths.h"
12#include "components/policy/core/common/mock_configuration_policy_provider.h"
13#include "components/policy/policy_constants.h"
14#include "extensions/browser/extension_registry.h"
15#include "extensions/browser/test_extension_registry_observer.h"
16#include "net/test/embedded_test_server/embedded_test_server.h"
17#include "net/test/embedded_test_server/http_request.h"
18#include "net/test/embedded_test_server/http_response.h"
19#include "url/gurl.h"
20
21namespace extensions {
22
23namespace policy_test_utils {
24
25namespace {
26
27constexpr char kFileNameToIntercept[] = "update_manifest.xml";
28
29// Replace "mock.http" with "127.0.0.1:<port>" on "update_manifest.xml" files.
30// Host resolver doesn't work here because the test file doesn't know the
31// correct port number.
32std::unique_ptr<net::test_server::HttpResponse> InterceptMockHttp(
33 net::EmbeddedTestServer* embedded_test_server,
34 const net::test_server::HttpRequest& request) {
35 if (request.GetURL().ExtractFileName() != kFileNameToIntercept)
36 return nullptr;
37
38 base::FilePath test_data_dir;
39 base::PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir);
40 // Remove the leading '/'.
41 std::string relative_manifest_path = request.GetURL().path().substr(1);
42 std::string manifest_response;
43 CHECK(base::ReadFileToString(test_data_dir.Append(relative_manifest_path),
44 &manifest_response));
45
46 base::ReplaceSubstringsAfterOffset(
47 &manifest_response, 0, "mock.http",
48 embedded_test_server->host_port_pair().ToString());
49
50 auto response = std::make_unique<net::test_server::BasicHttpResponse>();
51 response->set_content_type("text/xml");
52 response->set_content(manifest_response);
53 return response;
54}
55
56} // namespace
57
58void SetUpEmbeddedTestServer(net::EmbeddedTestServer* embedded_test_server) {
59 embedded_test_server->RegisterRequestHandler(
60 base::BindRepeating(&InterceptMockHttp, embedded_test_server));
61}
62
63void SetExtensionInstallForcelistPolicy(
64 const ExtensionId& extension_id,
65 const GURL& update_manifest_url,
66 Profile* profile,
67 policy::MockConfigurationPolicyProvider* policy_provider) {
68 // Extensions that are force-installed come from an update URL, which defaults
69 // to the webstore. Use a mock URL for test with an update manifest that
70 // includes the crx file of the test extension.
71 base::Value forcelist(base::Value::Type::LIST);
72 forcelist.Append(base::StringPrintf("%s;%s", extension_id.c_str(),
73 update_manifest_url.spec().c_str()));
74
75 policy::PolicyMap policy;
76 policy.Set(policy::key::kExtensionInstallForcelist,
77 policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_MACHINE,
78 policy::POLICY_SOURCE_CLOUD,
79 base::Value::ToUniquePtrValue(std::move(forcelist)), nullptr);
80
81 // Set the policy and wait until the extension is installed.
82 extensions::TestExtensionRegistryObserver observer(
83 ExtensionRegistry::Get(profile));
84 policy_provider->UpdateChromePolicy(policy);
85 observer.WaitForExtensionLoaded();
86}
87
88} // namespace policy_test_utils
89
90} // namespace extensions