| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "chrome/browser/browser_thread.h" | 10 #include "chrome/browser/browser_thread.h" |
| 11 #include "chrome/browser/net/gaia/token_service.h" | 11 #include "chrome/browser/net/gaia/token_service.h" |
| 12 #include "chrome/browser/policy/device_token_fetcher.h" | 12 #include "chrome/browser/policy/device_token_fetcher.h" |
| 13 #include "chrome/browser/policy/mock_device_management_backend.h" | 13 #include "chrome/browser/policy/mock_device_management_backend.h" |
| 14 #include "chrome/common/net/gaia/gaia_constants.h" | 14 #include "chrome/common/net/gaia/gaia_constants.h" |
| 15 #include "chrome/common/notification_service.h" | 15 #include "chrome/common/notification_service.h" |
| 16 #include "chrome/test/testing_device_token_fetcher.h" |
| 17 #include "chrome/test/testing_profile.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 20 |
| 19 namespace policy { | 21 namespace policy { |
| 20 | 22 |
| 21 const char kTestToken[] = "device_token_fetcher_test_auth_token"; | 23 const char kTestToken[] = "device_token_fetcher_test_auth_token"; |
| 22 | 24 |
| 23 using testing::_; | 25 using testing::_; |
| 24 using testing::Mock; | 26 using testing::Mock; |
| 25 | 27 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 41 DeviceTokenFetcherTest() | 43 DeviceTokenFetcherTest() |
| 42 : ui_thread_(BrowserThread::UI, &loop_), | 44 : ui_thread_(BrowserThread::UI, &loop_), |
| 43 file_thread_(BrowserThread::FILE, &loop_) { | 45 file_thread_(BrowserThread::FILE, &loop_) { |
| 44 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); | 46 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); |
| 45 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); | 47 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); |
| 46 fetcher_->StartFetching(); | 48 fetcher_->StartFetching(); |
| 47 } | 49 } |
| 48 | 50 |
| 49 virtual void TearDown() { | 51 virtual void TearDown() { |
| 50 backend_.reset(); | 52 backend_.reset(); |
| 51 token_service_.reset(); | 53 profile_.reset(); |
| 52 loop_.RunAllPending(); | 54 loop_.RunAllPending(); |
| 53 } | 55 } |
| 54 | 56 |
| 55 void SimulateSuccessfulLoginAndRunPending() { | 57 void SimulateSuccessfulLoginAndRunPending(const std::string& username) { |
| 56 loop_.RunAllPending(); | 58 loop_.RunAllPending(); |
| 57 token_service_->IssueAuthTokenForTest( | 59 profile_->GetTokenService()->IssueAuthTokenForTest( |
| 58 GaiaConstants::kDeviceManagementService, kTestToken); | 60 GaiaConstants::kDeviceManagementService, kTestToken); |
| 61 fetcher_->SimulateLogin(username); |
| 59 loop_.RunAllPending(); | 62 loop_.RunAllPending(); |
| 60 } | 63 } |
| 61 | 64 |
| 62 DeviceTokenFetcher* NewTestFetcher(const FilePath& token_dir) { | 65 TestingDeviceTokenFetcher* NewTestFetcher(const FilePath& token_dir) { |
| 63 token_service_.reset(new TokenService); | 66 profile_.reset(new TestingProfile()); |
| 64 backend_.reset(new MockDeviceManagementBackend()); | 67 backend_.reset(new MockDeviceManagementBackend()); |
| 65 return new DeviceTokenFetcher( | 68 return new TestingDeviceTokenFetcher( |
| 66 backend_.get(), | 69 backend_.get(), |
| 67 token_service_.get(), | 70 profile_.get(), |
| 68 token_dir.Append(FILE_PATH_LITERAL("test-token-file.txt"))); | 71 token_dir.Append(FILE_PATH_LITERAL("test-token-file.txt"))); |
| 69 } | 72 } |
| 70 | 73 |
| 71 static void GetDeviceTokenPath(const DeviceTokenFetcher* fetcher, | 74 static void GetDeviceTokenPath(const DeviceTokenFetcher* fetcher, |
| 72 FilePath* path) { | 75 FilePath* path) { |
| 73 fetcher->GetDeviceTokenPath(path); | 76 fetcher->GetDeviceTokenPath(path); |
| 74 } | 77 } |
| 75 | 78 |
| 76 const std::string& device_id(const DeviceTokenFetcher* fetcher) { | 79 const std::string& device_id(const DeviceTokenFetcher* fetcher) { |
| 77 return fetcher->device_id_; | 80 return fetcher->device_id_; |
| 78 } | 81 } |
| 79 | 82 |
| 80 MessageLoop loop_; | 83 MessageLoop loop_; |
| 81 scoped_ptr<TokenService> token_service_; | |
| 82 scoped_ptr<MockDeviceManagementBackend> backend_; | 84 scoped_ptr<MockDeviceManagementBackend> backend_; |
| 83 ScopedTempDir temp_user_data_dir_; | 85 ScopedTempDir temp_user_data_dir_; |
| 84 scoped_refptr<DeviceTokenFetcher> fetcher_; | 86 scoped_refptr<TestingDeviceTokenFetcher> fetcher_; |
| 87 scoped_ptr<Profile> profile_; |
| 85 | 88 |
| 86 private: | 89 private: |
| 87 BrowserThread ui_thread_; | 90 BrowserThread ui_thread_; |
| 88 BrowserThread file_thread_; | 91 BrowserThread file_thread_; |
| 89 }; | 92 }; |
| 90 | 93 |
| 91 TEST_F(DeviceTokenFetcherTest, IsPending) { | 94 TEST_F(DeviceTokenFetcherTest, IsPending) { |
| 92 ASSERT_TRUE(fetcher_->IsTokenPending()); | 95 ASSERT_TRUE(fetcher_->IsTokenPending()); |
| 93 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 96 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 94 MockDeviceManagementBackendSucceedRegister()); | 97 MockDeviceManagementBackendSucceedRegister()); |
| 95 SimulateSuccessfulLoginAndRunPending(); | 98 SimulateSuccessfulLoginAndRunPending(kTestManagedDomainUsername); |
| 96 ASSERT_FALSE(fetcher_->IsTokenPending()); | 99 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 97 } | 100 } |
| 98 | 101 |
| 99 TEST_F(DeviceTokenFetcherTest, StoreAndLoad) { | 102 TEST_F(DeviceTokenFetcherTest, StoreAndLoad) { |
| 100 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 103 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 101 MockDeviceManagementBackendSucceedRegister()); | 104 MockDeviceManagementBackendSucceedRegister()); |
| 102 SimulateSuccessfulLoginAndRunPending(); | 105 SimulateSuccessfulLoginAndRunPending(kTestManagedDomainUsername); |
| 103 ASSERT_FALSE(fetcher_->IsTokenPending()); | 106 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 104 std::string device_token = fetcher_->GetDeviceToken(); | 107 std::string device_token = fetcher_->GetDeviceToken(); |
| 105 std::string device_id = fetcher_->GetDeviceID(); | 108 std::string device_id = fetcher_->GetDeviceID(); |
| 106 ASSERT_NE("", device_id); | 109 ASSERT_NE("", device_id); |
| 107 | 110 |
| 108 FilePath token_path; | 111 FilePath token_path; |
| 109 GetDeviceTokenPath(fetcher_, &token_path); | 112 GetDeviceTokenPath(fetcher_, &token_path); |
| 110 scoped_refptr<DeviceTokenFetcher> fetcher2( | 113 scoped_refptr<DeviceTokenFetcher> fetcher2( |
| 111 new DeviceTokenFetcher(backend_.get(), token_service_.get(), token_path)); | 114 new TestingDeviceTokenFetcher( |
| 115 backend_.get(), profile_.get(), token_path)); |
| 112 fetcher2->StartFetching(); | 116 fetcher2->StartFetching(); |
| 113 loop_.RunAllPending(); | 117 loop_.RunAllPending(); |
| 114 ASSERT_EQ(device_id, fetcher2->GetDeviceID()); | 118 ASSERT_EQ(device_id, fetcher2->GetDeviceID()); |
| 115 ASSERT_EQ(device_token, fetcher2->GetDeviceToken()); | 119 ASSERT_EQ(device_token, fetcher2->GetDeviceToken()); |
| 116 } | 120 } |
| 117 | 121 |
| 118 TEST_F(DeviceTokenFetcherTest, SimpleFetchSingleLogin) { | 122 TEST_F(DeviceTokenFetcherTest, SimpleFetchSingleLogin) { |
| 119 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 123 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 120 MockDeviceManagementBackendSucceedRegister()); | 124 MockDeviceManagementBackendSucceedRegister()); |
| 121 SimulateSuccessfulLoginAndRunPending(); | 125 SimulateSuccessfulLoginAndRunPending(kTestManagedDomainUsername); |
| 122 ASSERT_FALSE(fetcher_->IsTokenPending()); | 126 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 123 ASSERT_TRUE(fetcher_->IsTokenValid()); | 127 ASSERT_TRUE(fetcher_->IsTokenValid()); |
| 124 const std::string token(fetcher_->GetDeviceToken()); | 128 const std::string token(fetcher_->GetDeviceToken()); |
| 125 EXPECT_NE("", token); | 129 EXPECT_NE("", token); |
| 126 } | 130 } |
| 127 | 131 |
| 128 TEST_F(DeviceTokenFetcherTest, SimpleFetchDoubleLogin) { | 132 TEST_F(DeviceTokenFetcherTest, SimpleFetchDoubleLogin) { |
| 129 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 133 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 130 MockDeviceManagementBackendSucceedRegister()); | 134 MockDeviceManagementBackendSucceedRegister()); |
| 131 SimulateSuccessfulLoginAndRunPending(); | 135 SimulateSuccessfulLoginAndRunPending(kTestManagedDomainUsername); |
| 132 ASSERT_FALSE(fetcher_->IsTokenPending()); | 136 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 133 const std::string token(fetcher_->GetDeviceToken()); | 137 const std::string token(fetcher_->GetDeviceToken()); |
| 134 EXPECT_NE("", token); | 138 EXPECT_NE("", token); |
| 135 | 139 |
| 136 SimulateSuccessfulLoginAndRunPending(); | 140 SimulateSuccessfulLoginAndRunPending(kTestManagedDomainUsername); |
| 137 ASSERT_FALSE(fetcher_->IsTokenPending()); | 141 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 138 const std::string token2(fetcher_->GetDeviceToken()); | 142 const std::string token2(fetcher_->GetDeviceToken()); |
| 139 EXPECT_NE("", token2); | 143 EXPECT_NE("", token2); |
| 140 EXPECT_EQ(token, token2); | 144 EXPECT_EQ(token, token2); |
| 141 } | 145 } |
| 142 | 146 |
| 143 TEST_F(DeviceTokenFetcherTest, FetchBetweenBrowserLaunchAndNotify) { | 147 TEST_F(DeviceTokenFetcherTest, FetchBetweenBrowserLaunchAndNotify) { |
| 144 MockTokenAvailableObserver observer; | 148 MockTokenAvailableObserver observer; |
| 145 DeviceTokenFetcher::ObserverRegistrar registrar; | 149 DeviceTokenFetcher::ObserverRegistrar registrar; |
| 146 registrar.Init(fetcher_); | 150 registrar.Init(fetcher_); |
| 147 registrar.AddObserver(&observer); | 151 registrar.AddObserver(&observer); |
| 148 EXPECT_CALL(observer, OnTokenSuccess()).Times(1); | 152 EXPECT_CALL(observer, OnTokenSuccess()).Times(1); |
| 149 EXPECT_CALL(observer, OnTokenError()).Times(0); | 153 EXPECT_CALL(observer, OnTokenError()).Times(0); |
| 150 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 154 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 151 MockDeviceManagementBackendSucceedRegister()); | 155 MockDeviceManagementBackendSucceedRegister()); |
| 152 SimulateSuccessfulLoginAndRunPending(); | 156 SimulateSuccessfulLoginAndRunPending(kTestManagedDomainUsername); |
| 153 ASSERT_FALSE(fetcher_->IsTokenPending()); | 157 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 154 const std::string token(fetcher_->GetDeviceToken()); | 158 const std::string token(fetcher_->GetDeviceToken()); |
| 155 EXPECT_NE("", token); | 159 EXPECT_NE("", token); |
| 156 Mock::VerifyAndClearExpectations(&observer); | 160 Mock::VerifyAndClearExpectations(&observer); |
| 157 | 161 |
| 158 // Swap out the fetchers, including copying the device management token on | 162 // Swap out the fetchers, including copying the device management token on |
| 159 // disk to where the new fetcher expects it. | 163 // disk to where the new fetcher expects it. |
| 160 registrar.RemoveAll(); | 164 registrar.RemoveAll(); |
| 161 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); | 165 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); |
| 162 registrar.Init(fetcher_); | 166 registrar.Init(fetcher_); |
| 163 fetcher_->StartFetching(); | 167 fetcher_->StartFetching(); |
| 164 ASSERT_TRUE(fetcher_->IsTokenPending()); | 168 ASSERT_TRUE(fetcher_->IsTokenPending()); |
| 165 loop_.RunAllPending(); | 169 loop_.RunAllPending(); |
| 166 ASSERT_FALSE(fetcher_->IsTokenPending()); | 170 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 167 const std::string token2(fetcher_->GetDeviceToken()); | 171 const std::string token2(fetcher_->GetDeviceToken()); |
| 168 EXPECT_NE("", token2); | 172 EXPECT_NE("", token2); |
| 169 EXPECT_EQ(token, token2); | 173 EXPECT_EQ(token, token2); |
| 170 } | 174 } |
| 171 | 175 |
| 172 TEST_F(DeviceTokenFetcherTest, FailedServerRequest) { | 176 TEST_F(DeviceTokenFetcherTest, FailedServerRequest) { |
| 173 MockTokenAvailableObserver observer; | 177 MockTokenAvailableObserver observer; |
| 174 DeviceTokenFetcher::ObserverRegistrar registrar; | 178 DeviceTokenFetcher::ObserverRegistrar registrar; |
| 175 registrar.Init(fetcher_); | 179 registrar.Init(fetcher_); |
| 176 registrar.AddObserver(&observer); | 180 registrar.AddObserver(&observer); |
| 177 EXPECT_CALL(observer, OnTokenSuccess()).Times(0); | 181 EXPECT_CALL(observer, OnTokenSuccess()).Times(0); |
| 178 EXPECT_CALL(observer, OnTokenError()).Times(1); | 182 EXPECT_CALL(observer, OnTokenError()).Times(1); |
| 179 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 183 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 180 MockDeviceManagementBackendFailRegister( | 184 MockDeviceManagementBackendFailRegister( |
| 181 DeviceManagementBackend::kErrorRequestFailed)); | 185 DeviceManagementBackend::kErrorRequestFailed)); |
| 182 SimulateSuccessfulLoginAndRunPending(); | 186 SimulateSuccessfulLoginAndRunPending(kTestManagedDomainUsername); |
| 183 ASSERT_FALSE(fetcher_->IsTokenPending()); | 187 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 184 const std::string token(fetcher_->GetDeviceToken()); | 188 const std::string token(fetcher_->GetDeviceToken()); |
| 185 EXPECT_EQ("", token); | 189 EXPECT_EQ("", token); |
| 186 } | 190 } |
| 187 | 191 |
| 188 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { | 192 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { |
| 189 FilePath token_path; | 193 FilePath token_path; |
| 190 GetDeviceTokenPath(fetcher_, &token_path); | 194 GetDeviceTokenPath(fetcher_, &token_path); |
| 191 file_util::WriteFile(token_path, "foo", 3); | 195 file_util::WriteFile(token_path, "foo", 3); |
| 192 ASSERT_TRUE(file_util::PathExists(token_path)); | 196 ASSERT_TRUE(file_util::PathExists(token_path)); |
| 193 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( | 197 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).WillOnce( |
| 194 MockDeviceManagementBackendFailRegister( | 198 MockDeviceManagementBackendFailRegister( |
| 195 DeviceManagementBackend::kErrorServiceManagementNotSupported)); | 199 DeviceManagementBackend::kErrorServiceManagementNotSupported)); |
| 196 SimulateSuccessfulLoginAndRunPending(); | 200 SimulateSuccessfulLoginAndRunPending(kTestManagedDomainUsername); |
| 197 ASSERT_FALSE(fetcher_->IsTokenPending()); | 201 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 198 ASSERT_EQ("", fetcher_->GetDeviceToken()); | 202 ASSERT_EQ("", fetcher_->GetDeviceToken()); |
| 199 ASSERT_EQ("", device_id(fetcher_)); | 203 ASSERT_EQ("", device_id(fetcher_)); |
| 200 ASSERT_FALSE(file_util::PathExists(token_path)); | 204 ASSERT_FALSE(file_util::PathExists(token_path)); |
| 201 } | 205 } |
| 202 | 206 |
| 207 |
| 208 TEST_F(DeviceTokenFetcherTest, FetchBetweenTokenNotifyAndLoginNotify) { |
| 209 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(0); |
| 210 |
| 211 // Simulate an available token, but without available user name. |
| 212 loop_.RunAllPending(); |
| 213 profile_->GetTokenService()->IssueAuthTokenForTest( |
| 214 GaiaConstants::kDeviceManagementService, kTestToken); |
| 215 loop_.RunAllPending(); |
| 216 |
| 217 ASSERT_TRUE(fetcher_->IsTokenPending()); |
| 218 ASSERT_FALSE(fetcher_->IsTokenValid()); |
| 219 } |
| 220 |
| 221 TEST_F(DeviceTokenFetcherTest, FetchWithNonManagedUsername) { |
| 222 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(0); |
| 223 SimulateSuccessfulLoginAndRunPending("___@gmail.com"); |
| 224 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 225 ASSERT_FALSE(fetcher_->IsTokenValid()); |
| 226 } |
| 227 |
| 203 } // namespace policy | 228 } // namespace policy |
| OLD | NEW |