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
|
// Copyright (c) 2013 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 <vector>
#include "base/callback.h"
#include "base/files/scoped_temp_dir.h"
#include "base/json/json_reader.h"
#include "base/run_loop.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/external_data_fetcher.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/core/common/schema.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/common/extension_builder.h"
#include "grit/components_strings.h"
#include "policy/policy_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
using testing::Return;
using testing::_;
namespace {
std::vector<std::string> PopulateExpectedPolicy(
const std::string& name,
const std::string& value,
const policy::PolicyMap::Entry* metadata,
bool unknown) {
std::vector<std::string> expected_policy;
// Populate expected scope.
if (metadata) {
expected_policy.push_back(l10n_util::GetStringUTF8(
metadata->scope == policy::POLICY_SCOPE_MACHINE ?
IDS_POLICY_SCOPE_DEVICE : IDS_POLICY_SCOPE_USER));
} else {
expected_policy.push_back(std::string());
}
// Populate expected level.
if (metadata) {
expected_policy.push_back(l10n_util::GetStringUTF8(
metadata->level == policy::POLICY_LEVEL_RECOMMENDED ?
IDS_POLICY_LEVEL_RECOMMENDED : IDS_POLICY_LEVEL_MANDATORY));
} else {
expected_policy.push_back(std::string());
}
// Populate expected policy name.
expected_policy.push_back(name);
// Populate expected policy value.
expected_policy.push_back(value);
// Populate expected status.
if (unknown)
expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_UNKNOWN));
else if (metadata)
expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_OK));
else
expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_UNSET));
// Populate expected expanded policy value.
expected_policy.push_back(value);
return expected_policy;
}
} // namespace
class PolicyUITest : public InProcessBrowserTest {
public:
PolicyUITest();
~PolicyUITest() override;
protected:
// InProcessBrowserTest implementation.
void SetUpInProcessBrowserTestFixture() override;
void UpdateProviderPolicy(const policy::PolicyMap& policy);
void VerifyPolicies(const std::vector<std::vector<std::string> >& expected);
protected:
policy::MockConfigurationPolicyProvider provider_;
private:
DISALLOW_COPY_AND_ASSIGN(PolicyUITest);
};
PolicyUITest::PolicyUITest() {
}
PolicyUITest::~PolicyUITest() {
}
void PolicyUITest::SetUpInProcessBrowserTestFixture() {
EXPECT_CALL(provider_, IsInitializationComplete(_))
.WillRepeatedly(Return(true));
policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
}
void PolicyUITest::UpdateProviderPolicy(const policy::PolicyMap& policy) {
provider_.UpdateChromePolicy(policy);
base::RunLoop loop;
loop.RunUntilIdle();
}
void PolicyUITest::VerifyPolicies(
const std::vector<std::vector<std::string> >& expected_policies) {
ui_test_utils::NavigateToURL(browser(), GURL("chrome://policy"));
// Retrieve the text contents of the policy table cells for all policies.
const std::string javascript =
"var entries = document.querySelectorAll("
" 'section.policy-table-section > * > tbody');"
"var policies = [];"
"for (var i = 0; i < entries.length; ++i) {"
" var items = entries[i].querySelectorAll('tr > td');"
" var values = [];"
" for (var j = 0; j < items.length; ++j) {"
" var item = items[j];"
" var children = item.getElementsByTagName('div');"
" if (children.length == 1)"
" item = children[0];"
" children = item.getElementsByTagName('span');"
" if (children.length == 1)"
" item = children[0];"
" values.push(item.textContent);"
" }"
" policies.push(values);"
"}"
"domAutomationController.send(JSON.stringify(policies));";
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
std::string json;
ASSERT_TRUE(content::ExecuteScriptAndExtractString(contents, javascript,
&json));
scoped_ptr<base::Value> value_ptr = base::JSONReader::Read(json);
const base::ListValue* actual_policies = NULL;
ASSERT_TRUE(value_ptr.get());
ASSERT_TRUE(value_ptr->GetAsList(&actual_policies));
// Verify that the cells contain the expected strings for all policies.
ASSERT_EQ(expected_policies.size(), actual_policies->GetSize());
for (size_t i = 0; i < expected_policies.size(); ++i) {
const std::vector<std::string> expected_policy = expected_policies[i];
const base::ListValue* actual_policy;
ASSERT_TRUE(actual_policies->GetList(i, &actual_policy));
ASSERT_EQ(expected_policy.size(), actual_policy->GetSize());
for (size_t j = 0; j < expected_policy.size(); ++j) {
std::string value;
ASSERT_TRUE(actual_policy->GetString(j, &value));
EXPECT_EQ(expected_policy[j], value);
}
}
}
IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyNames) {
// Verifies that the names of known policies are sent to the UI and processed
// there correctly by checking that the policy table contains all policies in
// the correct order.
// Expect that the policy table contains all known policies in alphabetical
// order and none of the policies have a set value.
std::vector<std::vector<std::string> > expected_policies;
policy::Schema chrome_schema =
policy::Schema::Wrap(policy::GetChromeSchemaData());
ASSERT_TRUE(chrome_schema.valid());
for (policy::Schema::Iterator it = chrome_schema.GetPropertiesIterator();
!it.IsAtEnd(); it.Advance()) {
expected_policies.push_back(
PopulateExpectedPolicy(it.key(), std::string(), NULL, false));
}
// Retrieve the contents of the policy table from the UI and verify that it
// matches the expectation.
VerifyPolicies(expected_policies);
}
IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyValues) {
// Verifies that policy values are sent to the UI and processed there
// correctly by setting the values of four known and one unknown policy and
// checking that the policy table contains the policy names, values and
// metadata in the correct order.
policy::PolicyMap values;
std::map<std::string, std::string> expected_values;
// Set the values of four existing policies.
base::ListValue* restore_on_startup_urls = new base::ListValue;
restore_on_startup_urls->Append(new base::StringValue("aaa"));
restore_on_startup_urls->Append(new base::StringValue("bbb"));
restore_on_startup_urls->Append(new base::StringValue("ccc"));
values.Set(policy::key::kRestoreOnStartupURLs,
policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER,
restore_on_startup_urls,
NULL);
expected_values[policy::key::kRestoreOnStartupURLs] = "aaa,bbb,ccc";
values.Set(policy::key::kHomepageLocation,
policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_MACHINE,
new base::StringValue("http://google.com"),
NULL);
expected_values[policy::key::kHomepageLocation] = "http://google.com";
values.Set(policy::key::kRestoreOnStartup,
policy::POLICY_LEVEL_RECOMMENDED,
policy::POLICY_SCOPE_USER,
new base::FundamentalValue(4),
NULL);
expected_values[policy::key::kRestoreOnStartup] = "4";
values.Set(policy::key::kShowHomeButton,
policy::POLICY_LEVEL_RECOMMENDED,
policy::POLICY_SCOPE_MACHINE,
new base::FundamentalValue(true),
NULL);
expected_values[policy::key::kShowHomeButton] = "true";
// Set the value of a policy that does not exist.
const std::string kUnknownPolicy = "NoSuchThing";
values.Set(kUnknownPolicy,
policy::POLICY_LEVEL_MANDATORY,
policy::POLICY_SCOPE_USER,
new base::FundamentalValue(true),
NULL);
expected_values[kUnknownPolicy] = "true";
UpdateProviderPolicy(values);
// Expect that the policy table contains, in order:
// * All known policies whose value has been set, in alphabetical order.
// * The unknown policy.
// * All known policies whose value has not been set, in alphabetical order.
std::vector<std::vector<std::string> > expected_policies;
size_t first_unset_position = 0;
policy::Schema chrome_schema =
policy::Schema::Wrap(policy::GetChromeSchemaData());
ASSERT_TRUE(chrome_schema.valid());
for (policy::Schema::Iterator props = chrome_schema.GetPropertiesIterator();
!props.IsAtEnd(); props.Advance()) {
std::map<std::string, std::string>::const_iterator it =
expected_values.find(props.key());
const std::string value =
it == expected_values.end() ? std::string() : it->second;
const policy::PolicyMap::Entry* metadata = values.Get(props.key());
expected_policies.insert(
metadata ? expected_policies.begin() + first_unset_position++ :
expected_policies.end(),
PopulateExpectedPolicy(props.key(), value, metadata, false));
}
expected_policies.insert(
expected_policies.begin() + first_unset_position++,
PopulateExpectedPolicy(kUnknownPolicy,
expected_values[kUnknownPolicy],
values.Get(kUnknownPolicy),
true));
// Retrieve the contents of the policy table from the UI and verify that it
// matches the expectation.
VerifyPolicies(expected_policies);
}
IN_PROC_BROWSER_TEST_F(PolicyUITest, ExtensionLoadAndSendPolicy) {
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIPolicyURL));
base::ScopedTempDir temp_dir_;
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
const std::string newly_added_policy_name = "new_policy";
std::string json_data = "{\"type\": \"object\",\"properties\": {\"" +
newly_added_policy_name +
"\": { \"type\": \"string\"}}}";
const std::string schema_file = "schema.json";
base::FilePath schema_path = temp_dir_.path().AppendASCII(schema_file);
base::WriteFile(schema_path, json_data.data(), json_data.size());
// Build extension that contains the policy schema.
extensions::DictionaryBuilder storage;
storage.Set("managed_schema", schema_file);
extensions::DictionaryBuilder manifest;
manifest.Set("name", "test")
.Set("version", "1")
.Set("manifest_version", 2)
.Set("storage", storage);
extensions::ExtensionBuilder builder;
builder.SetPath(temp_dir_.path());
builder.SetManifest(manifest);
// Install extension.
ExtensionService* service = extensions::ExtensionSystem::Get(
browser()->profile())->extension_service();
EXPECT_CALL(provider_, RefreshPolicies());
service->OnExtensionInstalled(builder.Build().get(), syncer::StringOrdinal(),
0);
std::vector<std::vector<std::string>> expected_policies;
policy::Schema chrome_schema =
policy::Schema::Wrap(policy::GetChromeSchemaData());
ASSERT_TRUE(chrome_schema.valid());
for (policy::Schema::Iterator it = chrome_schema.GetPropertiesIterator();
!it.IsAtEnd(); it.Advance()) {
expected_policies.push_back(
PopulateExpectedPolicy(it.key(), std::string(), NULL, false));
}
// Add newly added policy to expected policy list.
expected_policies.push_back(PopulateExpectedPolicy(
newly_added_policy_name, std::string(), NULL, false));
// Verify if policy UI includes policy that extension have.
VerifyPolicies(expected_policies);
}
|