OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h
" | 5 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h
" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "chromeos/cryptohome/system_salt_getter.h" | 10 #include "chromeos/cryptohome/system_salt_getter.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 chromeos::DeviceOAuth2TokenService* in_token_service) { | 24 chromeos::DeviceOAuth2TokenService* in_token_service) { |
25 *out_token_service = in_token_service; | 25 *out_token_service = in_token_service; |
26 ++(*counter); | 26 ++(*counter); |
27 } | 27 } |
28 | 28 |
29 // Sets up and tears down DeviceOAuth2TokenServiceFactory and its | 29 // Sets up and tears down DeviceOAuth2TokenServiceFactory and its |
30 // dependencies. Also exposes FakeDBusThreadManager. | 30 // dependencies. Also exposes FakeDBusThreadManager. |
31 class ScopedDeviceOAuth2TokenServiceFactorySetUp { | 31 class ScopedDeviceOAuth2TokenServiceFactorySetUp { |
32 public: | 32 public: |
33 ScopedDeviceOAuth2TokenServiceFactorySetUp() | 33 ScopedDeviceOAuth2TokenServiceFactorySetUp() |
34 : fake_dbus_manager_(new FakeDBusThreadManager) { | 34 : fake_cryptohome_client_(new FakeCryptohomeClient) { |
35 // Take ownership of fake_dbus_manager_. | 35 FakeDBusThreadManager* fake_dbus_manager = new FakeDBusThreadManager; |
36 DBusThreadManager::InitializeForTesting(fake_dbus_manager_); | 36 fake_dbus_manager->SetCryptohomeClient( |
| 37 scoped_ptr<CryptohomeClient>(fake_cryptohome_client_)); |
| 38 DBusThreadManager::InitializeForTesting(fake_dbus_manager); |
37 SystemSaltGetter::Initialize(); | 39 SystemSaltGetter::Initialize(); |
38 DeviceOAuth2TokenServiceFactory::Initialize(); | 40 DeviceOAuth2TokenServiceFactory::Initialize(); |
39 } | 41 } |
40 | 42 |
41 ~ScopedDeviceOAuth2TokenServiceFactorySetUp() { | 43 ~ScopedDeviceOAuth2TokenServiceFactorySetUp() { |
42 DeviceOAuth2TokenServiceFactory::Shutdown(); | 44 DeviceOAuth2TokenServiceFactory::Shutdown(); |
43 SystemSaltGetter::Shutdown(); | 45 SystemSaltGetter::Shutdown(); |
44 DBusThreadManager::Shutdown(); | 46 DBusThreadManager::Shutdown(); |
45 } | 47 } |
46 | 48 |
47 FakeDBusThreadManager* fake_dbus_manager() { | 49 FakeCryptohomeClient* fake_cryptohome_client() { |
48 return fake_dbus_manager_; | 50 return fake_cryptohome_client_; |
49 } | 51 } |
50 | 52 |
51 private: | 53 private: |
52 FakeDBusThreadManager* fake_dbus_manager_; | 54 FakeCryptohomeClient* fake_cryptohome_client_; |
53 }; | 55 }; |
54 | 56 |
55 } // namespace | 57 } // namespace |
56 | 58 |
57 class DeviceOAuth2TokenServiceFactoryTest : public testing::Test { | 59 class DeviceOAuth2TokenServiceFactoryTest : public testing::Test { |
58 protected: | 60 protected: |
59 content::TestBrowserThreadBundle thread_bundle_; | 61 content::TestBrowserThreadBundle thread_bundle_; |
60 }; | 62 }; |
61 | 63 |
62 // Test a case where Get() is called before the factory is initialized. | 64 // Test a case where Get() is called before the factory is initialized. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 EXPECT_TRUE(token_service3); | 132 EXPECT_TRUE(token_service3); |
131 | 133 |
132 // Make sure that token_service1,2,3 are the same one. | 134 // Make sure that token_service1,2,3 are the same one. |
133 EXPECT_EQ(token_service1, token_service2); | 135 EXPECT_EQ(token_service1, token_service2); |
134 EXPECT_EQ(token_service1, token_service3); | 136 EXPECT_EQ(token_service1, token_service3); |
135 } | 137 } |
136 | 138 |
137 // Test a case where it failed to obtain the system salt. | 139 // Test a case where it failed to obtain the system salt. |
138 TEST_F(DeviceOAuth2TokenServiceFactoryTest, Get_NoSystemSalt) { | 140 TEST_F(DeviceOAuth2TokenServiceFactoryTest, Get_NoSystemSalt) { |
139 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup; | 141 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup; |
140 scoped_setup.fake_dbus_manager()->fake_cryptohome_client()-> | 142 scoped_setup.fake_cryptohome_client()-> |
141 set_system_salt(std::vector<uint8>()); | 143 set_system_salt(std::vector<uint8>()); |
142 | 144 |
143 DeviceOAuth2TokenService* token_service = NULL; | 145 DeviceOAuth2TokenService* token_service = NULL; |
144 int counter = 0; | 146 int counter = 0; |
145 DeviceOAuth2TokenServiceFactory::Get( | 147 DeviceOAuth2TokenServiceFactory::Get( |
146 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter)); | 148 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter)); |
147 // The callback will be run asynchronously. | 149 // The callback will be run asynchronously. |
148 EXPECT_EQ(0, counter); | 150 EXPECT_EQ(0, counter); |
149 EXPECT_FALSE(token_service); | 151 EXPECT_FALSE(token_service); |
150 | 152 |
151 // This lets FakeCryptohomeClient return the system salt, which is empty. | 153 // This lets FakeCryptohomeClient return the system salt, which is empty. |
152 // NULL should be returned to the callback in this case. | 154 // NULL should be returned to the callback in this case. |
153 base::RunLoop().RunUntilIdle(); | 155 base::RunLoop().RunUntilIdle(); |
154 EXPECT_EQ(1, counter); | 156 EXPECT_EQ(1, counter); |
155 EXPECT_FALSE(token_service); | 157 EXPECT_FALSE(token_service); |
156 | 158 |
157 // Try it again, but the result should remain the same (NULL returned). | 159 // Try it again, but the result should remain the same (NULL returned). |
158 DeviceOAuth2TokenServiceFactory::Get( | 160 DeviceOAuth2TokenServiceFactory::Get( |
159 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter)); | 161 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter)); |
160 base::RunLoop().RunUntilIdle(); | 162 base::RunLoop().RunUntilIdle(); |
161 EXPECT_EQ(2, counter); | 163 EXPECT_EQ(2, counter); |
162 EXPECT_FALSE(token_service); | 164 EXPECT_FALSE(token_service); |
163 } | 165 } |
164 | 166 |
165 } // namespace chromeos | 167 } // namespace chromeos |
OLD | NEW |