Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "base/pickle.h" | |
| 6 #include "base/values.h" | |
| 7 #include "chrome/common/extensions/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" | |
| 12 | |
| 13 namespace extensions { | |
| 14 | |
| 15 class MockManifestPermission : public ManifestPermission { | |
| 16 public: | |
| 17 MockManifestPermission(const std::string& name) | |
| 18 : name_(name) { | |
| 19 } | |
| 20 | |
| 21 virtual std::string name() const OVERRIDE { | |
| 22 return name_; | |
| 23 } | |
| 24 | |
| 25 virtual std::string id() const OVERRIDE { | |
| 26 return name(); | |
| 27 } | |
| 28 | |
| 29 virtual bool HasMessages() const OVERRIDE { | |
| 30 return false; | |
| 31 } | |
| 32 | |
| 33 virtual PermissionMessages GetMessages() const OVERRIDE { | |
| 34 return PermissionMessages(); | |
| 35 } | |
| 36 | |
| 37 virtual bool FromValue(const base::Value* value) OVERRIDE { | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 virtual scoped_ptr<base::Value> ToValue() const OVERRIDE { | |
| 42 return scoped_ptr<base::Value>(base::Value::CreateNullValue()); | |
| 43 } | |
| 44 | |
| 45 virtual ManifestPermission* Clone() const OVERRIDE { | |
| 46 return new MockManifestPermission(name_); | |
| 47 } | |
| 48 | |
| 49 virtual ManifestPermission* Diff(const ManifestPermission* rhs) | |
| 50 const OVERRIDE { | |
| 51 const MockManifestPermission* other = | |
| 52 static_cast<const MockManifestPermission*>(rhs); | |
| 53 EXPECT_EQ(name_, other->name_); | |
| 54 return NULL; | |
| 55 } | |
| 56 | |
| 57 virtual ManifestPermission* Union(const ManifestPermission* rhs) | |
| 58 const OVERRIDE { | |
| 59 const MockManifestPermission* other = | |
| 60 static_cast<const MockManifestPermission*>(rhs); | |
| 61 EXPECT_EQ(name_, other->name_); | |
| 62 return new MockManifestPermission(name_); | |
| 63 } | |
| 64 | |
| 65 virtual ManifestPermission* Intersect(const ManifestPermission* rhs) | |
| 66 const OVERRIDE { | |
| 67 const MockManifestPermission* other = | |
| 68 static_cast<const MockManifestPermission*>(rhs); | |
| 69 EXPECT_EQ(name_, other->name_); | |
| 70 return new MockManifestPermission(name_); | |
| 71 } | |
| 72 | |
| 73 virtual bool Contains(const ManifestPermission* rhs) const OVERRIDE { | |
| 74 const MockManifestPermission* other = | |
| 75 static_cast<const MockManifestPermission*>(rhs); | |
| 76 EXPECT_EQ(name_, other->name_); | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 virtual bool Equal(const ManifestPermission* rhs) const OVERRIDE { | |
| 81 const MockManifestPermission* other = | |
| 82 static_cast<const MockManifestPermission*>(rhs); | |
| 83 EXPECT_EQ(name_, other->name_); | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 virtual void Write(IPC::Message* m) const OVERRIDE { | |
| 88 IPC::WriteParam(m, name_); | |
| 89 } | |
| 90 | |
| 91 virtual bool Read(const IPC::Message* m, PickleIterator* iter) OVERRIDE { | |
| 92 std::string read_name; | |
| 93 bool result = IPC::ReadParam(m, iter, &read_name); | |
| 94 if (!result) | |
| 95 return result; | |
| 96 EXPECT_EQ(read_name, name_); | |
| 97 return true; | |
| 98 } | |
| 99 | |
| 100 virtual void Log(std::string* log) const OVERRIDE { | |
| 101 } | |
| 102 | |
| 103 private: | |
| 104 std::string name_; | |
| 105 }; | |
| 106 | |
| 107 TEST(ManifestPermissionSetTest, General) { | |
| 108 ManifestPermissionSet set; | |
| 109 set.insert(new MockManifestPermission("p1")); | |
| 110 set.insert(new MockManifestPermission("p2")); | |
| 111 set.insert(new MockManifestPermission("p3")); | |
| 112 set.insert(new MockManifestPermission("p4")); | |
| 113 set.insert(new MockManifestPermission("p5")); | |
| 114 | |
| 115 EXPECT_EQ(set.find("p1")->id(), "p1"); | |
| 116 EXPECT_TRUE(set.find("p10") == set.end()); | |
| 117 | |
| 118 EXPECT_EQ(set.size(), 5u); | |
| 119 | |
| 120 EXPECT_EQ(set.erase("p1"), 1u); | |
| 121 EXPECT_EQ(set.size(), 4u); | |
| 122 | |
| 123 EXPECT_EQ(set.erase("p1"), 0u); | |
| 124 EXPECT_EQ(set.size(), 4u); | |
| 125 } | |
| 126 | |
| 127 TEST(ManifestPermissionSetTest, CreateUnion) { | |
| 128 ManifestPermission* permission = NULL; | |
| 129 | |
| 130 ManifestPermissionSet permssions1; | |
|
Yoyo Zhou
2013/11/13 02:57:07
typo: permissions
(throughout this file)
rpaquay
2013/11/13 21:28:55
Done.
| |
| 131 ManifestPermissionSet permssions2; | |
| 132 ManifestPermissionSet expected_permissions; | |
| 133 ManifestPermissionSet result; | |
| 134 | |
| 135 permission = new MockManifestPermission("p3"); | |
| 136 | |
| 137 // Union with an empty set. | |
| 138 permssions1.insert(new MockManifestPermission("p1")); | |
| 139 permssions1.insert(new MockManifestPermission("p2")); | |
| 140 permssions1.insert(permission->Clone()); | |
| 141 expected_permissions.insert(new MockManifestPermission("p1")); | |
| 142 expected_permissions.insert(new MockManifestPermission("p2")); | |
| 143 expected_permissions.insert(permission); | |
| 144 | |
| 145 ManifestPermissionSet::Union(permssions1, permssions2, &result); | |
| 146 | |
| 147 EXPECT_TRUE(permssions1.Contains(permssions2)); | |
| 148 EXPECT_TRUE(permssions1.Contains(result)); | |
| 149 EXPECT_FALSE(permssions2.Contains(permssions1)); | |
| 150 EXPECT_FALSE(permssions2.Contains(result)); | |
| 151 EXPECT_TRUE(result.Contains(permssions1)); | |
| 152 EXPECT_TRUE(result.Contains(permssions2)); | |
| 153 | |
| 154 EXPECT_EQ(expected_permissions, result); | |
| 155 | |
| 156 // Now use a real second set. | |
| 157 permssions2.insert(new MockManifestPermission("p1")); | |
| 158 permssions2.insert(new MockManifestPermission("p2")); | |
| 159 permssions2.insert(new MockManifestPermission("p33")); | |
| 160 permssions2.insert(new MockManifestPermission("p4")); | |
| 161 permssions2.insert(new MockManifestPermission("p5")); | |
| 162 | |
| 163 expected_permissions.insert(new MockManifestPermission("p1")); | |
| 164 expected_permissions.insert(new MockManifestPermission("p2")); | |
| 165 expected_permissions.insert(new MockManifestPermission("p3")); | |
| 166 expected_permissions.insert(new MockManifestPermission("p4")); | |
| 167 expected_permissions.insert(new MockManifestPermission("p5")); | |
| 168 expected_permissions.insert(new MockManifestPermission("p33")); | |
| 169 | |
| 170 ManifestPermissionSet::Union(permssions1, permssions2, &result); | |
| 171 | |
| 172 { | |
| 173 ManifestPermissionSet set1; | |
| 174 set1.insert(new MockManifestPermission("p1")); | |
| 175 set1.insert(new MockManifestPermission("p2")); | |
| 176 ManifestPermissionSet set2; | |
| 177 set2.insert(new MockManifestPermission("p3")); | |
| 178 | |
| 179 EXPECT_FALSE(set1.Contains(set2)); | |
| 180 EXPECT_FALSE(set2.Contains(set1)); | |
| 181 } | |
| 182 | |
| 183 EXPECT_FALSE(permssions1.Contains(permssions2)); | |
| 184 EXPECT_FALSE(permssions1.Contains(result)); | |
| 185 EXPECT_FALSE(permssions2.Contains(permssions1)); | |
| 186 EXPECT_FALSE(permssions2.Contains(result)); | |
| 187 EXPECT_TRUE(result.Contains(permssions1)); | |
| 188 EXPECT_TRUE(result.Contains(permssions2)); | |
| 189 | |
| 190 EXPECT_EQ(expected_permissions, result); | |
| 191 } | |
| 192 | |
| 193 TEST(ManifestPermissionSetTest, CreateIntersection) { | |
| 194 ManifestPermission* permission = NULL; | |
| 195 | |
| 196 ManifestPermissionSet permssions1; | |
| 197 ManifestPermissionSet permssions2; | |
| 198 ManifestPermissionSet expected_permissions; | |
| 199 ManifestPermissionSet result; | |
| 200 | |
| 201 // Intersection with an empty set. | |
| 202 permssions1.insert(new MockManifestPermission("p1")); | |
| 203 permssions1.insert(new MockManifestPermission("p2")); | |
| 204 permssions1.insert(new MockManifestPermission("p3")); | |
| 205 | |
| 206 ManifestPermissionSet::Intersection(permssions1, permssions2, &result); | |
| 207 EXPECT_TRUE(permssions1.Contains(result)); | |
| 208 EXPECT_TRUE(permssions2.Contains(result)); | |
| 209 EXPECT_TRUE(permssions1.Contains(permssions2)); | |
| 210 EXPECT_FALSE(permssions2.Contains(permssions1)); | |
| 211 EXPECT_FALSE(result.Contains(permssions1)); | |
| 212 EXPECT_TRUE(result.Contains(permssions2)); | |
| 213 | |
| 214 EXPECT_TRUE(result.empty()); | |
| 215 EXPECT_EQ(expected_permissions, result); | |
| 216 | |
| 217 // Now use a real second set. | |
| 218 permssions2.insert(new MockManifestPermission("p1")); | |
| 219 permssions2.insert(new MockManifestPermission("p3")); | |
| 220 permssions2.insert(new MockManifestPermission("p4")); | |
| 221 permssions2.insert(new MockManifestPermission("p5")); | |
| 222 | |
| 223 expected_permissions.insert(new MockManifestPermission("p1")); | |
| 224 expected_permissions.insert(new MockManifestPermission("p3")); | |
| 225 | |
| 226 ManifestPermissionSet::Intersection(permssions1, permssions2, &result); | |
| 227 | |
| 228 EXPECT_TRUE(permssions1.Contains(result)); | |
| 229 EXPECT_TRUE(permssions2.Contains(result)); | |
| 230 EXPECT_FALSE(permssions1.Contains(permssions2)); | |
| 231 EXPECT_FALSE(permssions2.Contains(permssions1)); | |
| 232 EXPECT_FALSE(result.Contains(permssions1)); | |
| 233 EXPECT_FALSE(result.Contains(permssions2)); | |
| 234 | |
| 235 EXPECT_EQ(expected_permissions, result); | |
| 236 } | |
| 237 | |
| 238 TEST(ManifestPermissionSetTest, CreateDifference) { | |
| 239 ManifestPermission* permission = NULL; | |
| 240 | |
| 241 ManifestPermissionSet permssions1; | |
| 242 ManifestPermissionSet permssions2; | |
| 243 ManifestPermissionSet expected_permissions; | |
| 244 ManifestPermissionSet result; | |
| 245 | |
| 246 // Difference with an empty set. | |
| 247 permssions1.insert(new MockManifestPermission("p1")); | |
| 248 permssions1.insert(new MockManifestPermission("p2")); | |
| 249 permssions1.insert(new MockManifestPermission("p3")); | |
| 250 | |
| 251 ManifestPermissionSet::Difference(permssions1, permssions2, &result); | |
| 252 | |
| 253 EXPECT_EQ(permssions1, result); | |
| 254 | |
| 255 // Now use a real second set. | |
| 256 permssions2.insert(new MockManifestPermission("p1")); | |
| 257 permssions2.insert(new MockManifestPermission("p2")); | |
| 258 permssions2.insert(new MockManifestPermission("p4")); | |
| 259 permssions2.insert(new MockManifestPermission("p5")); | |
| 260 permssions2.insert(new MockManifestPermission("p6")); | |
| 261 | |
| 262 expected_permissions.insert(new MockManifestPermission("p3")); | |
| 263 | |
| 264 ManifestPermissionSet::Difference(permssions1, permssions2, &result); | |
| 265 | |
| 266 EXPECT_TRUE(permssions1.Contains(result)); | |
| 267 EXPECT_FALSE(permssions2.Contains(result)); | |
| 268 | |
| 269 EXPECT_EQ(expected_permissions, result); | |
| 270 | |
| 271 // |result| = |permssions1| - |permssions2| --> | |
| 272 // |result| intersect |permssions2| == empty_set | |
| 273 ManifestPermissionSet result2; | |
| 274 ManifestPermissionSet::Intersection(result, permssions2, &result2); | |
| 275 EXPECT_TRUE(result2.empty()); | |
| 276 } | |
| 277 | |
| 278 } // namespace extensions | |
| OLD | NEW |