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 SucceedRegister()); |
66 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(1); | 66 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
67 SucceedBooleanPolicy(key::kDisableSpdy, true)); | |
67 SimulateSuccessfulLoginAndRunPending(); | 68 SimulateSuccessfulLoginAndRunPending(); |
68 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1); | 69 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1); |
69 provider_->Provide(&store); | 70 provider_->Provide(&store); |
70 ASSERT_EQ(1U, store.policy_map().size()); | 71 ASSERT_EQ(1U, store.policy_map().size()); |
71 Mock::VerifyAndClearExpectations(backend_); | 72 Mock::VerifyAndClearExpectations(backend_); |
72 Mock::VerifyAndClearExpectations(&store); | 73 Mock::VerifyAndClearExpectations(&store); |
73 } | 74 } |
74 | 75 |
75 virtual void TearDown() { | 76 virtual void TearDown() { |
76 loop_.RunAllPending(); | 77 loop_.RunAllPending(); |
(...skipping 21 matching lines...) Expand all Loading... | |
98 ScopedTempDir storage_dir_; | 99 ScopedTempDir storage_dir_; |
99 scoped_ptr<TokenService> token_service_; | 100 scoped_ptr<TokenService> token_service_; |
100 | 101 |
101 DISALLOW_COPY_AND_ASSIGN(DeviceManagementPolicyProviderTest); | 102 DISALLOW_COPY_AND_ASSIGN(DeviceManagementPolicyProviderTest); |
102 }; | 103 }; |
103 | 104 |
104 // If there's no login and no previously-fetched policy, the provider should | 105 // If there's no login and no previously-fetched policy, the provider should |
105 // provide an empty policy. | 106 // provide an empty policy. |
106 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideNoLogin) { | 107 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideNoLogin) { |
107 MockConfigurationPolicyStore store; | 108 MockConfigurationPolicyStore store; |
108 backend_->AllShouldSucceed(); | |
109 EXPECT_CALL(store, Apply(_, _)).Times(0); | 109 EXPECT_CALL(store, Apply(_, _)).Times(0); |
110 provider_->Provide(&store); | 110 provider_->Provide(&store); |
111 EXPECT_TRUE(store.policy_map().empty()); | 111 EXPECT_TRUE(store.policy_map().empty()); |
112 } | 112 } |
113 | 113 |
114 // If the login is successful and there's no previously-fetched policy, the | 114 // 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 | 115 // policy should be fetched from the server and should be available the first |
116 // time the Provide method is called. | 116 // time the Provide method is called. |
117 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideWithLogin) { | 117 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideWithLogin) { |
118 SimulateSuccessfulInitialPolicyFetch(); | 118 SimulateSuccessfulInitialPolicyFetch(); |
119 } | 119 } |
120 | 120 |
121 // If the login succeeds but the device management backend is unreachable, | 121 // If the login succeeds but the device management backend is unreachable, |
122 // there should be no policy provided if there's no previously-fetched policy, | 122 // there should be no policy provided if there's no previously-fetched policy, |
123 TEST_F(DeviceManagementPolicyProviderTest, EmptyProvideWithFailedBackend) { | 123 TEST_F(DeviceManagementPolicyProviderTest, EmptyProvideWithFailedBackend) { |
124 MockConfigurationPolicyStore store; | 124 MockConfigurationPolicyStore store; |
125 backend_->AllShouldFail(); | 125 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
126 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 126 FailRegister(DeviceManagementBackend::kErrorRequestFailed)); |
127 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(0); | 127 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(0); |
128 SimulateSuccessfulLoginAndRunPending(); | 128 SimulateSuccessfulLoginAndRunPending(); |
129 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(0); | 129 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(0); |
130 provider_->Provide(&store); | 130 provider_->Provide(&store); |
131 EXPECT_TRUE(store.policy_map().empty()); | 131 EXPECT_TRUE(store.policy_map().empty()); |
132 } | 132 } |
133 | 133 |
134 // If a policy has been fetched previously, if should be available even before | 134 // If a policy has been fetched previously, if should be available even before |
135 // the login succeeds or the device management backend is available. | 135 // the login succeeds or the device management backend is available. |
136 TEST_F(DeviceManagementPolicyProviderTest, SecondProvide) { | 136 TEST_F(DeviceManagementPolicyProviderTest, SecondProvide) { |
137 // Pre-fetch and persist a policy | 137 // Pre-fetch and persist a policy |
138 SimulateSuccessfulInitialPolicyFetch(); | 138 SimulateSuccessfulInitialPolicyFetch(); |
139 | 139 |
140 // Simulate a app relaunch by constructing a new provider. Policy should be | 140 // Simulate a app relaunch by constructing a new provider. Policy should be |
141 // refreshed (since that might be the purpose of the app relaunch). | 141 // refreshed (since that might be the purpose of the app relaunch). |
142 CreateNewBackend(); | 142 CreateNewBackend(); |
143 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(1); | 143 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
144 SucceedBooleanPolicy(key::kDisableSpdy, true)); | |
144 CreateNewProvider(); | 145 CreateNewProvider(); |
145 Mock::VerifyAndClearExpectations(backend_); | 146 Mock::VerifyAndClearExpectations(backend_); |
146 | 147 |
147 // Simulate another app relaunch, this time against a failing backend. | 148 // Simulate another app relaunch, this time against a failing backend. |
148 // Cached policy should still be available. | 149 // Cached policy should still be available. |
149 CreateNewBackend(); | 150 CreateNewBackend(); |
150 backend_->AllShouldFail(); | |
151 MockConfigurationPolicyStore store; | 151 MockConfigurationPolicyStore store; |
152 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(1); | 152 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
153 FailPolicy(DeviceManagementBackend::kErrorRequestFailed)); | |
153 CreateNewProvider(); | 154 CreateNewProvider(); |
154 SimulateSuccessfulLoginAndRunPending(); | 155 SimulateSuccessfulLoginAndRunPending(); |
155 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1); | 156 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1); |
156 provider_->Provide(&store); | 157 provider_->Provide(&store); |
157 ASSERT_EQ(1U, store.policy_map().size()); | 158 ASSERT_EQ(1U, store.policy_map().size()); |
158 } | 159 } |
159 | 160 |
160 // When policy is successfully fetched from the device management server, it | 161 // When policy is successfully fetched from the device management server, it |
161 // should force a policy refresh. | 162 // should force a policy refresh. |
162 TEST_F(DeviceManagementPolicyProviderTest, FetchTriggersRefresh) { | 163 TEST_F(DeviceManagementPolicyProviderTest, FetchTriggersRefresh) { |
163 MockNotificationObserver observer; | 164 MockNotificationObserver observer; |
164 NotificationRegistrar registrar; | 165 NotificationRegistrar registrar; |
165 registrar.Add(&observer, | 166 registrar.Add(&observer, |
166 NotificationType::POLICY_CHANGED, | 167 NotificationType::POLICY_CHANGED, |
167 NotificationService::AllSources()); | 168 NotificationService::AllSources()); |
168 EXPECT_CALL(observer, Observe(_, _, _)).Times(1); | 169 EXPECT_CALL(observer, Observe(_, _, _)).Times(1); |
169 SimulateSuccessfulInitialPolicyFetch(); | 170 SimulateSuccessfulInitialPolicyFetch(); |
170 } | 171 } |
171 | 172 |
172 TEST_F(DeviceManagementPolicyProviderTest, ErrorCausesNewRequest) { | 173 TEST_F(DeviceManagementPolicyProviderTest, ErrorCausesNewRequest) { |
173 backend_->RegisterFailsOncePolicyFailsTwice(); | 174 // backend_->RegisterFailsOncePolicyFailsTwice(); |
gfeher
2010/11/24 09:11:53
Nit: leftover code.
danno
2010/11/24 09:19:38
Done.
| |
175 InSequence s; | |
174 SetRefreshDelays(provider_.get(), 1000 * 1000, 0, 0, 0); | 176 SetRefreshDelays(provider_.get(), 1000 * 1000, 0, 0, 0); |
175 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(2); | 177 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
176 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(3); | 178 FailRegister(DeviceManagementBackend::kErrorRequestFailed)); |
179 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | |
180 SucceedRegister()); | |
181 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | |
182 FailPolicy(DeviceManagementBackend::kErrorRequestFailed)); | |
183 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | |
184 FailPolicy(DeviceManagementBackend::kErrorRequestFailed)); | |
185 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | |
186 SucceedBooleanPolicy(key::kDisableSpdy, true)); | |
177 SimulateSuccessfulLoginAndRunPending(); | 187 SimulateSuccessfulLoginAndRunPending(); |
178 } | 188 } |
179 | 189 |
180 TEST_F(DeviceManagementPolicyProviderTest, RefreshPolicies) { | 190 TEST_F(DeviceManagementPolicyProviderTest, RefreshPolicies) { |
181 backend_->AllWorksFirstPolicyFailsLater(); | 191 InSequence s; |
182 SetRefreshDelays(provider_.get(), 0, 0, 1000 * 1000, 1000); | 192 SetRefreshDelays(provider_.get(), 0, 0, 1000 * 1000, 1000); |
183 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 193 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
184 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).Times(4); | 194 SucceedRegister()); |
195 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | |
196 SucceedBooleanPolicy(key::kDisableSpdy, true)); | |
197 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | |
198 SucceedBooleanPolicy(key::kDisableSpdy, true)); | |
199 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | |
200 SucceedBooleanPolicy(key::kDisableSpdy, true)); | |
201 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( | |
202 FailPolicy(DeviceManagementBackend::kErrorRequestFailed)); | |
185 SimulateSuccessfulLoginAndRunPending(); | 203 SimulateSuccessfulLoginAndRunPending(); |
186 } | 204 } |
187 | 205 |
188 } // namespace policy | 206 } // namespace policy |
OLD | NEW |