| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/values.h" | |
| 10 #include "base/version.h" | |
| 11 #include "chrome/browser/extensions/external_extension_provider_impl.h" | |
| 12 #include "chrome/browser/extensions/external_extension_provider_interface.h" | |
| 13 #include "chrome/browser/extensions/external_policy_extension_loader.h" | |
| 14 #include "chrome/common/extensions/extension.h" | |
| 15 #include "chrome/common/pref_names.h" | |
| 16 #include "chrome/test/base/testing_pref_service.h" | |
| 17 #include "chrome/test/base/testing_profile.h" | |
| 18 #include "content/public/test/test_browser_thread.h" | |
| 19 #include "testing/gtest/include/gtest/gtest.h" | |
| 20 | |
| 21 using content::BrowserThread; | |
| 22 using extensions::Extension; | |
| 23 | |
| 24 class ExternalPolicyExtensionProviderTest : public testing::Test { | |
| 25 public: | |
| 26 ExternalPolicyExtensionProviderTest() | |
| 27 : loop_(MessageLoop::TYPE_IO), | |
| 28 ui_thread_(BrowserThread::UI, &loop_) { | |
| 29 } | |
| 30 | |
| 31 virtual ~ExternalPolicyExtensionProviderTest() {} | |
| 32 | |
| 33 private: | |
| 34 // We need these to satisfy BrowserThread::CurrentlyOn(BrowserThread::UI) | |
| 35 // checks in ExternalExtensionProviderImpl. | |
| 36 MessageLoop loop_; | |
| 37 content::TestBrowserThread ui_thread_; | |
| 38 }; | |
| 39 | |
| 40 class MockExternalPolicyExtensionProviderVisitor | |
| 41 : public ExternalExtensionProviderInterface::VisitorInterface { | |
| 42 public: | |
| 43 MockExternalPolicyExtensionProviderVisitor() { | |
| 44 } | |
| 45 | |
| 46 // Initialize a provider with |policy_forcelist|, and check that it parses | |
| 47 // exactly those extensions, that are specified in |policy_validlist|. | |
| 48 void Visit(ListValue* policy_forcelist, | |
| 49 ListValue* policy_validlist, | |
| 50 const std::set<std::string>& ignore_list) { | |
| 51 profile_.reset(new TestingProfile); | |
| 52 profile_->GetTestingPrefService()->SetManagedPref( | |
| 53 prefs::kExtensionInstallForceList, | |
| 54 policy_forcelist->DeepCopy()); | |
| 55 provider_.reset(new ExternalExtensionProviderImpl( | |
| 56 this, | |
| 57 new ExternalPolicyExtensionLoader(profile_.get()), | |
| 58 Extension::INVALID, | |
| 59 Extension::EXTERNAL_POLICY_DOWNLOAD, | |
| 60 Extension::NO_FLAGS)); | |
| 61 | |
| 62 // Extensions will be removed from this list as they visited, | |
| 63 // so it should be emptied by the end. | |
| 64 remaining_extensions = policy_validlist; | |
| 65 provider_->VisitRegisteredExtension(); | |
| 66 EXPECT_EQ(0u, remaining_extensions->GetSize()); | |
| 67 } | |
| 68 | |
| 69 virtual bool OnExternalExtensionFileFound(const std::string& id, | |
| 70 const Version* version, | |
| 71 const FilePath& path, | |
| 72 Extension::Location unused, | |
| 73 int unused2, | |
| 74 bool unused3) { | |
| 75 ADD_FAILURE() << "There should be no external extensions from files."; | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 virtual bool OnExternalExtensionUpdateUrlFound( | |
| 80 const std::string& id, const GURL& update_url, | |
| 81 Extension::Location location) { | |
| 82 // Extension has the correct location. | |
| 83 EXPECT_EQ(Extension::EXTERNAL_POLICY_DOWNLOAD, location); | |
| 84 | |
| 85 // Provider returns the correct location when asked. | |
| 86 Extension::Location location1; | |
| 87 scoped_ptr<Version> version1; | |
| 88 provider_->GetExtensionDetails(id, &location1, &version1); | |
| 89 EXPECT_EQ(Extension::EXTERNAL_POLICY_DOWNLOAD, location1); | |
| 90 EXPECT_FALSE(version1.get()); | |
| 91 | |
| 92 // Remove the extension from our list. | |
| 93 StringValue ext_str(id + ";" + update_url.spec()); | |
| 94 EXPECT_NE(false, remaining_extensions->Remove(ext_str, NULL)); | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 virtual void OnExternalProviderReady( | |
| 99 const ExternalExtensionProviderInterface* provider) { | |
| 100 EXPECT_EQ(provider, provider_.get()); | |
| 101 EXPECT_TRUE(provider->IsReady()); | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 ListValue* remaining_extensions; | |
| 106 | |
| 107 scoped_ptr<TestingProfile> profile_; | |
| 108 | |
| 109 scoped_ptr<ExternalExtensionProviderImpl> provider_; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(MockExternalPolicyExtensionProviderVisitor); | |
| 112 }; | |
| 113 | |
| 114 TEST_F(ExternalPolicyExtensionProviderTest, PolicyIsParsed) { | |
| 115 ListValue forced_extensions; | |
| 116 forced_extensions.Append(Value::CreateStringValue( | |
| 117 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;http://www.example.com/crx?a=5;b=6")); | |
| 118 forced_extensions.Append(Value::CreateStringValue( | |
| 119 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;" | |
| 120 "https://clients2.google.com/service/update2/crx")); | |
| 121 | |
| 122 MockExternalPolicyExtensionProviderVisitor mv; | |
| 123 std::set<std::string> empty; | |
| 124 mv.Visit(&forced_extensions, &forced_extensions, empty); | |
| 125 } | |
| 126 | |
| 127 TEST_F(ExternalPolicyExtensionProviderTest, InvalidPolicyIsNotParsed) { | |
| 128 ListValue forced_extensions, valid_extensions; | |
| 129 StringValue valid( | |
| 130 "cccccccccccccccccccccccccccccccc;http://www.example.com/crx"); | |
| 131 valid_extensions.Append(valid.DeepCopy()); | |
| 132 forced_extensions.Append(valid.DeepCopy()); | |
| 133 // Add invalid strings: | |
| 134 forced_extensions.Append(Value::CreateStringValue("")); | |
| 135 forced_extensions.Append(Value::CreateStringValue(";")); | |
| 136 forced_extensions.Append(Value::CreateStringValue(";;")); | |
| 137 forced_extensions.Append(Value::CreateStringValue( | |
| 138 ";http://www.example.com/crx")); | |
| 139 forced_extensions.Append(Value::CreateStringValue( | |
| 140 ";aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | |
| 141 forced_extensions.Append(Value::CreateStringValue( | |
| 142 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;")); | |
| 143 forced_extensions.Append(Value::CreateStringValue( | |
| 144 "http://www.example.com/crx;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | |
| 145 forced_extensions.Append(Value::CreateStringValue( | |
| 146 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;http#//www.example.com/crx")); | |
| 147 forced_extensions.Append(Value::CreateStringValue( | |
| 148 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | |
| 149 forced_extensions.Append(Value::CreateStringValue( | |
| 150 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahttp#//www.example.com/crx")); | |
| 151 | |
| 152 MockExternalPolicyExtensionProviderVisitor mv; | |
| 153 std::set<std::string> empty; | |
| 154 mv.Visit(&forced_extensions, &valid_extensions, empty); | |
| 155 } | |
| OLD | NEW |