summaryrefslogtreecommitdiffstats
path: root/chrome_frame/test/policy_settings_unittest.cc
blob: 52a3c5507c595787fd26d34c98299d83ee95e801 (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
// 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 "base/basictypes.h"
#include "base/logging.h"
#include "base/scoped_ptr.h"
#include "base/stringprintf.h"
#include "base/utf_string_conversions.h"
#include "base/win/registry.h"
#include "chrome/common/policy_constants.h"
#include "chrome_frame/policy_settings.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::win::RegKey;

namespace {

// A best effort way to zap CF policy entries that may be in the registry.
void DeleteChromeFramePolicyEntries(HKEY root) {
  RegKey key;
  if (key.Open(root, policy::kRegistrySubKey, KEY_ALL_ACCESS)) {
    key.DeleteValue(
        ASCIIToWide(policy::key::kChromeFrameRendererSettings).c_str());
    key.DeleteKey(ASCIIToWide(policy::key::kRenderInChromeFrameList).c_str());
    key.DeleteKey(ASCIIToWide(policy::key::kRenderInHostList).c_str());
    key.DeleteKey(ASCIIToWide(policy::key::kChromeFrameContentTypes).c_str());
  }
}

class TempRegKeyOverride {
 public:
  static const wchar_t kTempTestKeyPath[];

  TempRegKeyOverride(HKEY override, const wchar_t* temp_name)
      : override_(override), temp_name_(temp_name) {
    DCHECK(temp_name && lstrlenW(temp_name));
    std::wstring key_path(kTempTestKeyPath);
    key_path += L"\\" + temp_name_;
    EXPECT_TRUE(temp_key_.Create(HKEY_CURRENT_USER, key_path.c_str(),
                                 KEY_ALL_ACCESS));
    EXPECT_EQ(ERROR_SUCCESS,
              ::RegOverridePredefKey(override_, temp_key_.Handle()));
  }

  ~TempRegKeyOverride() {
    ::RegOverridePredefKey(override_, NULL);
    // The temp key will be deleted via a call to DeleteAllTempKeys().
  }

  static void DeleteAllTempKeys() {
    RegKey key;
    if (key.Open(HKEY_CURRENT_USER, L"", KEY_ALL_ACCESS)) {
      key.DeleteKey(kTempTestKeyPath);
    }
  }

 protected:
  HKEY override_;
  RegKey temp_key_;
  std::wstring temp_name_;
};

const wchar_t TempRegKeyOverride::kTempTestKeyPath[] =
    L"Software\\Chromium\\TempTestKeys";

bool InitializePolicyKey(HKEY policy_root, RegKey* policy_key) {
  EXPECT_TRUE(policy_key->Create(policy_root, policy::kRegistrySubKey,
                                 KEY_ALL_ACCESS));
  return policy_key->Valid();
}

void WritePolicyList(RegKey* policy_key, const wchar_t* list_name,
                     const wchar_t* values[], int count) {
  DCHECK(policy_key);
  // Remove any previous settings
  policy_key->DeleteKey(list_name);

  RegKey list_key;
  EXPECT_TRUE(list_key.Create(policy_key->Handle(), list_name, KEY_ALL_ACCESS));
  for (int i = 0; i < count; ++i) {
    EXPECT_TRUE(list_key.WriteValue(base::StringPrintf(L"%i", i).c_str(),
                                    values[i]));
  }
}

bool SetRendererSettings(HKEY policy_root,
                         PolicySettings::RendererForUrl renderer,
                         const wchar_t* exclusions[],
                         int exclusion_count) {
  RegKey policy_key;
  if (!InitializePolicyKey(policy_root, &policy_key))
    return false;

  policy_key.WriteValue(
      ASCIIToWide(policy::key::kChromeFrameRendererSettings).c_str(),
      static_cast<DWORD>(renderer));

  std::wstring in_cf(ASCIIToWide(policy::key::kRenderInChromeFrameList));
  std::wstring in_host(ASCIIToWide(policy::key::kRenderInHostList));
  std::wstring exclusion_list(
      renderer == PolicySettings::RENDER_IN_CHROME_FRAME ? in_host : in_cf);
  WritePolicyList(&policy_key, exclusion_list.c_str(), exclusions,
                  exclusion_count);

  return true;
}

bool SetCFContentTypes(HKEY policy_root, const wchar_t* content_types[],
                       int count) {
  RegKey policy_key;
  if (!InitializePolicyKey(policy_root, &policy_key))
    return false;

  std::wstring type_list(ASCIIToWide(policy::key::kChromeFrameContentTypes));
  WritePolicyList(&policy_key, type_list.c_str(), content_types, count);

  return true;
}

}  // end namespace

TEST(PolicySettings, RendererForUrl) {
  TempRegKeyOverride::DeleteAllTempKeys();

  scoped_ptr<TempRegKeyOverride> hklm_pol(
      new TempRegKeyOverride(HKEY_LOCAL_MACHINE, L"hklm_pol"));
  scoped_ptr<TempRegKeyOverride> hkcu_pol(
      new TempRegKeyOverride(HKEY_CURRENT_USER, L"hkcu_pol"));

  const wchar_t* kTestUrls[] = {
    L"http://www.example.com",
    L"http://www.pattern.com",
    L"http://www.test.com"
  };
  const wchar_t* kTestFilters[] = {
    L"*.example.com",
    L"*.pattern.com",
    L"*.test.com"
  };
  const wchar_t kNoMatchUrl[] = L"http://www.chromium.org";

  scoped_ptr<PolicySettings> settings(new PolicySettings());
  EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED,
            settings->default_renderer());
  EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED,
            settings->GetRendererForUrl(kNoMatchUrl));

  HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER };
  for (int i = 0; i < arraysize(root); ++i) {
    EXPECT_TRUE(SetRendererSettings(root[i],
        PolicySettings::RENDER_IN_CHROME_FRAME, kTestFilters,
        arraysize(kTestFilters)));

    settings.reset(new PolicySettings());
    EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME,
              settings->GetRendererForUrl(kNoMatchUrl));
    for (int j = 0; j < arraysize(kTestUrls); ++j) {
      EXPECT_EQ(PolicySettings::RENDER_IN_HOST,
                settings->GetRendererForUrl(kTestUrls[j]));
    }

    EXPECT_TRUE(SetRendererSettings(root[i],
        PolicySettings::RENDER_IN_HOST, NULL, 0));

    settings.reset(new PolicySettings());
    EXPECT_EQ(PolicySettings::RENDER_IN_HOST,
              settings->GetRendererForUrl(kNoMatchUrl));
    for (int j = 0; j < arraysize(kTestUrls); ++j) {
      EXPECT_EQ(PolicySettings::RENDER_IN_HOST,
                settings->GetRendererForUrl(kTestUrls[j]));
    }

    DeleteChromeFramePolicyEntries(root[i]);
  }

  hkcu_pol.reset(NULL);
  hklm_pol.reset(NULL);
  TempRegKeyOverride::DeleteAllTempKeys();
}

TEST(PolicySettings, RendererForContentType) {
  TempRegKeyOverride::DeleteAllTempKeys();

  scoped_ptr<TempRegKeyOverride> hklm_pol(
      new TempRegKeyOverride(HKEY_LOCAL_MACHINE, L"hklm_pol"));
  scoped_ptr<TempRegKeyOverride> hkcu_pol(
      new TempRegKeyOverride(HKEY_CURRENT_USER, L"hkcu_pol"));

  scoped_ptr<PolicySettings> settings(new PolicySettings());
  EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED,
            settings->GetRendererForContentType(L"text/xml"));

  const wchar_t* kTestPolicyContentTypes[] = {
    L"application/xml",
    L"text/xml",
    L"application/pdf",
  };

  HKEY root[] = { HKEY_LOCAL_MACHINE, HKEY_CURRENT_USER };
  for (int i = 0; i < arraysize(root); ++i) {
    SetCFContentTypes(root[i], kTestPolicyContentTypes,
                      arraysize(kTestPolicyContentTypes));
    settings.reset(new PolicySettings());
    for (int type = 0; type < arraysize(kTestPolicyContentTypes); ++type) {
      EXPECT_EQ(PolicySettings::RENDER_IN_CHROME_FRAME,
                settings->GetRendererForContentType(
                    kTestPolicyContentTypes[type]));
    }

    EXPECT_EQ(PolicySettings::RENDERER_NOT_SPECIFIED,
              settings->GetRendererForContentType(L"text/html"));

    DeleteChromeFramePolicyEntries(root[i]);
  }
}