summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/configuration_policy_provider_win_unittest.cc
blob: 39c4dce706de9a9f2edde7193984d821269b3413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <gtest/gtest.h>

#include <windows.h>

#include "base/scoped_ptr.h"
#include "base/stl_util-inl.h"
#include "base/string_number_conversions.h"
#include "base/string_piece.h"
#include "base/utf_string_conversions.h"
#include "base/win/registry.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/policy/configuration_policy_provider_win.h"
#include "chrome/browser/policy/mock_configuration_policy_store.h"
#include "chrome/common/policy_constants.h"
#include "chrome/common/pref_names.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::win::RegKey;

namespace policy {

const wchar_t kUnitTestRegistrySubKey[] = L"SOFTWARE\\Chromium Unit Tests";
const wchar_t kUnitTestMachineOverrideSubKey[] =
    L"SOFTWARE\\Chromium Unit Tests\\HKLM Override";
const wchar_t kUnitTestUserOverrideSubKey[] =
    L"SOFTWARE\\Chromium Unit Tests\\HKCU Override";

namespace {

// Holds policy type, corresponding policy name string and a valid value for use
// in parametrized value tests.
class PolicyTestParams {
 public:
  // Assumes ownership of |hklm_value| and |hkcu_value|.
  PolicyTestParams(ConfigurationPolicyType type,
                   const char* policy_name,
                   Value* hklm_value,
                   Value* hkcu_value)
      : type_(type),
        policy_name_(policy_name),
        hklm_value_(hklm_value),
        hkcu_value_(hkcu_value) {}

  // testing::TestWithParam does copy the parameters, so provide copy
  // constructor and assignment operator.
  PolicyTestParams(const PolicyTestParams& other)
      : type_(other.type_),
        policy_name_(other.policy_name_),
        hklm_value_(other.hklm_value_->DeepCopy()),
        hkcu_value_(other.hkcu_value_->DeepCopy()) {}

  const PolicyTestParams& operator=(PolicyTestParams other) {
    swap(other);
    return *this;
  }

  void swap(PolicyTestParams& other) {
    std::swap(type_, other.type_);
    std::swap(policy_name_, other.policy_name_);
    hklm_value_.swap(other.hklm_value_);
    hkcu_value_.swap(other.hkcu_value_);
  }

  ConfigurationPolicyType type() const { return type_; }
  const char* policy_name() const { return policy_name_; }
  const Value* hklm_value() const { return hklm_value_.get(); }
  const Value* hkcu_value() const { return hkcu_value_.get(); }

  // Factory methods for different value types.
  static PolicyTestParams ForStringPolicy(
      ConfigurationPolicyType type,
      const char* policy_name) {
    return PolicyTestParams(type,
                            policy_name,
                            Value::CreateStringValue("string_a"),
                            Value::CreateStringValue("string_b"));
  }
  static PolicyTestParams ForBooleanPolicy(
      ConfigurationPolicyType type,
      const char* policy_name) {
    return PolicyTestParams(type,
                            policy_name,
                            Value::CreateBooleanValue(true),
                            Value::CreateBooleanValue(false));
  }
  static PolicyTestParams ForIntegerPolicy(
      ConfigurationPolicyType type,
      const char* policy_name) {
    return PolicyTestParams(type,
                            policy_name,
                            Value::CreateIntegerValue(42),
                            Value::CreateIntegerValue(17));
  }
  static PolicyTestParams ForListPolicy(
      ConfigurationPolicyType type,
      const char* policy_name) {
    ListValue* hklm_value = new ListValue;
    hklm_value->Set(0U, Value::CreateStringValue("It's a plane!"));
    ListValue* hkcu_value = new ListValue;
    hkcu_value->Set(0U, Value::CreateStringValue("It's a bird!"));
    hkcu_value->Set(0U, Value::CreateStringValue("It's a flying carpet!"));
    return PolicyTestParams(type, policy_name, hklm_value, hkcu_value);
  }

 private:
  ConfigurationPolicyType type_;
  const char* policy_name_;
  scoped_ptr<Value> hklm_value_;
  scoped_ptr<Value> hkcu_value_;
};

}  // namespace

// This test class provides sandboxing and mocking for the parts of the
// Windows Registry implementing Group Policy. The |SetUp| method prepares
// two temporary sandbox keys in |kUnitTestRegistrySubKey|, one for HKLM and one
// for HKCU. A test's calls to the registry are redirected by Windows to these
// sandboxes, allowing the tests to manipulate and access policy as if it
// were active, but without actually changing the parts of the Registry that
// are managed by Group Policy.
class ConfigurationPolicyProviderWinTest
    : public testing::TestWithParam<PolicyTestParams> {
 public:
  ConfigurationPolicyProviderWinTest();

  // testing::Test method overrides:
  virtual void SetUp();
  virtual void TearDown();

  void ActivateOverrides();
  void DeactivateOverrides();

  // Deletes the registry key created during the tests.
  void DeleteRegistrySandbox();

  // Write a string value to the registry.
  void WriteString(HKEY hive, const char* name, const wchar_t* value);
  // Write a DWORD value to the registry.
  void WriteDWORD(HKEY hive, const char* name, DWORD value);

  // Write the given value to the registry.
  void WriteValue(HKEY hive, const char* name, const Value* value);
  // Write a value that is not compatible with the given |value|.
  void WriteInvalidValue(HKEY hive, const char* name, const Value* value);

 protected:
  scoped_ptr<MockConfigurationPolicyStore> store_;
  scoped_ptr<ConfigurationPolicyProviderWin> provider_;

 private:
  // A message loop must be declared and instantiated for these tests,
  // because Windows policy provider create WaitableEvents and
  // ObjectWatchers that require the tests to have a MessageLoop associated
  // with the thread executing the tests.
  MessageLoop loop_;

  // Keys are created for the lifetime of a test to contain
  // the sandboxed HKCU and HKLM hives, respectively.
  RegKey temp_hkcu_hive_key_;
  RegKey temp_hklm_hive_key_;
};

ConfigurationPolicyProviderWinTest::ConfigurationPolicyProviderWinTest()
    : temp_hklm_hive_key_(HKEY_CURRENT_USER, kUnitTestMachineOverrideSubKey,
                          KEY_READ),
      temp_hkcu_hive_key_(HKEY_CURRENT_USER, kUnitTestUserOverrideSubKey,
                          KEY_READ) {
}

void ConfigurationPolicyProviderWinTest::SetUp() {
  // Cleanup any remnants of previous tests.
  DeleteRegistrySandbox();

  // Create the subkeys to hold the overridden HKLM and HKCU
  // policy settings.
  temp_hklm_hive_key_.Create(HKEY_CURRENT_USER,
                             kUnitTestMachineOverrideSubKey,
                             KEY_ALL_ACCESS);
  temp_hkcu_hive_key_.Create(HKEY_CURRENT_USER,
                             kUnitTestUserOverrideSubKey,
                             KEY_ALL_ACCESS);

  ActivateOverrides();

  store_.reset(new MockConfigurationPolicyStore);
  provider_.reset(new ConfigurationPolicyProviderWin(
      ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList()));
}

void ConfigurationPolicyProviderWinTest::TearDown() {
  DeactivateOverrides();
  DeleteRegistrySandbox();
}

void ConfigurationPolicyProviderWinTest::ActivateOverrides() {
  HRESULT result = RegOverridePredefKey(HKEY_LOCAL_MACHINE,
                                        temp_hklm_hive_key_.Handle());
  EXPECT_EQ(ERROR_SUCCESS, result);
  result = RegOverridePredefKey(HKEY_CURRENT_USER,
                                temp_hkcu_hive_key_.Handle());
  EXPECT_EQ(ERROR_SUCCESS, result);
}

void ConfigurationPolicyProviderWinTest::DeactivateOverrides() {
  uint32 result = RegOverridePredefKey(HKEY_LOCAL_MACHINE, 0);
  EXPECT_EQ(ERROR_SUCCESS, result);
  result = RegOverridePredefKey(HKEY_CURRENT_USER, 0);
  EXPECT_EQ(ERROR_SUCCESS, result);
}

void ConfigurationPolicyProviderWinTest::DeleteRegistrySandbox() {
  temp_hklm_hive_key_.Close();
  temp_hkcu_hive_key_.Close();
  RegKey key(HKEY_CURRENT_USER, kUnitTestRegistrySubKey, KEY_ALL_ACCESS);
  key.DeleteKey(L"");
}

void ConfigurationPolicyProviderWinTest::WriteString(HKEY hive,
                                                     const char* name,
                                                     const wchar_t* value) {
  RegKey key(hive, policy::kRegistrySubKey, KEY_ALL_ACCESS);
  key.WriteValue(UTF8ToUTF16(name).c_str(), value);
}

void ConfigurationPolicyProviderWinTest::WriteDWORD(HKEY hive,
                                                    const char* name,
                                                    DWORD value) {
  RegKey key(hive, policy::kRegistrySubKey, KEY_ALL_ACCESS);
  key.WriteValue(UTF8ToUTF16(name).c_str(), value);
}

void ConfigurationPolicyProviderWinTest::WriteValue(HKEY hive,
                                                    const char* name,
                                                    const Value* value) {
  switch (value->GetType()) {
    case Value::TYPE_BOOLEAN: {
      bool v;
      ASSERT_TRUE(value->GetAsBoolean(&v));
      WriteDWORD(hive, name, v);
      break;
    }
    case Value::TYPE_INTEGER: {
      int v;
      ASSERT_TRUE(value->GetAsInteger(&v));
      WriteDWORD(hive, name, v);
      break;
    }
    case Value::TYPE_STRING: {
      std::string v;
      ASSERT_TRUE(value->GetAsString(&v));
      WriteString(hive, name, UTF8ToUTF16(v).c_str());
      break;
    }
    case Value::TYPE_LIST: {
      const ListValue* list = static_cast<const ListValue*>(value);
      RegKey key(hive,
                 (string16(policy::kRegistrySubKey) + ASCIIToUTF16("\\") +
                  UTF8ToUTF16(name)).c_str(),
                 KEY_ALL_ACCESS);
      int index = 1;
      for (ListValue::const_iterator element(list->begin());
           element != list->end(); ++element) {
        ASSERT_TRUE((*element)->IsType(Value::TYPE_STRING));
        std::string element_value;
        ASSERT_TRUE((*element)->GetAsString(&element_value));
        key.WriteValue(base::IntToString16(index++).c_str(),
                       UTF8ToUTF16(element_value).c_str());
      }
      break;
    }
    default:
      FAIL() << "Unsupported value type " << value->GetType();
      break;
  }
}

void ConfigurationPolicyProviderWinTest::WriteInvalidValue(HKEY hive,
                                                           const char* name,
                                                           const Value* value) {
  if (value->IsType(Value::TYPE_STRING))
    WriteDWORD(hive, name, -1);
  else
    WriteString(hive, name, L"bad value");
}

TEST_P(ConfigurationPolicyProviderWinTest, Default) {
  provider_->Provide(store_.get());
  EXPECT_TRUE(store_->policy_map().empty());
}

TEST_P(ConfigurationPolicyProviderWinTest, InvalidValue) {
  WriteInvalidValue(HKEY_LOCAL_MACHINE,
                    GetParam().policy_name(),
                    GetParam().hklm_value());
  WriteInvalidValue(HKEY_CURRENT_USER,
                    GetParam().policy_name(),
                    GetParam().hkcu_value());
  provider_->Provide(store_.get());
  EXPECT_TRUE(store_->policy_map().empty());
}

TEST_P(ConfigurationPolicyProviderWinTest, HKLM) {
  WriteValue(HKEY_LOCAL_MACHINE,
             GetParam().policy_name(),
             GetParam().hklm_value());
  provider_->Provide(store_.get());
  const Value* value = store_->Get(GetParam().type());
  ASSERT_TRUE(value);
  EXPECT_TRUE(value->Equals(GetParam().hklm_value()));
}

TEST_P(ConfigurationPolicyProviderWinTest, HKCU) {
  WriteValue(HKEY_CURRENT_USER,
             GetParam().policy_name(),
             GetParam().hkcu_value());
  provider_->Provide(store_.get());
  const Value* value = store_->Get(GetParam().type());
  ASSERT_TRUE(value);
  EXPECT_TRUE(value->Equals(GetParam().hkcu_value()));
}

TEST_P(ConfigurationPolicyProviderWinTest, HKLMOverHKCU) {
  WriteValue(HKEY_LOCAL_MACHINE,
             GetParam().policy_name(),
             GetParam().hklm_value());
  WriteValue(HKEY_CURRENT_USER,
             GetParam().policy_name(),
             GetParam().hkcu_value());
  provider_->Provide(store_.get());
  const Value* value = store_->Get(GetParam().type());
  ASSERT_TRUE(value);
  EXPECT_TRUE(value->Equals(GetParam().hklm_value()));
}

// Instantiate the test case for all supported policies.
INSTANTIATE_TEST_CASE_P(
    ConfigurationPolicyProviderWinTestInstance,
    ConfigurationPolicyProviderWinTest,
    testing::Values(
        PolicyTestParams::ForStringPolicy(
            kPolicyHomePage,
            key::kHomepageLocation),
        PolicyTestParams::ForBooleanPolicy(
            kPolicyHomepageIsNewTabPage,
            key::kHomepageIsNewTabPage),
        PolicyTestParams::ForIntegerPolicy(
            kPolicyRestoreOnStartup,
            key::kRestoreOnStartup),
        PolicyTestParams::ForListPolicy(
            kPolicyURLsToRestoreOnStartup,
            key::kURLsToRestoreOnStartup),
        PolicyTestParams::ForBooleanPolicy(
            kPolicyDefaultSearchProviderEnabled,
            key::kDefaultSearchProviderEnabled),
        PolicyTestParams::ForStringPolicy(
            kPolicyDefaultSearchProviderName,
            key::kDefaultSearchProviderName),
        PolicyTestParams::ForStringPolicy(
            kPolicyDefaultSearchProviderKeyword,
            key::kDefaultSearchProviderKeyword),
        PolicyTestParams::ForStringPolicy(
            kPolicyDefaultSearchProviderSearchURL,
            key::kDefaultSearchProviderSearchURL),
        PolicyTestParams::ForStringPolicy(
            kPolicyDefaultSearchProviderSuggestURL,
            key::kDefaultSearchProviderSuggestURL),
        PolicyTestParams::ForStringPolicy(
            kPolicyDefaultSearchProviderIconURL,
            key::kDefaultSearchProviderIconURL),
        PolicyTestParams::ForStringPolicy(
            kPolicyDefaultSearchProviderEncodings,
            key::kDefaultSearchProviderEncodings),
        PolicyTestParams::ForIntegerPolicy(
            kPolicyProxyServerMode,
            key::kProxyServerMode),
        PolicyTestParams::ForStringPolicy(
            kPolicyProxyServer,
            key::kProxyServer),
        PolicyTestParams::ForStringPolicy(
            kPolicyProxyPacUrl,
            key::kProxyPacUrl),
        PolicyTestParams::ForStringPolicy(
            kPolicyProxyBypassList,
            key::kProxyBypassList),
        PolicyTestParams::ForBooleanPolicy(
            kPolicyAlternateErrorPagesEnabled,
            key::kAlternateErrorPagesEnabled),
        PolicyTestParams::ForBooleanPolicy(
            kPolicySearchSuggestEnabled,
            key::kSearchSuggestEnabled),
        PolicyTestParams::ForBooleanPolicy(
            kPolicyDnsPrefetchingEnabled,
            key::kDnsPrefetchingEnabled),
        PolicyTestParams::ForBooleanPolicy(
            kPolicySafeBrowsingEnabled,
            key::kSafeBrowsingEnabled),
        PolicyTestParams::ForBooleanPolicy(
            kPolicyMetricsReportingEnabled,
            key::kMetricsReportingEnabled),
        PolicyTestParams::ForBooleanPolicy(
            kPolicyPasswordManagerEnabled,
            key::kPasswordManagerEnabled),
        PolicyTestParams::ForListPolicy(
            kPolicyDisabledPlugins,
            key::kDisabledPlugins),
        PolicyTestParams::ForBooleanPolicy(
            kPolicyAutoFillEnabled,
            key::kAutoFillEnabled),
        PolicyTestParams::ForBooleanPolicy(
            kPolicySyncDisabled,
            key::kSyncDisabled),
        PolicyTestParams::ForStringPolicy(
            kPolicyApplicationLocale,
            key::kApplicationLocaleValue),
        PolicyTestParams::ForListPolicy(
            kPolicyExtensionInstallAllowList,
            key::kExtensionInstallAllowList),
        PolicyTestParams::ForListPolicy(
            kPolicyExtensionInstallDenyList,
            key::kExtensionInstallDenyList),
        PolicyTestParams::ForBooleanPolicy(
            kPolicyShowHomeButton,
            key::kShowHomeButton),
        PolicyTestParams::ForBooleanPolicy(
            kPolicyPrintingEnabled,
            key::kPrintingEnabled)));

}  // namespace policy