Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: chrome/browser/chromeos/settings/device_oauth2_token_service_factory_unittest.cc

Issue 39443002: settings: Add async system salt retrieval logic in DeviceOAuth2TokenServiceFactory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.
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 run the callback with NULL.
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 // Call Get() again, and confirm it works.
96 token_service = NULL;
97 DeviceOAuth2TokenServiceFactory::Get(
98 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter));
99 base::RunLoop().RunUntilIdle();
100 EXPECT_EQ(2, counter);
101 EXPECT_TRUE(token_service);
102 }
103
104 // Test a case where Get() is called from multiple callers, before the token
105 // service is ready, and confirm that the callback of every caller is run.
106 TEST_F(DeviceOAuth2TokenServiceFactoryTest, Get_MultipleCallers) {
107 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup;
108
109 DeviceOAuth2TokenService* token_service1 = NULL;
110 DeviceOAuth2TokenService* token_service2 = NULL;
111 DeviceOAuth2TokenService* token_service3 = NULL;
112 int counter = 0;
113 DeviceOAuth2TokenServiceFactory::Get(
114 base::Bind(&CopyTokenServiceAndCount, &token_service1, &counter));
115 DeviceOAuth2TokenServiceFactory::Get(
116 base::Bind(&CopyTokenServiceAndCount, &token_service2, &counter));
117 DeviceOAuth2TokenServiceFactory::Get(
118 base::Bind(&CopyTokenServiceAndCount, &token_service3, &counter));
119 // The token service will be returned asynchronously.
120 EXPECT_EQ(0, counter);
121 EXPECT_FALSE(token_service1);
122 EXPECT_FALSE(token_service2);
123 EXPECT_FALSE(token_service3);
124
125 // This lets FakeCryptohomeClient return the system salt.
126 base::RunLoop().RunUntilIdle();
127 EXPECT_EQ(3, counter);
128 EXPECT_TRUE(token_service1);
129 EXPECT_TRUE(token_service2);
130 EXPECT_TRUE(token_service3);
131
132 // Make sure that token_service1,2,3 are the same one.
133 EXPECT_EQ(token_service1, token_service2);
134 EXPECT_EQ(token_service1, token_service3);
135 }
136
137 // Test a case where it failed to obtain the system salt.
138 TEST_F(DeviceOAuth2TokenServiceFactoryTest, Get_NoSystemSalt) {
139 ScopedDeviceOAuth2TokenServiceFactorySetUp scoped_setup;
140 scoped_setup.fake_dbus_manager()->fake_cryptohome_client()->
141 set_system_salt(std::vector<uint8>());
142
143 DeviceOAuth2TokenService* token_service = NULL;
144 int counter = 0;
145 DeviceOAuth2TokenServiceFactory::Get(
146 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter));
147 // The callback will be run asynchronously.
148 EXPECT_EQ(0, counter);
149 EXPECT_FALSE(token_service);
150
151 // This lets FakeCryptohomeClient return the system salt, which is empty.
152 // NULL should be returned to the callback in this case.
153 base::RunLoop().RunUntilIdle();
154 EXPECT_EQ(1, counter);
155 EXPECT_FALSE(token_service);
156
157 // Try it again, but the result should remain the same (NULL returned).
158 DeviceOAuth2TokenServiceFactory::Get(
159 base::Bind(&CopyTokenServiceAndCount, &token_service, &counter));
160 base::RunLoop().RunUntilIdle();
161 EXPECT_EQ(2, counter);
162 EXPECT_FALSE(token_service);
163 }
164
165 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698