Index: chrome/browser/extensions/api/permissions/permissions_api_helpers_unittest.cc |
diff --git a/chrome/browser/extensions/api/permissions/permissions_api_helpers_unittest.cc b/chrome/browser/extensions/api/permissions/permissions_api_helpers_unittest.cc |
index 4e88b9630dd79ce08f27853fcf628a201d9bd7c2..7d03989df1ccb32721f184e871b87912cc8abb66 100644 |
--- a/chrome/browser/extensions/api/permissions/permissions_api_helpers_unittest.cc |
+++ b/chrome/browser/extensions/api/permissions/permissions_api_helpers_unittest.cc |
@@ -5,13 +5,15 @@ |
#include "base/memory/scoped_ptr.h" |
#include "base/values.h" |
#include "chrome/browser/extensions/api/permissions/permissions_api_helpers.h" |
+#include "chrome/common/extensions/api/permissions.h" |
#include "chrome/common/extensions/extension_permission_set.h" |
#include "chrome/common/extensions/url_pattern_set.h" |
#include "googleurl/src/gurl.h" |
#include "testing/gtest/include/gtest/gtest.h" |
-using extensions::permissions_api::PackPermissionsToValue; |
-using extensions::permissions_api::UnpackPermissionsFromValue; |
+using extensions::permissions_api_helpers::PackPermissionSet; |
+using extensions::permissions_api_helpers::UnpackPermissionSet; |
+using extensions::api::permissions::Permissions; |
namespace { |
@@ -31,42 +33,42 @@ TEST(ExtensionPermissionsAPIHelpers, Pack) { |
AddPattern(&hosts, "http://a.com/*"); |
AddPattern(&hosts, "http://b.com/*"); |
- scoped_refptr<ExtensionPermissionSet> permissions = |
+ scoped_refptr<ExtensionPermissionSet> permission_set = |
new ExtensionPermissionSet(apis, hosts, URLPatternSet()); |
// Pack the permission set to value and verify its contents. |
- scoped_ptr<DictionaryValue> value(PackPermissionsToValue(permissions)); |
+ scoped_ptr<Permissions> permissions(PackPermissionSet(permission_set)); |
+ scoped_ptr<DictionaryValue> value(permissions->ToValue()); |
ListValue* api_list = NULL; |
ListValue* origin_list = NULL; |
- ASSERT_TRUE(value->GetList("permissions", &api_list)); |
- ASSERT_TRUE(value->GetList("origins", &origin_list)); |
+ EXPECT_TRUE(value->GetList("permissions", &api_list)); |
+ EXPECT_TRUE(value->GetList("origins", &origin_list)); |
- ASSERT_EQ(2u, api_list->GetSize()); |
- ASSERT_EQ(2u, origin_list->GetSize()); |
+ EXPECT_EQ(2u, api_list->GetSize()); |
+ EXPECT_EQ(2u, origin_list->GetSize()); |
std::string expected_apis[] = { "tabs", "webRequest" }; |
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_apis); ++i) { |
scoped_ptr<Value> value(Value::CreateStringValue(expected_apis[i])); |
- ASSERT_NE(api_list->end(), api_list->Find(*value)); |
+ EXPECT_NE(api_list->end(), api_list->Find(*value)); |
} |
std::string expected_origins[] = { "http://a.com/*", "http://b.com/*" }; |
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expected_origins); ++i) { |
scoped_ptr<Value> value(Value::CreateStringValue(expected_origins[i])); |
- ASSERT_NE(origin_list->end(), origin_list->Find(*value)); |
+ EXPECT_NE(origin_list->end(), origin_list->Find(*value)); |
} |
// Unpack the value back to a permission set and make sure its equal to the |
// original one. |
scoped_refptr<ExtensionPermissionSet> from_value; |
- bool bad_message = false; |
std::string error; |
- ASSERT_TRUE(UnpackPermissionsFromValue( |
- value.get(), &from_value, &bad_message, &error)); |
- ASSERT_FALSE(bad_message); |
- ASSERT_TRUE(error.empty()); |
+ Permissions permissions_object; |
+ EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
+ from_value = UnpackPermissionSet(permissions_object, &error); |
+ EXPECT_TRUE(error.empty()); |
- ASSERT_EQ(*permissions, *from_value); |
+ EXPECT_EQ(*permission_set, *from_value); |
} |
// Tests various error conditions and edge cases when unpacking values |
@@ -79,79 +81,90 @@ TEST(ExtensionPermissionsAPIHelpers, Unpack) { |
scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
scoped_refptr<ExtensionPermissionSet> permissions; |
- bool bad_message = false; |
std::string error; |
// Origins shouldn't have to be present. |
- value->Set("permissions", apis->DeepCopy()); |
- ASSERT_TRUE(UnpackPermissionsFromValue( |
- value.get(), &permissions, &bad_message, &error)); |
- ASSERT_TRUE(permissions->HasAPIPermission(ExtensionAPIPermission::kTab)); |
- ASSERT_FALSE(bad_message); |
- ASSERT_TRUE(error.empty()); |
+ { |
+ Permissions permissions_object; |
+ value->Set("permissions", apis->DeepCopy()); |
+ EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
+ permissions = UnpackPermissionSet(permissions_object, &error); |
+ EXPECT_TRUE(permissions->HasAPIPermission(ExtensionAPIPermission::kTab)); |
+ EXPECT_TRUE(permissions); |
+ EXPECT_TRUE(error.empty()); |
+ } |
// The api permissions don't need to be present either. |
- value->Clear(); |
- value->Set("origins", origins->DeepCopy()); |
- ASSERT_TRUE(UnpackPermissionsFromValue( |
- value.get(), &permissions, &bad_message, &error)); |
- ASSERT_FALSE(bad_message); |
- ASSERT_TRUE(error.empty()); |
- ASSERT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); |
+ { |
+ Permissions permissions_object; |
+ value->Clear(); |
+ value->Set("origins", origins->DeepCopy()); |
+ EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
+ permissions = UnpackPermissionSet(permissions_object, &error); |
+ EXPECT_TRUE(permissions); |
+ EXPECT_TRUE(error.empty()); |
+ EXPECT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); |
+ } |
// Throw errors for non-string API permissions. |
- value->Clear(); |
- scoped_ptr<ListValue> invalid_apis(apis->DeepCopy()); |
- invalid_apis->Append(Value::CreateIntegerValue(3)); |
- value->Set("permissions", invalid_apis->DeepCopy()); |
- ASSERT_FALSE(UnpackPermissionsFromValue( |
- value.get(), &permissions, &bad_message, &error)); |
- ASSERT_TRUE(bad_message); |
- bad_message = false; |
+ { |
+ Permissions permissions_object; |
+ value->Clear(); |
+ scoped_ptr<ListValue> invalid_apis(apis->DeepCopy()); |
+ invalid_apis->Append(Value::CreateIntegerValue(3)); |
+ value->Set("permissions", invalid_apis->DeepCopy()); |
+ EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); |
+ } |
// Throw errors for non-string origins. |
- value->Clear(); |
- scoped_ptr<ListValue> invalid_origins(origins->DeepCopy()); |
- invalid_origins->Append(Value::CreateIntegerValue(3)); |
- value->Set("origins", invalid_origins->DeepCopy()); |
- ASSERT_FALSE(UnpackPermissionsFromValue( |
- value.get(), &permissions, &bad_message, &error)); |
- ASSERT_TRUE(bad_message); |
- bad_message = false; |
+ { |
+ Permissions permissions_object; |
+ value->Clear(); |
+ scoped_ptr<ListValue> invalid_origins(origins->DeepCopy()); |
+ invalid_origins->Append(Value::CreateIntegerValue(3)); |
+ value->Set("origins", invalid_origins->DeepCopy()); |
+ EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); |
+ } |
// Throw errors when "origins" or "permissions" are not list values. |
- value->Clear(); |
- value->Set("origins", Value::CreateIntegerValue(2)); |
- ASSERT_FALSE(UnpackPermissionsFromValue( |
- value.get(), &permissions, &bad_message, &error)); |
- ASSERT_TRUE(bad_message); |
- bad_message = false; |
- |
- value->Clear(); |
- value->Set("permissions", Value::CreateIntegerValue(2)); |
- ASSERT_FALSE(UnpackPermissionsFromValue( |
- value.get(), &permissions, &bad_message, &error)); |
- ASSERT_TRUE(bad_message); |
- bad_message = false; |
+ { |
+ Permissions permissions_object; |
+ value->Clear(); |
+ value->Set("origins", Value::CreateIntegerValue(2)); |
+ EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); |
+ } |
+ |
+ { |
+ Permissions permissions_object; |
+ value->Clear(); |
+ value->Set("permissions", Value::CreateIntegerValue(2)); |
+ EXPECT_FALSE(Permissions::Populate(*value, &permissions_object)); |
+ } |
// Additional fields should be allowed. |
- value->Clear(); |
- value->Set("origins", origins->DeepCopy()); |
- value->Set("random", Value::CreateIntegerValue(3)); |
- ASSERT_TRUE(UnpackPermissionsFromValue( |
- value.get(), &permissions, &bad_message, &error)); |
- ASSERT_FALSE(bad_message); |
- ASSERT_TRUE(error.empty()); |
- ASSERT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); |
- |
- // Unknown permissions should throw an error but not set the bad_message bit. |
- value->Clear(); |
- invalid_apis.reset(apis->DeepCopy()); |
- invalid_apis->Append(Value::CreateStringValue("unknown_permission")); |
- value->Set("permissions", invalid_apis->DeepCopy()); |
- ASSERT_FALSE(UnpackPermissionsFromValue( |
- value.get(), &permissions, &bad_message, &error)); |
- ASSERT_FALSE(bad_message); |
- ASSERT_FALSE(error.empty()); |
- ASSERT_EQ(error, "'unknown_permission' is not a recognized permission."); |
+ { |
+ Permissions permissions_object; |
+ value->Clear(); |
+ value->Set("origins", origins->DeepCopy()); |
+ value->Set("random", Value::CreateIntegerValue(3)); |
+ EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
+ permissions = UnpackPermissionSet(permissions_object, &error); |
+ EXPECT_TRUE(permissions); |
+ EXPECT_TRUE(error.empty()); |
+ EXPECT_TRUE(permissions->HasExplicitAccessToOrigin(GURL("http://a.com/"))); |
+ } |
+ |
+ // Unknown permissions should throw an error. |
+ { |
+ Permissions permissions_object; |
+ value->Clear(); |
+ scoped_ptr<ListValue> invalid_apis(apis->DeepCopy()); |
+ invalid_apis->Append(Value::CreateStringValue("unknown_permission")); |
+ value->Set("permissions", invalid_apis->DeepCopy()); |
+ EXPECT_TRUE(Permissions::Populate(*value, &permissions_object)); |
+ permissions = UnpackPermissionSet(permissions_object, &error); |
+ EXPECT_FALSE(permissions); |
+ EXPECT_FALSE(error.empty()); |
+ EXPECT_EQ(error, "'unknown_permission' is not a recognized permission."); |
+ } |
} |