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
{
16 MockManifestPermission(const std::string
& name
)
20 std::string
name() const override
{ return name_
; }
22 std::string
id() const override
{ return name(); }
24 PermissionIDSet
GetPermissions() const override
{ return PermissionIDSet(); }
26 bool HasMessages() const override
{ return false; }
28 PermissionMessages
GetMessages() const override
{
29 return PermissionMessages();
32 bool FromValue(const base::Value
* value
) override
{ return true; }
34 scoped_ptr
<base::Value
> ToValue() const override
{
35 return make_scoped_ptr(base::Value::CreateNullValue());
38 ManifestPermission
* Diff(const ManifestPermission
* rhs
) const override
{
39 const MockManifestPermission
* other
=
40 static_cast<const MockManifestPermission
*>(rhs
);
41 EXPECT_EQ(name_
, other
->name_
);
45 ManifestPermission
* Union(const ManifestPermission
* rhs
) const override
{
46 const MockManifestPermission
* other
=
47 static_cast<const MockManifestPermission
*>(rhs
);
48 EXPECT_EQ(name_
, other
->name_
);
49 return new MockManifestPermission(name_
);
52 ManifestPermission
* Intersect(const ManifestPermission
* rhs
) const override
{
53 const MockManifestPermission
* other
=
54 static_cast<const MockManifestPermission
*>(rhs
);
55 EXPECT_EQ(name_
, other
->name_
);
56 return new MockManifestPermission(name_
);
63 TEST(ManifestPermissionSetTest
, General
) {
64 ManifestPermissionSet set
;
65 set
.insert(new MockManifestPermission("p1"));
66 set
.insert(new MockManifestPermission("p2"));
67 set
.insert(new MockManifestPermission("p3"));
68 set
.insert(new MockManifestPermission("p4"));
69 set
.insert(new MockManifestPermission("p5"));
71 EXPECT_EQ(set
.find("p1")->id(), "p1");
72 EXPECT_TRUE(set
.find("p10") == set
.end());
74 EXPECT_EQ(set
.size(), 5u);
76 EXPECT_EQ(set
.erase("p1"), 1u);
77 EXPECT_EQ(set
.size(), 4u);
79 EXPECT_EQ(set
.erase("p1"), 0u);
80 EXPECT_EQ(set
.size(), 4u);
83 TEST(ManifestPermissionSetTest
, CreateUnion
) {
84 ManifestPermissionSet permissions1
;
85 ManifestPermissionSet permissions2
;
86 ManifestPermissionSet expected_permissions
;
87 ManifestPermissionSet result
;
89 ManifestPermission
* permission
= new MockManifestPermission("p3");
91 // Union with an empty set.
92 permissions1
.insert(new MockManifestPermission("p1"));
93 permissions1
.insert(new MockManifestPermission("p2"));
94 permissions1
.insert(permission
->Clone());
95 expected_permissions
.insert(new MockManifestPermission("p1"));
96 expected_permissions
.insert(new MockManifestPermission("p2"));
97 expected_permissions
.insert(permission
);
99 ManifestPermissionSet::Union(permissions1
, permissions2
, &result
);
101 EXPECT_TRUE(permissions1
.Contains(permissions2
));
102 EXPECT_TRUE(permissions1
.Contains(result
));
103 EXPECT_FALSE(permissions2
.Contains(permissions1
));
104 EXPECT_FALSE(permissions2
.Contains(result
));
105 EXPECT_TRUE(result
.Contains(permissions1
));
106 EXPECT_TRUE(result
.Contains(permissions2
));
108 EXPECT_EQ(expected_permissions
, result
);
110 // Now use a real second set.
111 permissions2
.insert(new MockManifestPermission("p1"));
112 permissions2
.insert(new MockManifestPermission("p2"));
113 permissions2
.insert(new MockManifestPermission("p33"));
114 permissions2
.insert(new MockManifestPermission("p4"));
115 permissions2
.insert(new MockManifestPermission("p5"));
117 expected_permissions
.insert(new MockManifestPermission("p1"));
118 expected_permissions
.insert(new MockManifestPermission("p2"));
119 expected_permissions
.insert(new MockManifestPermission("p3"));
120 expected_permissions
.insert(new MockManifestPermission("p4"));
121 expected_permissions
.insert(new MockManifestPermission("p5"));
122 expected_permissions
.insert(new MockManifestPermission("p33"));
124 ManifestPermissionSet::Union(permissions1
, permissions2
, &result
);
127 ManifestPermissionSet set1
;
128 set1
.insert(new MockManifestPermission("p1"));
129 set1
.insert(new MockManifestPermission("p2"));
130 ManifestPermissionSet set2
;
131 set2
.insert(new MockManifestPermission("p3"));
133 EXPECT_FALSE(set1
.Contains(set2
));
134 EXPECT_FALSE(set2
.Contains(set1
));
137 EXPECT_FALSE(permissions1
.Contains(permissions2
));
138 EXPECT_FALSE(permissions1
.Contains(result
));
139 EXPECT_FALSE(permissions2
.Contains(permissions1
));
140 EXPECT_FALSE(permissions2
.Contains(result
));
141 EXPECT_TRUE(result
.Contains(permissions1
));
142 EXPECT_TRUE(result
.Contains(permissions2
));
144 EXPECT_EQ(expected_permissions
, result
);
147 TEST(ManifestPermissionSetTest
, CreateIntersection
) {
148 ManifestPermissionSet permissions1
;
149 ManifestPermissionSet permissions2
;
150 ManifestPermissionSet expected_permissions
;
151 ManifestPermissionSet result
;
153 // Intersection with an empty set.
154 permissions1
.insert(new MockManifestPermission("p1"));
155 permissions1
.insert(new MockManifestPermission("p2"));
156 permissions1
.insert(new MockManifestPermission("p3"));
158 ManifestPermissionSet::Intersection(permissions1
, permissions2
, &result
);
159 EXPECT_TRUE(permissions1
.Contains(result
));
160 EXPECT_TRUE(permissions2
.Contains(result
));
161 EXPECT_TRUE(permissions1
.Contains(permissions2
));
162 EXPECT_FALSE(permissions2
.Contains(permissions1
));
163 EXPECT_FALSE(result
.Contains(permissions1
));
164 EXPECT_TRUE(result
.Contains(permissions2
));
166 EXPECT_TRUE(result
.empty());
167 EXPECT_EQ(expected_permissions
, result
);
169 // Now use a real second set.
170 permissions2
.insert(new MockManifestPermission("p1"));
171 permissions2
.insert(new MockManifestPermission("p3"));
172 permissions2
.insert(new MockManifestPermission("p4"));
173 permissions2
.insert(new MockManifestPermission("p5"));
175 expected_permissions
.insert(new MockManifestPermission("p1"));
176 expected_permissions
.insert(new MockManifestPermission("p3"));
178 ManifestPermissionSet::Intersection(permissions1
, permissions2
, &result
);
180 EXPECT_TRUE(permissions1
.Contains(result
));
181 EXPECT_TRUE(permissions2
.Contains(result
));
182 EXPECT_FALSE(permissions1
.Contains(permissions2
));
183 EXPECT_FALSE(permissions2
.Contains(permissions1
));
184 EXPECT_FALSE(result
.Contains(permissions1
));
185 EXPECT_FALSE(result
.Contains(permissions2
));
187 EXPECT_EQ(expected_permissions
, result
);
190 TEST(ManifestPermissionSetTest
, CreateDifference
) {
191 ManifestPermissionSet permissions1
;
192 ManifestPermissionSet permissions2
;
193 ManifestPermissionSet expected_permissions
;
194 ManifestPermissionSet result
;
196 // Difference with an empty set.
197 permissions1
.insert(new MockManifestPermission("p1"));
198 permissions1
.insert(new MockManifestPermission("p2"));
199 permissions1
.insert(new MockManifestPermission("p3"));
201 ManifestPermissionSet::Difference(permissions1
, permissions2
, &result
);
203 EXPECT_EQ(permissions1
, result
);
205 // Now use a real second set.
206 permissions2
.insert(new MockManifestPermission("p1"));
207 permissions2
.insert(new MockManifestPermission("p2"));
208 permissions2
.insert(new MockManifestPermission("p4"));
209 permissions2
.insert(new MockManifestPermission("p5"));
210 permissions2
.insert(new MockManifestPermission("p6"));
212 expected_permissions
.insert(new MockManifestPermission("p3"));
214 ManifestPermissionSet::Difference(permissions1
, permissions2
, &result
);
216 EXPECT_TRUE(permissions1
.Contains(result
));
217 EXPECT_FALSE(permissions2
.Contains(result
));
219 EXPECT_EQ(expected_permissions
, result
);
221 // |result| = |permissions1| - |permissions2| -->
222 // |result| intersect |permissions2| == empty_set
223 ManifestPermissionSet result2
;
224 ManifestPermissionSet::Intersection(result
, permissions2
, &result2
);
225 EXPECT_TRUE(result2
.empty());
228 } // namespace extensions