OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extension_service_unittest.h" | 5 #include "chrome/browser/extensions/extension_service_unittest.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 // VisitRegisteredExtension(), which must be a const method to inherit | 202 // VisitRegisteredExtension(), which must be a const method to inherit |
203 // from the class being mocked. | 203 // from the class being mocked. |
204 mutable int visit_count_; | 204 mutable int visit_count_; |
205 | 205 |
206 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider); | 206 DISALLOW_COPY_AND_ASSIGN(MockExtensionProvider); |
207 }; | 207 }; |
208 | 208 |
209 class MockProviderVisitor | 209 class MockProviderVisitor |
210 : public ExternalExtensionProviderInterface::VisitorInterface { | 210 : public ExternalExtensionProviderInterface::VisitorInterface { |
211 public: | 211 public: |
212 MockProviderVisitor() : ids_found_(0) { | 212 |
| 213 // The provider will return |fake_base_path| from |
| 214 // GetBaseCrxFilePath(). User can test the behavior with |
| 215 // and without an empty path using this parameter. |
| 216 explicit MockProviderVisitor(FilePath fake_base_path) |
| 217 : ids_found_(0), |
| 218 fake_base_path_(fake_base_path) { |
213 } | 219 } |
214 | 220 |
215 int Visit(const std::string& json_data) { | 221 int Visit(const std::string& json_data) { |
216 // Give the test json file to the provider for parsing. | 222 // Give the test json file to the provider for parsing. |
217 provider_.reset(new ExternalExtensionProviderImpl( | 223 provider_.reset(new ExternalExtensionProviderImpl( |
218 this, | 224 this, |
219 new ExternalTestingExtensionLoader(json_data), | 225 new ExternalTestingExtensionLoader(json_data, fake_base_path_), |
220 Extension::EXTERNAL_PREF, | 226 Extension::EXTERNAL_PREF, |
221 Extension::EXTERNAL_PREF_DOWNLOAD)); | 227 Extension::EXTERNAL_PREF_DOWNLOAD)); |
222 | 228 |
223 // We also parse the file into a dictionary to compare what we get back | 229 // We also parse the file into a dictionary to compare what we get back |
224 // from the provider. | 230 // from the provider. |
225 JSONStringValueSerializer serializer(json_data); | 231 JSONStringValueSerializer serializer(json_data); |
226 Value* json_value = serializer.Deserialize(NULL, NULL); | 232 Value* json_value = serializer.Deserialize(NULL, NULL); |
227 | 233 |
228 if (!json_value || !json_value->IsType(Value::TYPE_DICTIONARY)) { | 234 if (!json_value || !json_value->IsType(Value::TYPE_DICTIONARY)) { |
229 NOTREACHED() << "Unable to deserialize json data"; | 235 NOTREACHED() << "Unable to deserialize json data"; |
(...skipping 17 matching lines...) Expand all Loading... |
247 const FilePath& path, | 253 const FilePath& path, |
248 Extension::Location unused) { | 254 Extension::Location unused) { |
249 ++ids_found_; | 255 ++ids_found_; |
250 DictionaryValue* pref; | 256 DictionaryValue* pref; |
251 // This tests is to make sure that the provider only notifies us of the | 257 // This tests is to make sure that the provider only notifies us of the |
252 // values we gave it. So if the id we doesn't exist in our internal | 258 // values we gave it. So if the id we doesn't exist in our internal |
253 // dictionary then something is wrong. | 259 // dictionary then something is wrong. |
254 EXPECT_TRUE(prefs_->GetDictionary(id, &pref)) | 260 EXPECT_TRUE(prefs_->GetDictionary(id, &pref)) |
255 << "Got back ID (" << id.c_str() << ") we weren't expecting"; | 261 << "Got back ID (" << id.c_str() << ") we weren't expecting"; |
256 | 262 |
| 263 EXPECT_TRUE(path.IsAbsolute()); |
| 264 if (!fake_base_path_.empty()) |
| 265 EXPECT_TRUE(fake_base_path_.IsParent(path)); |
| 266 |
257 if (pref) { | 267 if (pref) { |
258 EXPECT_TRUE(provider_->HasExtension(id)); | 268 EXPECT_TRUE(provider_->HasExtension(id)); |
259 | 269 |
260 // Ask provider if the extension we got back is registered. | 270 // Ask provider if the extension we got back is registered. |
261 Extension::Location location = Extension::INVALID; | 271 Extension::Location location = Extension::INVALID; |
262 scoped_ptr<Version> v1; | 272 scoped_ptr<Version> v1; |
263 FilePath crx_path; | 273 FilePath crx_path; |
264 | 274 |
265 EXPECT_TRUE(provider_->GetExtensionDetails(id, NULL, &v1)); | 275 EXPECT_TRUE(provider_->GetExtensionDetails(id, NULL, &v1)); |
266 EXPECT_STREQ(version->GetString().c_str(), v1->GetString().c_str()); | 276 EXPECT_STREQ(version->GetString().c_str(), v1->GetString().c_str()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 prefs_->Remove(id, NULL); | 312 prefs_->Remove(id, NULL); |
303 } | 313 } |
304 } | 314 } |
305 | 315 |
306 virtual void OnExternalProviderReady() { | 316 virtual void OnExternalProviderReady() { |
307 EXPECT_TRUE(provider_->IsReady()); | 317 EXPECT_TRUE(provider_->IsReady()); |
308 } | 318 } |
309 | 319 |
310 private: | 320 private: |
311 int ids_found_; | 321 int ids_found_; |
312 | 322 FilePath fake_base_path_; |
313 scoped_ptr<ExternalExtensionProviderImpl> provider_; | 323 scoped_ptr<ExternalExtensionProviderImpl> provider_; |
314 scoped_ptr<DictionaryValue> prefs_; | 324 scoped_ptr<DictionaryValue> prefs_; |
315 | 325 |
316 DISALLOW_COPY_AND_ASSIGN(MockProviderVisitor); | 326 DISALLOW_COPY_AND_ASSIGN(MockProviderVisitor); |
317 }; | 327 }; |
318 | 328 |
319 class ExtensionTestingProfile : public TestingProfile { | 329 class ExtensionTestingProfile : public TestingProfile { |
320 public: | 330 public: |
321 ExtensionTestingProfile() : service_(NULL) { | 331 ExtensionTestingProfile() : service_(NULL) { |
322 } | 332 } |
(...skipping 2674 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2997 set_extensions_enabled(true); | 3007 set_extensions_enabled(true); |
2998 service_->ReloadExtensions(); | 3008 service_->ReloadExtensions(); |
2999 loop_.RunAllPending(); | 3009 loop_.RunAllPending(); |
3000 | 3010 |
3001 ASSERT_EQ(0u, GetErrors().size()); | 3011 ASSERT_EQ(0u, GetErrors().size()); |
3002 ASSERT_EQ(0u, loaded_.size()); | 3012 ASSERT_EQ(0u, loaded_.size()); |
3003 } | 3013 } |
3004 | 3014 |
3005 TEST_F(ExtensionServiceTest, ExternalPrefProvider) { | 3015 TEST_F(ExtensionServiceTest, ExternalPrefProvider) { |
3006 InitializeEmptyExtensionService(); | 3016 InitializeEmptyExtensionService(); |
| 3017 |
| 3018 // Test some valid extension records. |
| 3019 // Set a base path to avoid erroring out on relative paths. |
| 3020 // Paths starting with // are absolute on every platform we support. |
| 3021 FilePath base_path(FILE_PATH_LITERAL("//base/path")); |
| 3022 ASSERT_TRUE(base_path.IsAbsolute()); |
| 3023 MockProviderVisitor visitor(base_path); |
3007 std::string json_data = | 3024 std::string json_data = |
3008 "{" | 3025 "{" |
3009 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {" | 3026 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {" |
3010 " \"external_crx\": \"RandomExtension.crx\"," | 3027 " \"external_crx\": \"RandomExtension.crx\"," |
3011 " \"external_version\": \"1.0\"" | 3028 " \"external_version\": \"1.0\"" |
3012 " }," | 3029 " }," |
3013 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {" | 3030 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {" |
3014 " \"external_crx\": \"RandomExtension2.crx\"," | 3031 " \"external_crx\": \"RandomExtension2.crx\"," |
3015 " \"external_version\": \"2.0\"" | 3032 " \"external_version\": \"2.0\"" |
3016 " }," | 3033 " }," |
3017 " \"cccccccccccccccccccccccccccccccc\": {" | 3034 " \"cccccccccccccccccccccccccccccccc\": {" |
3018 " \"external_update_url\": \"http:\\\\foo.com/update\"" | 3035 " \"external_update_url\": \"http:\\\\foo.com/update\"" |
3019 " }" | 3036 " }" |
3020 "}"; | 3037 "}"; |
3021 | 3038 EXPECT_EQ(3, visitor.Visit(json_data)); |
3022 MockProviderVisitor visitor; | |
3023 | 3039 |
3024 // Simulate an external_extensions.json file that contains seven invalid | 3040 // Simulate an external_extensions.json file that contains seven invalid |
3025 // extensions: | 3041 // records: |
3026 // - One that is missing the 'external_crx' key. | 3042 // - One that is missing the 'external_crx' key. |
3027 // - One that is missing the 'external_version' key. | 3043 // - One that is missing the 'external_version' key. |
3028 // - One that is specifying .. in the path. | 3044 // - One that is specifying .. in the path. |
3029 // - One that specifies both a file and update URL. | 3045 // - One that specifies both a file and update URL. |
3030 // - One that specifies no file or update URL. | 3046 // - One that specifies no file or update URL. |
3031 // - One that has an update URL that is not well formed. | 3047 // - One that has an update URL that is not well formed. |
3032 // - One that contains a malformed version. | 3048 // - One that contains a malformed version. |
3033 // - One that has an invalid id. | 3049 // - One that has an invalid id. |
3034 // - One that has a non-dictionary value. | 3050 // - One that has a non-dictionary value. |
3035 // The final extension is valid, and we check that it is read to make sure | 3051 // The final extension is valid, and we check that it is read to make sure |
(...skipping 25 matching lines...) Expand all Loading... |
3061 " \"external_version\": \"This is not a valid version!\"" | 3077 " \"external_version\": \"This is not a valid version!\"" |
3062 " }," | 3078 " }," |
3063 " \"This is not a valid id!\": {}," | 3079 " \"This is not a valid id!\": {}," |
3064 " \"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\": true," | 3080 " \"hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh\": true," |
3065 " \"pppppppppppppppppppppppppppppppp\": {" | 3081 " \"pppppppppppppppppppppppppppppppp\": {" |
3066 " \"external_crx\": \"RandomValidExtension.crx\"," | 3082 " \"external_crx\": \"RandomValidExtension.crx\"," |
3067 " \"external_version\": \"1.0\"" | 3083 " \"external_version\": \"1.0\"" |
3068 " }" | 3084 " }" |
3069 "}"; | 3085 "}"; |
3070 EXPECT_EQ(1, visitor.Visit(json_data)); | 3086 EXPECT_EQ(1, visitor.Visit(json_data)); |
| 3087 |
| 3088 // Check that if a base path is not provided, use of a relative |
| 3089 // path fails. |
| 3090 FilePath empty; |
| 3091 MockProviderVisitor visitor_no_relative_paths(empty); |
| 3092 |
| 3093 // Use absolute paths. Expect success. |
| 3094 json_data = |
| 3095 "{" |
| 3096 " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\": {" |
| 3097 " \"external_crx\": \"//RandomExtension1.crx\"," |
| 3098 " \"external_version\": \"3.0\"" |
| 3099 " }," |
| 3100 " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\": {" |
| 3101 " \"external_crx\": \"//path/to/RandomExtension2.crx\"," |
| 3102 " \"external_version\": \"3.0\"" |
| 3103 " }" |
| 3104 "}"; |
| 3105 EXPECT_EQ(2, visitor_no_relative_paths.Visit(json_data)); |
| 3106 |
| 3107 // Use a relative path. Expect that it will error out. |
| 3108 json_data = |
| 3109 "{" |
| 3110 " \"cccccccccccccccccccccccccccccccc\": {" |
| 3111 " \"external_crx\": \"RandomExtension2.crx\"," |
| 3112 " \"external_version\": \"3.0\"" |
| 3113 " }" |
| 3114 "}"; |
| 3115 EXPECT_EQ(0, visitor_no_relative_paths.Visit(json_data)); |
3071 } | 3116 } |
3072 | 3117 |
3073 // Test loading good extensions from the profile directory. | 3118 // Test loading good extensions from the profile directory. |
3074 TEST_F(ExtensionServiceTest, LoadAndRelocalizeExtensions) { | 3119 TEST_F(ExtensionServiceTest, LoadAndRelocalizeExtensions) { |
3075 // Initialize the test dir with a good Preferences/extensions. | 3120 // Initialize the test dir with a good Preferences/extensions. |
3076 FilePath source_install_dir; | 3121 FilePath source_install_dir; |
3077 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_install_dir)); | 3122 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_install_dir)); |
3078 source_install_dir = source_install_dir | 3123 source_install_dir = source_install_dir |
3079 .AppendASCII("extensions") | 3124 .AppendASCII("extensions") |
3080 .AppendASCII("l10n"); | 3125 .AppendASCII("l10n"); |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3285 // Component extensions shouldn't get recourded in the prefs. | 3330 // Component extensions shouldn't get recourded in the prefs. |
3286 ValidatePrefKeyCount(0); | 3331 ValidatePrefKeyCount(0); |
3287 | 3332 |
3288 // Reload all extensions, and make sure it comes back. | 3333 // Reload all extensions, and make sure it comes back. |
3289 std::string extension_id = service_->extensions()->at(0)->id(); | 3334 std::string extension_id = service_->extensions()->at(0)->id(); |
3290 loaded_.clear(); | 3335 loaded_.clear(); |
3291 service_->ReloadExtensions(); | 3336 service_->ReloadExtensions(); |
3292 ASSERT_EQ(1u, service_->extensions()->size()); | 3337 ASSERT_EQ(1u, service_->extensions()->size()); |
3293 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id()); | 3338 EXPECT_EQ(extension_id, service_->extensions()->at(0)->id()); |
3294 } | 3339 } |
OLD | NEW |