Chromium Code Reviews| 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 16 matching lines...) Expand all Loading... | |
| 42 DeviceTokenFetcherTest() | 44 DeviceTokenFetcherTest() |
| 43 : ui_thread_(BrowserThread::UI, &loop_), | 45 : ui_thread_(BrowserThread::UI, &loop_), |
| 44 file_thread_(BrowserThread::FILE, &loop_) { | 46 file_thread_(BrowserThread::FILE, &loop_) { |
| 45 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); | 47 EXPECT_TRUE(temp_user_data_dir_.CreateUniqueTempDir()); |
| 46 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); | 48 fetcher_ = NewTestFetcher(temp_user_data_dir_.path()); |
| 47 fetcher_->StartFetching(); | 49 fetcher_->StartFetching(); |
| 48 } | 50 } |
| 49 | 51 |
| 50 virtual void TearDown() { | 52 virtual void TearDown() { |
| 51 backend_.reset(); | 53 backend_.reset(); |
| 52 token_service_.reset(); | 54 profile_.reset(); |
| 53 loop_.RunAllPending(); | 55 loop_.RunAllPending(); |
| 54 } | 56 } |
| 55 | 57 |
| 56 void SimulateSuccessfulLoginAndRunPending() { | 58 void SimulateSuccessfulLoginAndRunPending(const std::string& username) { |
| 57 loop_.RunAllPending(); | 59 loop_.RunAllPending(); |
| 58 token_service_->IssueAuthTokenForTest( | 60 profile_->GetTokenService()->IssueAuthTokenForTest( |
| 59 GaiaConstants::kDeviceManagementService, kTestToken); | 61 GaiaConstants::kDeviceManagementService, kTestToken); |
| 62 fetcher_->SimulateLogin(username); | |
| 60 loop_.RunAllPending(); | 63 loop_.RunAllPending(); |
| 61 } | 64 } |
| 62 | 65 |
| 63 DeviceTokenFetcher* NewTestFetcher(const FilePath& token_dir) { | 66 TestingDeviceTokenFetcher* NewTestFetcher(const FilePath& token_dir) { |
| 64 token_service_.reset(new TokenService); | 67 profile_.reset(new TestingProfile()); |
| 65 backend_.reset(new MockDeviceManagementBackend()); | 68 backend_.reset(new MockDeviceManagementBackend()); |
| 66 return new DeviceTokenFetcher( | 69 return new TestingDeviceTokenFetcher( |
| 67 backend_.get(), | 70 backend_.get(), |
| 68 token_service_.get(), | 71 profile_.get(), |
| 69 token_dir.Append(FILE_PATH_LITERAL("test-token-file.txt"))); | 72 token_dir.Append(FILE_PATH_LITERAL("test-token-file.txt"))); |
| 70 } | 73 } |
| 71 | 74 |
| 72 static void GetDeviceTokenPath(const DeviceTokenFetcher* fetcher, | 75 static void GetDeviceTokenPath(const DeviceTokenFetcher* fetcher, |
| 73 FilePath* path) { | 76 FilePath* path) { |
| 74 fetcher->GetDeviceTokenPath(path); | 77 fetcher->GetDeviceTokenPath(path); |
| 75 } | 78 } |
| 76 | 79 |
| 77 const std::string& device_id(const DeviceTokenFetcher* fetcher) { | 80 const std::string& device_id(const DeviceTokenFetcher* fetcher) { |
| 78 return fetcher->device_id_; | 81 return fetcher->device_id_; |
| 79 } | 82 } |
| 80 | 83 |
| 81 MessageLoop loop_; | 84 MessageLoop loop_; |
| 82 scoped_ptr<TokenService> token_service_; | |
| 83 scoped_ptr<MockDeviceManagementBackend> backend_; | 85 scoped_ptr<MockDeviceManagementBackend> backend_; |
| 84 ScopedTempDir temp_user_data_dir_; | 86 ScopedTempDir temp_user_data_dir_; |
| 85 scoped_refptr<DeviceTokenFetcher> fetcher_; | 87 scoped_refptr<TestingDeviceTokenFetcher> fetcher_; |
| 88 scoped_ptr<Profile> profile_; | |
| 86 | 89 |
| 87 private: | 90 private: |
| 88 BrowserThread ui_thread_; | 91 BrowserThread ui_thread_; |
| 89 BrowserThread file_thread_; | 92 BrowserThread file_thread_; |
| 90 }; | 93 }; |
| 91 | 94 |
| 92 TEST_F(DeviceTokenFetcherTest, IsPending) { | 95 TEST_F(DeviceTokenFetcherTest, IsPending) { |
| 93 ASSERT_TRUE(fetcher_->IsTokenPending()); | 96 ASSERT_TRUE(fetcher_->IsTokenPending()); |
| 94 backend_->AllShouldSucceed(); | 97 backend_->AllShouldSucceed(); |
| 95 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 98 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 96 SimulateSuccessfulLoginAndRunPending(); | 99 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
| 97 ASSERT_FALSE(fetcher_->IsTokenPending()); | 100 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 98 } | 101 } |
| 99 | 102 |
| 100 TEST_F(DeviceTokenFetcherTest, StoreAndLoad) { | 103 TEST_F(DeviceTokenFetcherTest, StoreAndLoad) { |
| 101 backend_->AllShouldSucceed(); | 104 backend_->AllShouldSucceed(); |
| 102 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 105 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 103 SimulateSuccessfulLoginAndRunPending(); | 106 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
| 104 ASSERT_FALSE(fetcher_->IsTokenPending()); | 107 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 105 std::string device_token = fetcher_->GetDeviceToken(); | 108 std::string device_token = fetcher_->GetDeviceToken(); |
| 106 std::string device_id = fetcher_->GetDeviceID(); | 109 std::string device_id = fetcher_->GetDeviceID(); |
| 107 ASSERT_NE("", device_id); | 110 ASSERT_NE("", device_id); |
| 108 | 111 |
| 109 FilePath token_path; | 112 FilePath token_path; |
| 110 GetDeviceTokenPath(fetcher_, &token_path); | 113 GetDeviceTokenPath(fetcher_, &token_path); |
| 111 scoped_refptr<DeviceTokenFetcher> fetcher2( | 114 scoped_refptr<DeviceTokenFetcher> fetcher2( |
| 112 new DeviceTokenFetcher(backend_.get(), token_service_.get(), token_path)); | 115 new TestingDeviceTokenFetcher( |
| 116 backend_.get(), profile_.get(), token_path)); | |
| 113 fetcher2->StartFetching(); | 117 fetcher2->StartFetching(); |
| 114 loop_.RunAllPending(); | 118 loop_.RunAllPending(); |
| 115 ASSERT_EQ(device_id, fetcher2->GetDeviceID()); | 119 ASSERT_EQ(device_id, fetcher2->GetDeviceID()); |
| 116 ASSERT_EQ(device_token, fetcher2->GetDeviceToken()); | 120 ASSERT_EQ(device_token, fetcher2->GetDeviceToken()); |
| 117 } | 121 } |
| 118 | 122 |
| 119 TEST_F(DeviceTokenFetcherTest, SimpleFetchSingleLogin) { | 123 TEST_F(DeviceTokenFetcherTest, SimpleFetchSingleLogin) { |
| 120 backend_->AllShouldSucceed(); | 124 backend_->AllShouldSucceed(); |
| 121 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 125 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 122 SimulateSuccessfulLoginAndRunPending(); | 126 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
| 123 ASSERT_FALSE(fetcher_->IsTokenPending()); | 127 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 124 ASSERT_TRUE(fetcher_->IsTokenValid()); | 128 ASSERT_TRUE(fetcher_->IsTokenValid()); |
| 125 const std::string token(fetcher_->GetDeviceToken()); | 129 const std::string token(fetcher_->GetDeviceToken()); |
| 126 EXPECT_NE("", token); | 130 EXPECT_NE("", token); |
| 127 } | 131 } |
| 128 | 132 |
| 129 TEST_F(DeviceTokenFetcherTest, SimpleFetchDoubleLogin) { | 133 TEST_F(DeviceTokenFetcherTest, SimpleFetchDoubleLogin) { |
| 130 backend_->AllShouldSucceed(); | 134 backend_->AllShouldSucceed(); |
| 131 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 135 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 132 SimulateSuccessfulLoginAndRunPending(); | 136 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
| 133 ASSERT_FALSE(fetcher_->IsTokenPending()); | 137 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 134 const std::string token(fetcher_->GetDeviceToken()); | 138 const std::string token(fetcher_->GetDeviceToken()); |
| 135 EXPECT_NE("", token); | 139 EXPECT_NE("", token); |
| 136 | 140 |
| 137 SimulateSuccessfulLoginAndRunPending(); | 141 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
| 138 ASSERT_FALSE(fetcher_->IsTokenPending()); | 142 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 139 const std::string token2(fetcher_->GetDeviceToken()); | 143 const std::string token2(fetcher_->GetDeviceToken()); |
| 140 EXPECT_NE("", token2); | 144 EXPECT_NE("", token2); |
| 141 EXPECT_EQ(token, token2); | 145 EXPECT_EQ(token, token2); |
| 142 } | 146 } |
| 143 | 147 |
| 144 TEST_F(DeviceTokenFetcherTest, FetchBetweenBrowserLaunchAndNotify) { | 148 TEST_F(DeviceTokenFetcherTest, FetchBetweenBrowserLaunchAndNotify) { |
| 145 NotificationRegistrar registrar; | 149 NotificationRegistrar registrar; |
| 146 MockTokenAvailableObserver observer; | 150 MockTokenAvailableObserver observer; |
| 147 registrar.Add(&observer, | 151 registrar.Add(&observer, |
| 148 NotificationType::DEVICE_TOKEN_AVAILABLE, | 152 NotificationType::DEVICE_TOKEN_AVAILABLE, |
| 149 NotificationService::AllSources()); | 153 NotificationService::AllSources()); |
| 150 EXPECT_CALL(observer, Observe(_, _, _)).Times(1); | 154 EXPECT_CALL(observer, Observe(_, _, _)).Times(1); |
| 151 backend_->AllShouldSucceed(); | 155 backend_->AllShouldSucceed(); |
| 152 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 156 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 153 SimulateSuccessfulLoginAndRunPending(); | 157 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
| 154 ASSERT_FALSE(fetcher_->IsTokenPending()); | 158 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 155 const std::string token(fetcher_->GetDeviceToken()); | 159 const std::string token(fetcher_->GetDeviceToken()); |
| 156 EXPECT_NE("", token); | 160 EXPECT_NE("", token); |
| 157 Mock::VerifyAndClearExpectations(&observer); | 161 Mock::VerifyAndClearExpectations(&observer); |
| 158 | 162 |
| 159 // Swap out the fetchers, including copying the device management token on | 163 // Swap out the fetchers, including copying the device management token on |
| 160 // disk to where the new fetcher expects it. | 164 // disk to where the new fetcher expects it. |
| 161 fetcher_ = NewTestFetcher( | 165 fetcher_ = NewTestFetcher( |
| 162 temp_user_data_dir_.path()); | 166 temp_user_data_dir_.path()); |
| 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 backend_->AllShouldFail(); | 177 backend_->AllShouldFail(); |
| 174 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 178 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 175 SimulateSuccessfulLoginAndRunPending(); | 179 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
| 176 ASSERT_FALSE(fetcher_->IsTokenPending()); | 180 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 177 const std::string token(fetcher_->GetDeviceToken()); | 181 const std::string token(fetcher_->GetDeviceToken()); |
| 178 EXPECT_EQ("", token); | 182 EXPECT_EQ("", token); |
| 179 } | 183 } |
| 180 | 184 |
| 181 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { | 185 TEST_F(DeviceTokenFetcherTest, UnmanagedDevice) { |
| 182 backend_->UnmanagedDevice(); | 186 backend_->UnmanagedDevice(); |
| 183 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); | 187 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(1); |
| 184 SimulateSuccessfulLoginAndRunPending(); | 188 SimulateSuccessfulLoginAndRunPending(kTestDasherDomainUsername); |
| 185 ASSERT_FALSE(fetcher_->IsTokenPending()); | 189 ASSERT_FALSE(fetcher_->IsTokenPending()); |
| 186 ASSERT_EQ("", fetcher_->GetDeviceToken()); | 190 ASSERT_EQ("", fetcher_->GetDeviceToken()); |
| 187 ASSERT_EQ("", device_id(fetcher_)); | 191 ASSERT_EQ("", device_id(fetcher_)); |
| 188 FilePath token_path; | 192 FilePath token_path; |
| 189 GetDeviceTokenPath(fetcher_, &token_path); | 193 GetDeviceTokenPath(fetcher_, &token_path); |
| 190 ASSERT_FALSE(file_util::PathExists(token_path)); | 194 ASSERT_FALSE(file_util::PathExists(token_path)); |
| 191 } | 195 } |
| 192 | 196 |
| 197 TEST_F(DeviceTokenFetcherTest, FetchBetweenTokenNotifyAndLoginNotify) { | |
| 198 backend_->AllShouldSucceed(); | |
| 199 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(0); | |
| 200 SimulateSuccessfulLoginAndRunPending("___@gmail.com"); | |
| 201 ASSERT_TRUE(fetcher_->IsTokenPending()); | |
| 202 ASSERT_FALSE(fetcher_->IsTokenValid()); | |
| 203 } | |
| 204 | |
| 205 TEST_F(DeviceTokenFetcherTest, FetchWithNonDasherUsername) { | |
| 206 backend_->AllShouldSucceed(); | |
| 207 EXPECT_CALL(*backend_, ProcessRegisterRequest(_, _, _, _)).Times(0); | |
| 208 | |
| 209 // Simulate an available user, but without available user name. | |
| 210 loop_.RunAllPending(); | |
| 211 profile_->GetTokenService()->IssueAuthTokenForTest( | |
| 212 GaiaConstants::kDeviceManagementService, kTestToken); | |
| 213 loop_.RunAllPending(); | |
| 214 | |
| 215 ASSERT_TRUE(fetcher_->IsTokenPending()); | |
|
Mattias Nissler (ping if slow)
2010/11/22 20:36:08
I think IsTokenPending() should return false at th
gfeher
2010/11/23 13:47:51
I've mixed up the names of these two tests. :/
Don
| |
| 216 ASSERT_FALSE(fetcher_->IsTokenValid()); | |
| 217 } | |
| 218 | |
| 193 } // namespace policy | 219 } // namespace policy |
| OLD | NEW |