summaryrefslogtreecommitdiffstats
path: root/extensions/browser/extension_registry_unittest.cc
blob: 075019b244c4a4dde186bdf59e0a62422ab450e0 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/extension_registry.h"

#include <string>

#include "base/memory/ref_counted.h"
#include "extensions/common/extension.h"
#include "extensions/common/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {
namespace {

typedef testing::Test ExtensionRegistryTest;

TEST_F(ExtensionRegistryTest, FillAndClearRegistry) {
  ExtensionRegistry registry;
  scoped_refptr<Extension> extension1 = test_util::CreateExtensionWithID("id1");
  scoped_refptr<Extension> extension2 = test_util::CreateExtensionWithID("id2");
  scoped_refptr<Extension> extension3 = test_util::CreateExtensionWithID("id3");
  scoped_refptr<Extension> extension4 = test_util::CreateExtensionWithID("id4");

  // All the sets start empty.
  EXPECT_EQ(0u, registry.enabled_extensions().size());
  EXPECT_EQ(0u, registry.disabled_extensions().size());
  EXPECT_EQ(0u, registry.terminated_extensions().size());
  EXPECT_EQ(0u, registry.blacklisted_extensions().size());

  // Extensions can be added to each set.
  registry.AddEnabled(extension1);
  registry.AddDisabled(extension2);
  registry.AddTerminated(extension3);
  registry.AddBlacklisted(extension4);

  EXPECT_EQ(1u, registry.enabled_extensions().size());
  EXPECT_EQ(1u, registry.disabled_extensions().size());
  EXPECT_EQ(1u, registry.terminated_extensions().size());
  EXPECT_EQ(1u, registry.blacklisted_extensions().size());

  // Clearing the registry clears all sets.
  registry.ClearAll();

  EXPECT_EQ(0u, registry.enabled_extensions().size());
  EXPECT_EQ(0u, registry.disabled_extensions().size());
  EXPECT_EQ(0u, registry.terminated_extensions().size());
  EXPECT_EQ(0u, registry.blacklisted_extensions().size());
}

// A simple test of adding and removing things from sets.
TEST_F(ExtensionRegistryTest, AddAndRemoveExtensionFromRegistry) {
  ExtensionRegistry registry;

  // Adding an extension works.
  scoped_refptr<Extension> extension = test_util::CreateExtensionWithID("id");
  EXPECT_TRUE(registry.AddEnabled(extension));
  EXPECT_EQ(1u, registry.enabled_extensions().size());

  // The extension was only added to one set.
  EXPECT_EQ(0u, registry.disabled_extensions().size());
  EXPECT_EQ(0u, registry.terminated_extensions().size());
  EXPECT_EQ(0u, registry.blacklisted_extensions().size());

  // Removing an extension works.
  EXPECT_TRUE(registry.RemoveEnabled(extension->id()));
  EXPECT_EQ(0u, registry.enabled_extensions().size());

  // Trying to remove an extension that isn't in the set fails cleanly.
  EXPECT_FALSE(registry.RemoveEnabled(extension->id()));
}

TEST_F(ExtensionRegistryTest, AddExtensionToRegistryTwice) {
  ExtensionRegistry registry;
  scoped_refptr<Extension> extension = test_util::CreateExtensionWithID("id");

  // An extension can exist in two sets at once. It would be nice to eliminate
  // this functionality, but some users of ExtensionRegistry need it.
  EXPECT_TRUE(registry.AddEnabled(extension));
  EXPECT_TRUE(registry.AddDisabled(extension));

  EXPECT_EQ(1u, registry.enabled_extensions().size());
  EXPECT_EQ(1u, registry.disabled_extensions().size());
  EXPECT_EQ(0u, registry.terminated_extensions().size());
  EXPECT_EQ(0u, registry.blacklisted_extensions().size());
}

TEST_F(ExtensionRegistryTest, GetExtensionById) {
  ExtensionRegistry registry;

  // Trying to get an extension fails cleanly when the sets are empty.
  EXPECT_FALSE(
      registry.GetExtensionById("id", ExtensionRegistry::EVERYTHING));

  scoped_refptr<Extension> enabled =
      test_util::CreateExtensionWithID("enabled");
  scoped_refptr<Extension> disabled =
      test_util::CreateExtensionWithID("disabled");
  scoped_refptr<Extension> terminated =
      test_util::CreateExtensionWithID("terminated");
  scoped_refptr<Extension> blacklisted =
      test_util::CreateExtensionWithID("blacklisted");

  // Add an extension to each set.
  registry.AddEnabled(enabled);
  registry.AddDisabled(disabled);
  registry.AddTerminated(terminated);
  registry.AddBlacklisted(blacklisted);

  // Enabled is part of everything and the enabled list.
  EXPECT_TRUE(
      registry.GetExtensionById("enabled", ExtensionRegistry::EVERYTHING));
  EXPECT_TRUE(
      registry.GetExtensionById("enabled", ExtensionRegistry::ENABLED));
  EXPECT_FALSE(
      registry.GetExtensionById("enabled", ExtensionRegistry::DISABLED));
  EXPECT_FALSE(
      registry.GetExtensionById("enabled", ExtensionRegistry::TERMINATED));
  EXPECT_FALSE(
      registry.GetExtensionById("enabled", ExtensionRegistry::BLACKLISTED));

  // Disabled is part of everything and the disabled list.
  EXPECT_TRUE(
      registry.GetExtensionById("disabled", ExtensionRegistry::EVERYTHING));
  EXPECT_FALSE(
      registry.GetExtensionById("disabled", ExtensionRegistry::ENABLED));
  EXPECT_TRUE(
      registry.GetExtensionById("disabled", ExtensionRegistry::DISABLED));
  EXPECT_FALSE(
      registry.GetExtensionById("disabled", ExtensionRegistry::TERMINATED));
  EXPECT_FALSE(
      registry.GetExtensionById("disabled", ExtensionRegistry::BLACKLISTED));

  // Terminated is part of everything and the terminated list.
  EXPECT_TRUE(
      registry.GetExtensionById("terminated", ExtensionRegistry::EVERYTHING));
  EXPECT_FALSE(
      registry.GetExtensionById("terminated", ExtensionRegistry::ENABLED));
  EXPECT_FALSE(
      registry.GetExtensionById("terminated", ExtensionRegistry::DISABLED));
  EXPECT_TRUE(
      registry.GetExtensionById("terminated", ExtensionRegistry::TERMINATED));
  EXPECT_FALSE(
      registry.GetExtensionById("terminated", ExtensionRegistry::BLACKLISTED));

  // Blacklisted is part of everything and the blacklisted list.
  EXPECT_TRUE(
      registry.GetExtensionById("blacklisted", ExtensionRegistry::EVERYTHING));
  EXPECT_FALSE(
      registry.GetExtensionById("blacklisted", ExtensionRegistry::ENABLED));
  EXPECT_FALSE(
      registry.GetExtensionById("blacklisted", ExtensionRegistry::DISABLED));
  EXPECT_FALSE(
      registry.GetExtensionById("blacklisted", ExtensionRegistry::TERMINATED));
  EXPECT_TRUE(
      registry.GetExtensionById("blacklisted", ExtensionRegistry::BLACKLISTED));

  // Enabled can be found with multiple flags set.
  EXPECT_TRUE(registry.GetExtensionById(
      "enabled", ExtensionRegistry::ENABLED | ExtensionRegistry::TERMINATED));

  // Enabled isn't found if the wrong flags are set.
  EXPECT_FALSE(registry.GetExtensionById(
      "enabled", ExtensionRegistry::DISABLED | ExtensionRegistry::BLACKLISTED));
}

}  // namespace
}  // namespace extensions