OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/message_loop.h" | 5 #include "base/message_loop.h" |
6 #include "base/scoped_temp_dir.h" | 6 #include "base/scoped_temp_dir.h" |
7 #include "chrome/browser/browser_thread.h" | 7 #include "chrome/browser/browser_thread.h" |
8 #include "chrome/browser/net/gaia/token_service.h" | 8 #include "chrome/browser/net/gaia/token_service.h" |
9 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 9 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
10 #include "chrome/browser/policy/device_management_policy_provider.h" | 10 #include "chrome/browser/policy/device_management_policy_provider.h" |
11 #include "chrome/browser/policy/mock_configuration_policy_store.h" | 11 #include "chrome/browser/policy/mock_configuration_policy_store.h" |
12 #include "chrome/browser/policy/mock_device_management_backend.h" | 12 #include "chrome/browser/policy/mock_device_management_backend.h" |
13 #include "chrome/common/net/gaia/gaia_constants.h" | 13 #include "chrome/common/net/gaia/gaia_constants.h" |
14 #include "chrome/common/notification_service.h" | 14 #include "chrome/common/notification_service.h" |
15 #include "chrome/common/policy_constants.h" | 15 #include "chrome/common/policy_constants.h" |
16 #include "chrome/test/mock_notification_observer.h" | 16 #include "chrome/test/mock_notification_observer.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 const char kTestToken[] = "device_policy_provider_test_auth_token"; | 19 const char kTestToken[] = "device_policy_provider_test_auth_token"; |
20 | 20 |
21 namespace policy { | 21 namespace policy { |
22 | 22 |
23 using ::testing::_; | 23 using ::testing::_; |
| 24 using ::testing::InSequence; |
24 using ::testing::Mock; | 25 using ::testing::Mock; |
25 | 26 |
26 class DeviceManagementPolicyProviderTest : public testing::Test { | 27 class DeviceManagementPolicyProviderTest : public testing::Test { |
27 public: | 28 public: |
28 DeviceManagementPolicyProviderTest() | 29 DeviceManagementPolicyProviderTest() |
29 : ui_thread_(BrowserThread::UI, &loop_), | 30 : ui_thread_(BrowserThread::UI, &loop_), |
30 file_thread_(BrowserThread::FILE, &loop_) {} | 31 file_thread_(BrowserThread::FILE, &loop_) {} |
31 | 32 |
32 virtual ~DeviceManagementPolicyProviderTest() {} | 33 virtual ~DeviceManagementPolicyProviderTest() {} |
33 | 34 |
34 virtual void SetUp() { | 35 virtual void SetUp() { |
35 EXPECT_TRUE(storage_dir_.CreateUniqueTempDir()); | 36 EXPECT_TRUE(storage_dir_.CreateUniqueTempDir()); |
36 CreateNewBackend(); | 37 CreateNewBackend(); |
37 CreateNewProvider(); | 38 CreateNewProvider(); |
38 } | 39 } |
39 | 40 |
40 void CreateNewBackend() { | 41 void CreateNewBackend() { |
41 backend_ = new MockDeviceManagementBackend; | 42 backend_ = new MockDeviceManagementBackend; |
42 backend_->AddBooleanPolicy(key::kDisableSpdy, true); | |
43 } | 43 } |
44 | 44 |
45 void CreateNewProvider() { | 45 void CreateNewProvider() { |
46 token_service_.reset(new TokenService); | 46 token_service_.reset(new TokenService); |
47 provider_.reset(new DeviceManagementPolicyProvider( | 47 provider_.reset(new DeviceManagementPolicyProvider( |
48 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | 48 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), |
49 backend_, | 49 backend_, |
50 token_service_.get(), | 50 token_service_.get(), |
51 storage_dir_.path())); | 51 storage_dir_.path())); |
52 loop_.RunAllPending(); | 52 loop_.RunAllPending(); |
53 } | 53 } |
54 | 54 |
55 void SimulateSuccessfulLoginAndRunPending() { | 55 void SimulateSuccessfulLoginAndRunPending() { |
56 loop_.RunAllPending(); | 56 loop_.RunAllPending(); |
57 token_service_->IssueAuthTokenForTest( | 57 token_service_->IssueAuthTokenForTest( |
58 GaiaConstants::kDeviceManagementService, kTestToken); | 58 GaiaConstants::kDeviceManagementService, kTestToken); |
59 loop_.RunAllPending(); | 59 loop_.RunAllPending(); |
60 } | 60 } |
61 | 61 |
62 void SimulateSuccessfulInitialPolicyFetch() { | 62 void SimulateSuccessfulInitialPolicyFetch() { |
63 MockConfigurationPolicyStore store; | 63 MockConfigurationPolicyStore store; |
64 backend_->AllShouldSucceed(); | 64 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
65 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 65 MockDeviceManagementBackendSucceedRegister()); |
66 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(1); | 66 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 67 MockDeviceManagementBackendSucceedBooleanPolicy( |
| 68 key::kDisableSpdy, true)); |
67 SimulateSuccessfulLoginAndRunPending(); | 69 SimulateSuccessfulLoginAndRunPending(); |
68 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1); | 70 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1); |
69 provider_->Provide(&store); | 71 provider_->Provide(&store); |
70 ASSERT_EQ(1U, store.policy_map().size()); | 72 ASSERT_EQ(1U, store.policy_map().size()); |
71 Mock::VerifyAndClearExpectations(backend_); | 73 Mock::VerifyAndClearExpectations(backend_); |
72 Mock::VerifyAndClearExpectations(&store); | 74 Mock::VerifyAndClearExpectations(&store); |
73 } | 75 } |
74 | 76 |
75 virtual void TearDown() { | 77 virtual void TearDown() { |
76 loop_.RunAllPending(); | 78 loop_.RunAllPending(); |
(...skipping 21 matching lines...) Expand all Loading... |
98 ScopedTempDir storage_dir_; | 100 ScopedTempDir storage_dir_; |
99 scoped_ptr<TokenService> token_service_; | 101 scoped_ptr<TokenService> token_service_; |
100 | 102 |
101 DISALLOW_COPY_AND_ASSIGN(DeviceManagementPolicyProviderTest); | 103 DISALLOW_COPY_AND_ASSIGN(DeviceManagementPolicyProviderTest); |
102 }; | 104 }; |
103 | 105 |
104 // If there's no login and no previously-fetched policy, the provider should | 106 // If there's no login and no previously-fetched policy, the provider should |
105 // provide an empty policy. | 107 // provide an empty policy. |
106 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideNoLogin) { | 108 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideNoLogin) { |
107 MockConfigurationPolicyStore store; | 109 MockConfigurationPolicyStore store; |
108 backend_->AllShouldSucceed(); | |
109 EXPECT_CALL(store, Apply(_, _)).Times(0); | 110 EXPECT_CALL(store, Apply(_, _)).Times(0); |
110 provider_->Provide(&store); | 111 provider_->Provide(&store); |
111 EXPECT_TRUE(store.policy_map().empty()); | 112 EXPECT_TRUE(store.policy_map().empty()); |
112 } | 113 } |
113 | 114 |
114 // If the login is successful and there's no previously-fetched policy, the | 115 // If the login is successful and there's no previously-fetched policy, the |
115 // policy should be fetched from the server and should be available the first | 116 // policy should be fetched from the server and should be available the first |
116 // time the Provide method is called. | 117 // time the Provide method is called. |
117 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideWithLogin) { | 118 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideWithLogin) { |
118 SimulateSuccessfulInitialPolicyFetch(); | 119 SimulateSuccessfulInitialPolicyFetch(); |
119 } | 120 } |
120 | 121 |
121 // If the login succeeds but the device management backend is unreachable, | 122 // If the login succeed but the device management backend is unreachable, |
122 // there should be no policy provided if there's no previously-fetched policy, | 123 // there should be no policy provided if there's no previously-fetched policy, |
123 TEST_F(DeviceManagementPolicyProviderTest, EmptyProvideWithFailedBackend) { | 124 TEST_F(DeviceManagementPolicyProviderTest, EmptyProvideWithFailedBackend) { |
124 MockConfigurationPolicyStore store; | 125 MockConfigurationPolicyStore store; |
125 backend_->AllShouldFail(); | 126 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
126 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 127 MockDeviceManagementBackendFailRegister( |
| 128 DeviceManagementBackend::kErrorRequestFailed)); |
127 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(0); | 129 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(0); |
128 SimulateSuccessfulLoginAndRunPending(); | 130 SimulateSuccessfulLoginAndRunPending(); |
129 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(0); | 131 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(0); |
130 provider_->Provide(&store); | 132 provider_->Provide(&store); |
131 EXPECT_TRUE(store.policy_map().empty()); | 133 EXPECT_TRUE(store.policy_map().empty()); |
132 } | 134 } |
133 | 135 |
134 // If a policy has been fetched previously, if should be available even before | 136 // If a policy has been fetched previously, if should be available even before |
135 // the login succeeds or the device management backend is available. | 137 // the login succeeds or the device management backend is available. |
136 TEST_F(DeviceManagementPolicyProviderTest, SecondProvide) { | 138 TEST_F(DeviceManagementPolicyProviderTest, SecondProvide) { |
137 // Pre-fetch and persist a policy | 139 // Pre-fetch and persist a policy |
138 SimulateSuccessfulInitialPolicyFetch(); | 140 SimulateSuccessfulInitialPolicyFetch(); |
139 | 141 |
140 // Simulate a app relaunch by constructing a new provider. Policy should be | 142 // Simulate a app relaunch by constructing a new provider. Policy should be |
141 // refreshed (since that might be the purpose of the app relaunch). | 143 // refreshed (since that might be the purpose of the app relaunch). |
142 CreateNewBackend(); | 144 CreateNewBackend(); |
143 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(1); | 145 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 146 MockDeviceManagementBackendSucceedBooleanPolicy( |
| 147 key::kDisableSpdy, true)); |
144 CreateNewProvider(); | 148 CreateNewProvider(); |
145 Mock::VerifyAndClearExpectations(backend_); | 149 Mock::VerifyAndClearExpectations(backend_); |
146 | 150 |
147 // Simulate another app relaunch, this time against a failing backend. | 151 // Simulate another app relaunch, this time against a failing backend. |
148 // Cached policy should still be available. | 152 // Cached policy should still be available. |
149 CreateNewBackend(); | 153 CreateNewBackend(); |
150 backend_->AllShouldFail(); | |
151 MockConfigurationPolicyStore store; | 154 MockConfigurationPolicyStore store; |
152 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(1); | 155 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 156 MockDeviceManagementBackendFailPolicy( |
| 157 DeviceManagementBackend::kErrorRequestFailed)); |
153 CreateNewProvider(); | 158 CreateNewProvider(); |
154 SimulateSuccessfulLoginAndRunPending(); | 159 SimulateSuccessfulLoginAndRunPending(); |
155 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1); | 160 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1); |
156 provider_->Provide(&store); | 161 provider_->Provide(&store); |
157 ASSERT_EQ(1U, store.policy_map().size()); | 162 ASSERT_EQ(1U, store.policy_map().size()); |
158 } | 163 } |
159 | 164 |
160 // When policy is successfully fetched from the device management server, it | 165 // When policy is successfully fetched from the device management server, it |
161 // should force a policy refresh. | 166 // should force a policy refresh. |
162 TEST_F(DeviceManagementPolicyProviderTest, FetchTriggersRefresh) { | 167 TEST_F(DeviceManagementPolicyProviderTest, FetchTriggersRefresh) { |
163 MockNotificationObserver observer; | 168 MockNotificationObserver observer; |
164 NotificationRegistrar registrar; | 169 NotificationRegistrar registrar; |
165 registrar.Add(&observer, | 170 registrar.Add(&observer, |
166 NotificationType::POLICY_CHANGED, | 171 NotificationType::POLICY_CHANGED, |
167 NotificationService::AllSources()); | 172 NotificationService::AllSources()); |
168 EXPECT_CALL(observer, Observe(_, _, _)).Times(1); | 173 EXPECT_CALL(observer, Observe(_, _, _)).Times(1); |
169 SimulateSuccessfulInitialPolicyFetch(); | 174 SimulateSuccessfulInitialPolicyFetch(); |
170 } | 175 } |
171 | 176 |
172 TEST_F(DeviceManagementPolicyProviderTest, ErrorCausesNewRequest) { | 177 TEST_F(DeviceManagementPolicyProviderTest, ErrorCausesNewRequest) { |
173 backend_->RegisterFailsOncePolicyFailsTwice(); | 178 InSequence s; |
174 SetRefreshDelays(provider_.get(), 1000 * 1000, 0, 0, 0); | 179 SetRefreshDelays(provider_.get(), 1000 * 1000, 0, 0, 0); |
175 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(2); | 180 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
176 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(3); | 181 MockDeviceManagementBackendFailRegister( |
| 182 DeviceManagementBackend::kErrorRequestFailed)); |
| 183 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 184 MockDeviceManagementBackendSucceedRegister()); |
| 185 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 186 MockDeviceManagementBackendFailPolicy( |
| 187 DeviceManagementBackend::kErrorRequestFailed)); |
| 188 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 189 MockDeviceManagementBackendFailPolicy( |
| 190 DeviceManagementBackend::kErrorRequestFailed)); |
| 191 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 192 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
177 SimulateSuccessfulLoginAndRunPending(); | 193 SimulateSuccessfulLoginAndRunPending(); |
178 } | 194 } |
179 | 195 |
180 TEST_F(DeviceManagementPolicyProviderTest, RefreshPolicies) { | 196 TEST_F(DeviceManagementPolicyProviderTest, RefreshPolicies) { |
181 backend_->AllWorksFirstPolicyFailsLater(); | 197 InSequence s; |
182 SetRefreshDelays(provider_.get(), 0, 0, 1000 * 1000, 1000); | 198 SetRefreshDelays(provider_.get(), 0, 0, 1000 * 1000, 1000); |
183 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 199 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
184 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(4); | 200 MockDeviceManagementBackendSucceedRegister()); |
| 201 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 202 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
| 203 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 204 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
| 205 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 206 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); |
| 207 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 208 MockDeviceManagementBackendFailPolicy( |
| 209 DeviceManagementBackend::kErrorRequestFailed)); |
185 SimulateSuccessfulLoginAndRunPending(); | 210 SimulateSuccessfulLoginAndRunPending(); |
186 } | 211 } |
187 | 212 |
188 } // namespace policy | 213 } // namespace policy |
OLD | NEW |