OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/values.h" | 6 #include "base/values.h" |
7 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" | 7 #include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" |
8 #include "chrome/common/extensions/api/permissions.h" | 8 #include "chrome/common/extensions/api/permissions.h" |
9 #include "chrome/common/extensions/extension_permission_set.h" | 9 #include "chrome/common/extensions/permissions/permission_set.h" |
10 #include "chrome/common/extensions/url_pattern_set.h" | 10 #include "chrome/common/extensions/url_pattern_set.h" |
11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 using extensions::permissions_api_helpers::PackPermissionSet; | 14 using extensions::permissions_api_helpers::PackPermissionSet; |
15 using extensions::permissions_api_helpers::UnpackPermissionSet; | 15 using extensions::permissions_api_helpers::UnpackPermissionSet; |
16 using extensions::api::permissions::Permissions; | 16 using extensions::api::permissions::Permissions; |
| 17 using extensions::APIPermission; |
| 18 using extensions::APIPermissionSet; |
| 19 using extensions::PermissionSet; |
17 | 20 |
18 namespace { | 21 namespace { |
19 | 22 |
20 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { | 23 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { |
21 int schemes = URLPattern::SCHEME_ALL; | 24 int schemes = URLPattern::SCHEME_ALL; |
22 extent->AddPattern(URLPattern(schemes, pattern)); | 25 extent->AddPattern(URLPattern(schemes, pattern)); |
23 } | 26 } |
24 | 27 |
25 } // namespace | 28 } // namespace |
26 | 29 |
27 // Tests that we can convert ExtensionPermissionSets to and from values. | 30 // Tests that we can convert PermissionSets to and from values. |
28 TEST(ExtensionPermissionsAPIHelpers, Pack) { | 31 TEST(ExtensionPermissionsAPIHelpers, Pack) { |
29 ExtensionAPIPermissionSet apis; | 32 APIPermissionSet apis; |
30 apis.insert(ExtensionAPIPermission::kTab); | 33 apis.insert(APIPermission::kTab); |
31 apis.insert(ExtensionAPIPermission::kWebRequest); | 34 apis.insert(APIPermission::kWebRequest); |
32 URLPatternSet hosts; | 35 URLPatternSet hosts; |
33 AddPattern(&hosts, "http://a.com/*"); | 36 AddPattern(&hosts, "http://a.com/*"); |
34 AddPattern(&hosts, "http://b.com/*"); | 37 AddPattern(&hosts, "http://b.com/*"); |
35 | 38 |
36 scoped_refptr<ExtensionPermissionSet> permission_set = | 39 scoped_refptr<PermissionSet> permission_set = |
37 new ExtensionPermissionSet(apis, hosts, URLPatternSet()); | 40 new PermissionSet(apis, hosts, URLPatternSet()); |
38 | 41 |
39 // Pack the permission set to value and verify its contents. | 42 // Pack the permission set to value and verify its contents. |
40 scoped_ptr<Permissions> permissions(PackPermissionSet(permission_set)); | 43 scoped_ptr<Permissions> permissions(PackPermissionSet(permission_set)); |
41 scoped_ptr<DictionaryValue> value(permissions->ToValue()); | 44 scoped_ptr<DictionaryValue> value(permissions->ToValue()); |
42 ListValue* api_list = NULL; | 45 ListValue* api_list = NULL; |
43 ListValue* origin_list = NULL; | 46 ListValue* origin_list = NULL; |
44 EXPECT_TRUE(value->GetList("permissions", &api_list)); | 47 EXPECT_TRUE(value->GetList("permissions", &api_list)); |
45 EXPECT_TRUE(value->GetList("origins", &origin_list)); | 48 EXPECT_TRUE(value->GetList("origins", &origin_list)); |
46 | 49 |
47 EXPECT_EQ(2u, api_list->GetSize()); | 50 EXPECT_EQ(2u, api_list->GetSize()); |
48 EXPECT_EQ(2u, origin_list->GetSize()); | 51 EXPECT_EQ(2u, origin_list->GetSize()); |
49 | 52 |
50 std::string expected_apis[] = { "tabs", "webRequest" }; | 53 std::string expected_apis[] = { "tabs", "webRequest" }; |
51 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_apis); ++i) { | 54 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_apis); ++i) { |
52 scoped_ptr<Value> value(Value::CreateStringValue(expected_apis[i])); | 55 scoped_ptr<Value> value(Value::CreateStringValue(expected_apis[i])); |
53 EXPECT_NE(api_list->end(), api_list->Find(*value)); | 56 EXPECT_NE(api_list->end(), api_list->Find(*value)); |
54 } | 57 } |
55 | 58 |
56 std::string expected_origins[] = { "http://a.com/*", "http://b.com/*" }; | 59 std::string expected_origins[] = { "http://a.com/*", "http://b.com/*" }; |
57 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_origins); ++i) { | 60 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_origins); ++i) { |
58 scoped_ptr<Value> value(Value::CreateStringValue(expected_origins[i])); | 61 scoped_ptr<Value> value(Value::CreateStringValue(expected_origins[i])); |
59 EXPECT_NE(origin_list->end(), origin_list->Find(*value)); | 62 EXPECT_NE(origin_list->end(), origin_list->Find(*value)); |
60 } | 63 } |
61 | 64 |
62 // Unpack the value back to a permission set and make sure its equal to the | 65 // Unpack the value back to a permission set and make sure its equal to the |
63 // original one. | 66 // original one. |
64 scoped_refptr<ExtensionPermissionSet> from_value; | 67 scoped_refptr<PermissionSet> from_value; |
65 std::string error; | 68 std::string error; |
66 Permissions permissions_object; | 69 Permissions permissions_object; |
67 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); | 70 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
68 from_value = UnpackPermissionSet(permissions_object, &error); | 71 from_value = UnpackPermissionSet(permissions_object, &error); |
69 EXPECT_TRUE(error.empty()); | 72 EXPECT_TRUE(error.empty()); |
70 | 73 |
71 EXPECT_EQ(*permission_set, *from_value); | 74 EXPECT_EQ(*permission_set, *from_value); |
72 } | 75 } |
73 | 76 |
74 // Tests various error conditions and edge cases when unpacking values | 77 // Tests various error conditions and edge cases when unpacking values |
75 // into ExtensionPermissionSets. | 78 // into PermissionSets. |
76 TEST(ExtensionPermissionsAPIHelpers, Unpack) { | 79 TEST(ExtensionPermissionsAPIHelpers, Unpack) { |
77 scoped_ptr<ListValue> apis(new ListValue()); | 80 scoped_ptr<ListValue> apis(new ListValue()); |
78 apis->Append(Value::CreateStringValue("tabs")); | 81 apis->Append(Value::CreateStringValue("tabs")); |
79 scoped_ptr<ListValue> origins(new ListValue()); | 82 scoped_ptr<ListValue> origins(new ListValue()); |
80 origins->Append(Value::CreateStringValue("http://a.com/*")); | 83 origins->Append(Value::CreateStringValue("http://a.com/*")); |
81 | 84 |
82 scoped_ptr<DictionaryValue> value(new DictionaryValue()); | 85 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
83 scoped_refptr<ExtensionPermissionSet> permissions; | 86 scoped_refptr<PermissionSet> permissions; |
84 std::string error; | 87 std::string error; |
85 | 88 |
86 // Origins shouldn't have to be present. | 89 // Origins shouldn't have to be present. |
87 { | 90 { |
88 Permissions permissions_object; | 91 Permissions permissions_object; |
89 value->Set("permissions", apis->DeepCopy()); | 92 value->Set("permissions", apis->DeepCopy()); |
90 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); | 93 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
91 permissions = UnpackPermissionSet(permissions_object, &error); | 94 permissions = UnpackPermissionSet(permissions_object, &error); |
92 EXPECT_TRUE(permissions->HasAPIPermission(ExtensionAPIPermission::kTab)); | 95 EXPECT_TRUE(permissions->HasAPIPermission(APIPermission::kTab)); |
93 EXPECT_TRUE(permissions); | 96 EXPECT_TRUE(permissions); |
94 EXPECT_TRUE(error.empty()); | 97 EXPECT_TRUE(error.empty()); |
95 } | 98 } |
96 | 99 |
97 // The api permissions don't need to be present either. | 100 // The api permissions don't need to be present either. |
98 { | 101 { |
99 Permissions permissions_object; | 102 Permissions permissions_object; |
100 value->Clear(); | 103 value->Clear(); |
101 value->Set("origins", origins->DeepCopy()); | 104 value->Set("origins", origins->DeepCopy()); |
102 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); | 105 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 scoped_ptr<ListValue> invalid_apis(apis->DeepCopy()); | 164 scoped_ptr<ListValue> invalid_apis(apis->DeepCopy()); |
162 invalid_apis->Append(Value::CreateStringValue("unknown_permission")); | 165 invalid_apis->Append(Value::CreateStringValue("unknown_permission")); |
163 value->Set("permissions", invalid_apis->DeepCopy()); | 166 value->Set("permissions", invalid_apis->DeepCopy()); |
164 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); | 167 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
165 permissions = UnpackPermissionSet(permissions_object, &error); | 168 permissions = UnpackPermissionSet(permissions_object, &error); |
166 EXPECT_FALSE(permissions); | 169 EXPECT_FALSE(permissions); |
167 EXPECT_FALSE(error.empty()); | 170 EXPECT_FALSE(error.empty()); |
168 EXPECT_EQ(error, "'unknown_permission' is not a recognized permission."); | 171 EXPECT_EQ(error, "'unknown_permission' is not a recognized permission."); |
169 } | 172 } |
170 } | 173 } |
OLD | NEW |