OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h " | |
6 | |
7 #include "base/bind.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" | |
16 | |
17 namespace chromeos { | |
18 namespace { | |
19 | |
20 // Copies the token service and increments the counter. | |
21 void CopyTokenServiceAndCount( | |
22 chromeos::DeviceOAuth2TokenService** out_token_service, | |
23 int* counter, | |
24 chromeos::DeviceOAuth2TokenService* in_token_service) { | |
25 *out_token_service = in_token_service; | |
26 ++(*counter); | |
27 } | |
28 | |
29 // Sets up and tears down DeviceOAuth2TokenServiceFactory and its | |
30 // dependencies. Also exposes FakeDBusThreadManager. | |
31 class ScopedDeviceOAuth2TokenServiceFactorySetUp { | |
32 public: | |
33 ScopedDeviceOAuth2TokenServiceFactorySetUp() | |
34 : fake_dbus_manager_(new FakeDBusThreadManager) { | |
35 // Take ownership of fake_dbus_manager_. | |
36 DBusThreadManager::InitializeForTesting(fake_dbus_manager_); | |
37 SystemSaltGetter::Initialize(); | |
38 DeviceOAuth2TokenServiceFactory::Initialize(); | |
39 } | |
40 | |
41 ~ScopedDeviceOAuth2TokenServiceFactorySetUp() { | |
42 DeviceOAuth2TokenServiceFactory::Shutdown(); | |
43 SystemSaltGetter::Shutdown(); | |
44 DBusThreadManager::Shutdown(); | |
45 } | |
46 | |
47 FakeDBusThreadManager* fake_dbus_manager() { | |
48 return fake_dbus_manager_; | |
49 } | |
50 | |
51 private: | |
52 FakeDBusThreadManager* fake_dbus_manager_; | |
53 }; | |
54 | |
55 } // namespace | |
56 | |
57 class DeviceOAuth2TokenServiceFactoryTest : public testing::Test { | |
58 protected: | |
59 content::TestBrowserThreadBundle thread_bundle_; | |
60 }; | |
61 | |
62 // Test a case where Get() is called before the factory is initialized. | |
pneubeck (no reviews)
2013/10/24 19:44:13
considering how much testing this logic requires,
satorux1
2013/10/25 02:36:02
I think smaller test cases are often clearer than
pneubeck (no reviews)
2013/10/25 07:35:02
I think my comment was misleading.
I meant, if thi
| |
63 TEST_F(DeviceOAuth2TokenServiceFactoryTest, Get_Uninitialized) { | |
64 DeviceOAuth2TokenService* token_service = NULL; | |
65 int counter = 0; | |
66 DeviceOAuth2TokenServiceFactory::Get( | |
67 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter)); | |
68 // The callback will be run asynchronously. | |
69 EXPECT_EQ(0, counter); | |
70 EXPECT_FALSE(token_service); | |
71 | |
72 // This lets the factory to run callback with NULL. | |
pneubeck (no reviews)
2013/10/24 19:44:13
nit: somethings wrong with this sentence, maybe re
satorux1
2013/10/25 02:36:02
thanks. done.
| |
73 base::RunLoop().RunUntilIdle(); | |
74 EXPECT_EQ(1, counter); | |
75 EXPECT_FALSE(token_service); | |
76 } | |
77 | |
78 // Test a case where Get() is called from only one caller. | |
79 TEST_F(DeviceOAuth2TokenServiceFactoryTest, Get_Simple) { | |
80 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup; | |
81 | |
82 DeviceOAuth2TokenService* token_service = NULL; | |
83 int counter = 0; | |
84 DeviceOAuth2TokenServiceFactory::Get( | |
85 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter)); | |
86 // The callback will be run asynchronously. | |
87 EXPECT_EQ(0, counter); | |
88 EXPECT_FALSE(token_service); | |
89 | |
90 // This lets FakeCryptohomeClient return the system salt. | |
91 base::RunLoop().RunUntilIdle(); | |
92 EXPECT_EQ(1, counter); | |
93 EXPECT_TRUE(token_service); | |
94 } | |
95 | |
96 // Test a case where Get() is called from multiple callers, before the token | |
97 // service is ready, and confirm that the callback of every caller is run. | |
98 TEST_F(DeviceOAuth2TokenServiceFactoryTest, Get_MultipleCallers) { | |
pneubeck (no reviews)
2013/10/24 19:44:13
This test subsumes "Get_Simple". Maybe remove "Get
satorux1
2013/10/25 02:36:02
I wanted to separate Simple And MultipleCallers to
| |
99 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup; | |
100 | |
101 DeviceOAuth2TokenService* token_service1 = NULL; | |
102 DeviceOAuth2TokenService* token_service2 = NULL; | |
103 DeviceOAuth2TokenService* token_service3 = NULL; | |
104 int counter = 0; | |
105 DeviceOAuth2TokenServiceFactory::Get( | |
106 base::Bind(&CopyTokenServiceAndCount, &token_service1, &counter)); | |
107 DeviceOAuth2TokenServiceFactory::Get( | |
108 base::Bind(&CopyTokenServiceAndCount, &token_service2, &counter)); | |
109 DeviceOAuth2TokenServiceFactory::Get( | |
110 base::Bind(&CopyTokenServiceAndCount, &token_service3, &counter)); | |
111 // The token service will be returned asynchronously. | |
112 EXPECT_EQ(0, counter); | |
113 EXPECT_FALSE(token_service1); | |
114 EXPECT_FALSE(token_service2); | |
115 EXPECT_FALSE(token_service3); | |
116 | |
117 // This lets FakeCryptohomeClient return the system salt. | |
118 base::RunLoop().RunUntilIdle(); | |
119 EXPECT_EQ(3, counter); | |
120 EXPECT_TRUE(token_service1); | |
121 EXPECT_TRUE(token_service2); | |
122 EXPECT_TRUE(token_service3); | |
123 | |
124 // Make sure that token_service1,2,3 are the same one. | |
125 EXPECT_EQ(token_service1, token_service2); | |
126 EXPECT_EQ(token_service1, token_service3); | |
127 } | |
128 | |
129 // Test a case where it failed to obtain the system salt. | |
130 TEST_F(DeviceOAuth2TokenServiceFactoryTest, Get_NoSystemSalt) { | |
131 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup; | |
132 scoped_setup.fake_dbus_manager()->fake_cryptohome_client()-> | |
133 set_system_salt(std::vector<uint8>()); | |
134 | |
135 DeviceOAuth2TokenService* token_service = NULL; | |
136 int counter = 0; | |
137 DeviceOAuth2TokenServiceFactory::Get( | |
138 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter)); | |
139 // The callback will be run asynchronously. | |
140 EXPECT_EQ(0, counter); | |
141 EXPECT_FALSE(token_service); | |
142 | |
143 // This lets FakeCryptohomeClient return the system salt, which is empty. | |
144 // NULL should be returned to the callback in this case. | |
145 base::RunLoop().RunUntilIdle(); | |
146 EXPECT_EQ(1, counter); | |
147 EXPECT_FALSE(token_service); | |
148 | |
149 // Try it again, but the result should remain the same (NULL returned). | |
150 DeviceOAuth2TokenServiceFactory::Get( | |
151 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter)); | |
152 base::RunLoop().RunUntilIdle(); | |
153 EXPECT_EQ(2, counter); | |
154 EXPECT_FALSE(token_service); | |
155 } | |
156 | |
157 } // namespace chromeos | |
OLD | NEW |