OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 "base/message_loop/message_loop.h" | |
6 #include "chrome/browser/chrome_notification_types.h" | |
7 #include "chrome/browser/chromeos/login/oauth2_login_manager.h" | |
8 #include "chrome/browser/chromeos/login/oauth2_login_manager_factory.h" | |
9 #include "chrome/browser/chromeos/login/oobe_base_test.h" | |
10 #include "chrome/browser/chromeos/login/wizard_controller.h" | |
11 #include "chrome/browser/profiles/profile_manager.h" | |
12 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" | |
13 #include "content/public/browser/notification_service.h" | |
14 #include "google_apis/gaia/gaia_constants.h" | |
15 #include "google_apis/gaia/gaia_urls.h" | |
16 | |
17 namespace chromeos { | |
18 | |
19 namespace { | |
20 | |
21 // Timeout while waiting for network connectivity during tests. | |
22 const int kTestNetworkTimeoutSeconds = 1; | |
xiyuan
2013/12/16 21:04:55
Seems not used.
zel
2013/12/16 21:27:38
Done.
| |
23 | |
24 // Email of owner account for test. | |
25 const char kTestAccountId[] = "username@gmail.com"; | |
26 | |
27 const char kTestAuthCode[] = "fake-auth-code"; | |
28 const char kTestGaiaUberToken[] = "fake-uber-token"; | |
29 const char kTestAuthLoginAccessToken[] = "fake-access-token"; | |
30 const char kTestRefreshToken[] = "fake-refresh-token"; | |
31 const char kTestSessionSIDCookie[] = "fake-session-SID-cookie"; | |
32 const char kTestSessionLSIDCookie[] = "fake-session-LSID-cookie"; | |
33 const char kTestUserinfoToken[] = "fake-userinfo-token"; | |
34 const char kTestLoginToken[] = "fake-login-token"; | |
35 const char kTestAccessToken[] = "fake-access-token"; | |
36 const char kTestSyncToken[] = "fake-sync-token"; | |
37 const char kTestAuthLoginToken[] = "fake-oauthlogin-token"; | |
38 const char kTestClientId[] = "fake-client-id"; | |
39 const char kTestAppScope[] = | |
40 "https://www.googleapis.com/auth/userinfo.profile"; | |
41 | |
42 // Note the path name must be the same as in shill stub. | |
43 const char kStubEthernetServicePath[] = "eth1"; | |
xiyuan
2013/12/16 21:04:55
Seems not used.
zel
2013/12/16 21:27:38
Done.
| |
44 | |
45 // Helper function for WaitForNetworkTimeOut. | |
46 void OnNetworkWaitTimedOut(const base::Closure& runner_quit_task) { | |
xiyuan
2013/12/16 21:04:55
Seems not used.
zel
2013/12/16 21:27:38
Done.
| |
47 runner_quit_task.Run(); | |
48 } | |
49 | |
50 } // namespace | |
51 | |
52 class OAuth2Test : public OobeBaseTest { | |
53 protected: | |
54 OAuth2Test() {} | |
55 | |
56 virtual void SetUpOnMainThread() OVERRIDE { | |
57 OobeBaseTest::SetUpOnMainThread(); | |
58 | |
59 // Configure OAuth authentication. | |
60 GaiaUrls* gaia_urls = GaiaUrls::GetInstance(); | |
61 | |
62 fake_gaia_.SetAuthTokens(kTestAuthCode, | |
63 kTestRefreshToken, | |
64 kTestAuthLoginAccessToken, | |
65 kTestGaiaUberToken, | |
66 kTestSessionSIDCookie, | |
67 kTestSessionLSIDCookie); | |
68 // This token satisfies the userinfo.email request from | |
69 // DeviceOAuth2TokenService used in token validation. | |
70 FakeGaia::AccessTokenInfo userinfo_token_info; | |
71 userinfo_token_info.token = kTestUserinfoToken; | |
72 userinfo_token_info.scopes.insert( | |
73 "https://www.googleapis.com/auth/userinfo.email"); | |
74 userinfo_token_info.audience = gaia_urls->oauth2_chrome_client_id(); | |
75 userinfo_token_info.email = kTestAccountId; | |
76 fake_gaia_.IssueOAuthToken(kTestRefreshToken, userinfo_token_info); | |
77 | |
78 FakeGaia::AccessTokenInfo userinfo_profile_token_info; | |
79 userinfo_profile_token_info.token = kTestUserinfoToken; | |
80 userinfo_profile_token_info.scopes.insert( | |
81 "https://www.googleapis.com/auth/userinfo.profile"); | |
82 userinfo_profile_token_info.audience = gaia_urls->oauth2_chrome_client_id(); | |
83 userinfo_profile_token_info.email = kTestAccountId; | |
84 fake_gaia_.IssueOAuthToken(kTestRefreshToken, userinfo_profile_token_info); | |
85 | |
86 // The any-api access token for accessing the token minting endpoint. | |
87 FakeGaia::AccessTokenInfo login_token_info; | |
88 login_token_info.token = kTestLoginToken; | |
89 login_token_info.scopes.insert(GaiaConstants::kAnyApiOAuth2Scope); | |
90 login_token_info.audience = gaia_urls->oauth2_chrome_client_id(); | |
91 fake_gaia_.IssueOAuthToken(kTestRefreshToken, login_token_info); | |
92 | |
93 // The /auth/chromesync access token for accessing sync endpoint. | |
94 FakeGaia::AccessTokenInfo sync_token_info; | |
95 sync_token_info.token = kTestSyncToken; | |
96 sync_token_info.scopes.insert(GaiaConstants::kChromeSyncOAuth2Scope); | |
97 sync_token_info.audience = gaia_urls->oauth2_chrome_client_id(); | |
98 fake_gaia_.IssueOAuthToken(kTestRefreshToken, sync_token_info); | |
99 | |
100 FakeGaia::AccessTokenInfo auth_login_token_info; | |
101 auth_login_token_info.token = kTestAuthLoginToken; | |
102 auth_login_token_info.scopes.insert(gaia_urls->oauth1_login_scope()); | |
103 auth_login_token_info.audience = gaia_urls->oauth2_chrome_client_id(); | |
104 fake_gaia_.IssueOAuthToken(kTestRefreshToken, auth_login_token_info); | |
105 } | |
106 | |
107 private: | |
108 DISALLOW_COPY_AND_ASSIGN(OAuth2Test); | |
109 }; | |
110 | |
111 class OAuth2LoginManagerStateWaiter : public OAuth2LoginManager::Observer { | |
112 public: | |
113 explicit OAuth2LoginManagerStateWaiter(Profile* profile) | |
114 : profile_(profile), | |
115 waiting_for_state_(false), | |
116 final_state_(OAuth2LoginManager::SESSION_RESTORE_NOT_STARTED) { | |
117 } | |
118 | |
119 void WaitForStates( | |
120 const std::set<OAuth2LoginManager::SessionRestoreState>& states) { | |
121 DCHECK(!waiting_for_state_); | |
122 OAuth2LoginManager* login_manager = | |
123 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile_); | |
124 states_ = states; | |
125 if (states_.find(login_manager->state()) != states_.end()) { | |
126 final_state_ = login_manager->state(); | |
127 return; | |
128 } | |
129 | |
130 waiting_for_state_ = true; | |
131 login_manager->AddObserver(this); | |
132 runner_ = new content::MessageLoopRunner; | |
133 runner_->Run(); | |
134 login_manager->RemoveObserver(this); | |
135 } | |
136 | |
137 OAuth2LoginManager::SessionRestoreState final_state() { return final_state_; } | |
138 | |
139 private: | |
140 // OAuth2LoginManager::Observer overrides. | |
141 virtual void OnSessionRestoreStateChanged( | |
142 Profile* user_profile, | |
143 OAuth2LoginManager::SessionRestoreState state) OVERRIDE { | |
144 if (!waiting_for_state_) | |
145 return; | |
146 | |
147 if (states_.find(state) == states_.end()) | |
148 return; | |
149 | |
150 OAuth2LoginManager* login_manager = | |
151 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile_); | |
152 login_manager->RemoveObserver(this); | |
xiyuan
2013/12/16 21:04:55
This is not necessary since we do that on line 134
zel
2013/12/16 21:27:38
Done.
| |
153 final_state_ = state; | |
154 waiting_for_state_ = false; | |
155 runner_->Quit(); | |
156 } | |
157 | |
158 Profile* profile_; | |
159 std::set<OAuth2LoginManager::SessionRestoreState> states_; | |
160 bool waiting_for_state_; | |
161 OAuth2LoginManager::SessionRestoreState final_state_; | |
162 scoped_refptr<content::MessageLoopRunner> runner_; | |
163 | |
164 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginManagerStateWaiter); | |
165 }; | |
166 | |
167 IN_PROC_BROWSER_TEST_F(OAuth2Test, NewUser) { | |
168 SimulateNetworkOnline(); | |
169 chromeos::WizardController::SkipPostLoginScreensForTesting(); | |
170 chromeos::WizardController* wizard_controller = | |
171 chromeos::WizardController::default_controller(); | |
172 wizard_controller->SkipToLoginForTesting(LoginScreenContext()); | |
173 | |
174 content::WindowedNotificationObserver( | |
175 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | |
176 content::NotificationService::AllSources()).Wait(); | |
177 | |
178 // Use capitalized and dotted user name on purpose to make sure | |
179 // our email normalization kicks in. | |
180 GetLoginDisplay()->ShowSigninScreenForCreds("User.Name", "password"); | |
181 | |
182 content::WindowedNotificationObserver( | |
183 chrome::NOTIFICATION_SESSION_STARTED, | |
184 content::NotificationService::AllSources()).Wait(); | |
185 | |
186 std::set<OAuth2LoginManager::SessionRestoreState> states; | |
187 states.insert(OAuth2LoginManager::SESSION_RESTORE_DONE); | |
188 states.insert(OAuth2LoginManager::SESSION_RESTORE_FAILED); | |
189 states.insert(OAuth2LoginManager::SESSION_RESTORE_CONNECTION_FAILED); | |
190 OAuth2LoginManagerStateWaiter merge_session_waiter( | |
191 ProfileManager::GetPrimaryUserProfile()); | |
192 merge_session_waiter.WaitForStates(states); | |
193 EXPECT_EQ(merge_session_waiter.final_state(), | |
194 OAuth2LoginManager::SESSION_RESTORE_DONE); | |
195 } | |
196 | |
197 } // namespace chromeos | |
OLD | NEW |