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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "chromeos/cryptohome/system_salt_getter.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/fake_cryptohome_client.h"
13 #include "chromeos/dbus/fake_dbus_thread_manager.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 // Copies the token service and increments the counter.
21 void CopyTokenServiceAndCount(
22 chromeos::DeviceOAuth2TokenService
** out_token_service
,
24 chromeos::DeviceOAuth2TokenService
* in_token_service
) {
25 *out_token_service
= in_token_service
;
29 // Sets up and tears down DeviceOAuth2TokenServiceFactory and its
30 // dependencies. Also exposes FakeDBusThreadManager.
31 class ScopedDeviceOAuth2TokenServiceFactorySetUp
{
33 ScopedDeviceOAuth2TokenServiceFactorySetUp()
34 : fake_cryptohome_client_(new FakeCryptohomeClient
) {
35 FakeDBusThreadManager
* fake_dbus_manager
= new FakeDBusThreadManager
;
36 fake_dbus_manager
->SetCryptohomeClient(
37 scoped_ptr
<CryptohomeClient
>(fake_cryptohome_client_
));
38 DBusThreadManager::InitializeForTesting(fake_dbus_manager
);
39 SystemSaltGetter::Initialize();
40 DeviceOAuth2TokenServiceFactory::Initialize();
43 ~ScopedDeviceOAuth2TokenServiceFactorySetUp() {
44 DeviceOAuth2TokenServiceFactory::Shutdown();
45 SystemSaltGetter::Shutdown();
46 DBusThreadManager::Shutdown();
49 FakeCryptohomeClient
* fake_cryptohome_client() {
50 return fake_cryptohome_client_
;
54 FakeCryptohomeClient
* fake_cryptohome_client_
;
59 class DeviceOAuth2TokenServiceFactoryTest
: public testing::Test
{
61 content::TestBrowserThreadBundle thread_bundle_
;
64 // Test a case where Get() is called before the factory is initialized.
65 TEST_F(DeviceOAuth2TokenServiceFactoryTest
, Get_Uninitialized
) {
66 DeviceOAuth2TokenService
* token_service
= NULL
;
68 DeviceOAuth2TokenServiceFactory::Get(
69 base::Bind(&CopyTokenServiceAndCount
, &token_service
, &counter
));
70 // The callback will be run asynchronously.
71 EXPECT_EQ(0, counter
);
72 EXPECT_FALSE(token_service
);
74 // This lets the factory run the callback with NULL.
75 base::RunLoop().RunUntilIdle();
76 EXPECT_EQ(1, counter
);
77 EXPECT_FALSE(token_service
);
80 // Test a case where Get() is called from only one caller.
81 TEST_F(DeviceOAuth2TokenServiceFactoryTest
, Get_Simple
) {
82 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup
;
84 DeviceOAuth2TokenService
* token_service
= NULL
;
86 DeviceOAuth2TokenServiceFactory::Get(
87 base::Bind(&CopyTokenServiceAndCount
, &token_service
, &counter
));
88 // The callback will be run asynchronously.
89 EXPECT_EQ(0, counter
);
90 EXPECT_FALSE(token_service
);
92 // This lets FakeCryptohomeClient return the system salt.
93 base::RunLoop().RunUntilIdle();
94 EXPECT_EQ(1, counter
);
95 EXPECT_TRUE(token_service
);
97 // Call Get() again, and confirm it works.
99 DeviceOAuth2TokenServiceFactory::Get(
100 base::Bind(&CopyTokenServiceAndCount
, &token_service
, &counter
));
101 base::RunLoop().RunUntilIdle();
102 EXPECT_EQ(2, counter
);
103 EXPECT_TRUE(token_service
);
106 // Test a case where Get() is called from multiple callers, before the token
107 // service is ready, and confirm that the callback of every caller is run.
108 TEST_F(DeviceOAuth2TokenServiceFactoryTest
, Get_MultipleCallers
) {
109 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup
;
111 DeviceOAuth2TokenService
* token_service1
= NULL
;
112 DeviceOAuth2TokenService
* token_service2
= NULL
;
113 DeviceOAuth2TokenService
* token_service3
= NULL
;
115 DeviceOAuth2TokenServiceFactory::Get(
116 base::Bind(&CopyTokenServiceAndCount
, &token_service1
, &counter
));
117 DeviceOAuth2TokenServiceFactory::Get(
118 base::Bind(&CopyTokenServiceAndCount
, &token_service2
, &counter
));
119 DeviceOAuth2TokenServiceFactory::Get(
120 base::Bind(&CopyTokenServiceAndCount
, &token_service3
, &counter
));
121 // The token service will be returned asynchronously.
122 EXPECT_EQ(0, counter
);
123 EXPECT_FALSE(token_service1
);
124 EXPECT_FALSE(token_service2
);
125 EXPECT_FALSE(token_service3
);
127 // This lets FakeCryptohomeClient return the system salt.
128 base::RunLoop().RunUntilIdle();
129 EXPECT_EQ(3, counter
);
130 EXPECT_TRUE(token_service1
);
131 EXPECT_TRUE(token_service2
);
132 EXPECT_TRUE(token_service3
);
134 // Make sure that token_service1,2,3 are the same one.
135 EXPECT_EQ(token_service1
, token_service2
);
136 EXPECT_EQ(token_service1
, token_service3
);
139 // Test a case where it failed to obtain the system salt.
140 TEST_F(DeviceOAuth2TokenServiceFactoryTest
, Get_NoSystemSalt
) {
141 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup
;
142 scoped_setup
.fake_cryptohome_client()->
143 set_system_salt(std::vector
<uint8
>());
145 DeviceOAuth2TokenService
* token_service
= NULL
;
147 DeviceOAuth2TokenServiceFactory::Get(
148 base::Bind(&CopyTokenServiceAndCount
, &token_service
, &counter
));
149 // The callback will be run asynchronously.
150 EXPECT_EQ(0, counter
);
151 EXPECT_FALSE(token_service
);
153 // This lets FakeCryptohomeClient return the system salt, which is empty.
154 // NULL should be returned to the callback in this case.
155 base::RunLoop().RunUntilIdle();
156 EXPECT_EQ(1, counter
);
157 EXPECT_FALSE(token_service
);
159 // Try it again, but the result should remain the same (NULL returned).
160 DeviceOAuth2TokenServiceFactory::Get(
161 base::Bind(&CopyTokenServiceAndCount
, &token_service
, &counter
));
162 base::RunLoop().RunUntilIdle();
163 EXPECT_EQ(2, counter
);
164 EXPECT_FALSE(token_service
);
167 } // namespace chromeos