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/extension_messages.h"
8 #include "extensions/common/permissions/manifest_permission.h"
9 #include "extensions/common/permissions/manifest_permission_set.h"
10 #include "ipc/ipc_message.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace extensions
{
15 class MockManifestPermission
: public ManifestPermission
{
17 MockManifestPermission(const std::string
& name
)
21 virtual std::string
name() const OVERRIDE
{
25 virtual std::string
id() const OVERRIDE
{
29 virtual bool HasMessages() const OVERRIDE
{
33 virtual PermissionMessages
GetMessages() const OVERRIDE
{
34 return PermissionMessages();
37 virtual bool FromValue(const base::Value
* value
) OVERRIDE
{ return true; }
39 virtual scoped_ptr
<base::Value
> ToValue() const OVERRIDE
{
40 return make_scoped_ptr(base::Value::CreateNullValue());
43 virtual ManifestPermission
* Diff(const ManifestPermission
* rhs
)
45 const MockManifestPermission
* other
=
46 static_cast<const MockManifestPermission
*>(rhs
);
47 EXPECT_EQ(name_
, other
->name_
);
51 virtual ManifestPermission
* Union(const ManifestPermission
* rhs
)
53 const MockManifestPermission
* other
=
54 static_cast<const MockManifestPermission
*>(rhs
);
55 EXPECT_EQ(name_
, other
->name_
);
56 return new MockManifestPermission(name_
);
59 virtual ManifestPermission
* Intersect(const ManifestPermission
* rhs
)
61 const MockManifestPermission
* other
=
62 static_cast<const MockManifestPermission
*>(rhs
);
63 EXPECT_EQ(name_
, other
->name_
);
64 return new MockManifestPermission(name_
);
71 TEST(ManifestPermissionSetTest
, General
) {
72 ManifestPermissionSet set
;
73 set
.insert(new MockManifestPermission("p1"));
74 set
.insert(new MockManifestPermission("p2"));
75 set
.insert(new MockManifestPermission("p3"));
76 set
.insert(new MockManifestPermission("p4"));
77 set
.insert(new MockManifestPermission("p5"));
79 EXPECT_EQ(set
.find("p1")->id(), "p1");
80 EXPECT_TRUE(set
.find("p10") == set
.end());
82 EXPECT_EQ(set
.size(), 5u);
84 EXPECT_EQ(set
.erase("p1"), 1u);
85 EXPECT_EQ(set
.size(), 4u);
87 EXPECT_EQ(set
.erase("p1"), 0u);
88 EXPECT_EQ(set
.size(), 4u);
91 TEST(ManifestPermissionSetTest
, CreateUnion
) {
92 ManifestPermissionSet permissions1
;
93 ManifestPermissionSet permissions2
;
94 ManifestPermissionSet expected_permissions
;
95 ManifestPermissionSet result
;
97 ManifestPermission
* permission
= new MockManifestPermission("p3");
99 // Union with an empty set.
100 permissions1
.insert(new MockManifestPermission("p1"));
101 permissions1
.insert(new MockManifestPermission("p2"));
102 permissions1
.insert(permission
->Clone());
103 expected_permissions
.insert(new MockManifestPermission("p1"));
104 expected_permissions
.insert(new MockManifestPermission("p2"));
105 expected_permissions
.insert(permission
);
107 ManifestPermissionSet::Union(permissions1
, permissions2
, &result
);
109 EXPECT_TRUE(permissions1
.Contains(permissions2
));
110 EXPECT_TRUE(permissions1
.Contains(result
));
111 EXPECT_FALSE(permissions2
.Contains(permissions1
));
112 EXPECT_FALSE(permissions2
.Contains(result
));
113 EXPECT_TRUE(result
.Contains(permissions1
));
114 EXPECT_TRUE(result
.Contains(permissions2
));
116 EXPECT_EQ(expected_permissions
, result
);
118 // Now use a real second set.
119 permissions2
.insert(new MockManifestPermission("p1"));
120 permissions2
.insert(new MockManifestPermission("p2"));
121 permissions2
.insert(new MockManifestPermission("p33"));
122 permissions2
.insert(new MockManifestPermission("p4"));
123 permissions2
.insert(new MockManifestPermission("p5"));
125 expected_permissions
.insert(new MockManifestPermission("p1"));
126 expected_permissions
.insert(new MockManifestPermission("p2"));
127 expected_permissions
.insert(new MockManifestPermission("p3"));
128 expected_permissions
.insert(new MockManifestPermission("p4"));
129 expected_permissions
.insert(new MockManifestPermission("p5"));
130 expected_permissions
.insert(new MockManifestPermission("p33"));
132 ManifestPermissionSet::Union(permissions1
, permissions2
, &result
);
135 ManifestPermissionSet set1
;
136 set1
.insert(new MockManifestPermission("p1"));
137 set1
.insert(new MockManifestPermission("p2"));
138 ManifestPermissionSet set2
;
139 set2
.insert(new MockManifestPermission("p3"));
141 EXPECT_FALSE(set1
.Contains(set2
));
142 EXPECT_FALSE(set2
.Contains(set1
));
145 EXPECT_FALSE(permissions1
.Contains(permissions2
));
146 EXPECT_FALSE(permissions1
.Contains(result
));
147 EXPECT_FALSE(permissions2
.Contains(permissions1
));
148 EXPECT_FALSE(permissions2
.Contains(result
));
149 EXPECT_TRUE(result
.Contains(permissions1
));
150 EXPECT_TRUE(result
.Contains(permissions2
));
152 EXPECT_EQ(expected_permissions
, result
);
155 TEST(ManifestPermissionSetTest
, CreateIntersection
) {
156 ManifestPermissionSet permissions1
;
157 ManifestPermissionSet permissions2
;
158 ManifestPermissionSet expected_permissions
;
159 ManifestPermissionSet result
;
161 // Intersection with an empty set.
162 permissions1
.insert(new MockManifestPermission("p1"));
163 permissions1
.insert(new MockManifestPermission("p2"));
164 permissions1
.insert(new MockManifestPermission("p3"));
166 ManifestPermissionSet::Intersection(permissions1
, permissions2
, &result
);
167 EXPECT_TRUE(permissions1
.Contains(result
));
168 EXPECT_TRUE(permissions2
.Contains(result
));
169 EXPECT_TRUE(permissions1
.Contains(permissions2
));
170 EXPECT_FALSE(permissions2
.Contains(permissions1
));
171 EXPECT_FALSE(result
.Contains(permissions1
));
172 EXPECT_TRUE(result
.Contains(permissions2
));
174 EXPECT_TRUE(result
.empty());
175 EXPECT_EQ(expected_permissions
, result
);
177 // Now use a real second set.
178 permissions2
.insert(new MockManifestPermission("p1"));
179 permissions2
.insert(new MockManifestPermission("p3"));
180 permissions2
.insert(new MockManifestPermission("p4"));
181 permissions2
.insert(new MockManifestPermission("p5"));
183 expected_permissions
.insert(new MockManifestPermission("p1"));
184 expected_permissions
.insert(new MockManifestPermission("p3"));
186 ManifestPermissionSet::Intersection(permissions1
, permissions2
, &result
);
188 EXPECT_TRUE(permissions1
.Contains(result
));
189 EXPECT_TRUE(permissions2
.Contains(result
));
190 EXPECT_FALSE(permissions1
.Contains(permissions2
));
191 EXPECT_FALSE(permissions2
.Contains(permissions1
));
192 EXPECT_FALSE(result
.Contains(permissions1
));
193 EXPECT_FALSE(result
.Contains(permissions2
));
195 EXPECT_EQ(expected_permissions
, result
);
198 TEST(ManifestPermissionSetTest
, CreateDifference
) {
199 ManifestPermissionSet permissions1
;
200 ManifestPermissionSet permissions2
;
201 ManifestPermissionSet expected_permissions
;
202 ManifestPermissionSet result
;
204 // Difference with an empty set.
205 permissions1
.insert(new MockManifestPermission("p1"));
206 permissions1
.insert(new MockManifestPermission("p2"));
207 permissions1
.insert(new MockManifestPermission("p3"));
209 ManifestPermissionSet::Difference(permissions1
, permissions2
, &result
);
211 EXPECT_EQ(permissions1
, result
);
213 // Now use a real second set.
214 permissions2
.insert(new MockManifestPermission("p1"));
215 permissions2
.insert(new MockManifestPermission("p2"));
216 permissions2
.insert(new MockManifestPermission("p4"));
217 permissions2
.insert(new MockManifestPermission("p5"));
218 permissions2
.insert(new MockManifestPermission("p6"));
220 expected_permissions
.insert(new MockManifestPermission("p3"));
222 ManifestPermissionSet::Difference(permissions1
, permissions2
, &result
);
224 EXPECT_TRUE(permissions1
.Contains(result
));
225 EXPECT_FALSE(permissions2
.Contains(result
));
227 EXPECT_EQ(expected_permissions
, result
);
229 // |result| = |permissions1| - |permissions2| -->
230 // |result| intersect |permissions2| == empty_set
231 ManifestPermissionSet result2
;
232 ManifestPermissionSet::Intersection(result
, permissions2
, &result2
);
233 EXPECT_TRUE(result2
.empty());
236 } // namespace extensions