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/extension_permission_set.h" | 9 #include "chrome/common/extensions/extension_permission_set.h" |
9 #include "chrome/common/extensions/url_pattern_set.h" | 10 #include "chrome/common/extensions/url_pattern_set.h" |
10 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 using extensions::permissions_api::PackPermissionsToValue; | 14 using extensions::permissions_api_helpers::PackPermissionSet; |
14 using extensions::permissions_api::UnpackPermissionsFromValue; | 15 using extensions::permissions_api_helpers::UnpackPermissionSet; |
| 16 using extensions::api::permissions::Permissions; |
15 | 17 |
16 namespace { | 18 namespace { |
17 | 19 |
18 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { | 20 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { |
19 int schemes = URLPattern::SCHEME_ALL; | 21 int schemes = URLPattern::SCHEME_ALL; |
20 extent->AddPattern(URLPattern(schemes, pattern)); | 22 extent->AddPattern(URLPattern(schemes, pattern)); |
21 } | 23 } |
22 | 24 |
23 } // namespace | 25 } // namespace |
24 | 26 |
25 // Tests that we can convert ExtensionPermissionSets to and from values. | 27 // Tests that we can convert ExtensionPermissionSets to and from values. |
26 TEST(ExtensionPermissionsAPIHelpers, Pack) { | 28 TEST(ExtensionPermissionsAPIHelpers, Pack) { |
27 ExtensionAPIPermissionSet apis; | 29 ExtensionAPIPermissionSet apis; |
28 apis.insert(ExtensionAPIPermission::kTab); | 30 apis.insert(ExtensionAPIPermission::kTab); |
29 apis.insert(ExtensionAPIPermission::kWebRequest); | 31 apis.insert(ExtensionAPIPermission::kWebRequest); |
30 URLPatternSet hosts; | 32 URLPatternSet hosts; |
31 AddPattern(&hosts, "http://a.com/*"); | 33 AddPattern(&hosts, "http://a.com/*"); |
32 AddPattern(&hosts, "http://b.com/*"); | 34 AddPattern(&hosts, "http://b.com/*"); |
33 | 35 |
34 scoped_refptr<ExtensionPermissionSet> permissions = | 36 scoped_refptr<ExtensionPermissionSet> permission_set = |
35 new ExtensionPermissionSet(apis, hosts, URLPatternSet()); | 37 new ExtensionPermissionSet(apis, hosts, URLPatternSet()); |
36 | 38 |
37 // Pack the permission set to value and verify its contents. | 39 // Pack the permission set to value and verify its contents. |
38 scoped_ptr<DictionaryValue> value(PackPermissionsToValue(permissions)); | 40 scoped_ptr<Permissions> permissions(PackPermissionSet(permission_set)); |
| 41 scoped_ptr<DictionaryValue> value(permissions->ToValue()); |
39 ListValue* api_list = NULL; | 42 ListValue* api_list = NULL; |
40 ListValue* origin_list = NULL; | 43 ListValue* origin_list = NULL; |
41 ASSERT_TRUE(value->GetList("permissions", &api_list)); | 44 EXPECT_TRUE(value->GetList("permissions", &api_list)); |
42 ASSERT_TRUE(value->GetList("origins", &origin_list)); | 45 EXPECT_TRUE(value->GetList("origins", &origin_list)); |
43 | 46 |
44 ASSERT_EQ(2u, api_list->GetSize()); | 47 EXPECT_EQ(2u, api_list->GetSize()); |
45 ASSERT_EQ(2u, origin_list->GetSize()); | 48 EXPECT_EQ(2u, origin_list->GetSize()); |
46 | 49 |
47 std::string expected_apis[] = { "tabs", "webRequest" }; | 50 std::string expected_apis[] = { "tabs", "webRequest" }; |
48 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_apis); ++i) { | 51 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_apis); ++i) { |
49 scoped_ptr<Value> value(Value::CreateStringValue(expected_apis[i])); | 52 scoped_ptr<Value> value(Value::CreateStringValue(expected_apis[i])); |
50 ASSERT_NE(api_list->end(), api_list->Find(*value)); | 53 EXPECT_NE(api_list->end(), api_list->Find(*value)); |
51 } | 54 } |
52 | 55 |
53 std::string expected_origins[] = { "http://a.com/*", "http://b.com/*" }; | 56 std::string expected_origins[] = { "http://a.com/*", "http://b.com/*" }; |
54 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_origins); ++i) { | 57 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_origins); ++i) { |
55 scoped_ptr<Value> value(Value::CreateStringValue(expected_origins[i])); | 58 scoped_ptr<Value> value(Value::CreateStringValue(expected_origins[i])); |
56 ASSERT_NE(origin_list->end(), origin_list->Find(*value)); | 59 EXPECT_NE(origin_list->end(), origin_list->Find(*value)); |
57 } | 60 } |
58 | 61 |
59 // Unpack the value back to a permission set and make sure its equal to the | 62 // Unpack the value back to a permission set and make sure its equal to the |
60 // original one. | 63 // original one. |
61 scoped_refptr<ExtensionPermissionSet> from_value; | 64 scoped_refptr<ExtensionPermissionSet> from_value; |
62 bool bad_message = false; | |
63 std::string error; | 65 std::string error; |
64 ASSERT_TRUE(UnpackPermissionsFromValue( | 66 Permissions permissions_object; |
65 value.get(), &from_value, &bad_message, &error)); | 67 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
66 ASSERT_FALSE(bad_message); | 68 from_value = UnpackPermissionSet(permissions_object, &error); |
67 ASSERT_TRUE(error.empty()); | 69 EXPECT_TRUE(error.empty()); |
68 | 70 |
69 ASSERT_EQ(*permissions, *from_value); | 71 EXPECT_EQ(*permission_set, *from_value); |
70 } | 72 } |
71 | 73 |
72 // Tests various error conditions and edge cases when unpacking values | 74 // Tests various error conditions and edge cases when unpacking values |
73 // into ExtensionPermissionSets. | 75 // into ExtensionPermissionSets. |
74 TEST(ExtensionPermissionsAPIHelpers, Unpack) { | 76 TEST(ExtensionPermissionsAPIHelpers, Unpack) { |
75 scoped_ptr<ListValue> apis(new ListValue()); | 77 scoped_ptr<ListValue> apis(new ListValue()); |
76 apis->Append(Value::CreateStringValue("tabs")); | 78 apis->Append(Value::CreateStringValue("tabs")); |
77 scoped_ptr<ListValue> origins(new ListValue()); | 79 scoped_ptr<ListValue> origins(new ListValue()); |
78 origins->Append(Value::CreateStringValue("http://a.com/*")); | 80 origins->Append(Value::CreateStringValue("http://a.com/*")); |
79 | 81 |
80 scoped_ptr<DictionaryValue> value(new DictionaryValue()); | 82 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
81 scoped_refptr<ExtensionPermissionSet> permissions; | 83 scoped_refptr<ExtensionPermissionSet> permissions; |
82 bool bad_message = false; | |
83 std::string error; | 84 std::string error; |
84 | 85 |
85 // Origins shouldn't have to be present. | 86 // Origins shouldn't have to be present. |
86 value->Set("permissions", apis->DeepCopy()); | 87 { |
87 ASSERT_TRUE(UnpackPermissionsFromValue( | 88 Permissions permissions_object; |
88 value.get(), &permissions, &bad_message, &error)); | 89 value->Set("permissions", apis->DeepCopy()); |
89 ASSERT_TRUE(permissions->HasAPIPermission(ExtensionAPIPermission::kTab)); | 90 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
90 ASSERT_FALSE(bad_message); | 91 permissions = UnpackPermissionSet(permissions_object, &error); |
91 ASSERT_TRUE(error.empty()); | 92 EXPECT_TRUE(permissions->HasAPIPermission(ExtensionAPIPermission::kTab)); |
| 93 EXPECT_TRUE(permissions); |
| 94 EXPECT_TRUE(error.empty()); |
| 95 } |
92 | 96 |
93 // The api permissions don't need to be present either. | 97 // The api permissions don't need to be present either. |
94 value->Clear(); | 98 { |
95 value->Set("origins", origins->DeepCopy()); | 99 Permissions permissions_object; |
96 ASSERT_TRUE(UnpackPermissionsFromValue( | 100 value->Clear(); |
97 value.get(), &permissions, &bad_message, &error)); | 101 value->Set("origins", origins->DeepCopy()); |
98 ASSERT_FALSE(bad_message); | 102 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
99 ASSERT_TRUE(error.empty()); | 103 permissions = UnpackPermissionSet(permissions_object, &error); |
100 ASSERT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); | 104 EXPECT_TRUE(permissions); |
| 105 EXPECT_TRUE(error.empty()); |
| 106 EXPECT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); |
| 107 } |
101 | 108 |
102 // Throw errors for non-string API permissions. | 109 // Throw errors for non-string API permissions. |
103 value->Clear(); | 110 { |
104 scoped_ptr<ListValue> invalid_apis(apis->DeepCopy()); | 111 Permissions permissions_object; |
105 invalid_apis->Append(Value::CreateIntegerValue(3)); | 112 value->Clear(); |
106 value->Set("permissions", invalid_apis->DeepCopy()); | 113 scoped_ptr<ListValue> invalid_apis(apis->DeepCopy()); |
107 ASSERT_FALSE(UnpackPermissionsFromValue( | 114 invalid_apis->Append(Value::CreateIntegerValue(3)); |
108 value.get(), &permissions, &bad_message, &error)); | 115 value->Set("permissions", invalid_apis->DeepCopy()); |
109 ASSERT_TRUE(bad_message); | 116 EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); |
110 bad_message = false; | 117 } |
111 | 118 |
112 // Throw errors for non-string origins. | 119 // Throw errors for non-string origins. |
113 value->Clear(); | 120 { |
114 scoped_ptr<ListValue> invalid_origins(origins->DeepCopy()); | 121 Permissions permissions_object; |
115 invalid_origins->Append(Value::CreateIntegerValue(3)); | 122 value->Clear(); |
116 value->Set("origins", invalid_origins->DeepCopy()); | 123 scoped_ptr<ListValue> invalid_origins(origins->DeepCopy()); |
117 ASSERT_FALSE(UnpackPermissionsFromValue( | 124 invalid_origins->Append(Value::CreateIntegerValue(3)); |
118 value.get(), &permissions, &bad_message, &error)); | 125 value->Set("origins", invalid_origins->DeepCopy()); |
119 ASSERT_TRUE(bad_message); | 126 EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); |
120 bad_message = false; | 127 } |
121 | 128 |
122 // Throw errors when "origins" or "permissions" are not list values. | 129 // Throw errors when "origins" or "permissions" are not list values. |
123 value->Clear(); | 130 { |
124 value->Set("origins", Value::CreateIntegerValue(2)); | 131 Permissions permissions_object; |
125 ASSERT_FALSE(UnpackPermissionsFromValue( | 132 value->Clear(); |
126 value.get(), &permissions, &bad_message, &error)); | 133 value->Set("origins", Value::CreateIntegerValue(2)); |
127 ASSERT_TRUE(bad_message); | 134 EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); |
128 bad_message = false; | 135 } |
129 | 136 |
130 value->Clear(); | 137 { |
131 value->Set("permissions", Value::CreateIntegerValue(2)); | 138 Permissions permissions_object; |
132 ASSERT_FALSE(UnpackPermissionsFromValue( | 139 value->Clear(); |
133 value.get(), &permissions, &bad_message, &error)); | 140 value->Set("permissions", Value::CreateIntegerValue(2)); |
134 ASSERT_TRUE(bad_message); | 141 EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); |
135 bad_message = false; | 142 } |
136 | 143 |
137 // Additional fields should be allowed. | 144 // Additional fields should be allowed. |
138 value->Clear(); | 145 { |
139 value->Set("origins", origins->DeepCopy()); | 146 Permissions permissions_object; |
140 value->Set("random", Value::CreateIntegerValue(3)); | 147 value->Clear(); |
141 ASSERT_TRUE(UnpackPermissionsFromValue( | 148 value->Set("origins", origins->DeepCopy()); |
142 value.get(), &permissions, &bad_message, &error)); | 149 value->Set("random", Value::CreateIntegerValue(3)); |
143 ASSERT_FALSE(bad_message); | 150 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
144 ASSERT_TRUE(error.empty()); | 151 permissions = UnpackPermissionSet(permissions_object, &error); |
145 ASSERT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); | 152 EXPECT_TRUE(permissions); |
| 153 EXPECT_TRUE(error.empty()); |
| 154 EXPECT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); |
| 155 } |
146 | 156 |
147 // Unknown permissions should throw an error but not set the bad_message bit. | 157 // Unknown permissions should throw an error. |
148 value->Clear(); | 158 { |
149 invalid_apis.reset(apis->DeepCopy()); | 159 Permissions permissions_object; |
150 invalid_apis->Append(Value::CreateStringValue("unknown_permission")); | 160 value->Clear(); |
151 value->Set("permissions", invalid_apis->DeepCopy()); | 161 scoped_ptr<ListValue> invalid_apis(apis->DeepCopy()); |
152 ASSERT_FALSE(UnpackPermissionsFromValue( | 162 invalid_apis->Append(Value::CreateStringValue("unknown_permission")); |
153 value.get(), &permissions, &bad_message, &error)); | 163 value->Set("permissions", invalid_apis->DeepCopy()); |
154 ASSERT_FALSE(bad_message); | 164 EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
155 ASSERT_FALSE(error.empty()); | 165 permissions = UnpackPermissionSet(permissions_object, &error); |
156 ASSERT_EQ(error, "'unknown_permission' is not a recognized permission."); | 166 EXPECT_FALSE(permissions); |
| 167 EXPECT_FALSE(error.empty()); |
| 168 EXPECT_EQ(error, "'unknown_permission' is not a recognized permission."); |
| 169 } |
157 } | 170 } |
OLD | NEW |