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 FromValue(const base::Value
* value
) override
{ return true; }
28 scoped_ptr
<base::Value
> ToValue() const override
{
29 return base::Value::CreateNullValue();
32 ManifestPermission
* Diff(const ManifestPermission
* rhs
) const override
{
33 const MockManifestPermission
* other
=
34 static_cast<const MockManifestPermission
*>(rhs
);
35 EXPECT_EQ(name_
, other
->name_
);
39 ManifestPermission
* Union(const ManifestPermission
* rhs
) const override
{
40 const MockManifestPermission
* other
=
41 static_cast<const MockManifestPermission
*>(rhs
);
42 EXPECT_EQ(name_
, other
->name_
);
43 return new MockManifestPermission(name_
);
46 ManifestPermission
* Intersect(const ManifestPermission
* rhs
) const override
{
47 const MockManifestPermission
* other
=
48 static_cast<const MockManifestPermission
*>(rhs
);
49 EXPECT_EQ(name_
, other
->name_
);
50 return new MockManifestPermission(name_
);
57 TEST(ManifestPermissionSetTest
, General
) {
58 ManifestPermissionSet set
;
59 set
.insert(new MockManifestPermission("p1"));
60 set
.insert(new MockManifestPermission("p2"));
61 set
.insert(new MockManifestPermission("p3"));
62 set
.insert(new MockManifestPermission("p4"));
63 set
.insert(new MockManifestPermission("p5"));
65 EXPECT_EQ(set
.find("p1")->id(), "p1");
66 EXPECT_TRUE(set
.find("p10") == set
.end());
68 EXPECT_EQ(set
.size(), 5u);
70 EXPECT_EQ(set
.erase("p1"), 1u);
71 EXPECT_EQ(set
.size(), 4u);
73 EXPECT_EQ(set
.erase("p1"), 0u);
74 EXPECT_EQ(set
.size(), 4u);
77 TEST(ManifestPermissionSetTest
, CreateUnion
) {
78 ManifestPermissionSet permissions1
;
79 ManifestPermissionSet permissions2
;
80 ManifestPermissionSet expected_permissions
;
81 ManifestPermissionSet result
;
83 ManifestPermission
* permission
= new MockManifestPermission("p3");
85 // Union with an empty set.
86 permissions1
.insert(new MockManifestPermission("p1"));
87 permissions1
.insert(new MockManifestPermission("p2"));
88 permissions1
.insert(permission
->Clone());
89 expected_permissions
.insert(new MockManifestPermission("p1"));
90 expected_permissions
.insert(new MockManifestPermission("p2"));
91 expected_permissions
.insert(permission
);
93 ManifestPermissionSet::Union(permissions1
, permissions2
, &result
);
95 EXPECT_TRUE(permissions1
.Contains(permissions2
));
96 EXPECT_TRUE(permissions1
.Contains(result
));
97 EXPECT_FALSE(permissions2
.Contains(permissions1
));
98 EXPECT_FALSE(permissions2
.Contains(result
));
99 EXPECT_TRUE(result
.Contains(permissions1
));
100 EXPECT_TRUE(result
.Contains(permissions2
));
102 EXPECT_EQ(expected_permissions
, result
);
104 // Now use a real second set.
105 permissions2
.insert(new MockManifestPermission("p1"));
106 permissions2
.insert(new MockManifestPermission("p2"));
107 permissions2
.insert(new MockManifestPermission("p33"));
108 permissions2
.insert(new MockManifestPermission("p4"));
109 permissions2
.insert(new MockManifestPermission("p5"));
111 expected_permissions
.insert(new MockManifestPermission("p1"));
112 expected_permissions
.insert(new MockManifestPermission("p2"));
113 expected_permissions
.insert(new MockManifestPermission("p3"));
114 expected_permissions
.insert(new MockManifestPermission("p4"));
115 expected_permissions
.insert(new MockManifestPermission("p5"));
116 expected_permissions
.insert(new MockManifestPermission("p33"));
118 ManifestPermissionSet::Union(permissions1
, permissions2
, &result
);
121 ManifestPermissionSet set1
;
122 set1
.insert(new MockManifestPermission("p1"));
123 set1
.insert(new MockManifestPermission("p2"));
124 ManifestPermissionSet set2
;
125 set2
.insert(new MockManifestPermission("p3"));
127 EXPECT_FALSE(set1
.Contains(set2
));
128 EXPECT_FALSE(set2
.Contains(set1
));
131 EXPECT_FALSE(permissions1
.Contains(permissions2
));
132 EXPECT_FALSE(permissions1
.Contains(result
));
133 EXPECT_FALSE(permissions2
.Contains(permissions1
));
134 EXPECT_FALSE(permissions2
.Contains(result
));
135 EXPECT_TRUE(result
.Contains(permissions1
));
136 EXPECT_TRUE(result
.Contains(permissions2
));
138 EXPECT_EQ(expected_permissions
, result
);
141 TEST(ManifestPermissionSetTest
, CreateIntersection
) {
142 ManifestPermissionSet permissions1
;
143 ManifestPermissionSet permissions2
;
144 ManifestPermissionSet expected_permissions
;
145 ManifestPermissionSet result
;
147 // Intersection with an empty set.
148 permissions1
.insert(new MockManifestPermission("p1"));
149 permissions1
.insert(new MockManifestPermission("p2"));
150 permissions1
.insert(new MockManifestPermission("p3"));
152 ManifestPermissionSet::Intersection(permissions1
, permissions2
, &result
);
153 EXPECT_TRUE(permissions1
.Contains(result
));
154 EXPECT_TRUE(permissions2
.Contains(result
));
155 EXPECT_TRUE(permissions1
.Contains(permissions2
));
156 EXPECT_FALSE(permissions2
.Contains(permissions1
));
157 EXPECT_FALSE(result
.Contains(permissions1
));
158 EXPECT_TRUE(result
.Contains(permissions2
));
160 EXPECT_TRUE(result
.empty());
161 EXPECT_EQ(expected_permissions
, result
);
163 // Now use a real second set.
164 permissions2
.insert(new MockManifestPermission("p1"));
165 permissions2
.insert(new MockManifestPermission("p3"));
166 permissions2
.insert(new MockManifestPermission("p4"));
167 permissions2
.insert(new MockManifestPermission("p5"));
169 expected_permissions
.insert(new MockManifestPermission("p1"));
170 expected_permissions
.insert(new MockManifestPermission("p3"));
172 ManifestPermissionSet::Intersection(permissions1
, permissions2
, &result
);
174 EXPECT_TRUE(permissions1
.Contains(result
));
175 EXPECT_TRUE(permissions2
.Contains(result
));
176 EXPECT_FALSE(permissions1
.Contains(permissions2
));
177 EXPECT_FALSE(permissions2
.Contains(permissions1
));
178 EXPECT_FALSE(result
.Contains(permissions1
));
179 EXPECT_FALSE(result
.Contains(permissions2
));
181 EXPECT_EQ(expected_permissions
, result
);
184 TEST(ManifestPermissionSetTest
, CreateDifference
) {
185 ManifestPermissionSet permissions1
;
186 ManifestPermissionSet permissions2
;
187 ManifestPermissionSet expected_permissions
;
188 ManifestPermissionSet result
;
190 // Difference with an empty set.
191 permissions1
.insert(new MockManifestPermission("p1"));
192 permissions1
.insert(new MockManifestPermission("p2"));
193 permissions1
.insert(new MockManifestPermission("p3"));
195 ManifestPermissionSet::Difference(permissions1
, permissions2
, &result
);
197 EXPECT_EQ(permissions1
, result
);
199 // Now use a real second set.
200 permissions2
.insert(new MockManifestPermission("p1"));
201 permissions2
.insert(new MockManifestPermission("p2"));
202 permissions2
.insert(new MockManifestPermission("p4"));
203 permissions2
.insert(new MockManifestPermission("p5"));
204 permissions2
.insert(new MockManifestPermission("p6"));
206 expected_permissions
.insert(new MockManifestPermission("p3"));
208 ManifestPermissionSet::Difference(permissions1
, permissions2
, &result
);
210 EXPECT_TRUE(permissions1
.Contains(result
));
211 EXPECT_FALSE(permissions2
.Contains(result
));
213 EXPECT_EQ(expected_permissions
, result
);
215 // |result| = |permissions1| - |permissions2| -->
216 // |result| intersect |permissions2| == empty_set
217 ManifestPermissionSet result2
;
218 ManifestPermissionSet::Intersection(result
, permissions2
, &result2
);
219 EXPECT_TRUE(result2
.empty());
222 } // namespace extensions