external_policy_loader_unittest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 <set>
6#include <string>
7
8#include "base/logging.h"
9#include "base/message_loop.h"
10#include "base/values.h"
11#include "base/version.h"
12#include "chrome/browser/extensions/external_policy_loader.h"
13#include "chrome/browser/extensions/external_provider_impl.h"
14#include "chrome/browser/extensions/external_provider_interface.h"
15#include "chrome/common/extensions/extension.h"
16#include "chrome/common/extensions/manifest.h"
17#include "chrome/common/pref_names.h"
18#include "chrome/test/base/testing_pref_service_syncable.h"
19#include "chrome/test/base/testing_profile.h"
20#include "content/public/test/test_browser_thread.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23using content::BrowserThread;
24
25namespace extensions {
26
27class ExternalPolicyLoaderTest : public testing::Test {
28 public:
29  ExternalPolicyLoaderTest()
30      : loop_(MessageLoop::TYPE_IO),
31        ui_thread_(BrowserThread::UI, &loop_) {
32  }
33
34  virtual ~ExternalPolicyLoaderTest() {}
35
36 private:
37  // We need these to satisfy BrowserThread::CurrentlyOn(BrowserThread::UI)
38  // checks in ExternalProviderImpl.
39  MessageLoop loop_;
40  content::TestBrowserThread ui_thread_;
41};
42
43class MockExternalPolicyProviderVisitor
44    : public ExternalProviderInterface::VisitorInterface {
45 public:
46  MockExternalPolicyProviderVisitor() {
47  }
48
49  // Initialize a provider with |policy_forcelist|, and check that it installs
50  // exactly the extensions specified in |expected_extensions|.
51  void Visit(const base::DictionaryValue& policy_forcelist,
52             const std::set<std::string>& expected_extensions) {
53    profile_.reset(new TestingProfile);
54    profile_->GetTestingPrefService()->SetManagedPref(
55        prefs::kExtensionInstallForceList,
56        policy_forcelist.DeepCopy());
57    provider_.reset(new ExternalProviderImpl(
58        this,
59        new ExternalPolicyLoader(profile_.get()),
60        Manifest::INVALID_LOCATION,
61        Manifest::EXTERNAL_POLICY_DOWNLOAD,
62        Extension::NO_FLAGS));
63
64    // Extensions will be removed from this list as they visited,
65    // so it should be emptied by the end.
66    expected_extensions_ = expected_extensions;
67    provider_->VisitRegisteredExtension();
68    EXPECT_TRUE(expected_extensions_.empty());
69  }
70
71  virtual bool OnExternalExtensionFileFound(const std::string& id,
72                                            const Version* version,
73                                            const base::FilePath& path,
74                                            Manifest::Location unused,
75                                            int unused2,
76                                            bool unused3) OVERRIDE {
77    ADD_FAILURE() << "There should be no external extensions from files.";
78    return false;
79  }
80
81  virtual bool OnExternalExtensionUpdateUrlFound(
82      const std::string& id, const GURL& update_url,
83      Manifest::Location location) OVERRIDE {
84    // Extension has the correct location.
85    EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, location);
86
87    // Provider returns the correct location when asked.
88    Manifest::Location location1;
89    scoped_ptr<Version> version1;
90    provider_->GetExtensionDetails(id, &location1, &version1);
91    EXPECT_EQ(Manifest::EXTERNAL_POLICY_DOWNLOAD, location1);
92    EXPECT_FALSE(version1.get());
93
94    // Remove the extension from our list.
95    EXPECT_EQ(1U, expected_extensions_.erase(id));
96    return true;
97  }
98
99  virtual void OnExternalProviderReady(
100      const ExternalProviderInterface* provider) OVERRIDE {
101    EXPECT_EQ(provider, provider_.get());
102    EXPECT_TRUE(provider->IsReady());
103  }
104
105 private:
106  std::set<std::string> expected_extensions_;
107
108  scoped_ptr<TestingProfile> profile_;
109
110  scoped_ptr<ExternalProviderImpl> provider_;
111
112  DISALLOW_COPY_AND_ASSIGN(MockExternalPolicyProviderVisitor);
113};
114
115TEST_F(ExternalPolicyLoaderTest, PolicyIsParsed) {
116  base::DictionaryValue forced_extensions;
117  std::set<std::string> expected_extensions;
118  extensions::ExternalPolicyLoader::AddExtension(
119      &forced_extensions, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
120      "http://www.example.com/crx?a=5;b=6");
121  expected_extensions.insert("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
122  extensions::ExternalPolicyLoader::AddExtension(
123      &forced_extensions, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
124      "https://clients2.google.com/service/update2/crx");
125  expected_extensions.insert("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
126
127  MockExternalPolicyProviderVisitor mv;
128  mv.Visit(forced_extensions, expected_extensions);
129}
130
131TEST_F(ExternalPolicyLoaderTest, InvalidEntriesIgnored) {
132  base::DictionaryValue forced_extensions;
133  std::set<std::string> expected_extensions;
134
135  extensions::ExternalPolicyLoader::AddExtension(
136      &forced_extensions, "cccccccccccccccccccccccccccccccc",
137      "http://www.example.com/crx");
138  expected_extensions.insert("cccccccccccccccccccccccccccccccc");
139
140  // Add invalid entries.
141  forced_extensions.SetString("invalid", "http://www.example.com/crx");
142  forced_extensions.SetString("dddddddddddddddddddddddddddddddd",
143                              std::string());
144  forced_extensions.SetString("invalid", "bad");
145
146  MockExternalPolicyProviderVisitor mv;
147  mv.Visit(forced_extensions, expected_extensions);
148}
149
150}  // namespace extensions
151