Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: chrome/browser/policy/device_management_policy_provider_unittest.cc

Issue 5331008: Persist 'this device is not managed' information (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/file_util.h"
5 #include "base/message_loop.h" 6 #include "base/message_loop.h"
6 #include "base/scoped_temp_dir.h" 7 #include "base/scoped_temp_dir.h"
7 #include "chrome/browser/browser_thread.h" 8 #include "chrome/browser/browser_thread.h"
8 #include "chrome/browser/net/gaia/token_service.h" 9 #include "chrome/browser/net/gaia/token_service.h"
9 #include "chrome/browser/policy/configuration_policy_pref_store.h" 10 #include "chrome/browser/policy/configuration_policy_pref_store.h"
10 #include "chrome/browser/policy/device_management_policy_provider.h" 11 #include "chrome/browser/policy/device_management_policy_provider.h"
11 #include "chrome/browser/policy/mock_configuration_policy_store.h" 12 #include "chrome/browser/policy/mock_configuration_policy_store.h"
12 #include "chrome/browser/policy/mock_device_management_backend.h" 13 #include "chrome/browser/policy/mock_device_management_backend.h"
13 #include "chrome/common/net/gaia/gaia_constants.h" 14 #include "chrome/common/net/gaia/gaia_constants.h"
14 #include "chrome/common/notification_service.h" 15 #include "chrome/common/notification_service.h"
(...skipping 12 matching lines...) Expand all
27 class DeviceManagementPolicyProviderTest : public testing::Test { 28 class DeviceManagementPolicyProviderTest : public testing::Test {
28 public: 29 public:
29 DeviceManagementPolicyProviderTest() 30 DeviceManagementPolicyProviderTest()
30 : ui_thread_(BrowserThread::UI, &loop_), 31 : ui_thread_(BrowserThread::UI, &loop_),
31 file_thread_(BrowserThread::FILE, &loop_) {} 32 file_thread_(BrowserThread::FILE, &loop_) {}
32 33
33 virtual ~DeviceManagementPolicyProviderTest() {} 34 virtual ~DeviceManagementPolicyProviderTest() {}
34 35
35 virtual void SetUp() { 36 virtual void SetUp() {
36 EXPECT_TRUE(storage_dir_.CreateUniqueTempDir()); 37 EXPECT_TRUE(storage_dir_.CreateUniqueTempDir());
38 device_management_dir_ =
39 DeviceManagementPolicyProvider::GetOrCreateDeviceManagementDir(
40 storage_dir_.path());
37 CreateNewBackend(); 41 CreateNewBackend();
38 CreateNewProvider(); 42 CreateNewProviderDontRunPending();
43 EXPECT_TRUE(provider_->WaitingForInitialPolicies());
44 loop_.RunAllPending();
39 } 45 }
40 46
41 void CreateNewBackend() { 47 void CreateNewBackend() {
42 backend_ = new MockDeviceManagementBackend; 48 backend_ = new MockDeviceManagementBackend;
43 } 49 }
44 50
45 void CreateNewProvider() { 51 void CreateNewProvider() {
danno 2010/11/25 16:35:01 Probably would be clearer to have: CreateNewProvid
Jakob Kummerow 2010/11/25 16:54:24 Done. See Mattias' comment and my reply.
52 CreateNewProviderDontRunPending();
53 loop_.RunAllPending();
54 }
55
56 void CreateNewProviderDontRunPending() {
Mattias Nissler (ping if slow) 2010/11/25 16:12:36 Hm, maybe it's worth to just have one of them and
Jakob Kummerow 2010/11/25 16:28:45 Done. I went for the first option -- only provide
46 token_service_.reset(new TokenService); 57 token_service_.reset(new TokenService);
47 provider_.reset(new DeviceManagementPolicyProvider( 58 provider_.reset(new DeviceManagementPolicyProvider(
48 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), 59 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(),
49 backend_, 60 backend_,
50 token_service_.get(), 61 token_service_.get(),
51 storage_dir_.path())); 62 storage_dir_.path()));
52 loop_.RunAllPending();
53 } 63 }
54 64
55 void SimulateSuccessfulLoginAndRunPending() { 65 void SimulateSuccessfulLoginAndRunPending() {
56 loop_.RunAllPending(); 66 loop_.RunAllPending();
57 token_service_->IssueAuthTokenForTest( 67 token_service_->IssueAuthTokenForTest(
58 GaiaConstants::kDeviceManagementService, kTestToken); 68 GaiaConstants::kDeviceManagementService, kTestToken);
59 loop_.RunAllPending(); 69 loop_.RunAllPending();
60 } 70 }
61 71
62 void SimulateSuccessfulInitialPolicyFetch() { 72 void SimulateSuccessfulInitialPolicyFetch() {
63 MockConfigurationPolicyStore store; 73 MockConfigurationPolicyStore store;
64 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( 74 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce(
65 MockDeviceManagementBackendSucceedRegister()); 75 MockDeviceManagementBackendSucceedRegister());
66 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( 76 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(
67 MockDeviceManagementBackendSucceedBooleanPolicy( 77 MockDeviceManagementBackendSucceedBooleanPolicy(
68 key::kDisableSpdy, true)); 78 key::kDisableSpdy, true));
69 SimulateSuccessfulLoginAndRunPending(); 79 SimulateSuccessfulLoginAndRunPending();
80 EXPECT_FALSE(provider_->WaitingForInitialPolicies());
70 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1); 81 EXPECT_CALL(store, Apply(kPolicyDisableSpdy, _)).Times(1);
71 provider_->Provide(&store); 82 provider_->Provide(&store);
72 ASSERT_EQ(1U, store.policy_map().size()); 83 ASSERT_EQ(1U, store.policy_map().size());
73 Mock::VerifyAndClearExpectations(backend_); 84 Mock::VerifyAndClearExpectations(backend_);
74 Mock::VerifyAndClearExpectations(&store); 85 Mock::VerifyAndClearExpectations(&store);
75 } 86 }
76 87
88 void OverwriteUnmanagedDeviceTimestamp(const FilePath& storage_dir,
89 const base::Time& timestamp) {
90 DeviceManagementPolicyProvider::CreateUnmanagedDeviceMarker(
91 storage_dir, timestamp);
92 }
93
77 virtual void TearDown() { 94 virtual void TearDown() {
78 loop_.RunAllPending(); 95 loop_.RunAllPending();
79 } 96 }
80 97
81 MockDeviceManagementBackend* backend_; // weak 98 MockDeviceManagementBackend* backend_; // weak
82 scoped_ptr<DeviceManagementPolicyProvider> provider_; 99 scoped_ptr<DeviceManagementPolicyProvider> provider_;
83 100
84 protected: 101 protected:
85 void SetRefreshDelays(DeviceManagementPolicyProvider* provider, 102 void SetRefreshDelays(DeviceManagementPolicyProvider* provider,
86 int64 policy_refresh_rate_ms, 103 int64 policy_refresh_rate_ms,
87 int64 policy_refresh_max_earlier_ms, 104 int64 policy_refresh_max_earlier_ms,
88 int64 policy_refresh_error_delay_ms, 105 int64 policy_refresh_error_delay_ms,
89 int64 token_fetch_error_delay_ms) { 106 int64 token_fetch_error_delay_ms) {
90 provider->set_policy_refresh_rate_ms(policy_refresh_rate_ms); 107 provider->set_policy_refresh_rate_ms(policy_refresh_rate_ms);
91 provider->set_policy_refresh_max_earlier_ms(policy_refresh_max_earlier_ms); 108 provider->set_policy_refresh_max_earlier_ms(policy_refresh_max_earlier_ms);
92 provider->set_policy_refresh_error_delay_ms(policy_refresh_error_delay_ms); 109 provider->set_policy_refresh_error_delay_ms(policy_refresh_error_delay_ms);
93 provider->set_token_fetch_error_delay_ms(token_fetch_error_delay_ms); 110 provider->set_token_fetch_error_delay_ms(token_fetch_error_delay_ms);
94 } 111 }
95 112
113 MessageLoop loop_;
114 ScopedTempDir storage_dir_;
115 FilePath device_management_dir_;
116
96 private: 117 private:
97 MessageLoop loop_;
98 BrowserThread ui_thread_; 118 BrowserThread ui_thread_;
99 BrowserThread file_thread_; 119 BrowserThread file_thread_;
100 ScopedTempDir storage_dir_;
101 scoped_ptr<TokenService> token_service_; 120 scoped_ptr<TokenService> token_service_;
102 121
103 DISALLOW_COPY_AND_ASSIGN(DeviceManagementPolicyProviderTest); 122 DISALLOW_COPY_AND_ASSIGN(DeviceManagementPolicyProviderTest);
104 }; 123 };
105 124
106 // If there's no login and no previously-fetched policy, the provider should 125 // If there's no login and no previously-fetched policy, the provider should
107 // provide an empty policy. 126 // provide an empty policy.
108 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideNoLogin) { 127 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideNoLogin) {
109 MockConfigurationPolicyStore store; 128 MockConfigurationPolicyStore store;
110 EXPECT_CALL(store, Apply(_, _)).Times(0); 129 EXPECT_CALL(store, Apply(_, _)).Times(0);
111 provider_->Provide(&store); 130 provider_->Provide(&store);
112 EXPECT_TRUE(store.policy_map().empty()); 131 EXPECT_TRUE(store.policy_map().empty());
132 EXPECT_TRUE(provider_->WaitingForInitialPolicies());
113 } 133 }
114 134
115 // If the login is successful and there's no previously-fetched policy, the 135 // If the login is successful and there's no previously-fetched policy, the
116 // policy should be fetched from the server and should be available the first 136 // policy should be fetched from the server and should be available the first
117 // time the Provide method is called. 137 // time the Provide method is called.
118 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideWithLogin) { 138 TEST_F(DeviceManagementPolicyProviderTest, InitialProvideWithLogin) {
139 EXPECT_TRUE(provider_->WaitingForInitialPolicies());
119 SimulateSuccessfulInitialPolicyFetch(); 140 SimulateSuccessfulInitialPolicyFetch();
120 } 141 }
121 142
122 // If the login succeed but the device management backend is unreachable, 143 // If the login succeed but the device management backend is unreachable,
123 // there should be no policy provided if there's no previously-fetched policy, 144 // there should be no policy provided if there's no previously-fetched policy,
124 TEST_F(DeviceManagementPolicyProviderTest, EmptyProvideWithFailedBackend) { 145 TEST_F(DeviceManagementPolicyProviderTest, EmptyProvideWithFailedBackend) {
125 MockConfigurationPolicyStore store; 146 MockConfigurationPolicyStore store;
126 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( 147 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce(
127 MockDeviceManagementBackendFailRegister( 148 MockDeviceManagementBackendFailRegister(
128 DeviceManagementBackend::kErrorRequestFailed)); 149 DeviceManagementBackend::kErrorRequestFailed));
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( 224 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(
204 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); 225 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true));
205 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( 226 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(
206 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true)); 227 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true));
207 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( 228 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(
208 MockDeviceManagementBackendFailPolicy( 229 MockDeviceManagementBackendFailPolicy(
209 DeviceManagementBackend::kErrorRequestFailed)); 230 DeviceManagementBackend::kErrorRequestFailed));
210 SimulateSuccessfulLoginAndRunPending(); 231 SimulateSuccessfulLoginAndRunPending();
211 } 232 }
212 233
234 // This test tests three things (see numbered comments below):
235 TEST_F(DeviceManagementPolicyProviderTest, UnmanagedDevice) {
236 InSequence s;
237 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce(
238 MockDeviceManagementBackendFailRegister(
239 DeviceManagementBackend::kErrorServiceManagementNotSupported));
240 SimulateSuccessfulLoginAndRunPending();
241 // (1) Since the backend call returned "unmanaged", the corresponding marker
242 // file should have been created.
243 FilePath path(device_management_dir_.Append("UnmanagedDevice"));
244 EXPECT_TRUE(file_util::PathExists(path));
245 // Manipulate the timestamp to trick the restarted provider into re-checking
246 // immediately.
247 OverwriteUnmanagedDeviceTimestamp(
248 device_management_dir_,
249 base::Time::NowFromSystemTime() - base::TimeDelta::FromDays(2));
250 CreateNewBackend();
251 CreateNewProviderDontRunPending();
252 // (2) On restart, the provider should detect that this is not the first
253 // login.
254 EXPECT_FALSE(provider_->WaitingForInitialPolicies());
255 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce(
256 MockDeviceManagementBackendSucceedRegister());
257 EXPECT_CALL(*backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(
258 MockDeviceManagementBackendSucceedBooleanPolicy(key::kDisableSpdy, true));
259 SimulateSuccessfulLoginAndRunPending();
260 // (3) Since the backend call this time returned a device id, the "unmanaged"
261 // marker should have been deleted.
262 EXPECT_FALSE(file_util::PathExists(path));
263 }
264
213 } // namespace policy 265 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698