File: extension_migrator_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (133 lines) | stat: -rw-r--r-- 4,767 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_migrator.h"

#include <memory>

#include "base/files/file_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_service_test_base.h"
#include "chrome/browser/extensions/external_provider_impl.h"
#include "chrome/browser/extensions/external_provider_manager.h"
#include "chrome/test/base/testing_profile.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/pending_extension_manager.h"
#include "extensions/common/extension_builder.h"

namespace extensions {

namespace {

const char kOldId[] = "oooooooooooooooooooooooooooooooo";
const char kNewId[] = "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn";

scoped_refptr<const Extension> CreateExtension(
    const std::string& id,
    mojom::ManifestLocation location) {
  return ExtensionBuilder("test").SetID(id).SetLocation(location).Build();
}

}  // namespace

class ExtensionMigratorTest : public ExtensionServiceTestBase {
 public:
  ExtensionMigratorTest() = default;

  ExtensionMigratorTest(const ExtensionMigratorTest&) = delete;
  ExtensionMigratorTest& operator=(const ExtensionMigratorTest&) = delete;

  ~ExtensionMigratorTest() override = default;

 protected:
  void InitWithExistingProfile() {
    ExtensionServiceInitParams params;
    // Create prefs file to make the profile not new.
    params.prefs_content = "{}";
    params.is_first_run = false;
    InitializeExtensionService(std::move(params));
    service()->Init();
    AddMigratorProvider();
  }

  void AddMigratorProvider() {
    ExternalProviderManager::Get(profile())->AddProviderForTesting(
        std::make_unique<ExternalProviderImpl>(
            external_provider_manager(),
            base::MakeRefCounted<ExtensionMigrator>(profile(), kOldId, kNewId),
            profile(), mojom::ManifestLocation::kExternalPref,
            mojom::ManifestLocation::kExternalPrefDownload,
            Extension::FROM_WEBSTORE | Extension::WAS_INSTALLED_BY_DEFAULT));
  }

  scoped_refptr<const Extension> AddExtension(
      const std::string& id,
      mojom::ManifestLocation location) {
    scoped_refptr<const Extension> fake_app = CreateExtension(id, location);
    registrar()->AddExtension(fake_app);
    return fake_app;
  }

  bool HasNewExtension() {
    return PendingExtensionManager::Get(profile())->IsIdPending(kNewId) ||
           registry()->GetInstalledExtension(kNewId);
  }

  ExternalProviderManager* external_provider_manager() {
    return ExternalProviderManager::Get(profile());
  }
};

TEST_F(ExtensionMigratorTest, NoExistingOld) {
  InitWithExistingProfile();
  external_provider_manager()->CheckForExternalUpdates();
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(HasNewExtension());
}

TEST_F(ExtensionMigratorTest, HasExistingOld) {
  InitWithExistingProfile();
  AddExtension(kOldId, mojom::ManifestLocation::kExternalPrefDownload);
  external_provider_manager()->CheckForExternalUpdates();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(HasNewExtension());
  EXPECT_TRUE(registry()->GetInstalledExtension(kOldId));
}

TEST_F(ExtensionMigratorTest, KeepExistingNew) {
  InitWithExistingProfile();
  AddExtension(kNewId, mojom::ManifestLocation::kExternalPrefDownload);
  external_provider_manager()->CheckForExternalUpdates();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(registry()->GetInstalledExtension(kNewId));
}

TEST_F(ExtensionMigratorTest, HasBothOldAndNew) {
  InitWithExistingProfile();
  AddExtension(kOldId, mojom::ManifestLocation::kExternalPrefDownload);
  AddExtension(kNewId, mojom::ManifestLocation::kExternalPrefDownload);
  external_provider_manager()->CheckForExternalUpdates();
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(registry()->GetInstalledExtension(kOldId));
  EXPECT_TRUE(registry()->GetInstalledExtension(kNewId));
}

// Tests that a previously-force-installed extension can be uninstalled.
// crbug.com/1416682
TEST_F(ExtensionMigratorTest, HasPreviouslyForceInstalledNew) {
  InitWithExistingProfile();
  scoped_refptr<const Extension> extension =
      AddExtension(kNewId, mojom::ManifestLocation::kExternalPolicyDownload);
  registrar()->OnExtensionInstalled(extension.get(), syncer::StringOrdinal());
  external_provider_manager()->CheckForExternalUpdates();
  base::RunLoop().RunUntilIdle();
  // A previously-force-installed-extension should not be persisted by the
  // ExtensionMigrator.
  EXPECT_FALSE(registry()->GetInstalledExtension(kNewId));
}

}  // namespace extensions