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

Side by Side Diff: chrome/browser/extensions/extensions_service_unittest.cc

Issue 4687005: Track permissions granted to extensions in prefs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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/browser/extensions/extensions_service_unittest.h" 5 #include "chrome/browser/extensions/extensions_service_unittest.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 // Extension ids used during testing. 65 // Extension ids used during testing.
66 const char* const all_zero = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 66 const char* const all_zero = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
67 const char* const zero_n_one = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"; 67 const char* const zero_n_one = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab";
68 const char* const good0 = "behllobkkfkfnphdnhnkndlbkcpglgmj"; 68 const char* const good0 = "behllobkkfkfnphdnhnkndlbkcpglgmj";
69 const char* const good1 = "hpiknbiabeeppbpihjehijgoemciehgk"; 69 const char* const good1 = "hpiknbiabeeppbpihjehijgoemciehgk";
70 const char* const good2 = "bjafgdebaacbbbecmhlhpofkepfkgcpa"; 70 const char* const good2 = "bjafgdebaacbbbecmhlhpofkepfkgcpa";
71 const char* const good_crx = "ldnnhddmnhbkjipkidpdiheffobcpfmf"; 71 const char* const good_crx = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
72 const char* const page_action = "obcimlgaoabeegjmmpldobjndiealpln"; 72 const char* const page_action = "obcimlgaoabeegjmmpldobjndiealpln";
73 const char* const theme_crx = "iamefpfkojoapidjnbafmgkgncegbkad"; 73 const char* const theme_crx = "iamefpfkojoapidjnbafmgkgncegbkad";
74 const char* const theme2_crx = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf"; 74 const char* const theme2_crx = "pjpgmfcmabopnnfonnhmdjglfpjjfkbf";
75 const char* const permissions_unknown_crx = "bcflfiohkjolmhmjjocpfbloomonhpkc";
75 76
76 struct ExtensionsOrder { 77 struct ExtensionsOrder {
77 bool operator()(const Extension* a, const Extension* b) { 78 bool operator()(const Extension* a, const Extension* b) {
78 return a->name() < b->name(); 79 return a->name() < b->name();
79 } 80 }
80 }; 81 };
81 82
82 static std::vector<std::string> GetErrors() { 83 static std::vector<std::string> GetErrors() {
83 const std::vector<std::string>* errors = 84 const std::vector<std::string>* errors =
84 ExtensionErrorReporter::GetInstance()->GetErrors(); 85 ExtensionErrorReporter::GetInstance()->GetErrors();
(...skipping 918 matching lines...) Expand 10 before | Expand all | Expand 10 after
1003 EXPECT_EQ(0u, errors.size()) << "There were errors: " 1004 EXPECT_EQ(0u, errors.size()) << "There were errors: "
1004 << JoinString(errors, ','); 1005 << JoinString(errors, ',');
1005 EXPECT_TRUE(service_->GetExtensionById(loaded_[0]->id(), false)) << 1006 EXPECT_TRUE(service_->GetExtensionById(loaded_[0]->id(), false)) <<
1006 path.value(); 1007 path.value();
1007 1008
1008 installed_ = NULL; 1009 installed_ = NULL;
1009 loaded_.clear(); 1010 loaded_.clear();
1010 ExtensionErrorReporter::GetInstance()->ClearErrors(); 1011 ExtensionErrorReporter::GetInstance()->ClearErrors();
1011 } 1012 }
1012 1013
1014 // This tests that the granted permissions preferences are correctly set when
1015 // installing an extension.
1016 TEST_F(ExtensionsServiceTest, GrantedPermissions) {
1017 InitializeEmptyExtensionsService();
1018
1019 FilePath path;
1020 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
1021 path = path.AppendASCII("extensions")
1022 .AppendASCII("permissions-unknown-v1.crx");
Aaron Boodman 2010/11/12 00:05:24 It is better to use unpacked extensions in tests s
jstritar 2010/11/19 21:38:36 Done.
1023 ASSERT_TRUE(file_util::PathExists(path));
1024
1025 InstallExtension(path, true);
Aaron Boodman 2010/11/12 00:05:24 Add a test to validate that there are no granted p
jstritar 2010/11/19 21:38:36 Now that I'm packing the extensions dynamically, I
1026
1027 ExtensionPrefs* prefs = service_->extension_prefs();
1028 const char* extension_id = permissions_unknown_crx;
1029
1030 // Verify that the valid API permissions have been recognized.
1031 std::set<std::string> known_api_perms;
1032 std::set<std::string> known_host_perms;
1033
1034 std::set<std::string> expected_api_perms;
1035 expected_api_perms.insert("tabs");
1036
1037 std::set<std::string> expected_host_perms;
1038 expected_host_perms.insert("http://*.google.com/*");
1039 expected_host_perms.insert("https://*.google.com/*");
1040
1041
1042 EXPECT_TRUE(prefs->GetGrantedPermissions(extension_id,
1043 &known_api_perms,
1044 &known_host_perms));
1045
1046 EXPECT_EQ(expected_api_perms, known_api_perms);
1047 EXPECT_EQ(expected_host_perms, known_host_perms);
1048 }
1049
1050 // Tests that the extension is disabled when an API permission is missing from
1051 // the extension's granted api permissions preference. (This simulates updating
1052 // the browser to a version which recognizes a new API permission).
1053 TEST_F(ExtensionsServiceTest, GrantedAPIPermissions) {
1054 FilePath extensions_path;
1055 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1056 extensions_path = extensions_path.AppendASCII("extensions");
1057
1058 FilePath source_install_dir = extensions_path
1059 .AppendASCII("permissions")
1060 .AppendASCII("Extensions");
1061 FilePath pref_path = extensions_path
1062 .AppendASCII("permissions")
1063 .AppendASCII("Preferences-unknown-api");
1064
1065 const char* extension_id = permissions_unknown_crx;
1066
1067 InitializeInstalledExtensionsService(pref_path, source_install_dir);
Aaron Boodman 2010/11/12 00:05:24 I think it would be a little bit better to test th
jstritar 2010/11/19 21:38:36 Done. I also combined these tests into one, so I d
1068 service_->Init();
1069 loop_.RunAllPending();
1070
1071 ExtensionPrefs* prefs = service_->extension_prefs();
1072
1073 ASSERT_EQ(0u, GetErrors().size());
1074 ASSERT_EQ(0u, loaded_.size());
1075
1076 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::DISABLED);
1077 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
1078 }
1079
1080 // Tests that the extension is disabled when a host permission is missing from
1081 // the extension's granted host permissions preference. (This simulates updating
1082 // the browser to a version which recognizes additional host permissions).
1083 TEST_F(ExtensionsServiceTest, GrantedHostPermissions) {
1084 FilePath extensions_path;
Aaron Boodman 2010/11/12 00:05:24 Same thing here.
jstritar 2010/11/19 21:38:36 Done.
1085 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1086 extensions_path = extensions_path.AppendASCII("extensions");
1087
1088 FilePath source_install_dir = extensions_path
1089 .AppendASCII("permissions")
1090 .AppendASCII("Extensions");
1091 FilePath pref_path = extensions_path
1092 .AppendASCII("permissions")
1093 .AppendASCII("Preferences-unknown-host");
1094
1095 const char* extension_id = permissions_unknown_crx;
1096
1097 InitializeInstalledExtensionsService(pref_path, source_install_dir);
1098 service_->Init();
1099 loop_.RunAllPending();
1100
1101 ExtensionPrefs* prefs = service_->extension_prefs();
1102
1103 ASSERT_EQ(0u, GetErrors().size());
1104 ASSERT_EQ(0u, loaded_.size());
1105
1106 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::DISABLED);
1107 ASSERT_TRUE(prefs->DidExtensionEscalatePermissions(extension_id));
1108 }
1109
1110 // Tests that the granted permissions preferences are initialized when
1111 // migrating from the old pref schema.
1112 TEST_F(ExtensionsServiceTest, GrantedPermissionsInitialization) {
1113 FilePath extensions_path;
1114 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
Aaron Boodman 2010/11/12 00:05:24 Same thing here.
jstritar 2010/11/19 21:38:36 Done.
1115 extensions_path = extensions_path.AppendASCII("extensions");
1116
1117 FilePath source_install_dir = extensions_path
1118 .AppendASCII("permissions")
1119 .AppendASCII("Extensions");
1120 FilePath pref_path = extensions_path
1121 .AppendASCII("permissions")
1122 .AppendASCII("Preferences-initialization");
1123
1124 const char* extension_id = permissions_unknown_crx;
1125
1126 InitializeInstalledExtensionsService(pref_path, source_install_dir);
1127 service_->Init();
1128 loop_.RunAllPending();
1129
1130 ExtensionPrefs* prefs = service_->extension_prefs();
1131
1132 ASSERT_EQ(0u, GetErrors().size());
1133 ASSERT_EQ(1u, loaded_.size());
1134
1135 ASSERT_TRUE(prefs->GetExtensionState(extension_id) == Extension::ENABLED);
1136 ASSERT_FALSE(prefs->DidExtensionEscalatePermissions(extension_id));
1137
1138 std::set<std::string> known_api_perms;
1139 std::set<std::string> known_host_perms;
1140
1141 std::set<std::string> expected_api_perms;
1142 expected_api_perms.insert("tabs");
1143
1144 std::set<std::string> expected_host_perms;
1145 expected_host_perms.insert("http://*.google.com/*");
1146 expected_host_perms.insert("https://*.google.com/*");
1147
1148
1149 EXPECT_TRUE(prefs->GetGrantedPermissions(extension_id,
1150 &known_api_perms,
1151 &known_host_perms));
1152
1153 EXPECT_EQ(expected_api_perms, known_api_perms);
1154 EXPECT_EQ(expected_host_perms, known_host_perms);
1155 }
1156
1013 // Test Packaging and installing an extension. 1157 // Test Packaging and installing an extension.
1014 TEST_F(ExtensionsServiceTest, PackExtension) { 1158 TEST_F(ExtensionsServiceTest, PackExtension) {
1015 InitializeEmptyExtensionsService(); 1159 InitializeEmptyExtensionsService();
1016 FilePath extensions_path; 1160 FilePath extensions_path;
1017 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); 1161 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
1018 extensions_path = extensions_path.AppendASCII("extensions"); 1162 extensions_path = extensions_path.AppendASCII("extensions");
1019 FilePath input_directory = extensions_path 1163 FilePath input_directory = extensions_path
1020 .AppendASCII("good") 1164 .AppendASCII("good")
1021 .AppendASCII("Extensions") 1165 .AppendASCII("Extensions")
1022 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") 1166 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
(...skipping 1730 matching lines...) Expand 10 before | Expand all | Expand 10 after
2753 // Component extensions shouldn't get recourded in the prefs. 2897 // Component extensions shouldn't get recourded in the prefs.
2754 ValidatePrefKeyCount(0); 2898 ValidatePrefKeyCount(0);
2755 2899
2756 // Reload all extensions, and make sure it comes back. 2900 // Reload all extensions, and make sure it comes back.
2757 std::string extension_id = service_->extensions()->at(0)->id(); 2901 std::string extension_id = service_->extensions()->at(0)->id();
2758 loaded_.clear(); 2902 loaded_.clear();
2759 service_->ReloadExtensions(); 2903 service_->ReloadExtensions();
2760 ASSERT_EQ(1u, service_->extensions()->size()); 2904 ASSERT_EQ(1u, service_->extensions()->size());
2761 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id()); 2905 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id());
2762 } 2906 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698