1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/policy/device_token_fetcher.h"
6
7#include "base/file_util.h"
8#include "base/memory/scoped_temp_dir.h"
9#include "base/message_loop.h"
10#include "chrome/browser/net/gaia/token_service.h"
11#include "chrome/browser/policy/device_management_service.h"
12#include "chrome/browser/policy/mock_device_management_backend.h"
13#include "chrome/browser/policy/mock_device_management_service.h"
14#include "chrome/browser/policy/policy_notifier.h"
15#include "chrome/browser/policy/proto/device_management_backend.pb.h"
16#include "chrome/browser/policy/user_policy_cache.h"
17#include "chrome/common/net/gaia/gaia_constants.h"
18#include "chrome/test/testing_profile.h"
19#include "content/browser/browser_thread.h"
20#include "testing/gmock/include/gmock/gmock.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23namespace policy {
24
25const char kTestToken[] = "device_token_fetcher_test_auth_token";
26
27using testing::_;
28using testing::Mock;
29
30class MockTokenAvailableObserver : public DeviceTokenFetcher::Observer {
31 public:
32  MockTokenAvailableObserver() {}
33  virtual ~MockTokenAvailableObserver() {}
34
35  MOCK_METHOD0(OnDeviceTokenAvailable, void());
36
37 private:
38  DISALLOW_COPY_AND_ASSIGN(MockTokenAvailableObserver);
39};
40
41class DeviceTokenFetcherTest : public testing::Test {
42 protected:
43  DeviceTokenFetcherTest()
44      : ui_thread_(BrowserThread::UI, &loop_),
45        file_thread_(BrowserThread::FILE, &loop_) {
46    EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir());
47  }
48
49  virtual void SetUp() {
50    cache_.reset(new UserPolicyCache(
51        temp_user_data_dir_.path().AppendASCII("DeviceTokenFetcherTest")));
52    service_.set_backend(&backend_);
53  }
54
55  virtual void TearDown() {
56    loop_.RunAllPending();
57  }
58
59  MessageLoop loop_;
60  MockDeviceManagementBackend backend_;
61  MockDeviceManagementService service_;
62  scoped_ptr<CloudPolicyCacheBase> cache_;
63  PolicyNotifier notifier_;
64  ScopedTempDir temp_user_data_dir_;
65
66 private:
67  BrowserThread ui_thread_;
68  BrowserThread file_thread_;
69};
70
71TEST_F(DeviceTokenFetcherTest, FetchToken) {
72  testing::InSequence s;
73  EXPECT_CALL(backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce(
74      MockDeviceManagementBackendSucceedRegister());
75  DeviceTokenFetcher fetcher(&service_, cache_.get(), &notifier_);
76  MockTokenAvailableObserver observer;
77  EXPECT_CALL(observer, OnDeviceTokenAvailable());
78  fetcher.AddObserver(&observer);
79  EXPECT_EQ("", fetcher.GetDeviceToken());
80  fetcher.FetchToken("fake_auth_token", "fake_device_id",
81                     em::DeviceRegisterRequest::USER,
82                     "fake_machine_id", "fake_machine_model");
83  loop_.RunAllPending();
84  Mock::VerifyAndClearExpectations(&observer);
85  std::string token = fetcher.GetDeviceToken();
86  EXPECT_NE("", token);
87
88  // Calling FetchToken() again should result in a new token being fetched.
89  EXPECT_CALL(backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce(
90      MockDeviceManagementBackendSucceedRegister());
91  EXPECT_CALL(observer, OnDeviceTokenAvailable());
92  fetcher.FetchToken("fake_auth_token", "fake_device_id",
93                     em::DeviceRegisterRequest::USER,
94                     "fake_machine_id", "fake_machine_model");
95  loop_.RunAllPending();
96  Mock::VerifyAndClearExpectations(&observer);
97  std::string token2 = fetcher.GetDeviceToken();
98  EXPECT_NE("", token2);
99  EXPECT_NE(token, token2);
100  fetcher.RemoveObserver(&observer);
101}
102
103TEST_F(DeviceTokenFetcherTest, RetryOnError) {
104  testing::InSequence s;
105  EXPECT_CALL(backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce(
106      MockDeviceManagementBackendFailRegister(
107          DeviceManagementBackend::kErrorRequestFailed)).WillOnce(
108      MockDeviceManagementBackendSucceedRegister());
109  DeviceTokenFetcher fetcher(&service_, cache_.get(), &notifier_, 0, 0, 0);
110  MockTokenAvailableObserver observer;
111  EXPECT_CALL(observer, OnDeviceTokenAvailable());
112  fetcher.AddObserver(&observer);
113  fetcher.FetchToken("fake_auth_token", "fake_device_id",
114                     em::DeviceRegisterRequest::USER,
115                     "fake_machine_id", "fake_machine_model");
116  loop_.RunAllPending();
117  Mock::VerifyAndClearExpectations(&observer);
118  EXPECT_NE("", fetcher.GetDeviceToken());
119  fetcher.RemoveObserver(&observer);
120}
121
122TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) {
123  EXPECT_CALL(backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce(
124      MockDeviceManagementBackendFailRegister(
125          DeviceManagementBackend::kErrorServiceManagementNotSupported));
126  EXPECT_FALSE(cache_->is_unmanaged());
127  DeviceTokenFetcher fetcher(&service_, cache_.get(), &notifier_);
128  MockTokenAvailableObserver observer;
129  EXPECT_CALL(observer, OnDeviceTokenAvailable()).Times(0);
130  fetcher.AddObserver(&observer);
131  fetcher.FetchToken("fake_auth_token", "fake_device_id",
132                     em::DeviceRegisterRequest::USER,
133                     "fake_machine_id", "fake_machine_model");
134  loop_.RunAllPending();
135  Mock::VerifyAndClearExpectations(&observer);
136  EXPECT_EQ("", fetcher.GetDeviceToken());
137  EXPECT_TRUE(cache_->is_unmanaged());
138  fetcher.RemoveObserver(&observer);
139}
140
141}  // namespace policy
142