summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/cloud_policy_controller_unittest.cc
blob: b5181c96a780888386760e91d057da92c22c3a34 (plain)
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
// Copyright (c) 2011 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 "chrome/browser/policy/cloud_policy_controller.h"

#include "base/message_loop.h"
#include "base/scoped_temp_dir.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/browser/policy/cloud_policy_cache.h"
#include "chrome/browser/policy/mock_configuration_policy_store.h"
#include "chrome/browser/policy/mock_device_management_backend.h"
#include "policy/policy_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

const char kTestToken[] = "cloud_policy_controller_test_auth_token";

namespace policy {

using ::testing::_;
using ::testing::AtLeast;
using ::testing::InSequence;
using ::testing::Mock;
using ::testing::Return;

class MockCloudPolicyIdentityStrategy : public CloudPolicyIdentityStrategy {
 public:
  MockCloudPolicyIdentityStrategy() {}
  virtual ~MockCloudPolicyIdentityStrategy() {}

  MOCK_METHOD0(GetDeviceToken, std::string());
  MOCK_METHOD0(GetDeviceID, std::string());
  MOCK_METHOD2(GetCredentials, bool(std::string*, std::string*));
  virtual void OnDeviceTokenAvailable(const std::string&) {}

 private:
  DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyIdentityStrategy);
};

ACTION_P2(MockCloudPolicyIdentityStrategyGetCredentials, username, auth_token) {
  *arg0 = username;
  *arg1 = auth_token;
  return true;
}

class MockDeviceTokenFetcher : public DeviceTokenFetcher {
 public:
  explicit MockDeviceTokenFetcher(CloudPolicyCache* cache)
      : DeviceTokenFetcher(NULL, cache) {}
  virtual ~MockDeviceTokenFetcher() {}

  MOCK_METHOD0(GetDeviceToken, std::string&());
  MOCK_METHOD2(FetchToken, void(const std::string&, const std::string&));

 private:
  DISALLOW_COPY_AND_ASSIGN(MockDeviceTokenFetcher);
};

class CloudPolicyControllerTest : public testing::Test {
 public:
  CloudPolicyControllerTest()
      : ui_thread_(BrowserThread::UI, &loop_),
        file_thread_(BrowserThread::FILE, &loop_) {}

  virtual ~CloudPolicyControllerTest() {}

  virtual void SetUp() {
    ASSERT_TRUE(temp_user_data_dir_.CreateUniqueTempDir());
    cache_.reset(new CloudPolicyCache(
        temp_user_data_dir_.path().AppendASCII("CloudPolicyControllerTest")));
    token_fetcher_.reset(new MockDeviceTokenFetcher(cache_.get()));
  }

  virtual void TearDown() {
    controller_.reset();  // Unregisters observers.
  }

  // Takes ownership of |backend|.
  void CreateNewController(DeviceManagementBackend* backend) {
    controller_.reset(new CloudPolicyController(
        cache_.get(), backend, token_fetcher_.get(), &identity_strategy_));
  }

  void CreateNewController(DeviceManagementBackend* backend,
                           int64 policy_refresh_rate_ms,
                           int policy_refresh_deviation_factor_percent,
                           int64 policy_refresh_deviation_max_ms,
                           int64 policy_refresh_error_delay_ms) {
    controller_.reset(new CloudPolicyController(
        cache_.get(), backend, token_fetcher_.get(), &identity_strategy_,
        policy_refresh_rate_ms,
        policy_refresh_deviation_factor_percent,
        policy_refresh_deviation_max_ms,
        policy_refresh_error_delay_ms));
  }

  void ExpectHasSpdyPolicy() {
    MockConfigurationPolicyStore store;
    EXPECT_CALL(store, Apply(_, _)).Times(AtLeast(1));
    cache_->GetManagedPolicyProvider()->Provide(&store);
    FundamentalValue expected(true);
    ASSERT_TRUE(store.Get(kPolicyDisableSpdy) != NULL);
    EXPECT_TRUE(store.Get(kPolicyDisableSpdy)->Equals(&expected));
  }

  void SetupIdentityStrategy(const std::string& device_token,
                             const std::string& device_id,
                             const std::string& user_name,
                             const std::string& auth_token) {
    EXPECT_CALL(identity_strategy_, GetDeviceToken()).WillRepeatedly(
        Return(device_token));
    EXPECT_CALL(identity_strategy_, GetDeviceID()).WillRepeatedly(
        Return(device_id));
    if (!user_name.empty()) {
      EXPECT_CALL(identity_strategy_, GetCredentials(_, _)).WillRepeatedly(
          MockCloudPolicyIdentityStrategyGetCredentials(user_name, auth_token));
    }
  }

 protected:
  scoped_ptr<CloudPolicyCache> cache_;
  scoped_ptr<CloudPolicyController> controller_;
  scoped_ptr<MockDeviceTokenFetcher> token_fetcher_;
  MockCloudPolicyIdentityStrategy identity_strategy_;
  ScopedTempDir temp_user_data_dir_;
  MessageLoop loop_;

 private:
  BrowserThread ui_thread_;
  BrowserThread file_thread_;

  DISALLOW_COPY_AND_ASSIGN(CloudPolicyControllerTest);
};

// If a device token is present when the controller starts up, it should
// fetch and apply policy.
TEST_F(CloudPolicyControllerTest, StartupWithDeviceToken) {
  SetupIdentityStrategy("fake_device_token", "device_id", "", "");
  MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
  EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce(
      MockDeviceManagementBackendSucceedSpdyCloudPolicy());
  CreateNewController(backend);
  loop_.RunAllPending();
  ExpectHasSpdyPolicy();
}

// If no device token is present when the controller starts up, it should
// instruct the token_fetcher_ to fetch one.
TEST_F(CloudPolicyControllerTest, StartupWithoutDeviceToken) {
  SetupIdentityStrategy("", "device_id", "a@b.com", "auth_token");
  EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1);
  CreateNewController(NULL);
  loop_.RunAllPending();
}

// If the current user belongs to a known non-managed domain, no token fetch
// should be initiated.
TEST_F(CloudPolicyControllerTest, StartupUnmanagedUser) {
  SetupIdentityStrategy("", "device_id", "DannoHelper@gmail.com", "auth_token");
  EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(0);
  CreateNewController(NULL);
  loop_.RunAllPending();
}

// After policy has been fetched successfully, a new fetch should be triggered
// after the refresh interval has timed out.
TEST_F(CloudPolicyControllerTest, RefreshAfterSuccessfulPolicy) {
  SetupIdentityStrategy("device_token", "device_id",
                        "DannoHelperDelegate@b.com", "auth_token");
  MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
  EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce(
      MockDeviceManagementBackendSucceedSpdyCloudPolicy()).WillOnce(
      MockDeviceManagementBackendFailPolicy(
          DeviceManagementBackend::kErrorRequestFailed));
  CreateNewController(backend, 0, 0, 0, 1000 * 1000);
  loop_.RunAllPending();
  ExpectHasSpdyPolicy();
}

// If poliy fetching failed, it should be retried.
TEST_F(CloudPolicyControllerTest, RefreshAfterError) {
  SetupIdentityStrategy("device_token", "device_id",
                        "DannoHelperDelegateImpl@b.com", "auth_token");
  MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
  EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce(
      MockDeviceManagementBackendFailPolicy(
          DeviceManagementBackend::kErrorRequestFailed)).WillOnce(
      MockDeviceManagementBackendSucceedSpdyCloudPolicy());
  CreateNewController(backend, 1000 * 1000, 0, 0, 0);
  loop_.RunAllPending();
  ExpectHasSpdyPolicy();
}

// If the backend reports that the device token was invalid, the controller
// should instruct the token fetcher to fetch a new token.
TEST_F(CloudPolicyControllerTest, InvalidToken) {
  SetupIdentityStrategy("device_token", "device_id", "standup@ten.am", "auth");
  MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
  EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce(
      MockDeviceManagementBackendFailPolicy(
          DeviceManagementBackend::kErrorServiceManagementTokenInvalid));
  EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1);
  CreateNewController(backend);
  loop_.RunAllPending();
}

// If the backend reports that the device is unknown to the server, the
// controller should instruct the token fetcher to fetch a new token.
TEST_F(CloudPolicyControllerTest, DeviceNotFound) {
  SetupIdentityStrategy("device_token", "device_id", "me@you.com", "auth");
  MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
  EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce(
      MockDeviceManagementBackendFailPolicy(
          DeviceManagementBackend::kErrorServiceDeviceNotFound));
  EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1);
  CreateNewController(backend);
  loop_.RunAllPending();
}

// If the backend reports that the device is no longer managed, the controller
// shoud instruct the token fetcher to fetch a new token (which will in turn
// set and persist the correct 'unmanaged' state).
TEST_F(CloudPolicyControllerTest, NoLongerManaged) {
  SetupIdentityStrategy("device_token", "device_id", "who@what.com", "auth");
  MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
  EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce(
      MockDeviceManagementBackendFailPolicy(
          DeviceManagementBackend::kErrorServiceManagementNotSupported));
  EXPECT_CALL(*token_fetcher_.get(), FetchToken(_, _)).Times(1);
  CreateNewController(backend);
  loop_.RunAllPending();
}

// If the server doesn't support the new protocol, the controller should fall
// back to the old protocol.
TEST_F(CloudPolicyControllerTest, FallbackToOldProtocol) {
  SetupIdentityStrategy("device_token", "device_id", "a@b.com", "auth");
  MockDeviceManagementBackend* backend = new MockDeviceManagementBackend();
  // If the CloudPolicyRequest fails with kErrorRequestInvalid...
  EXPECT_CALL(*backend, ProcessCloudPolicyRequest(_, _, _, _)).WillOnce(
      MockDeviceManagementBackendFailPolicy(
          DeviceManagementBackend::kErrorRequestInvalid));
  // ...the client should fall back to a classic PolicyRequest,
  // and remember this fallback for any future request,
  // both after successful fetches and after errors.
  EXPECT_CALL(*backend, ProcessPolicyRequest(_, _, _, _)).WillOnce(
      MockDeviceManagementBackendSucceedBooleanPolicy(
          key::kDisableSpdy, true)).WillOnce(
      MockDeviceManagementBackendFailPolicy(
          DeviceManagementBackend::kErrorHttpStatus)).WillOnce(
      Return());
  CreateNewController(backend, 0, 0, 0, 0);
  loop_.RunAllPending();
  ExpectHasSpdyPolicy();
}

}  // namespace policy