summaryrefslogtreecommitdiffstats
path: root/extensions/common/permissions/manifest_permission_set_unittest.cc
blob: f3660363f9aa2c75c2dfcf4a2d8df8b200fa072f (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// 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 "base/pickle.h"
#include "base/values.h"
#include "extensions/common/permissions/manifest_permission.h"
#include "extensions/common/permissions/manifest_permission_set.h"
#include "ipc/ipc_message.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

class MockManifestPermission : public ManifestPermission {
 public:
  MockManifestPermission(const std::string& name)
      : name_(name) {
  }

  std::string name() const override { return name_; }

  std::string id() const override { return name(); }

  PermissionIDSet GetPermissions() const override { return PermissionIDSet(); }

  bool FromValue(const base::Value* value) override { return true; }

  scoped_ptr<base::Value> ToValue() const override {
    return base::Value::CreateNullValue();
  }

  ManifestPermission* Diff(const ManifestPermission* rhs) const override {
    const MockManifestPermission* other =
        static_cast<const MockManifestPermission*>(rhs);
    EXPECT_EQ(name_, other->name_);
    return NULL;
  }

  ManifestPermission* Union(const ManifestPermission* rhs) const override {
    const MockManifestPermission* other =
        static_cast<const MockManifestPermission*>(rhs);
    EXPECT_EQ(name_, other->name_);
    return new MockManifestPermission(name_);
  }

  ManifestPermission* Intersect(const ManifestPermission* rhs) const override {
    const MockManifestPermission* other =
        static_cast<const MockManifestPermission*>(rhs);
    EXPECT_EQ(name_, other->name_);
    return new MockManifestPermission(name_);
  }

 private:
  std::string name_;
};

TEST(ManifestPermissionSetTest, General) {
  ManifestPermissionSet set;
  set.insert(new MockManifestPermission("p1"));
  set.insert(new MockManifestPermission("p2"));
  set.insert(new MockManifestPermission("p3"));
  set.insert(new MockManifestPermission("p4"));
  set.insert(new MockManifestPermission("p5"));

  EXPECT_EQ(set.find("p1")->id(), "p1");
  EXPECT_TRUE(set.find("p10") == set.end());

  EXPECT_EQ(set.size(), 5u);

  EXPECT_EQ(set.erase("p1"), 1u);
  EXPECT_EQ(set.size(), 4u);

  EXPECT_EQ(set.erase("p1"), 0u);
  EXPECT_EQ(set.size(), 4u);
}

TEST(ManifestPermissionSetTest, CreateUnion) {
  ManifestPermissionSet permissions1;
  ManifestPermissionSet permissions2;
  ManifestPermissionSet expected_permissions;
  ManifestPermissionSet result;

  ManifestPermission* permission = new MockManifestPermission("p3");

  // Union with an empty set.
  permissions1.insert(new MockManifestPermission("p1"));
  permissions1.insert(new MockManifestPermission("p2"));
  permissions1.insert(permission->Clone());
  expected_permissions.insert(new MockManifestPermission("p1"));
  expected_permissions.insert(new MockManifestPermission("p2"));
  expected_permissions.insert(permission);

  ManifestPermissionSet::Union(permissions1, permissions2, &result);

  EXPECT_TRUE(permissions1.Contains(permissions2));
  EXPECT_TRUE(permissions1.Contains(result));
  EXPECT_FALSE(permissions2.Contains(permissions1));
  EXPECT_FALSE(permissions2.Contains(result));
  EXPECT_TRUE(result.Contains(permissions1));
  EXPECT_TRUE(result.Contains(permissions2));

  EXPECT_EQ(expected_permissions, result);

  // Now use a real second set.
  permissions2.insert(new MockManifestPermission("p1"));
  permissions2.insert(new MockManifestPermission("p2"));
  permissions2.insert(new MockManifestPermission("p33"));
  permissions2.insert(new MockManifestPermission("p4"));
  permissions2.insert(new MockManifestPermission("p5"));

  expected_permissions.insert(new MockManifestPermission("p1"));
  expected_permissions.insert(new MockManifestPermission("p2"));
  expected_permissions.insert(new MockManifestPermission("p3"));
  expected_permissions.insert(new MockManifestPermission("p4"));
  expected_permissions.insert(new MockManifestPermission("p5"));
  expected_permissions.insert(new MockManifestPermission("p33"));

  ManifestPermissionSet::Union(permissions1, permissions2, &result);

  {
    ManifestPermissionSet set1;
    set1.insert(new MockManifestPermission("p1"));
    set1.insert(new MockManifestPermission("p2"));
    ManifestPermissionSet set2;
    set2.insert(new MockManifestPermission("p3"));

    EXPECT_FALSE(set1.Contains(set2));
    EXPECT_FALSE(set2.Contains(set1));
  }

  EXPECT_FALSE(permissions1.Contains(permissions2));
  EXPECT_FALSE(permissions1.Contains(result));
  EXPECT_FALSE(permissions2.Contains(permissions1));
  EXPECT_FALSE(permissions2.Contains(result));
  EXPECT_TRUE(result.Contains(permissions1));
  EXPECT_TRUE(result.Contains(permissions2));

  EXPECT_EQ(expected_permissions, result);
}

TEST(ManifestPermissionSetTest, CreateIntersection) {
  ManifestPermissionSet permissions1;
  ManifestPermissionSet permissions2;
  ManifestPermissionSet expected_permissions;
  ManifestPermissionSet result;

  // Intersection with an empty set.
  permissions1.insert(new MockManifestPermission("p1"));
  permissions1.insert(new MockManifestPermission("p2"));
  permissions1.insert(new MockManifestPermission("p3"));

  ManifestPermissionSet::Intersection(permissions1, permissions2, &result);
  EXPECT_TRUE(permissions1.Contains(result));
  EXPECT_TRUE(permissions2.Contains(result));
  EXPECT_TRUE(permissions1.Contains(permissions2));
  EXPECT_FALSE(permissions2.Contains(permissions1));
  EXPECT_FALSE(result.Contains(permissions1));
  EXPECT_TRUE(result.Contains(permissions2));

  EXPECT_TRUE(result.empty());
  EXPECT_EQ(expected_permissions, result);

  // Now use a real second set.
  permissions2.insert(new MockManifestPermission("p1"));
  permissions2.insert(new MockManifestPermission("p3"));
  permissions2.insert(new MockManifestPermission("p4"));
  permissions2.insert(new MockManifestPermission("p5"));

  expected_permissions.insert(new MockManifestPermission("p1"));
  expected_permissions.insert(new MockManifestPermission("p3"));

  ManifestPermissionSet::Intersection(permissions1, permissions2, &result);

  EXPECT_TRUE(permissions1.Contains(result));
  EXPECT_TRUE(permissions2.Contains(result));
  EXPECT_FALSE(permissions1.Contains(permissions2));
  EXPECT_FALSE(permissions2.Contains(permissions1));
  EXPECT_FALSE(result.Contains(permissions1));
  EXPECT_FALSE(result.Contains(permissions2));

  EXPECT_EQ(expected_permissions, result);
}

TEST(ManifestPermissionSetTest, CreateDifference) {
  ManifestPermissionSet permissions1;
  ManifestPermissionSet permissions2;
  ManifestPermissionSet expected_permissions;
  ManifestPermissionSet result;

  // Difference with an empty set.
  permissions1.insert(new MockManifestPermission("p1"));
  permissions1.insert(new MockManifestPermission("p2"));
  permissions1.insert(new MockManifestPermission("p3"));

  ManifestPermissionSet::Difference(permissions1, permissions2, &result);

  EXPECT_EQ(permissions1, result);

  // Now use a real second set.
  permissions2.insert(new MockManifestPermission("p1"));
  permissions2.insert(new MockManifestPermission("p2"));
  permissions2.insert(new MockManifestPermission("p4"));
  permissions2.insert(new MockManifestPermission("p5"));
  permissions2.insert(new MockManifestPermission("p6"));

  expected_permissions.insert(new MockManifestPermission("p3"));

  ManifestPermissionSet::Difference(permissions1, permissions2, &result);

  EXPECT_TRUE(permissions1.Contains(result));
  EXPECT_FALSE(permissions2.Contains(result));

  EXPECT_EQ(expected_permissions, result);

  // |result| = |permissions1| - |permissions2| -->
  //   |result| intersect |permissions2| == empty_set
  ManifestPermissionSet result2;
  ManifestPermissionSet::Intersection(result, permissions2, &result2);
  EXPECT_TRUE(result2.empty());
}

}  // namespace extensions