1// Copyright (c) 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// These tests make sure MediaGalleriesPermission values are parsed correctly.
6
7#include "base/values.h"
8#include "extensions/common/permissions/api_permission.h"
9#include "extensions/common/permissions/media_galleries_permission.h"
10#include "extensions/common/permissions/media_galleries_permission_data.h"
11#include "extensions/common/permissions/permissions_info.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14using content::SocketPermissionRequest;
15using extensions::SocketPermissionData;
16
17namespace extensions {
18
19namespace {
20
21void CheckFromValue(APIPermission* permission, base::ListValue* value,
22                    bool success_expected) {
23  std::string error;
24  std::vector<std::string> unhandled;
25  EXPECT_EQ(success_expected, permission->FromValue(value, &error, &unhandled));
26  EXPECT_EQ(success_expected, error.empty());
27  EXPECT_TRUE(unhandled.empty());
28}
29
30TEST(MediaGalleriesPermissionTest, GoodValues) {
31  const APIPermissionInfo* permission_info =
32    PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries);
33
34  scoped_ptr<APIPermission> permission(
35      permission_info->CreateAPIPermission());
36
37  // access_type + all_detected
38  scoped_ptr<base::ListValue> value(new base::ListValue());
39  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
40  value->AppendString(MediaGalleriesPermission::kReadPermission);
41  CheckFromValue(permission.get(), value.get(), true);
42
43  value.reset(new base::ListValue());
44  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
45  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
46  value->AppendString(MediaGalleriesPermission::kReadPermission);
47  value->AppendString(MediaGalleriesPermission::kDeletePermission);
48  CheckFromValue(permission.get(), value.get(), true);
49
50  // all_detected
51  value.reset(new base::ListValue());
52  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
53  CheckFromValue(permission.get(), value.get(), true);
54
55  // access_type
56  value.reset(new base::ListValue());
57  value->AppendString(MediaGalleriesPermission::kReadPermission);
58  CheckFromValue(permission.get(), value.get(), true);
59
60  value.reset(new base::ListValue());
61  value->AppendString(MediaGalleriesPermission::kDeletePermission);
62  value->AppendString(MediaGalleriesPermission::kReadPermission);
63  CheckFromValue(permission.get(), value.get(), true);
64
65  value.reset(new base::ListValue());
66  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
67  value->AppendString(MediaGalleriesPermission::kDeletePermission);
68  value->AppendString(MediaGalleriesPermission::kReadPermission);
69  CheckFromValue(permission.get(), value.get(), true);
70
71  // Repeats do not make a difference.
72  value.reset(new base::ListValue());
73  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
74  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
75  CheckFromValue(permission.get(), value.get(), true);
76
77  value.reset(new base::ListValue());
78  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
79  value->AppendString(MediaGalleriesPermission::kReadPermission);
80  value->AppendString(MediaGalleriesPermission::kReadPermission);
81  value->AppendString(MediaGalleriesPermission::kDeletePermission);
82  value->AppendString(MediaGalleriesPermission::kDeletePermission);
83  value->AppendString(MediaGalleriesPermission::kDeletePermission);
84  CheckFromValue(permission.get(), value.get(), true);
85}
86
87TEST(MediaGalleriesPermissionTest, BadValues) {
88  const APIPermissionInfo* permission_info =
89    PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries);
90
91  scoped_ptr<APIPermission> permission(permission_info->CreateAPIPermission());
92
93  // Empty
94  scoped_ptr<base::ListValue> value(new base::ListValue());
95  CheckFromValue(permission.get(), value.get(), false);
96
97  // copyTo and delete without read
98  value.reset(new base::ListValue());
99  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
100  CheckFromValue(permission.get(), value.get(), false);
101
102  value.reset(new base::ListValue());
103  value->AppendString(MediaGalleriesPermission::kDeletePermission);
104  CheckFromValue(permission.get(), value.get(), false);
105
106  value.reset(new base::ListValue());
107  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
108  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
109  value->AppendString(MediaGalleriesPermission::kDeletePermission);
110  CheckFromValue(permission.get(), value.get(), false);
111
112  // copyTo without delete
113  value.reset(new base::ListValue());
114  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
115  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
116  value->AppendString(MediaGalleriesPermission::kReadPermission);
117  CheckFromValue(permission.get(), value.get(), false);
118
119  // Repeats do not make a difference.
120  value.reset(new base::ListValue());
121  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
122  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
123  CheckFromValue(permission.get(), value.get(), false);
124
125  value.reset(new base::ListValue());
126  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
127  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
128  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
129  value->AppendString(MediaGalleriesPermission::kDeletePermission);
130  value->AppendString(MediaGalleriesPermission::kDeletePermission);
131  CheckFromValue(permission.get(), value.get(), false);
132}
133
134TEST(MediaGalleriesPermissionTest, UnknownValues) {
135  std::string error;
136  std::vector<std::string> unhandled;
137  const APIPermissionInfo* permission_info =
138      PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries);
139
140  scoped_ptr<APIPermission> permission(permission_info->CreateAPIPermission());
141
142  // A good one and an unknown one.
143  scoped_ptr<base::ListValue> value(new base::ListValue());
144  value->AppendString(MediaGalleriesPermission::kReadPermission);
145  value->AppendString("Unknown");
146  EXPECT_TRUE(permission->FromValue(value.get(), &error, &unhandled));
147  EXPECT_TRUE(error.empty());
148  EXPECT_EQ(1U, unhandled.size());
149  error.clear();
150  unhandled.clear();
151
152  // Multiple unknown permissions.
153  value.reset(new base::ListValue());
154  value->AppendString("Unknown1");
155  value->AppendString("Unknown2");
156  EXPECT_TRUE(permission->FromValue(value.get(), &error, &unhandled));
157  EXPECT_TRUE(error.empty());
158  EXPECT_EQ(2U, unhandled.size());
159  error.clear();
160  unhandled.clear();
161
162  // Unnknown with a NULL argument.
163  value.reset(new base::ListValue());
164  value->AppendString("Unknown1");
165  EXPECT_FALSE(permission->FromValue(value.get(), &error, NULL));
166  EXPECT_FALSE(error.empty());
167  error.clear();
168}
169
170TEST(MediaGalleriesPermissionTest, Equal) {
171  const APIPermissionInfo* permission_info =
172    PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries);
173
174  scoped_ptr<APIPermission> permission1(
175      permission_info->CreateAPIPermission());
176  scoped_ptr<APIPermission> permission2(
177      permission_info->CreateAPIPermission());
178
179  scoped_ptr<base::ListValue> value(new base::ListValue());
180  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
181  value->AppendString(MediaGalleriesPermission::kReadPermission);
182  ASSERT_TRUE(permission1->FromValue(value.get(), NULL, NULL));
183
184  value.reset(new base::ListValue());
185  value->AppendString(MediaGalleriesPermission::kReadPermission);
186  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
187  ASSERT_TRUE(permission2->FromValue(value.get(), NULL, NULL));
188  EXPECT_TRUE(permission1->Equal(permission2.get()));
189
190  value.reset(new base::ListValue());
191  value->AppendString(MediaGalleriesPermission::kReadPermission);
192  value->AppendString(MediaGalleriesPermission::kReadPermission);
193  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
194  ASSERT_TRUE(permission2->FromValue(value.get(), NULL, NULL));
195  EXPECT_TRUE(permission1->Equal(permission2.get()));
196
197  value.reset(new base::ListValue());
198  value->AppendString(MediaGalleriesPermission::kReadPermission);
199  value->AppendString(MediaGalleriesPermission::kDeletePermission);
200  ASSERT_TRUE(permission1->FromValue(value.get(), NULL, NULL));
201
202  value.reset(new base::ListValue());
203  value->AppendString(MediaGalleriesPermission::kDeletePermission);
204  value->AppendString(MediaGalleriesPermission::kReadPermission);
205  ASSERT_TRUE(permission2->FromValue(value.get(), NULL, NULL));
206  EXPECT_TRUE(permission1->Equal(permission2.get()));
207
208  value.reset(new base::ListValue());
209  value->AppendString(MediaGalleriesPermission::kReadPermission);
210  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
211  value->AppendString(MediaGalleriesPermission::kDeletePermission);
212  ASSERT_TRUE(permission1->FromValue(value.get(), NULL, NULL));
213
214  value.reset(new base::ListValue());
215  value->AppendString(MediaGalleriesPermission::kDeletePermission);
216  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
217  value->AppendString(MediaGalleriesPermission::kReadPermission);
218  ASSERT_TRUE(permission2->FromValue(value.get(), NULL, NULL));
219  EXPECT_TRUE(permission1->Equal(permission2.get()));
220}
221
222TEST(MediaGalleriesPermissionTest, NotEqual) {
223  const APIPermissionInfo* permission_info =
224    PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries);
225
226  scoped_ptr<APIPermission> permission1(
227      permission_info->CreateAPIPermission());
228  scoped_ptr<APIPermission> permission2(
229      permission_info->CreateAPIPermission());
230
231  scoped_ptr<base::ListValue> value(new base::ListValue());
232  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
233  value->AppendString(MediaGalleriesPermission::kReadPermission);
234  ASSERT_TRUE(permission1->FromValue(value.get(), NULL, NULL));
235
236  value.reset(new base::ListValue());
237  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
238  value->AppendString(MediaGalleriesPermission::kReadPermission);
239  value->AppendString(MediaGalleriesPermission::kDeletePermission);
240  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
241  ASSERT_TRUE(permission2->FromValue(value.get(), NULL, NULL));
242  EXPECT_FALSE(permission1->Equal(permission2.get()));
243}
244
245TEST(MediaGalleriesPermissionTest, ToFromValue) {
246  const APIPermissionInfo* permission_info =
247    PermissionsInfo::GetInstance()->GetByID(APIPermission::kMediaGalleries);
248
249  scoped_ptr<APIPermission> permission1(
250      permission_info->CreateAPIPermission());
251  scoped_ptr<APIPermission> permission2(
252      permission_info->CreateAPIPermission());
253
254  scoped_ptr<base::ListValue> value(new base::ListValue());
255  value->AppendString(MediaGalleriesPermission::kAllAutoDetectedPermission);
256  value->AppendString(MediaGalleriesPermission::kReadPermission);
257  ASSERT_TRUE(permission1->FromValue(value.get(), NULL, NULL));
258
259  scoped_ptr<base::Value> vtmp(permission1->ToValue());
260  ASSERT_TRUE(vtmp);
261  ASSERT_TRUE(permission2->FromValue(vtmp.get(), NULL, NULL));
262  EXPECT_TRUE(permission1->Equal(permission2.get()));
263
264  value.reset(new base::ListValue());
265  value->AppendString(MediaGalleriesPermission::kReadPermission);
266  value->AppendString(MediaGalleriesPermission::kDeletePermission);
267  value->AppendString(MediaGalleriesPermission::kCopyToPermission);
268  ASSERT_TRUE(permission1->FromValue(value.get(), NULL, NULL));
269
270  vtmp = permission1->ToValue();
271  ASSERT_TRUE(vtmp);
272  ASSERT_TRUE(permission2->FromValue(vtmp.get(), NULL, NULL));
273  EXPECT_TRUE(permission1->Equal(permission2.get()));
274
275  value.reset(new base::ListValue());
276  value->AppendString(MediaGalleriesPermission::kReadPermission);
277  value->AppendString(MediaGalleriesPermission::kDeletePermission);
278  ASSERT_TRUE(permission1->FromValue(value.get(), NULL, NULL));
279
280  vtmp = permission1->ToValue();
281  ASSERT_TRUE(vtmp);
282  ASSERT_TRUE(permission2->FromValue(vtmp.get(), NULL, NULL));
283  EXPECT_TRUE(permission1->Equal(permission2.get()));
284}
285
286}  // namespace
287
288}  // namespace extensions
289