Devlin Cronin | fe8a5cf | 2017-12-15 02:58:12 | [diff] [blame] | 1 | // Copyright 2017 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/install_verifier.h" |
| 6 | |
| 7 | #include "base/macros.h" |
| 8 | #include "base/values.h" |
| 9 | #include "chrome/browser/extensions/extension_management.h" |
| 10 | #include "chrome/browser/extensions/extension_service_test_base.h" |
| 11 | #include "chrome/browser/profiles/profile.h" |
| 12 | #include "chrome/test/base/testing_profile.h" |
| 13 | #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 14 | #include "extensions/browser/pref_names.h" |
| 15 | #include "extensions/common/extension.h" |
| 16 | #include "extensions/common/extension_builder.h" |
| 17 | #include "extensions/common/extension_urls.h" |
| 18 | #include "extensions/common/manifest.h" |
| 19 | #include "extensions/common/value_builder.h" |
| 20 | #include "testing/gtest/include/gtest/gtest.h" |
| 21 | |
| 22 | namespace extensions { |
| 23 | |
| 24 | class InstallVerifierTest : public ExtensionServiceTestBase { |
| 25 | public: |
| 26 | InstallVerifierTest() = default; |
| 27 | ~InstallVerifierTest() override = default; |
| 28 | |
| 29 | void SetUp() override { |
| 30 | ExtensionServiceTestBase::SetUp(); |
| 31 | |
| 32 | InitializeExtensionService(ExtensionServiceInitParams()); |
| 33 | } |
| 34 | |
| 35 | // Adds an extension as being allowed by policy. |
| 36 | void AddExtensionAsPolicyInstalled(const ExtensionId& id) { |
| 37 | std::unique_ptr<base::DictionaryValue> extension_entry = |
| 38 | DictionaryBuilder().Set("installation_mode", "allowed").Build(); |
| 39 | testing_profile()->GetTestingPrefService()->SetManagedPref( |
| 40 | pref_names::kExtensionManagement, |
| 41 | DictionaryBuilder().Set(id, std::move(extension_entry)).Build()); |
| 42 | EXPECT_TRUE(ExtensionManagementFactory::GetForBrowserContext(profile()) |
| 43 | ->IsInstallationExplicitlyAllowed(id)); |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | ScopedInstallVerifierBypassForTest force_install_verification{ |
| 48 | ScopedInstallVerifierBypassForTest::kForceOn}; |
| 49 | std::unique_ptr<ExtensionManagement> extension_management_; |
| 50 | std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 51 | |
| 52 | DISALLOW_COPY_AND_ASSIGN(InstallVerifierTest); |
| 53 | }; |
| 54 | |
| 55 | // Test the behavior of the InstallVerifier for various extensions. |
| 56 | TEST_F(InstallVerifierTest, TestIsFromStoreAndMustRemainDisabled) { |
| 57 | enum FromStoreStatus { |
| 58 | FROM_STORE, |
| 59 | NOT_FROM_STORE, |
| 60 | }; |
| 61 | |
| 62 | enum MustRemainDisabledStatus { |
| 63 | MUST_REMAIN_DISABLED, |
| 64 | CAN_BE_ENABLED, |
| 65 | }; |
| 66 | |
| 67 | GURL store_update_url = extension_urls::GetWebstoreUpdateUrl(); |
| 68 | GURL non_store_update_url("https://example.com"); |
| 69 | struct { |
| 70 | const char* test_name; |
| 71 | Manifest::Location location; |
| 72 | base::Optional<GURL> update_url; |
| 73 | FromStoreStatus expected_from_store_status; |
| 74 | MustRemainDisabledStatus expected_must_remain_disabled_status; |
| 75 | } test_cases[] = { |
| 76 | {"internal from store", Manifest::INTERNAL, store_update_url, FROM_STORE, |
| 77 | CAN_BE_ENABLED}, |
| 78 | {"internal non-store update url", Manifest::INTERNAL, |
| 79 | non_store_update_url, NOT_FROM_STORE, MUST_REMAIN_DISABLED}, |
| 80 | {"internal no update url", Manifest::INTERNAL, base::nullopt, |
| 81 | NOT_FROM_STORE, MUST_REMAIN_DISABLED}, |
| 82 | {"unpacked from store", Manifest::UNPACKED, store_update_url, FROM_STORE, |
| 83 | CAN_BE_ENABLED}, |
| 84 | {"unpacked non-store update url", Manifest::UNPACKED, |
| 85 | non_store_update_url, NOT_FROM_STORE, CAN_BE_ENABLED}, |
| 86 | {"unpacked no update url", Manifest::UNPACKED, base::nullopt, |
| 87 | NOT_FROM_STORE, CAN_BE_ENABLED}, |
| 88 | {"external from store", Manifest::EXTERNAL_POLICY_DOWNLOAD, |
| 89 | store_update_url, FROM_STORE, CAN_BE_ENABLED}, |
| 90 | {"external non-store update url", Manifest::EXTERNAL_POLICY_DOWNLOAD, |
| 91 | non_store_update_url, NOT_FROM_STORE, CAN_BE_ENABLED}, |
| 92 | }; |
| 93 | |
| 94 | InstallVerifier* install_verifier = InstallVerifier::Get(profile()); |
| 95 | for (const auto& test_case : test_cases) { |
| 96 | SCOPED_TRACE(test_case.test_name); |
| 97 | ExtensionBuilder extension_builder(test_case.test_name); |
| 98 | extension_builder.SetLocation(test_case.location); |
| 99 | if (test_case.update_url) { |
Devlin Cronin | 98cd658 | 2018-05-08 19:18:12 | [diff] [blame] | 100 | extension_builder.SetManifestKey("update_url", |
| 101 | test_case.update_url->spec()); |
Devlin Cronin | fe8a5cf | 2017-12-15 02:58:12 | [diff] [blame] | 102 | } |
| 103 | scoped_refptr<const Extension> extension = extension_builder.Build(); |
| 104 | |
| 105 | if (Manifest::IsPolicyLocation(test_case.location)) |
| 106 | AddExtensionAsPolicyInstalled(extension->id()); |
| 107 | |
| 108 | EXPECT_EQ(test_case.expected_from_store_status == FROM_STORE, |
| 109 | InstallVerifier::IsFromStore(*extension)); |
| 110 | disable_reason::DisableReason disable_reason; |
| 111 | base::string16 error; |
| 112 | EXPECT_EQ( |
| 113 | test_case.expected_must_remain_disabled_status == MUST_REMAIN_DISABLED, |
| 114 | install_verifier->MustRemainDisabled(extension.get(), &disable_reason, |
| 115 | &error)) |
| 116 | << error; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | } // namespace extensions |