summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/configuration_policy_reader_unittest.cc
blob: e57b6c66efd3fd1ee11fd191a2784a992b2a993f (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
// Copyright (c) 2012 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 <string>

#include "base/memory/scoped_ptr.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/policy/configuration_policy_reader.h"
#include "chrome/browser/policy/mock_configuration_policy_provider.h"
#include "chrome/browser/policy/mock_configuration_policy_reader.h"
#include "policy/policy_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::Return;
using ::testing::ReturnNull;
using ::testing::_;

namespace policy {

class ConfigurationPolicyReaderTest : public testing::Test {
 protected:
  ConfigurationPolicyReaderTest() : provider_() {
    reader_.reset(new ConfigurationPolicyReader(&provider_));
    status_ok_ = ASCIIToUTF16("OK");
  }

  DictionaryValue* CreateDictionary(const char* policy_name,
                                    Value* policy_value) {
    DictionaryValue* dict = new DictionaryValue();
    dict->SetString(PolicyStatusInfo::kNameDictPath, ASCIIToUTF16(policy_name));
    dict->SetString(PolicyStatusInfo::kLevelDictPath,
                    PolicyStatusInfo::GetPolicyLevelString(
                        POLICY_LEVEL_MANDATORY));
    dict->SetString(PolicyStatusInfo::kScopeDictPath,
                    PolicyStatusInfo::GetPolicyScopeString(
                        POLICY_SCOPE_USER));
    dict->Set(PolicyStatusInfo::kValueDictPath, policy_value);
    dict->SetBoolean(PolicyStatusInfo::kSetDictPath, true);
    dict->SetString(PolicyStatusInfo::kStatusDictPath, status_ok_);

    return dict;
  }

  MockConfigurationPolicyProvider provider_;
  scoped_ptr<ConfigurationPolicyReader> reader_;
  string16 status_ok_;
};

TEST_F(ConfigurationPolicyReaderTest, GetDefault) {
  EXPECT_EQ(NULL, reader_->GetPolicyStatus(key::kHomepageLocation));
}

// Test for list-valued policy settings.
TEST_F(ConfigurationPolicyReaderTest, SetListValue) {
  ListValue mandatory_value;
  mandatory_value.Append(Value::CreateStringValue("test1"));
  mandatory_value.Append(Value::CreateStringValue("test2"));
  ListValue recommended_value;
  recommended_value.Append(Value::CreateStringValue("test3"));
  recommended_value.Append(Value::CreateStringValue("test4"));
  provider_.AddMandatoryPolicy(key::kRestoreOnStartupURLs,
                               mandatory_value.DeepCopy());
  provider_.AddRecommendedPolicy(key::kDisabledSchemes,
                                 recommended_value.DeepCopy());
  reader_->OnUpdatePolicy(&provider_);

  scoped_ptr<DictionaryValue> dict(
      CreateDictionary(key::kRestoreOnStartupURLs,
                       mandatory_value.DeepCopy()));
  scoped_ptr<DictionaryValue> result(
      reader_->GetPolicyStatus(key::kRestoreOnStartupURLs));
  EXPECT_TRUE(dict->Equals(result.get()));

  dict.reset(CreateDictionary(key::kDisabledSchemes,
                              recommended_value.DeepCopy()));
  dict->SetString(
      "level",
      PolicyStatusInfo::GetPolicyLevelString(POLICY_LEVEL_RECOMMENDED));
  result.reset(reader_->GetPolicyStatus(key::kDisabledSchemes));
  EXPECT_TRUE(dict->Equals(result.get()));
}

// Test for string-valued policy settings.
TEST_F(ConfigurationPolicyReaderTest, SetStringValue) {
  StringValue mandatory_value("http://chromium.org");
  StringValue recommended_value("http://www.google.com/q={searchTerms}");
  provider_.AddMandatoryPolicy(key::kHomepageLocation,
                               mandatory_value.DeepCopy());
  provider_.AddRecommendedPolicy(key::kDefaultSearchProviderSearchURL,
                                 recommended_value.DeepCopy());
  reader_->OnUpdatePolicy(&provider_);
  scoped_ptr<DictionaryValue> dict(
      CreateDictionary(key::kHomepageLocation,
                       mandatory_value.DeepCopy()));
  scoped_ptr<DictionaryValue> result(
      reader_->GetPolicyStatus(key::kHomepageLocation));
  EXPECT_TRUE(dict->Equals(result.get()));

  dict.reset(CreateDictionary(key::kDefaultSearchProviderSearchURL,
                              recommended_value.DeepCopy()));
  dict->SetString(
      "level",
      PolicyStatusInfo::GetPolicyLevelString(POLICY_LEVEL_RECOMMENDED));
  result.reset(reader_->GetPolicyStatus(key::kDefaultSearchProviderSearchURL));
  EXPECT_TRUE(dict->Equals(result.get()));
}

// Test for boolean-valued policy settings.
TEST_F(ConfigurationPolicyReaderTest, SetBooleanValue) {
  provider_.AddMandatoryPolicy(key::kShowHomeButton,
                               Value::CreateBooleanValue(true));
  provider_.AddRecommendedPolicy(key::kHomepageIsNewTabPage,
                                 Value::CreateBooleanValue(false));
  reader_->OnUpdatePolicy(&provider_);
  scoped_ptr<DictionaryValue> dict(
      CreateDictionary(key::kShowHomeButton, Value::CreateBooleanValue(true)));
  scoped_ptr<DictionaryValue> result(
      reader_->GetPolicyStatus(key::kShowHomeButton));
  EXPECT_TRUE(dict->Equals(result.get()));

  dict.reset(CreateDictionary(key::kHomepageIsNewTabPage,
                              Value::CreateBooleanValue(false)));
  dict->SetString(
      "level",
      PolicyStatusInfo::GetPolicyLevelString(POLICY_LEVEL_RECOMMENDED));
  result.reset(reader_->GetPolicyStatus(key::kHomepageIsNewTabPage));
  EXPECT_TRUE(dict->Equals(result.get()));
}

// Test for integer-valued policy settings.
TEST_F(ConfigurationPolicyReaderTest, SetIntegerValue) {
  provider_.AddMandatoryPolicy(key::kRestoreOnStartup,
                               Value::CreateIntegerValue(3));
  provider_.AddRecommendedPolicy(key::kIncognitoModeAvailability,
                                 Value::CreateIntegerValue(2));
  reader_->OnUpdatePolicy(&provider_);
  scoped_ptr<DictionaryValue> dict(
      CreateDictionary(key::kRestoreOnStartup, Value::CreateIntegerValue(3)));
  scoped_ptr<DictionaryValue> result(
      reader_->GetPolicyStatus(key::kRestoreOnStartup));
  EXPECT_TRUE(dict->Equals(result.get()));

  dict.reset(CreateDictionary(key::kIncognitoModeAvailability,
                              Value::CreateIntegerValue(2)));
  dict->SetString(
      "level",
      PolicyStatusInfo::GetPolicyLevelString(POLICY_LEVEL_RECOMMENDED));
  result.reset(reader_->GetPolicyStatus(key::kIncognitoModeAvailability));
  EXPECT_TRUE(dict->Equals(result.get()));
}

class PolicyStatusTest : public testing::Test {
 protected:
  PolicyStatusTest() {
    managed_platform_ = new MockConfigurationPolicyReader();
    managed_cloud_ = new MockConfigurationPolicyReader();
    recommended_platform_ = new MockConfigurationPolicyReader();
    recommended_cloud_ = new MockConfigurationPolicyReader();

    policy_status_.reset(new PolicyStatus(managed_platform_,
                                          managed_cloud_,
                                          recommended_platform_,
                                          recommended_cloud_));
    status_ok_ = ASCIIToUTF16("OK");
    status_not_set_ = ASCIIToUTF16("Not set.");

    policy_list_ = GetChromePolicyDefinitionList();
    policy_list_size_ =
        static_cast<size_t>(policy_list_->end - policy_list_->begin);
  }

  void DontSendPolicies() {
    EXPECT_CALL(*managed_platform_, GetPolicyStatus(_))
        .WillRepeatedly(ReturnNull());
    EXPECT_CALL(*managed_cloud_, GetPolicyStatus(_))
        .WillRepeatedly(ReturnNull());
    EXPECT_CALL(*recommended_platform_, GetPolicyStatus(_))
        .WillRepeatedly(ReturnNull());
    EXPECT_CALL(*recommended_cloud_, GetPolicyStatus(_))
        .WillRepeatedly(ReturnNull());
  }

  void SendPolicies() {
    EXPECT_CALL(*managed_platform_, GetPolicyStatus(_))
        .WillRepeatedly(ReturnNull());
    EXPECT_CALL(*managed_cloud_, GetPolicyStatus(_))
        .WillRepeatedly(ReturnNull());
    EXPECT_CALL(*recommended_platform_, GetPolicyStatus(_))
        .WillRepeatedly(ReturnNull());
    EXPECT_CALL(*recommended_cloud_, GetPolicyStatus(_))
        .WillRepeatedly(ReturnNull());

    EXPECT_CALL(*managed_platform_, GetPolicyStatus(key::kInstantEnabled))
        .WillRepeatedly(Return(CreateDictionary(key::kInstantEnabled,
                                                POLICY_LEVEL_MANDATORY)));
    EXPECT_CALL(*managed_cloud_, GetPolicyStatus(key::kDisablePluginFinder))
        .WillRepeatedly(Return(CreateDictionary(key::kDisablePluginFinder,
                                                POLICY_LEVEL_MANDATORY)));
    EXPECT_CALL(*recommended_platform_, GetPolicyStatus(key::kSyncDisabled))
        .WillRepeatedly(Return(CreateDictionary(key::kSyncDisabled,
                                                POLICY_LEVEL_RECOMMENDED)));
    EXPECT_CALL(*recommended_cloud_, GetPolicyStatus(key::kTranslateEnabled))
        .WillRepeatedly(Return(CreateDictionary(key::kTranslateEnabled,
                                                POLICY_LEVEL_RECOMMENDED)));
  }

  DictionaryValue* CreateDictionary(const char* name, PolicyLevel level) {
    DictionaryValue* value = new DictionaryValue();
    value->SetString(PolicyStatusInfo::kNameDictPath, ASCIIToUTF16(name));
    value->SetString(PolicyStatusInfo::kLevelDictPath,
                     PolicyStatusInfo::GetPolicyLevelString(level));
    value->SetString(PolicyStatusInfo::kScopeDictPath,
                     PolicyStatusInfo::GetPolicyScopeString(POLICY_SCOPE_USER));
    value->SetBoolean(PolicyStatusInfo::kValueDictPath, true);
    value->SetBoolean(PolicyStatusInfo::kSetDictPath, true);
    value->SetString(PolicyStatusInfo::kStatusDictPath, status_ok_);

    return value;
  }

  void SetDictionaryPaths(DictionaryValue* dict,
                          const char* policy_name,
                          bool defined,
                          PolicyLevel level) {
    dict->SetString(PolicyStatusInfo::kNameDictPath,
                    ASCIIToUTF16(policy_name));
    if (defined) {
      dict->SetString(PolicyStatusInfo::kLevelDictPath,
                      PolicyStatusInfo::GetPolicyLevelString(level));
    }
  }

  MockConfigurationPolicyReader* managed_platform_;
  MockConfigurationPolicyReader* managed_cloud_;
  MockConfigurationPolicyReader* recommended_platform_;
  MockConfigurationPolicyReader* recommended_cloud_;
  scoped_ptr<PolicyStatus> policy_status_;
  const PolicyDefinitionList* policy_list_;
  size_t policy_list_size_;
  string16 status_ok_;
  string16 status_not_set_;
};

TEST_F(PolicyStatusTest, GetPolicyStatusListNoSetPolicies) {
  DontSendPolicies();
  bool any_policies_sent;
  scoped_ptr<ListValue> status_list(
      policy_status_->GetPolicyStatusList(&any_policies_sent));
  EXPECT_FALSE(any_policies_sent);
  EXPECT_EQ(policy_list_size_, status_list->GetSize());
}

TEST_F(PolicyStatusTest, GetPolicyStatusListSetPolicies) {
  SendPolicies();
  bool any_policies_sent;
  scoped_ptr<ListValue> status_list(
      policy_status_->GetPolicyStatusList(&any_policies_sent));
  EXPECT_TRUE(any_policies_sent);
  EXPECT_EQ(policy_list_size_, status_list->GetSize());

  scoped_ptr<DictionaryValue> undefined_dict(new DictionaryValue());
  undefined_dict->SetString(PolicyStatusInfo::kLevelDictPath, "");
  undefined_dict->SetString(PolicyStatusInfo::kScopeDictPath, "");
  undefined_dict->SetString(PolicyStatusInfo::kValueDictPath, "");
  undefined_dict->SetBoolean(PolicyStatusInfo::kSetDictPath, false);
  undefined_dict->SetString(PolicyStatusInfo::kStatusDictPath, status_not_set_);

  scoped_ptr<DictionaryValue> defined_dict(new DictionaryValue());
  defined_dict->SetString(PolicyStatusInfo::kScopeDictPath,
                          PolicyStatusInfo::GetPolicyScopeString(
                              POLICY_SCOPE_USER));
  defined_dict->Set(PolicyStatusInfo::kValueDictPath,
                    Value::CreateBooleanValue(true));
  defined_dict->SetBoolean(PolicyStatusInfo::kSetDictPath, true);
  defined_dict->SetString(PolicyStatusInfo::kStatusDictPath, status_ok_);

  struct {
    const char *name;
    PolicyLevel level;
  } cases[] = {
    { key::kInstantEnabled,       POLICY_LEVEL_MANDATORY },
    { key::kDisablePluginFinder,  POLICY_LEVEL_MANDATORY },
    { key::kSyncDisabled,         POLICY_LEVEL_RECOMMENDED },
    { key::kTranslateEnabled,     POLICY_LEVEL_RECOMMENDED },
    { NULL,                       POLICY_LEVEL_MANDATORY },
  };

  for (const PolicyDefinitionList::Entry* entry = policy_list_->begin;
       entry != policy_list_->end; ++entry) {
    Value* status_dict = NULL;

    // Every policy in |policy_list_| has to appear in the returned ListValue.
    string16 name = ASCIIToUTF16(entry->name);
    for (ListValue::const_iterator status_entry = status_list->begin();
         status_entry != status_list->end();
         ++status_entry) {
      string16 value;
      ASSERT_TRUE((*status_entry)->IsType(Value::TYPE_DICTIONARY));
      DictionaryValue* dict = static_cast<DictionaryValue*>(*status_entry);
      ASSERT_TRUE(dict->GetString(PolicyStatusInfo::kNameDictPath,
                                  &value));
      if (value == name)
        status_dict = *status_entry;
    }

    ASSERT_FALSE(status_dict == NULL);

    for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
      if (entry->name == cases[i].name || cases[i].name == NULL) {
        DictionaryValue* dict =
            cases[i].name ? defined_dict.get() : undefined_dict.get();
        SetDictionaryPaths(dict,
                           entry->name,
                           cases[i].name != NULL,
                           cases[i].level);
        EXPECT_TRUE(dict->Equals(status_dict));
        break;
      }
    }
  }
}

} // namespace policy