Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: chrome/common/extensions/extension_unittest.cc

Issue 4687005: Track permissions granted to extensions in prefs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: incorporate feedback Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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
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 const char** granted_apis;
827 const char** granted_hosts;
819 bool expect_success; 828 bool expect_success;
820 } kTests[] = { 829 } kTests[] = {
821 { "allhosts1", false }, // all -> all 830 { "allhosts1", (const char*[]){NULL},
822 { "allhosts2", false }, // all -> one 831 (const char*[]){"http://*/", NULL},
823 { "allhosts3", true }, // one -> all 832 false }, // all -> all
824 { "hosts1", false }, // http://a,http://b -> http://a,http://b 833 { "allhosts2", (const char*[]){NULL},
825 { "hosts2", false }, // http://a,http://b -> https://a,http://*.b 834 (const char*[]){"http://*/", NULL},
826 { "hosts3", false }, // http://a,http://b -> http://a 835 false }, // all -> one
827 { "hosts4", true }, // http://a -> http://a,http://b 836 { "allhosts3", (const char*[]){NULL},
828 { "hosts5", false }, // http://a,b,c -> http://a,b,c + https://a,b,c 837 (const char*[]){NULL}, true }, // one -> all
829 { "hosts6", false }, // http://a.com -> http://a.com + http://a.co.uk 838 { "hosts1", (const char*[]){NULL},
830 { "permissions1", false }, // tabs -> tabs 839 (const char*[]){"http://www.google.com/", "http://www.reddit.com/", NULL},
831 { "permissions2", true }, // tabs -> tabs,bookmarks 840 false }, // http://a,http://b -> http://a,http://b
832 { "permissions3", true }, // http://a -> http://a,tabs 841 { "hosts2", (const char*[]){NULL},
833 { "permissions5", true }, // bookmarks -> bookmarks,history 842 (const char*[]){"http://www.google.com/", "http://www.reddit.com/", NULL},
843 false }, // http://a,http://b -> https://a,http://*.b
844 { "hosts3", (const char*[]){NULL},
845 (const char*[]){"http://www.google.com/", "http://www.reddit.com/", NULL},
846 false }, // http://a,http://b -> http://a
847 { "hosts4", (const char*[]){NULL},
848 (const char*[]){"http://www.google.com/", NULL},
849 true }, // http://a -> http://a,http://b
850 { "hosts5", (const char*[]){"tabs", "notifications", NULL},
851 (const char*[]){"http://*.example.com/", "http://*.example.com/*",
852 "http://*.example.co.uk/*", "http://*.example.com.au/*",
853 NULL},
854 false }, // http://a,b,c -> http://a,b,c + https://a,b,c
855 { "hosts6", (const char*[]){"tabs", "notifications", NULL},
856 (const char*[]){"http://*.example.com/", "http://*.example.com/*", NULL},
857 false }, // http://a.com -> http://a.com + http://a.co.uk
858 { "permissions1", (const char*[]){"tabs", NULL},
859 (const char*[]){NULL}, false }, // tabs -> tabs
860 { "permissions2", (const char*[]){"tabs", NULL},
861 (const char*[]){NULL}, true }, // tabs -> tabs,bookmarks
862 { "permissions3", (const char*[]){NULL},
863 (const char*[]){"http://*/*", NULL},
864 true }, // http://a -> http://a,tabs
865 { "permissions5", (const char*[]){"bookmarks", NULL},
866 (const char*[]){NULL}, true }, // bookmarks -> bookmarks,history
834 #if !defined(OS_CHROMEOS) // plugins aren't allowed in ChromeOS 867 #if !defined(OS_CHROMEOS) // plugins aren't allowed in ChromeOS
835 { "permissions4", false }, // plugin -> plugin,tabs 868 { "permissions4", (const char*[]){NULL},
836 { "plugin1", false }, // plugin -> plugin 869 (const char*[]){NULL}, false }, // plugin -> plugin,tabs
837 { "plugin2", false }, // plugin -> none 870 { "plugin1", (const char*[]){NULL},
838 { "plugin3", true }, // none -> plugin 871 (const char*[]){NULL}, false }, // plugin -> plugin
872 { "plugin2", (const char*[]){NULL},
873 (const char*[]){NULL}, false }, // plugin -> none
874 { "plugin3", (const char*[]){NULL},
875 (const char*[]){NULL}, true }, // none -> plugin
839 #endif 876 #endif
840 { "storage", false }, // none -> storage 877 { "storage", (const char*[]){NULL},
841 { "notifications", false } // none -> notifications 878 (const char*[]){NULL}, false }, // none -> storage
879 { "notifications", (const char*[]){NULL},
880 (const char*[]){NULL}, false } // none -> notifications
842 }; 881 };
843 882
844 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { 883 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) {
845 scoped_refptr<Extension> old_extension( 884 scoped_refptr<Extension> old_extension(
846 LoadManifest("allow_silent_upgrade", 885 LoadManifest("allow_silent_upgrade",
847 std::string(kTests[i].base_name) + "_old.json")); 886 std::string(kTests[i].base_name) + "_old.json"));
848 scoped_refptr<Extension> new_extension( 887 scoped_refptr<Extension> new_extension(
849 LoadManifest("allow_silent_upgrade", 888 LoadManifest("allow_silent_upgrade",
850 std::string(kTests[i].base_name) + "_new.json")); 889 std::string(kTests[i].base_name) + "_new.json"));
851 890
891 std::set<std::string> granted_apis;
892 for (size_t j = 0; kTests[i].granted_apis[j] != NULL; ++j)
893 granted_apis.insert(kTests[i].granted_apis[j]);
894
895 ExtensionExtent granted_hosts;
896 for (size_t j = 0; kTests[i].granted_hosts[j] != NULL; ++j)
897 AddPattern(&granted_hosts, kTests[i].granted_hosts[j]);
898
852 EXPECT_TRUE(old_extension.get()) << kTests[i].base_name << "_old.json"; 899 EXPECT_TRUE(old_extension.get()) << kTests[i].base_name << "_old.json";
853 EXPECT_TRUE(new_extension.get()) << kTests[i].base_name << "_new.json"; 900 EXPECT_TRUE(new_extension.get()) << kTests[i].base_name << "_new.json";
854 if (!old_extension.get() || !new_extension.get()) 901 if (!old_extension.get() || !new_extension.get())
855 continue; 902 continue;
856 903
857 EXPECT_EQ(kTests[i].expect_success, 904 EXPECT_EQ(kTests[i].expect_success,
858 Extension::IsPrivilegeIncrease(old_extension.get(), 905 Extension::IsPrivilegeIncrease(granted_apis, granted_hosts,
906 old_extension.get(),
859 new_extension.get())) 907 new_extension.get()))
860 << kTests[i].base_name; 908 << kTests[i].base_name;
861 } 909 }
862 } 910 }
863 911
864 TEST(ExtensionTest, PermissionMessages) { 912 TEST(ExtensionTest, PermissionMessages) {
865 // Ensure that all permissions that needs to show install UI actually have 913 // Ensure that all permissions that needs to show install UI actually have
866 // strings associated with them. 914 // strings associated with them.
867 915
868 std::set<std::string> skip; 916 std::set<std::string> skip;
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
1160 EXPECT_TRUE(Extension::GenerateId("test", &result)); 1208 EXPECT_TRUE(Extension::GenerateId("test", &result));
1161 EXPECT_EQ(result, "jpignaibiiemhngfjkcpokkamffknabf"); 1209 EXPECT_EQ(result, "jpignaibiiemhngfjkcpokkamffknabf");
1162 1210
1163 EXPECT_TRUE(Extension::GenerateId("_", &result)); 1211 EXPECT_TRUE(Extension::GenerateId("_", &result));
1164 EXPECT_EQ(result, "ncocknphbhhlhkikpnnlmbcnbgdempcd"); 1212 EXPECT_EQ(result, "ncocknphbhhlhkikpnnlmbcnbgdempcd");
1165 1213
1166 EXPECT_TRUE(Extension::GenerateId( 1214 EXPECT_TRUE(Extension::GenerateId(
1167 "this_string_is_longer_than_a_single_sha256_hash_digest", &result)); 1215 "this_string_is_longer_than_a_single_sha256_hash_digest", &result));
1168 EXPECT_EQ(result, "jimneklojkjdibfkgiiophfhjhbdgcfi"); 1216 EXPECT_EQ(result, "jimneklojkjdibfkgiiophfhjhbdgcfi");
1169 } 1217 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698