Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / common / api / sockets / sockets_manifest_permission_unittest.cc
blob4b2dbcf6a34cdb5ce1a6cb6bcbec56a88da8c964
1 // Copyright 2014 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 <set>
7 #include "base/json/json_reader.h"
8 #include "base/pickle.h"
9 #include "base/values.h"
10 #include "extensions/common/api/sockets/sockets_manifest_permission.h"
11 #include "extensions/common/manifest_constants.h"
12 #include "ipc/ipc_message.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using content::SocketPermissionRequest;
17 namespace extensions {
19 namespace {
21 const char kUdpBindPermission[] =
22 "{ \"udp\": { \"bind\": [\"127.0.0.1:3007\", \"a.com:80\"] } }";
24 const char kUdpSendPermission[] =
25 "{ \"udp\": { \"send\": [\"\", \"a.com:80\"] } }";
27 const char kTcpConnectPermission[] =
28 "{ \"tcp\": { \"connect\": [\"127.0.0.1:80\", \"a.com:80\"] } }";
30 const char kTcpServerListenPermission[] =
31 "{ \"tcpServer\": { \"listen\": [\"127.0.0.1:80\", \"a.com:80\"] } }";
33 static void AssertEmptyPermission(const SocketsManifestPermission* permission) {
34 EXPECT_TRUE(permission);
35 EXPECT_EQ(std::string(extensions::manifest_keys::kSockets), permission->id());
36 EXPECT_EQ(permission->id(), permission->name());
37 EXPECT_FALSE(permission->HasMessages());
38 EXPECT_EQ(0u, permission->entries().size());
41 static scoped_ptr<base::Value> ParsePermissionJSON(const std::string& json) {
42 scoped_ptr<base::Value> result(base::JSONReader::Read(json));
43 EXPECT_TRUE(result) << "Invalid JSON string: " << json;
44 return result.Pass();
47 static scoped_ptr<SocketsManifestPermission> PermissionFromValue(
48 const base::Value& value) {
49 base::string16 error16;
50 scoped_ptr<SocketsManifestPermission> permission(
51 SocketsManifestPermission::FromValue(value, &error16));
52 EXPECT_TRUE(permission) << "Error parsing Value into permission: " << error16;
53 return permission.Pass();
56 static scoped_ptr<SocketsManifestPermission> PermissionFromJSON(
57 const std::string& json) {
58 scoped_ptr<base::Value> value(ParsePermissionJSON(json));
59 return PermissionFromValue(*value);
62 struct CheckFormatEntry {
63 CheckFormatEntry(SocketPermissionRequest::OperationType operation_type,
64 std::string host_pattern)
65 : operation_type(operation_type), host_pattern(host_pattern) {}
67 // operators <, == are needed by container std::set and algorithms
68 // std::set_includes and std::set_differences.
69 bool operator<(const CheckFormatEntry& rhs) const {
70 if (operation_type == rhs.operation_type)
71 return host_pattern < rhs.host_pattern;
73 return operation_type < rhs.operation_type;
76 bool operator==(const CheckFormatEntry& rhs) const {
77 return operation_type == rhs.operation_type &&
78 host_pattern == rhs.host_pattern;
81 SocketPermissionRequest::OperationType operation_type;
82 std::string host_pattern;
85 static testing::AssertionResult CheckFormat(
86 std::multiset<CheckFormatEntry> permissions,
87 const std::string& json) {
88 scoped_ptr<SocketsManifestPermission> permission(PermissionFromJSON(json));
89 if (!permission)
90 return testing::AssertionFailure() << "Invalid permission " << json;
92 if (permissions.size() != permission->entries().size()) {
93 return testing::AssertionFailure()
94 << "Incorrect # of entries in json: " << json;
97 // Note: We use multiset because SocketsManifestPermission does not have to
98 // store entries in the order found in the json message.
99 std::multiset<CheckFormatEntry> parsed_permissions;
100 for (SocketPermissionEntrySet::const_iterator it =
101 permission->entries().begin();
102 it != permission->entries().end(); ++it) {
103 parsed_permissions.insert(
104 CheckFormatEntry(it->pattern().type, it->GetHostPatternAsString()));
107 if (!std::equal(
108 permissions.begin(), permissions.end(), parsed_permissions.begin())) {
109 return testing::AssertionFailure() << "Incorrect socket operations.";
111 return testing::AssertionSuccess();
114 static testing::AssertionResult CheckFormat(const std::string& json) {
115 return CheckFormat(std::multiset<CheckFormatEntry>(), json);
118 static testing::AssertionResult CheckFormat(const std::string& json,
119 const CheckFormatEntry& op1) {
120 CheckFormatEntry entries[] = {op1};
121 return CheckFormat(
122 std::multiset<CheckFormatEntry>(entries, entries + arraysize(entries)),
123 json);
126 static testing::AssertionResult CheckFormat(const std::string& json,
127 const CheckFormatEntry& op1,
128 const CheckFormatEntry& op2) {
129 CheckFormatEntry entries[] = {op1, op2};
130 return CheckFormat(
131 std::multiset<CheckFormatEntry>(entries, entries + arraysize(entries)),
132 json);
135 static testing::AssertionResult CheckFormat(const std::string& json,
136 const CheckFormatEntry& op1,
137 const CheckFormatEntry& op2,
138 const CheckFormatEntry& op3,
139 const CheckFormatEntry& op4,
140 const CheckFormatEntry& op5,
141 const CheckFormatEntry& op6,
142 const CheckFormatEntry& op7,
143 const CheckFormatEntry& op8,
144 const CheckFormatEntry& op9) {
145 CheckFormatEntry entries[] = {op1, op2, op3, op4, op5, op6, op7, op8, op9};
146 return CheckFormat(
147 std::multiset<CheckFormatEntry>(entries, entries + arraysize(entries)),
148 json);
151 } // namespace
153 TEST(SocketsManifestPermissionTest, Empty) {
154 // Construction
155 scoped_ptr<SocketsManifestPermission> permission(
156 new SocketsManifestPermission());
157 AssertEmptyPermission(permission.get());
159 // Clone()/Equal()
160 scoped_ptr<SocketsManifestPermission> clone(
161 static_cast<SocketsManifestPermission*>(permission->Clone()));
162 AssertEmptyPermission(clone.get());
164 EXPECT_TRUE(permission->Equal(clone.get()));
166 // ToValue()/FromValue()
167 scoped_ptr<const base::Value> value(permission->ToValue());
168 EXPECT_TRUE(value.get());
170 scoped_ptr<SocketsManifestPermission> permission2(
171 new SocketsManifestPermission());
172 EXPECT_TRUE(permission2->FromValue(value.get()));
173 AssertEmptyPermission(permission2.get());
175 // Union/Diff/Intersection
176 scoped_ptr<SocketsManifestPermission> diff_perm(
177 static_cast<SocketsManifestPermission*>(permission->Diff(clone.get())));
178 AssertEmptyPermission(diff_perm.get());
180 scoped_ptr<SocketsManifestPermission> union_perm(
181 static_cast<SocketsManifestPermission*>(permission->Union(clone.get())));
182 AssertEmptyPermission(union_perm.get());
184 scoped_ptr<SocketsManifestPermission> intersect_perm(
185 static_cast<SocketsManifestPermission*>(
186 permission->Intersect(clone.get())));
187 AssertEmptyPermission(intersect_perm.get());
189 // IPC
190 scoped_ptr<SocketsManifestPermission> ipc_perm(
191 new SocketsManifestPermission());
192 scoped_ptr<SocketsManifestPermission> ipc_perm2(
193 new SocketsManifestPermission());
195 IPC::Message m;
196 ipc_perm->Write(&m);
197 base::PickleIterator iter(m);
198 EXPECT_TRUE(ipc_perm2->Read(&m, &iter));
199 AssertEmptyPermission(ipc_perm2.get());
202 TEST(SocketsManifestPermissionTest, JSONFormats) {
203 EXPECT_TRUE(CheckFormat(
204 "{\"udp\":{\"send\":\"\"}}",
205 CheckFormatEntry(SocketPermissionRequest::UDP_SEND_TO, "*:*")));
206 EXPECT_TRUE(CheckFormat("{\"udp\":{\"send\":[]}}"));
207 EXPECT_TRUE(CheckFormat(
208 "{\"udp\":{\"send\":[\"\"]}}",
209 CheckFormatEntry(SocketPermissionRequest::UDP_SEND_TO, "*:*")));
210 EXPECT_TRUE(CheckFormat(
211 "{\"udp\":{\"send\":[\"a:80\", \"b:10\"]}}",
212 CheckFormatEntry(SocketPermissionRequest::UDP_SEND_TO, "a:80"),
213 CheckFormatEntry(SocketPermissionRequest::UDP_SEND_TO, "b:10")));
215 EXPECT_TRUE(
216 CheckFormat("{\"udp\":{\"bind\":\"\"}}",
217 CheckFormatEntry(SocketPermissionRequest::UDP_BIND, "*:*")));
218 EXPECT_TRUE(CheckFormat("{\"udp\":{\"bind\":[]}}"));
219 EXPECT_TRUE(
220 CheckFormat("{\"udp\":{\"bind\":[\"\"]}}",
221 CheckFormatEntry(SocketPermissionRequest::UDP_BIND, "*:*")));
222 EXPECT_TRUE(
223 CheckFormat("{\"udp\":{\"bind\":[\"a:80\", \"b:10\"]}}",
224 CheckFormatEntry(SocketPermissionRequest::UDP_BIND, "a:80"),
225 CheckFormatEntry(SocketPermissionRequest::UDP_BIND, "b:10")));
227 EXPECT_TRUE(CheckFormat(
228 "{\"udp\":{\"multicastMembership\":\"\"}}",
229 CheckFormatEntry(SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, "")));
230 EXPECT_TRUE(CheckFormat("{\"udp\":{\"multicastMembership\":[]}}"));
231 EXPECT_TRUE(CheckFormat(
232 "{\"udp\":{\"multicastMembership\":[\"\"]}}",
233 CheckFormatEntry(SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, "")));
234 EXPECT_TRUE(CheckFormat(
235 "{\"udp\":{\"multicastMembership\":[\"\", \"\"]}}",
236 CheckFormatEntry(SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, "")));
238 EXPECT_TRUE(CheckFormat(
239 "{\"tcp\":{\"connect\":\"\"}}",
240 CheckFormatEntry(SocketPermissionRequest::TCP_CONNECT, "*:*")));
241 EXPECT_TRUE(CheckFormat("{\"tcp\":{\"connect\":[]}}"));
242 EXPECT_TRUE(CheckFormat(
243 "{\"tcp\":{\"connect\":[\"\"]}}",
244 CheckFormatEntry(SocketPermissionRequest::TCP_CONNECT, "*:*")));
245 EXPECT_TRUE(CheckFormat(
246 "{\"tcp\":{\"connect\":[\"a:80\", \"b:10\"]}}",
247 CheckFormatEntry(SocketPermissionRequest::TCP_CONNECT, "a:80"),
248 CheckFormatEntry(SocketPermissionRequest::TCP_CONNECT, "b:10")));
250 EXPECT_TRUE(CheckFormat(
251 "{\"tcpServer\":{\"listen\":\"\"}}",
252 CheckFormatEntry(SocketPermissionRequest::TCP_LISTEN, "*:*")));
253 EXPECT_TRUE(CheckFormat("{\"tcpServer\":{\"listen\":[]}}"));
254 EXPECT_TRUE(CheckFormat(
255 "{\"tcpServer\":{\"listen\":[\"\"]}}",
256 CheckFormatEntry(SocketPermissionRequest::TCP_LISTEN, "*:*")));
257 EXPECT_TRUE(CheckFormat(
258 "{\"tcpServer\":{\"listen\":[\"a:80\", \"b:10\"]}}",
259 CheckFormatEntry(SocketPermissionRequest::TCP_LISTEN, "a:80"),
260 CheckFormatEntry(SocketPermissionRequest::TCP_LISTEN, "b:10")));
262 EXPECT_TRUE(CheckFormat(
264 "\"udp\":{"
265 "\"send\":[\"a:80\", \"b:10\"],"
266 "\"bind\":[\"a:80\", \"b:10\"],"
267 "\"multicastMembership\":\"\""
268 "},"
269 "\"tcp\":{\"connect\":[\"a:80\", \"b:10\"]},"
270 "\"tcpServer\":{\"listen\":[\"a:80\", \"b:10\"]}"
271 "}",
272 CheckFormatEntry(SocketPermissionRequest::UDP_SEND_TO, "a:80"),
273 CheckFormatEntry(SocketPermissionRequest::UDP_SEND_TO, "b:10"),
274 CheckFormatEntry(SocketPermissionRequest::UDP_BIND, "a:80"),
275 CheckFormatEntry(SocketPermissionRequest::UDP_BIND, "b:10"),
276 CheckFormatEntry(SocketPermissionRequest::UDP_MULTICAST_MEMBERSHIP, ""),
277 CheckFormatEntry(SocketPermissionRequest::TCP_CONNECT, "a:80"),
278 CheckFormatEntry(SocketPermissionRequest::TCP_CONNECT, "b:10"),
279 CheckFormatEntry(SocketPermissionRequest::TCP_LISTEN, "a:80"),
280 CheckFormatEntry(SocketPermissionRequest::TCP_LISTEN, "b:10")));
283 TEST(SocketsManifestPermissionTest, FromToValue) {
284 scoped_ptr<base::Value> udp_send(ParsePermissionJSON(kUdpBindPermission));
285 scoped_ptr<base::Value> udp_bind(ParsePermissionJSON(kUdpSendPermission));
286 scoped_ptr<base::Value> tcp_connect(
287 ParsePermissionJSON(kTcpConnectPermission));
288 scoped_ptr<base::Value> tcp_server_listen(
289 ParsePermissionJSON(kTcpServerListenPermission));
291 // FromValue()
292 scoped_ptr<SocketsManifestPermission> permission1(
293 new SocketsManifestPermission());
294 EXPECT_TRUE(permission1->FromValue(udp_send.get()));
295 EXPECT_EQ(2u, permission1->entries().size());
297 scoped_ptr<SocketsManifestPermission> permission2(
298 new SocketsManifestPermission());
299 EXPECT_TRUE(permission2->FromValue(udp_bind.get()));
300 EXPECT_EQ(2u, permission2->entries().size());
302 scoped_ptr<SocketsManifestPermission> permission3(
303 new SocketsManifestPermission());
304 EXPECT_TRUE(permission3->FromValue(tcp_connect.get()));
305 EXPECT_EQ(2u, permission3->entries().size());
307 scoped_ptr<SocketsManifestPermission> permission4(
308 new SocketsManifestPermission());
309 EXPECT_TRUE(permission4->FromValue(tcp_server_listen.get()));
310 EXPECT_EQ(2u, permission4->entries().size());
312 // ToValue()
313 scoped_ptr<base::Value> value1 = permission1->ToValue();
314 EXPECT_TRUE(value1);
315 scoped_ptr<SocketsManifestPermission> permission1_1(
316 new SocketsManifestPermission());
317 EXPECT_TRUE(permission1_1->FromValue(value1.get()));
318 EXPECT_TRUE(permission1->Equal(permission1_1.get()));
320 scoped_ptr<base::Value> value2 = permission2->ToValue();
321 EXPECT_TRUE(value2);
322 scoped_ptr<SocketsManifestPermission> permission2_1(
323 new SocketsManifestPermission());
324 EXPECT_TRUE(permission2_1->FromValue(value2.get()));
325 EXPECT_TRUE(permission2->Equal(permission2_1.get()));
327 scoped_ptr<base::Value> value3 = permission3->ToValue();
328 EXPECT_TRUE(value3);
329 scoped_ptr<SocketsManifestPermission> permission3_1(
330 new SocketsManifestPermission());
331 EXPECT_TRUE(permission3_1->FromValue(value3.get()));
332 EXPECT_TRUE(permission3->Equal(permission3_1.get()));
334 scoped_ptr<base::Value> value4 = permission4->ToValue();
335 EXPECT_TRUE(value4);
336 scoped_ptr<SocketsManifestPermission> permission4_1(
337 new SocketsManifestPermission());
338 EXPECT_TRUE(permission4_1->FromValue(value4.get()));
339 EXPECT_TRUE(permission4->Equal(permission4_1.get()));
342 TEST(SocketsManifestPermissionTest, SetOperations) {
343 scoped_ptr<SocketsManifestPermission> permission1(
344 PermissionFromJSON(kUdpBindPermission));
345 scoped_ptr<SocketsManifestPermission> permission2(
346 PermissionFromJSON(kUdpSendPermission));
347 scoped_ptr<SocketsManifestPermission> permission3(
348 PermissionFromJSON(kTcpConnectPermission));
349 scoped_ptr<SocketsManifestPermission> permission4(
350 PermissionFromJSON(kTcpServerListenPermission));
352 // Union
353 scoped_ptr<SocketsManifestPermission> union_perm(
354 static_cast<SocketsManifestPermission*>(
355 permission1->Union(permission2.get())));
356 EXPECT_TRUE(union_perm);
357 EXPECT_EQ(4u, union_perm->entries().size());
359 EXPECT_TRUE(union_perm->Contains(permission1.get()));
360 EXPECT_TRUE(union_perm->Contains(permission2.get()));
361 EXPECT_FALSE(union_perm->Contains(permission3.get()));
362 EXPECT_FALSE(union_perm->Contains(permission4.get()));
364 // Diff
365 scoped_ptr<SocketsManifestPermission> diff_perm1(
366 static_cast<SocketsManifestPermission*>(
367 permission1->Diff(permission2.get())));
368 EXPECT_TRUE(diff_perm1);
369 EXPECT_EQ(2u, diff_perm1->entries().size());
371 EXPECT_TRUE(permission1->Equal(diff_perm1.get()));
372 EXPECT_TRUE(diff_perm1->Equal(permission1.get()));
374 scoped_ptr<SocketsManifestPermission> diff_perm2(
375 static_cast<SocketsManifestPermission*>(
376 permission1->Diff(union_perm.get())));
377 EXPECT_TRUE(diff_perm2);
378 AssertEmptyPermission(diff_perm2.get());
380 // Intersection
381 scoped_ptr<SocketsManifestPermission> intersect_perm1(
382 static_cast<SocketsManifestPermission*>(
383 union_perm->Intersect(permission1.get())));
384 EXPECT_TRUE(intersect_perm1);
385 EXPECT_EQ(2u, intersect_perm1->entries().size());
387 EXPECT_TRUE(permission1->Equal(intersect_perm1.get()));
388 EXPECT_TRUE(intersect_perm1->Equal(permission1.get()));
391 TEST(SocketsManifestPermissionTest, IPC) {
392 scoped_ptr<SocketsManifestPermission> permission(
393 PermissionFromJSON(kUdpBindPermission));
395 scoped_ptr<SocketsManifestPermission> ipc_perm(
396 static_cast<SocketsManifestPermission*>(permission->Clone()));
397 scoped_ptr<SocketsManifestPermission> ipc_perm2(
398 new SocketsManifestPermission());
400 IPC::Message m;
401 ipc_perm->Write(&m);
402 base::PickleIterator iter(m);
403 EXPECT_TRUE(ipc_perm2->Read(&m, &iter));
404 EXPECT_TRUE(permission->Equal(ipc_perm2.get()));
407 } // namespace extensions