| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/common/extensions/extension.h" | 5 #include "chrome/common/extensions/extension.h" |
| 6 | 6 |
| 7 #if defined(TOOLKIT_GTK) | 7 #if defined(TOOLKIT_GTK) |
| 8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 void CompareLists(const std::vector<std::string>& expected, | 42 void CompareLists(const std::vector<std::string>& expected, |
| 43 const std::vector<std::string>& actual) { | 43 const std::vector<std::string>& actual) { |
| 44 ASSERT_EQ(expected.size(), actual.size()); | 44 ASSERT_EQ(expected.size(), actual.size()); |
| 45 | 45 |
| 46 for (size_t i = 0; i < expected.size(); ++i) { | 46 for (size_t i = 0; i < expected.size(); ++i) { |
| 47 EXPECT_EQ(expected[i], actual[i]); | 47 EXPECT_EQ(expected[i], actual[i]); |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 static void AddPattern(ExtensionExtent* extent, const std::string& pattern) { |
| 52 int schemes = URLPattern::SCHEME_ALL; |
| 53 extent->AddPattern(URLPattern(schemes, pattern)); |
| 54 } |
| 55 |
| 51 } | 56 } |
| 52 | 57 |
| 53 class ExtensionTest : public testing::Test { | 58 class ExtensionTest : public testing::Test { |
| 54 }; | 59 }; |
| 55 | 60 |
| 56 // We persist location values in the preferences, so this is a sanity test that | 61 // We persist location values in the preferences, so this is a sanity test that |
| 57 // someone doesn't accidentally change them. | 62 // someone doesn't accidentally change them. |
| 58 TEST(ExtensionTest, LocationValuesTest) { | 63 TEST(ExtensionTest, LocationValuesTest) { |
| 59 ASSERT_EQ(0, Extension::INVALID); | 64 ASSERT_EQ(0, Extension::INVALID); |
| 60 ASSERT_EQ(1, Extension::INTERNAL); | 65 ASSERT_EQ(1, Extension::INTERNAL); |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 input_value->Set(keys::kPermissions, Value::CreateIntegerValue(9)); | 250 input_value->Set(keys::kPermissions, Value::CreateIntegerValue(9)); |
| 246 EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); | 251 EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); |
| 247 EXPECT_TRUE(MatchPattern(error, errors::kInvalidPermissions)); | 252 EXPECT_TRUE(MatchPattern(error, errors::kInvalidPermissions)); |
| 248 | 253 |
| 249 input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); | 254 input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); |
| 250 input_value->GetList(keys::kPermissions, &permissions); | 255 input_value->GetList(keys::kPermissions, &permissions); |
| 251 permissions->Set(0, Value::CreateIntegerValue(24)); | 256 permissions->Set(0, Value::CreateIntegerValue(24)); |
| 252 EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); | 257 EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); |
| 253 EXPECT_TRUE(MatchPattern(error, errors::kInvalidPermission)); | 258 EXPECT_TRUE(MatchPattern(error, errors::kInvalidPermission)); |
| 254 | 259 |
| 260 // We allow unknown API permissions, so this will be valid until we better |
| 261 // distinguish between API and host permissions. |
| 255 permissions->Set(0, Value::CreateStringValue("www.google.com")); | 262 permissions->Set(0, Value::CreateStringValue("www.google.com")); |
| 256 EXPECT_FALSE(extension.InitFromValue(*input_value, true, &error)); | 263 EXPECT_TRUE(extension.InitFromValue(*input_value, true, &error)); |
| 257 EXPECT_TRUE(MatchPattern(error, errors::kInvalidPermission)); | |
| 258 | 264 |
| 259 // Multiple page actions are not allowed. | 265 // Multiple page actions are not allowed. |
| 260 input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); | 266 input_value.reset(static_cast<DictionaryValue*>(valid_value->DeepCopy())); |
| 261 DictionaryValue* action = new DictionaryValue; | 267 DictionaryValue* action = new DictionaryValue; |
| 262 action->SetString(keys::kPageActionId, "MyExtensionActionId"); | 268 action->SetString(keys::kPageActionId, "MyExtensionActionId"); |
| 263 action->SetString(keys::kName, "MyExtensionActionName"); | 269 action->SetString(keys::kName, "MyExtensionActionName"); |
| 264 ListValue* action_list = new ListValue; | 270 ListValue* action_list = new ListValue; |
| 265 action_list->Append(action->DeepCopy()); | 271 action_list->Append(action->DeepCopy()); |
| 266 action_list->Append(action); | 272 action_list->Append(action); |
| 267 input_value->Set(keys::kPageActions, action_list); | 273 input_value->Set(keys::kPageActions, action_list); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 EXPECT_TRUE(Extension::IdIsValid(extension.id())); | 326 EXPECT_TRUE(Extension::IdIsValid(extension.id())); |
| 321 EXPECT_EQ("1.0.0.0", extension.VersionString()); | 327 EXPECT_EQ("1.0.0.0", extension.VersionString()); |
| 322 EXPECT_EQ("my extension", extension.name()); | 328 EXPECT_EQ("my extension", extension.name()); |
| 323 EXPECT_EQ(extension.id(), extension.url().host()); | 329 EXPECT_EQ(extension.id(), extension.url().host()); |
| 324 EXPECT_EQ(path.value(), extension.path().value()); | 330 EXPECT_EQ(path.value(), extension.path().value()); |
| 325 | 331 |
| 326 // Test permissions scheme. | 332 // Test permissions scheme. |
| 327 ListValue* permissions = new ListValue; | 333 ListValue* permissions = new ListValue; |
| 328 permissions->Set(0, Value::CreateStringValue("file:///C:/foo.txt")); | 334 permissions->Set(0, Value::CreateStringValue("file:///C:/foo.txt")); |
| 329 input_value.Set(keys::kPermissions, permissions); | 335 input_value.Set(keys::kPermissions, permissions); |
| 330 EXPECT_FALSE(extension.InitFromValue(input_value, false, &error)); | 336 |
| 331 EXPECT_TRUE(MatchPattern(error, errors::kInvalidPermission)); | 337 // We allow unknown API permissions, so this will be valid until we better |
| 338 // distinguish between API and host permissions. |
| 339 EXPECT_TRUE(extension.InitFromValue(input_value, false, &error)); |
| 332 input_value.Remove(keys::kPermissions, NULL); | 340 input_value.Remove(keys::kPermissions, NULL); |
| 333 error.clear(); | |
| 334 | 341 |
| 335 // Test with an options page. | 342 // Test with an options page. |
| 336 input_value.SetString(keys::kOptionsPage, "options.html"); | 343 input_value.SetString(keys::kOptionsPage, "options.html"); |
| 337 EXPECT_TRUE(extension.InitFromValue(input_value, false, &error)); | 344 EXPECT_TRUE(extension.InitFromValue(input_value, false, &error)); |
| 338 EXPECT_EQ("", error); | 345 EXPECT_EQ("", error); |
| 339 EXPECT_EQ("chrome-extension", extension.options_url().scheme()); | 346 EXPECT_EQ("chrome-extension", extension.options_url().scheme()); |
| 340 EXPECT_EQ("/options.html", extension.options_url().path()); | 347 EXPECT_EQ("/options.html", extension.options_url().path()); |
| 341 | 348 |
| 342 // Test that an empty list of page actions does not stop a browser action | 349 // Test that an empty list of page actions does not stop a browser action |
| 343 // from being loaded. | 350 // from being loaded. |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 hosts = extension->GetEffectiveHostPermissions(); | 816 hosts = extension->GetEffectiveHostPermissions(); |
| 810 EXPECT_FALSE(hosts.ContainsURL(GURL("http://test/"))); | 817 EXPECT_FALSE(hosts.ContainsURL(GURL("http://test/"))); |
| 811 EXPECT_TRUE(hosts.ContainsURL(GURL("https://test/"))); | 818 EXPECT_TRUE(hosts.ContainsURL(GURL("https://test/"))); |
| 812 EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); | 819 EXPECT_TRUE(hosts.ContainsURL(GURL("http://www.google.com"))); |
| 813 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); | 820 EXPECT_TRUE(extension->HasEffectiveAccessToAllHosts()); |
| 814 } | 821 } |
| 815 | 822 |
| 816 TEST(ExtensionTest, IsPrivilegeIncrease) { | 823 TEST(ExtensionTest, IsPrivilegeIncrease) { |
| 817 const struct { | 824 const struct { |
| 818 const char* base_name; | 825 const char* base_name; |
| 826 // Increase these sizes if you have more than 10. |
| 827 const char* granted_apis[10]; |
| 828 const char* granted_hosts[10]; |
| 829 bool full_access; |
| 819 bool expect_success; | 830 bool expect_success; |
| 820 } kTests[] = { | 831 } kTests[] = { |
| 821 { "allhosts1", false }, // all -> all | 832 { "allhosts1", {NULL}, {"http://*/", NULL}, false, |
| 822 { "allhosts2", false }, // all -> one | 833 false }, // all -> all |
| 823 { "allhosts3", true }, // one -> all | 834 { "allhosts2", {NULL}, {"http://*/", NULL}, false, |
| 824 { "hosts1", false }, // http://a,http://b -> http://a,http://b | 835 false }, // all -> one |
| 825 { "hosts2", false }, // http://a,http://b -> https://a,http://*.b | 836 { "allhosts3", {NULL}, {NULL}, false, true }, // one -> all |
| 826 { "hosts3", false }, // http://a,http://b -> http://a | 837 { "hosts1", {NULL}, |
| 827 { "hosts4", true }, // http://a -> http://a,http://b | 838 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false, |
| 828 { "hosts5", false }, // http://a,b,c -> http://a,b,c + https://a,b,c | 839 false }, // http://a,http://b -> http://a,http://b |
| 829 { "hosts6", false }, // http://a.com -> http://a.com + http://a.co.uk | 840 { "hosts2", {NULL}, |
| 830 { "permissions1", false }, // tabs -> tabs | 841 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false, |
| 831 { "permissions2", true }, // tabs -> tabs,bookmarks | 842 false }, // http://a,http://b -> https://a,http://*.b |
| 832 { "permissions3", true }, // http://a -> http://a,tabs | 843 { "hosts3", {NULL}, |
| 833 { "permissions5", true }, // bookmarks -> bookmarks,history | 844 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false, |
| 845 false }, // http://a,http://b -> http://a |
| 846 { "hosts4", {NULL}, |
| 847 {"http://www.google.com/", NULL}, false, |
| 848 true }, // http://a -> http://a,http://b |
| 849 { "hosts5", {"tabs", "notifications", NULL}, |
| 850 {"http://*.example.com/", "http://*.example.com/*", |
| 851 "http://*.example.co.uk/*", "http://*.example.com.au/*", |
| 852 NULL}, false, |
| 853 false }, // http://a,b,c -> http://a,b,c + https://a,b,c |
| 854 { "hosts6", {"tabs", "notifications", NULL}, |
| 855 {"http://*.example.com/", "http://*.example.com/*", NULL}, false, |
| 856 false }, // http://a.com -> http://a.com + http://a.co.uk |
| 857 { "permissions1", {"tabs", NULL}, |
| 858 {NULL}, false, false }, // tabs -> tabs |
| 859 { "permissions2", {"tabs", NULL}, |
| 860 {NULL}, false, true }, // tabs -> tabs,bookmarks |
| 861 { "permissions3", {NULL}, |
| 862 {"http://*/*", NULL}, |
| 863 false, true }, // http://a -> http://a,tabs |
| 864 { "permissions5", {"bookmarks", NULL}, |
| 865 {NULL}, false, true }, // bookmarks -> bookmarks,history |
| 834 #if !defined(OS_CHROMEOS) // plugins aren't allowed in ChromeOS | 866 #if !defined(OS_CHROMEOS) // plugins aren't allowed in ChromeOS |
| 835 { "permissions4", false }, // plugin -> plugin,tabs | 867 { "permissions4", {NULL}, |
| 836 { "plugin1", false }, // plugin -> plugin | 868 {NULL}, true, false }, // plugin -> plugin,tabs |
| 837 { "plugin2", false }, // plugin -> none | 869 { "plugin1", {NULL}, |
| 838 { "plugin3", true }, // none -> plugin | 870 {NULL}, true, false }, // plugin -> plugin |
| 871 { "plugin2", {NULL}, |
| 872 {NULL}, true, false }, // plugin -> none |
| 873 { "plugin3", {NULL}, |
| 874 {NULL}, false, true }, // none -> plugin |
| 839 #endif | 875 #endif |
| 840 { "storage", false }, // none -> storage | 876 { "storage", {NULL}, |
| 841 { "notifications", false } // none -> notifications | 877 {NULL}, false, false }, // none -> storage |
| 878 { "notifications", {NULL}, |
| 879 {NULL}, false, false } // none -> notifications |
| 842 }; | 880 }; |
| 843 | 881 |
| 844 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { | 882 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { |
| 845 scoped_refptr<Extension> old_extension( | 883 scoped_refptr<Extension> old_extension( |
| 846 LoadManifest("allow_silent_upgrade", | 884 LoadManifest("allow_silent_upgrade", |
| 847 std::string(kTests[i].base_name) + "_old.json")); | 885 std::string(kTests[i].base_name) + "_old.json")); |
| 848 scoped_refptr<Extension> new_extension( | 886 scoped_refptr<Extension> new_extension( |
| 849 LoadManifest("allow_silent_upgrade", | 887 LoadManifest("allow_silent_upgrade", |
| 850 std::string(kTests[i].base_name) + "_new.json")); | 888 std::string(kTests[i].base_name) + "_new.json")); |
| 851 | 889 |
| 852 EXPECT_TRUE(old_extension.get()) << kTests[i].base_name << "_old.json"; | 890 std::set<std::string> granted_apis; |
| 891 for (size_t j = 0; kTests[i].granted_apis[j] != NULL; ++j) |
| 892 granted_apis.insert(kTests[i].granted_apis[j]); |
| 893 |
| 894 ExtensionExtent granted_hosts; |
| 895 for (size_t j = 0; kTests[i].granted_hosts[j] != NULL; ++j) |
| 896 AddPattern(&granted_hosts, kTests[i].granted_hosts[j]); |
| 897 |
| 853 EXPECT_TRUE(new_extension.get()) << kTests[i].base_name << "_new.json"; | 898 EXPECT_TRUE(new_extension.get()) << kTests[i].base_name << "_new.json"; |
| 854 if (!old_extension.get() || !new_extension.get()) | 899 if (!new_extension.get()) |
| 855 continue; | 900 continue; |
| 856 | 901 |
| 857 EXPECT_EQ(kTests[i].expect_success, | 902 EXPECT_EQ(kTests[i].expect_success, |
| 858 Extension::IsPrivilegeIncrease(old_extension.get(), | 903 Extension::IsPrivilegeIncrease(kTests[i].full_access, |
| 904 granted_apis, |
| 905 granted_hosts, |
| 859 new_extension.get())) | 906 new_extension.get())) |
| 860 << kTests[i].base_name; | 907 << kTests[i].base_name; |
| 861 } | 908 } |
| 862 } | 909 } |
| 863 | 910 |
| 864 TEST(ExtensionTest, PermissionMessages) { | 911 TEST(ExtensionTest, PermissionMessages) { |
| 865 // Ensure that all permissions that needs to show install UI actually have | 912 // Ensure that all permissions that needs to show install UI actually have |
| 866 // strings associated with them. | 913 // strings associated with them. |
| 867 | 914 |
| 868 std::set<std::string> skip; | 915 std::set<std::string> skip; |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1160 EXPECT_TRUE(Extension::GenerateId("test", &result)); | 1207 EXPECT_TRUE(Extension::GenerateId("test", &result)); |
| 1161 EXPECT_EQ(result, "jpignaibiiemhngfjkcpokkamffknabf"); | 1208 EXPECT_EQ(result, "jpignaibiiemhngfjkcpokkamffknabf"); |
| 1162 | 1209 |
| 1163 EXPECT_TRUE(Extension::GenerateId("_", &result)); | 1210 EXPECT_TRUE(Extension::GenerateId("_", &result)); |
| 1164 EXPECT_EQ(result, "ncocknphbhhlhkikpnnlmbcnbgdempcd"); | 1211 EXPECT_EQ(result, "ncocknphbhhlhkikpnnlmbcnbgdempcd"); |
| 1165 | 1212 |
| 1166 EXPECT_TRUE(Extension::GenerateId( | 1213 EXPECT_TRUE(Extension::GenerateId( |
| 1167 "this_string_is_longer_than_a_single_sha256_hash_digest", &result)); | 1214 "this_string_is_longer_than_a_single_sha256_hash_digest", &result)); |
| 1168 EXPECT_EQ(result, "jimneklojkjdibfkgiiophfhjhbdgcfi"); | 1215 EXPECT_EQ(result, "jimneklojkjdibfkgiiophfhjhbdgcfi"); |
| 1169 } | 1216 } |
| OLD | NEW |