Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / extensions / common / permissions / manifest_permission_set_unittest.cc
blob7716c9afa2c9b301c3a6d26c5a36dd320c305ca9
1 // Copyright 2013 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 "base/pickle.h"
6 #include "base/values.h"
7 #include "extensions/common/permissions/manifest_permission.h"
8 #include "extensions/common/permissions/manifest_permission_set.h"
9 #include "ipc/ipc_message.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace extensions {
14 class MockManifestPermission : public ManifestPermission {
15 public:
16 MockManifestPermission(const std::string& name)
17 : name_(name) {
20 std::string name() const override { return name_; }
22 std::string id() const override { return name(); }
24 bool HasMessages() const override { return false; }
26 PermissionMessages GetMessages() const override {
27 return PermissionMessages();
30 bool FromValue(const base::Value* value) override { return true; }
32 scoped_ptr<base::Value> ToValue() const override {
33 return make_scoped_ptr(base::Value::CreateNullValue());
36 ManifestPermission* Diff(const ManifestPermission* rhs) const override {
37 const MockManifestPermission* other =
38 static_cast<const MockManifestPermission*>(rhs);
39 EXPECT_EQ(name_, other->name_);
40 return NULL;
43 ManifestPermission* Union(const ManifestPermission* rhs) const override {
44 const MockManifestPermission* other =
45 static_cast<const MockManifestPermission*>(rhs);
46 EXPECT_EQ(name_, other->name_);
47 return new MockManifestPermission(name_);
50 ManifestPermission* Intersect(const ManifestPermission* rhs) const override {
51 const MockManifestPermission* other =
52 static_cast<const MockManifestPermission*>(rhs);
53 EXPECT_EQ(name_, other->name_);
54 return new MockManifestPermission(name_);
57 private:
58 std::string name_;
61 TEST(ManifestPermissionSetTest, General) {
62 ManifestPermissionSet set;
63 set.insert(new MockManifestPermission("p1"));
64 set.insert(new MockManifestPermission("p2"));
65 set.insert(new MockManifestPermission("p3"));
66 set.insert(new MockManifestPermission("p4"));
67 set.insert(new MockManifestPermission("p5"));
69 EXPECT_EQ(set.find("p1")->id(), "p1");
70 EXPECT_TRUE(set.find("p10") == set.end());
72 EXPECT_EQ(set.size(), 5u);
74 EXPECT_EQ(set.erase("p1"), 1u);
75 EXPECT_EQ(set.size(), 4u);
77 EXPECT_EQ(set.erase("p1"), 0u);
78 EXPECT_EQ(set.size(), 4u);
81 TEST(ManifestPermissionSetTest, CreateUnion) {
82 ManifestPermissionSet permissions1;
83 ManifestPermissionSet permissions2;
84 ManifestPermissionSet expected_permissions;
85 ManifestPermissionSet result;
87 ManifestPermission* permission = new MockManifestPermission("p3");
89 // Union with an empty set.
90 permissions1.insert(new MockManifestPermission("p1"));
91 permissions1.insert(new MockManifestPermission("p2"));
92 permissions1.insert(permission->Clone());
93 expected_permissions.insert(new MockManifestPermission("p1"));
94 expected_permissions.insert(new MockManifestPermission("p2"));
95 expected_permissions.insert(permission);
97 ManifestPermissionSet::Union(permissions1, permissions2, &result);
99 EXPECT_TRUE(permissions1.Contains(permissions2));
100 EXPECT_TRUE(permissions1.Contains(result));
101 EXPECT_FALSE(permissions2.Contains(permissions1));
102 EXPECT_FALSE(permissions2.Contains(result));
103 EXPECT_TRUE(result.Contains(permissions1));
104 EXPECT_TRUE(result.Contains(permissions2));
106 EXPECT_EQ(expected_permissions, result);
108 // Now use a real second set.
109 permissions2.insert(new MockManifestPermission("p1"));
110 permissions2.insert(new MockManifestPermission("p2"));
111 permissions2.insert(new MockManifestPermission("p33"));
112 permissions2.insert(new MockManifestPermission("p4"));
113 permissions2.insert(new MockManifestPermission("p5"));
115 expected_permissions.insert(new MockManifestPermission("p1"));
116 expected_permissions.insert(new MockManifestPermission("p2"));
117 expected_permissions.insert(new MockManifestPermission("p3"));
118 expected_permissions.insert(new MockManifestPermission("p4"));
119 expected_permissions.insert(new MockManifestPermission("p5"));
120 expected_permissions.insert(new MockManifestPermission("p33"));
122 ManifestPermissionSet::Union(permissions1, permissions2, &result);
125 ManifestPermissionSet set1;
126 set1.insert(new MockManifestPermission("p1"));
127 set1.insert(new MockManifestPermission("p2"));
128 ManifestPermissionSet set2;
129 set2.insert(new MockManifestPermission("p3"));
131 EXPECT_FALSE(set1.Contains(set2));
132 EXPECT_FALSE(set2.Contains(set1));
135 EXPECT_FALSE(permissions1.Contains(permissions2));
136 EXPECT_FALSE(permissions1.Contains(result));
137 EXPECT_FALSE(permissions2.Contains(permissions1));
138 EXPECT_FALSE(permissions2.Contains(result));
139 EXPECT_TRUE(result.Contains(permissions1));
140 EXPECT_TRUE(result.Contains(permissions2));
142 EXPECT_EQ(expected_permissions, result);
145 TEST(ManifestPermissionSetTest, CreateIntersection) {
146 ManifestPermissionSet permissions1;
147 ManifestPermissionSet permissions2;
148 ManifestPermissionSet expected_permissions;
149 ManifestPermissionSet result;
151 // Intersection with an empty set.
152 permissions1.insert(new MockManifestPermission("p1"));
153 permissions1.insert(new MockManifestPermission("p2"));
154 permissions1.insert(new MockManifestPermission("p3"));
156 ManifestPermissionSet::Intersection(permissions1, permissions2, &result);
157 EXPECT_TRUE(permissions1.Contains(result));
158 EXPECT_TRUE(permissions2.Contains(result));
159 EXPECT_TRUE(permissions1.Contains(permissions2));
160 EXPECT_FALSE(permissions2.Contains(permissions1));
161 EXPECT_FALSE(result.Contains(permissions1));
162 EXPECT_TRUE(result.Contains(permissions2));
164 EXPECT_TRUE(result.empty());
165 EXPECT_EQ(expected_permissions, result);
167 // Now use a real second set.
168 permissions2.insert(new MockManifestPermission("p1"));
169 permissions2.insert(new MockManifestPermission("p3"));
170 permissions2.insert(new MockManifestPermission("p4"));
171 permissions2.insert(new MockManifestPermission("p5"));
173 expected_permissions.insert(new MockManifestPermission("p1"));
174 expected_permissions.insert(new MockManifestPermission("p3"));
176 ManifestPermissionSet::Intersection(permissions1, permissions2, &result);
178 EXPECT_TRUE(permissions1.Contains(result));
179 EXPECT_TRUE(permissions2.Contains(result));
180 EXPECT_FALSE(permissions1.Contains(permissions2));
181 EXPECT_FALSE(permissions2.Contains(permissions1));
182 EXPECT_FALSE(result.Contains(permissions1));
183 EXPECT_FALSE(result.Contains(permissions2));
185 EXPECT_EQ(expected_permissions, result);
188 TEST(ManifestPermissionSetTest, CreateDifference) {
189 ManifestPermissionSet permissions1;
190 ManifestPermissionSet permissions2;
191 ManifestPermissionSet expected_permissions;
192 ManifestPermissionSet result;
194 // Difference with an empty set.
195 permissions1.insert(new MockManifestPermission("p1"));
196 permissions1.insert(new MockManifestPermission("p2"));
197 permissions1.insert(new MockManifestPermission("p3"));
199 ManifestPermissionSet::Difference(permissions1, permissions2, &result);
201 EXPECT_EQ(permissions1, result);
203 // Now use a real second set.
204 permissions2.insert(new MockManifestPermission("p1"));
205 permissions2.insert(new MockManifestPermission("p2"));
206 permissions2.insert(new MockManifestPermission("p4"));
207 permissions2.insert(new MockManifestPermission("p5"));
208 permissions2.insert(new MockManifestPermission("p6"));
210 expected_permissions.insert(new MockManifestPermission("p3"));
212 ManifestPermissionSet::Difference(permissions1, permissions2, &result);
214 EXPECT_TRUE(permissions1.Contains(result));
215 EXPECT_FALSE(permissions2.Contains(result));
217 EXPECT_EQ(expected_permissions, result);
219 // |result| = |permissions1| - |permissions2| -->
220 // |result| intersect |permissions2| == empty_set
221 ManifestPermissionSet result2;
222 ManifestPermissionSet::Intersection(result, permissions2, &result2);
223 EXPECT_TRUE(result2.empty());
226 } // namespace extensions