OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 <memory> | |
6 #include <string> | |
7 | |
8 #include "base/command_line.h" | |
9 #include "base/files/file_path.h" | |
10 #include "base/files/scoped_temp_dir.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/run_loop.h" | |
14 #include "base/time/time.h" | |
15 #include "chrome/browser/chromeos/arc/arc_auth_service.h" | |
16 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | |
17 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
18 #include "chrome/browser/policy/profile_policy_connector.h" | |
19 #include "chrome/browser/policy/profile_policy_connector_factory.h" | |
20 #include "chrome/browser/policy/test/local_policy_test_server.h" | |
21 #include "chrome/browser/profiles/profile.h" | |
22 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h" | |
23 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
24 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | |
25 #include "chrome/common/pref_names.h" | |
26 #include "chrome/test/base/in_process_browser_test.h" | |
27 #include "chrome/test/base/testing_profile.h" | |
28 #include "chromeos/dbus/dbus_thread_manager.h" | |
29 #include "chromeos/dbus/fake_session_manager_client.h" | |
30 #include "chromeos/dbus/session_manager_client.h" | |
31 #include "components/arc/arc_bridge_service_impl.h" | |
32 #include "components/arc/arc_service_manager.h" | |
33 #include "components/arc/test/fake_arc_bridge_bootstrap.h" | |
34 #include "components/arc/test/fake_arc_bridge_instance.h" | |
35 #include "components/policy/core/common/policy_switches.h" | |
36 #include "components/prefs/pref_service.h" | |
37 #include "components/signin/core/account_id/account_id.h" | |
38 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h" | |
39 #include "components/user_manager/user_manager.h" | |
40 #include "testing/gtest/include/gtest/gtest.h" | |
41 #include "url/gurl.h" | |
42 | |
43 namespace { | |
44 | |
45 const char kRefreshToken[] = "fake-refresh-token"; | |
46 // Set managed auth token for Android managed accounts. | |
47 const char kManagedAuthToken[] = "managed-auth-token"; | |
48 // Set unmanaged auth token for other Android unmanaged accounts. | |
49 const char kUnmanagedAuthToken[] = "unmanaged-auth-token"; | |
50 const char kWellKnownConsumerName[] = "test@gmail.com"; | |
51 const char kFakeUserName[] = "test@example.com"; | |
52 | |
53 } // namespace | |
54 | |
55 namespace arc { | |
56 | |
57 // Base ArcAuthService observer. | |
58 class ArcAuthServiceObserver : public ArcAuthService::Observer { | |
59 public: | |
60 // ArcAuthService::Observer: | |
61 ~ArcAuthServiceObserver() override { | |
62 ArcAuthService::Get()->RemoveObserver(this); | |
63 } | |
64 | |
65 void Wait() { | |
66 run_loop_.reset(new base::RunLoop); | |
67 run_loop_->Run(); | |
68 run_loop_.reset(); | |
69 } | |
70 | |
71 protected: | |
72 ArcAuthServiceObserver() { ArcAuthService::Get()->AddObserver(this); } | |
73 | |
74 std::unique_ptr<base::RunLoop> run_loop_; | |
75 | |
76 private: | |
77 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceObserver); | |
78 }; | |
79 | |
80 // Observer of ArcAuthService state change. | |
81 class ArcAuthServiceStateObserver : public ArcAuthServiceObserver { | |
82 public: | |
83 ArcAuthServiceStateObserver() : ArcAuthServiceObserver() {} | |
84 | |
85 // ArcAuthService::Observer: | |
86 void OnOptInChanged(ArcAuthService::State state) override { | |
87 if (!run_loop_) | |
88 return; | |
89 run_loop_->Quit(); | |
90 } | |
91 | |
92 private: | |
93 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceStateObserver); | |
94 }; | |
95 | |
96 // Observer of ARC bridge shutdown. | |
97 class ArcAuthServiceShutdownObserver : public ArcAuthServiceObserver { | |
98 public: | |
99 ArcAuthServiceShutdownObserver() : ArcAuthServiceObserver() {} | |
100 | |
101 // ArcAuthService::Observer: | |
102 void OnShutdownBridge() override { | |
103 if (!run_loop_) | |
104 return; | |
105 run_loop_->Quit(); | |
106 } | |
107 | |
108 private: | |
109 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceShutdownObserver); | |
110 }; | |
111 | |
112 class ArcAuthServiceTest : public InProcessBrowserTest { | |
113 protected: | |
114 ArcAuthServiceTest() {} | |
115 | |
116 // InProcessBrowserTest: | |
117 ~ArcAuthServiceTest() override {} | |
118 | |
119 void SetUpInProcessBrowserTestFixture() override { | |
120 // Start test device management server. | |
121 test_server_.reset(new policy::LocalPolicyTestServer()); | |
122 ASSERT_TRUE(test_server_->Start()); | |
123 | |
124 // Specify device management server URL. | |
125 std::string url = test_server_->GetServiceURL().spec(); | |
126 base::CommandLine* const command_line = | |
127 base::CommandLine::ForCurrentProcess(); | |
128 command_line->AppendSwitchASCII(policy::switches::kDeviceManagementUrl, | |
129 url); | |
130 | |
131 // Enable ARC. | |
132 chromeos::FakeSessionManagerClient* const fake_session_manager_client = | |
133 new chromeos::FakeSessionManagerClient; | |
134 fake_session_manager_client->set_arc_available(true); | |
135 chromeos::DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( | |
136 std::unique_ptr<chromeos::SessionManagerClient>( | |
137 fake_session_manager_client)); | |
138 | |
139 // Mock out ARC bridge. | |
140 fake_arc_bridge_instance_.reset(new FakeArcBridgeInstance); | |
141 ArcServiceManager::SetArcBridgeServiceForTesting( | |
142 base::WrapUnique(new ArcBridgeServiceImpl(base::WrapUnique( | |
143 new FakeArcBridgeBootstrap(fake_arc_bridge_instance_.get()))))); | |
144 } | |
145 | |
146 void SetUpOnMainThread() override { | |
147 user_manager_enabler_.reset(new chromeos::ScopedUserManagerEnabler( | |
148 new chromeos::FakeChromeUserManager)); | |
149 // Init ArcAuthService for testing. | |
150 ArcAuthService::DisableUIForTesting(); | |
151 ArcAuthService::EnableCheckAndroidManagementForTesting(); | |
152 | |
153 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
154 | |
155 // Create test profile. | |
156 TestingProfile::Builder profile_builder; | |
157 profile_builder.SetPath(temp_dir_.path().AppendASCII("TestArcProfile")); | |
158 profile_builder.SetProfileName(kFakeUserName); | |
159 profile_builder.AddTestingFactory( | |
160 ProfileOAuth2TokenServiceFactory::GetInstance(), | |
161 BuildFakeProfileOAuth2TokenService); | |
162 profile_ = profile_builder.Build(); | |
163 token_service_ = static_cast<FakeProfileOAuth2TokenService*>( | |
164 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())); | |
165 token_service_->UpdateCredentials("", kRefreshToken); | |
166 | |
167 profile()->GetPrefs()->SetBoolean(prefs::kArcSignedIn, true); | |
168 | |
169 const AccountId account_id( | |
170 AccountId::FromUserEmailGaiaId(kFakeUserName, "1234567890")); | |
171 GetFakeUserManager()->AddUser(account_id); | |
172 GetFakeUserManager()->LoginUser(account_id); | |
173 | |
174 // Set up ARC for test profile. | |
175 ArcServiceManager::Get()->OnPrimaryUserProfilePrepared( | |
176 multi_user_util::GetAccountIdFromProfile(profile())); | |
177 ArcAuthService::Get()->OnPrimaryUserProfilePrepared(profile()); | |
178 } | |
179 | |
180 void TearDownOnMainThread() override { | |
181 ArcAuthService::Get()->Shutdown(); | |
182 profile_.reset(); | |
183 user_manager_enabler_.reset(); | |
184 } | |
185 | |
186 chromeos::FakeChromeUserManager* GetFakeUserManager() const { | |
187 return static_cast<chromeos::FakeChromeUserManager*>( | |
188 user_manager::UserManager::Get()); | |
189 } | |
190 | |
191 void set_profile_name(const std::string& username) { | |
192 profile_->set_profile_name(username); | |
193 } | |
194 | |
195 Profile* profile() { return profile_.get(); } | |
196 | |
197 FakeProfileOAuth2TokenService* token_service() { return token_service_; } | |
198 | |
199 private: | |
200 std::unique_ptr<policy::LocalPolicyTestServer> test_server_; | |
201 std::unique_ptr<FakeArcBridgeInstance> fake_arc_bridge_instance_; | |
202 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | |
203 base::ScopedTempDir temp_dir_; | |
204 std::unique_ptr<TestingProfile> profile_; | |
205 FakeProfileOAuth2TokenService* token_service_; | |
206 | |
207 DISALLOW_COPY_AND_ASSIGN(ArcAuthServiceTest); | |
208 }; | |
209 | |
210 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ConsumerAccount) { | |
211 PrefService* const prefs = profile()->GetPrefs(); | |
212 prefs->SetBoolean(prefs::kArcEnabled, true); | |
213 token_service()->IssueTokenForAllPendingRequests(kUnmanagedAuthToken, | |
214 base::Time::Max()); | |
215 ArcAuthServiceStateObserver observer; | |
216 observer.Wait(); | |
217 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); | |
218 } | |
219 | |
220 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, WellKnownConsumerAccount) { | |
221 set_profile_name(kWellKnownConsumerName); | |
222 PrefService* const prefs = profile()->GetPrefs(); | |
223 | |
224 prefs->SetBoolean(prefs::kArcEnabled, true); | |
225 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); | |
226 } | |
227 | |
228 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ManagedChromeAccount) { | |
229 policy::ProfilePolicyConnector* const connector = | |
230 policy::ProfilePolicyConnectorFactory::GetForBrowserContext(profile()); | |
231 connector->OverrideIsManagedForTesting(true); | |
232 | |
233 PrefService* const pref = profile()->GetPrefs(); | |
234 | |
235 pref->SetBoolean(prefs::kArcEnabled, true); | |
236 ASSERT_EQ(ArcAuthService::State::ACTIVE, ArcAuthService::Get()->state()); | |
237 } | |
238 | |
239 IN_PROC_BROWSER_TEST_F(ArcAuthServiceTest, ManagedAndroidAccount) { | |
240 PrefService* const prefs = profile()->GetPrefs(); | |
241 | |
242 prefs->SetBoolean(prefs::kArcEnabled, true); | |
243 token_service()->IssueTokenForAllPendingRequests(kManagedAuthToken, | |
244 base::Time::Max()); | |
245 ArcAuthServiceShutdownObserver observer; | |
246 observer.Wait(); | |
247 ASSERT_EQ(ArcAuthService::State::STOPPED, ArcAuthService::Get()->state()); | |
248 } | |
249 | |
250 } // namespace arc | |
OLD | NEW |