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
|
// 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/message_loop.h"
#include "base/scoped_temp_dir.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/net/gaia/token_service.h"
#include "chrome/browser/policy/configuration_policy_pref_store.h"
#include "chrome/browser/policy/device_management_policy_provider.h"
#include "chrome/browser/policy/mock_configuration_policy_store.h"
#include "chrome/browser/policy/mock_device_management_backend.h"
#include "chrome/common/net/gaia/gaia_constants.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/policy_constants.h"
#include "chrome/test/device_management_test_util.h"
#include "chrome/test/mock_notification_observer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
using ::testing::_;
using ::testing::Mock;
class DeviceManagementPolicyProviderTest : public testing::Test {
public:
DeviceManagementPolicyProviderTest()
: ui_thread_(BrowserThread::UI, &loop_),
file_thread_(BrowserThread::FILE, &loop_) {}
virtual ~DeviceManagementPolicyProviderTest() {}
virtual void SetUp() {
EXPECT_TRUE(storage_dir_.CreateUniqueTempDir());
CreateNewBackend();
CreateNewProvider();
}
void CreateNewBackend() {
backend_ = new MockDeviceManagementBackend;
backend_->AddBooleanPolicy(key::kDisableSpdy, true);
}
void CreateNewProvider() {
provider_.reset(new DeviceManagementPolicyProvider(
ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
backend_,
storage_dir_.path()));
loop_.RunAllPending();
}
void SimulateSuccessfulLoginAndRunPending() {
loop_.RunAllPending();
SimulateSuccessfulLogin();
loop_.RunAllPending();
}
void SimulateSuccessfulInitialPolicyFetch() {
MockConfigurationPolicyStore store;
backend_->AllShouldSucceed();
EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1);
EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _)).Times(1);
SimulateSuccessfulLoginAndRunPending();
EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1);
provider_->Provide(&store);
ASSERT_EQ(1U, store.policy_map().size());
Mock::VerifyAndClearExpectations(backend_);
Mock::VerifyAndClearExpectations(&store);
}
virtual void TearDown() {
loop_.RunAllPending();
}
protected:
MockDeviceManagementBackend* backend_; // weak
scoped_ptr<DeviceManagementPolicyProvider> provider_;
private:
MessageLoop loop_;
BrowserThread ui_thread_;
BrowserThread file_thread_;
ScopedTempDir storage_dir_;
DISALLOW_COPY_AND_ASSIGN(DeviceManagementPolicyProviderTest);
};
// If there's no login and no previously-fetched policy, the provider should
// provide an empty policy.
TEST_F(DeviceManagementPolicyProviderTest, InitialProvideNoLogin) {
MockConfigurationPolicyStore store;
backend_->AllShouldSucceed();
EXPECT_CALL(store, Apply(_, _)).Times(0);
provider_->Provide(&store);
EXPECT_TRUE(store.policy_map().empty());
}
// If the login is successful and there's no previously-fetched policy, the
// policy should be fetched from the server and should be available the first
// time the Provide method is called.
TEST_F(DeviceManagementPolicyProviderTest, InitialProvideWithLogin) {
SimulateSuccessfulInitialPolicyFetch();
}
// If the login succeeds but the device management backend is unreachable,
// there should be no policy provided if there's no previously-fetched policy,
TEST_F(DeviceManagementPolicyProviderTest, EmptyProvideWithFailedBackend) {
MockConfigurationPolicyStore store;
backend_->AllShouldFail();
EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1);
EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _)).Times(0);
SimulateSuccessfulLoginAndRunPending();
EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(0);
provider_->Provide(&store);
EXPECT_TRUE(store.policy_map().empty());
}
// If a policy has been fetched previously, if should be available even before
// the login succeeds or the device management backend is available.
TEST_F(DeviceManagementPolicyProviderTest, SecondProvide) {
// Pre-fetch and persist a policy
SimulateSuccessfulInitialPolicyFetch();
// Simulate a app relaunch by constructing a new provider. Policy should be
// immediately provided and no refresh should be triggered.
CreateNewBackend();
EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _)).Times(0);
CreateNewProvider();
MockConfigurationPolicyStore store;
EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1);
provider_->Provide(&store);
}
// When policy is successfully fetched from the device management server, it
// should force a policy refresh.
TEST_F(DeviceManagementPolicyProviderTest, FetchTriggersRefresh) {
MockNotificationObserver observer;
NotificationRegistrar registrar;
registrar.Add(&observer,
NotificationType::POLICY_CHANGED,
NotificationService::AllSources());
EXPECT_CALL(observer,
Observe(_, _, _)).Times(1);
SimulateSuccessfulInitialPolicyFetch();
}
}
|