OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/password_manager/password_form_manager.h" | 9 #include "chrome/browser/password_manager/password_form_manager.h" |
10 #include "chrome/browser/password_manager/password_manager.h" | 10 #include "chrome/browser/password_manager/password_manager.h" |
(...skipping 15 matching lines...) Expand all Loading... | |
26 observed_form_.submit_element = ASCIIToUTF16("signIn"); | 26 observed_form_.submit_element = ASCIIToUTF16("signIn"); |
27 observed_form_.signon_realm = "http://www.google.com"; | 27 observed_form_.signon_realm = "http://www.google.com"; |
28 | 28 |
29 saved_match_ = observed_form_; | 29 saved_match_ = observed_form_; |
30 saved_match_.origin = GURL("http://www.google.com/a/ServiceLoginAuth"); | 30 saved_match_.origin = GURL("http://www.google.com/a/ServiceLoginAuth"); |
31 saved_match_.action = GURL("http://www.google.com/a/ServiceLogin"); | 31 saved_match_.action = GURL("http://www.google.com/a/ServiceLogin"); |
32 saved_match_.preferred = true; | 32 saved_match_.preferred = true; |
33 saved_match_.username_value = ASCIIToUTF16("test@gmail.com"); | 33 saved_match_.username_value = ASCIIToUTF16("test@gmail.com"); |
34 saved_match_.password_value = ASCIIToUTF16("test1"); | 34 saved_match_.password_value = ASCIIToUTF16("test1"); |
35 profile_ = new TestingProfile(); | 35 profile_ = new TestingProfile(); |
36 | |
37 // A form for testing a case there's no username (there's only password). | |
38 observed_form2_.origin = GURL("http://www.google.com/b/LoginAuth"); | |
39 observed_form2_.action = GURL("http://www.google.com/b/Login"); | |
40 observed_form2_.password_element = ASCIIToUTF16("Passwd"); | |
41 observed_form2_.submit_element = ASCIIToUTF16("signIn"); | |
42 observed_form2_.signon_realm = "http://www.google.com"; | |
43 | |
44 saved_match2_ = observed_form_; | |
45 saved_match2_.origin = GURL("http://www.google.com/a/ServiceLoginAuth"); | |
46 saved_match2_.action = GURL("http://www.google.com/a/ServiceLogin"); | |
47 saved_match2_.preferred = true; | |
48 saved_match2_.password_value = ASCIIToUTF16("test2"); | |
49 profile2_ = new TestingProfile(); | |
36 } | 50 } |
37 | 51 |
38 virtual void TearDown() { | 52 virtual void TearDown() { |
39 delete profile_; | 53 delete profile_; |
54 delete profile2_; | |
40 } | 55 } |
41 | 56 |
42 PasswordForm* GetPendingCredentials(PasswordFormManager* p) { | 57 PasswordForm* GetPendingCredentials(PasswordFormManager* p) { |
43 return &p->pending_credentials_; | 58 return &p->pending_credentials_; |
44 } | 59 } |
45 | 60 |
46 void SimulateMatchingPhase(PasswordFormManager* p, bool find_match) { | 61 void SimulateMatchingPhase(PasswordFormManager* p, bool find_match) { |
47 // Roll up the state to mock out the matching phase. | 62 // Roll up the state to mock out the matching phase. |
48 p->state_ = PasswordFormManager::POST_MATCHING_PHASE; | 63 p->state_ = PasswordFormManager::POST_MATCHING_PHASE; |
49 if (!find_match) | 64 if (!find_match) |
50 return; | 65 return; |
51 | 66 |
52 PasswordForm* match = new PasswordForm(saved_match_); | 67 PasswordForm* match = new PasswordForm(saved_match_); |
53 // Heap-allocated form is owned by p. | 68 // Heap-allocated form is owned by p. |
54 p->best_matches_[match->username_value] = match; | 69 p->best_matches_[match->username_value] = match; |
55 p->preferred_match_ = match; | 70 p->preferred_match_ = match; |
56 } | 71 } |
57 | 72 |
58 bool IgnoredResult(PasswordFormManager* p, PasswordForm* form) { | 73 bool IgnoredResult(PasswordFormManager* p, PasswordForm* form) { |
59 return p->IgnoreResult(*form); | 74 return p->IgnoreResult(*form); |
60 } | 75 } |
61 | 76 |
62 Profile* profile() { return profile_; } | 77 Profile* profile() { return profile_; } |
78 Profile* profile2() { return profile2_; } | |
63 | 79 |
64 PasswordForm* observed_form() { return &observed_form_; } | 80 PasswordForm* observed_form() { return &observed_form_; } |
65 PasswordForm* saved_match() { return &saved_match_; } | 81 PasswordForm* saved_match() { return &saved_match_; } |
82 PasswordForm* observed_form2() { return &observed_form2_; } | |
83 PasswordForm* saved_match2() { return &saved_match2_; } | |
84 | |
66 | 85 |
67 private: | 86 private: |
68 PasswordForm observed_form_; | 87 PasswordForm observed_form_; |
69 PasswordForm saved_match_; | 88 PasswordForm saved_match_; |
70 Profile* profile_; | 89 Profile* profile_; |
90 PasswordForm observed_form2_; | |
91 PasswordForm saved_match2_; | |
92 Profile* profile2_; | |
71 }; | 93 }; |
72 | 94 |
73 TEST_F(PasswordFormManagerTest, TestNewLogin) { | 95 TEST_F(PasswordFormManagerTest, TestNewLogin) { |
74 PasswordFormManager* manager = new PasswordFormManager( | 96 PasswordFormManager* manager = new PasswordFormManager( |
75 profile(), NULL, *observed_form(), false); | 97 profile(), NULL, *observed_form(), false); |
76 SimulateMatchingPhase(manager, false); | 98 SimulateMatchingPhase(manager, false); |
77 // User submits credentials for the observed form. | 99 // User submits credentials for the observed form. |
78 PasswordForm credentials = *observed_form(); | 100 PasswordForm credentials = *observed_form(); |
79 credentials.username_value = saved_match()->username_value; | 101 credentials.username_value = saved_match()->username_value; |
80 credentials.password_value = saved_match()->password_value; | 102 credentials.password_value = saved_match()->password_value; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 GetPendingCredentials(manager)->signon_realm); | 138 GetPendingCredentials(manager)->signon_realm); |
117 EXPECT_TRUE(GetPendingCredentials(manager)->preferred); | 139 EXPECT_TRUE(GetPendingCredentials(manager)->preferred); |
118 EXPECT_EQ(new_pass, | 140 EXPECT_EQ(new_pass, |
119 GetPendingCredentials(manager)->password_value); | 141 GetPendingCredentials(manager)->password_value); |
120 EXPECT_EQ(new_user, | 142 EXPECT_EQ(new_user, |
121 GetPendingCredentials(manager)->username_value); | 143 GetPendingCredentials(manager)->username_value); |
122 // Done. | 144 // Done. |
123 delete manager; | 145 delete manager; |
124 } | 146 } |
125 | 147 |
148 TEST_F(PasswordFormManagerTest, TestNewLoginAndUpdateWithoutUsername) { | |
149 PasswordFormManager* manager = new PasswordFormManager( | |
150 profile(), NULL, *observed_form2(), false); | |
tim (not reviewing)
2012/04/03 16:40:27
Why do we need observed_form2 It seems tests use e
Yumikiyo Osanai
2012/04/05 00:19:50
I basically agree with your idea.
I've removed th
| |
151 SimulateMatchingPhase(manager, false); | |
152 | |
153 // User submits credentials for the observed form. | |
154 PasswordForm credentials = *observed_form2(); | |
155 credentials.username_value = saved_match2()->username_value; | |
156 credentials.password_value = saved_match2()->password_value; | |
157 credentials.preferred = true; | |
158 manager->ProvisionallySave(credentials); | |
159 | |
160 // Successful login. The PasswordManager would instruct PasswordFormManager | |
161 // to save, which should know this is a new login. | |
162 EXPECT_TRUE(manager->IsNewLogin()); | |
163 // Make sure the credentials that would be submitted on successful login | |
164 // are going to match the stored entry in the db. | |
165 EXPECT_EQ(observed_form2()->origin.spec(), | |
166 GetPendingCredentials(manager)->origin.spec()); | |
167 EXPECT_EQ(observed_form2()->signon_realm, | |
168 GetPendingCredentials(manager)->signon_realm); | |
169 EXPECT_TRUE(GetPendingCredentials(manager)->preferred); | |
170 EXPECT_EQ(saved_match2()->password_value, | |
171 GetPendingCredentials(manager)->password_value); | |
172 EXPECT_EQ(saved_match2()->username_value, | |
173 GetPendingCredentials(manager)->username_value); | |
174 | |
175 // Now, suppose the user re-visits the site and wants to save an additional | |
176 // login for the site with a new password. In this case, the matching phase | |
177 // will yield the previously saved login. | |
178 SimulateMatchingPhase(manager, true); | |
179 | |
180 // Set up the new login. | |
181 string16 new_pass = ASCIIToUTF16("newpass2"); | |
182 credentials.password_value = new_pass; | |
183 manager->ProvisionallySave(credentials); | |
184 | |
185 // Again, the PasswordFormManager should know this is still a new login. | |
186 EXPECT_TRUE(manager->IsNewLogin()); | |
187 | |
188 // And make sure everything squares up again. | |
189 EXPECT_EQ(observed_form2()->origin.spec(), | |
190 GetPendingCredentials(manager)->origin.spec()); | |
191 EXPECT_EQ(observed_form2()->signon_realm, | |
192 GetPendingCredentials(manager)->signon_realm); | |
193 EXPECT_TRUE(GetPendingCredentials(manager)->preferred); | |
194 EXPECT_EQ(new_pass, | |
195 GetPendingCredentials(manager)->password_value); | |
196 | |
197 // Done. | |
198 delete manager; | |
199 } | |
200 | |
201 | |
126 TEST_F(PasswordFormManagerTest, TestUpdatePassword) { | 202 TEST_F(PasswordFormManagerTest, TestUpdatePassword) { |
127 // Create a PasswordFormManager with observed_form, as if we just | 203 // Create a PasswordFormManager with observed_form, as if we just |
128 // saw this form and need to find matching logins. | 204 // saw this form and need to find matching logins. |
129 PasswordFormManager* manager = new PasswordFormManager( | 205 PasswordFormManager* manager = new PasswordFormManager( |
130 profile(), NULL, *observed_form(), false); | 206 profile(), NULL, *observed_form(), false); |
131 SimulateMatchingPhase(manager, true); | 207 SimulateMatchingPhase(manager, true); |
132 | 208 |
133 // User submits credentials for the observed form using a username previously | 209 // User submits credentials for the observed form using a username previously |
134 // stored, but a new password. Note that the observed form may have different | 210 // stored, but a new password. Note that the observed form may have different |
135 // origin URL (as it does in this case) than the saved_match, but we want to | 211 // origin URL (as it does in this case) than the saved_match, but we want to |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 login.username_value = saved_match()->username_value; | 266 login.username_value = saved_match()->username_value; |
191 login.password_value = saved_match()->password_value; | 267 login.password_value = saved_match()->password_value; |
192 manager->ProvisionallySave(login); | 268 manager->ProvisionallySave(login); |
193 EXPECT_FALSE(manager->IsNewLogin()); | 269 EXPECT_FALSE(manager->IsNewLogin()); |
194 // We bless our saved PasswordForm entry with the action URL of the | 270 // We bless our saved PasswordForm entry with the action URL of the |
195 // observed form. | 271 // observed form. |
196 EXPECT_EQ(observed_form()->action, | 272 EXPECT_EQ(observed_form()->action, |
197 GetPendingCredentials(manager.get())->action); | 273 GetPendingCredentials(manager.get())->action); |
198 } | 274 } |
199 | 275 |
276 TEST_F(PasswordFormManagerTest, TestEmptyActionWithoutUsername) { | |
277 scoped_ptr<PasswordFormManager> manager(new PasswordFormManager( | |
278 profile(), NULL, *observed_form2(), false)); | |
279 | |
280 saved_match2()->action = GURL(); | |
281 SimulateMatchingPhase(manager.get(), true); | |
282 // User logs in with the autofilled username / password from saved_match. | |
283 PasswordForm login = *observed_form2(); | |
284 login.password_value = saved_match2()->password_value; | |
285 manager->ProvisionallySave(login); | |
286 | |
287 // If there's no username, we supporse it's the new login. | |
288 EXPECT_TRUE(manager->IsNewLogin()); | |
289 // We bless our saved PasswordForm entry with the action URL of the | |
290 // observed form. | |
291 EXPECT_EQ(observed_form2()->action, | |
292 GetPendingCredentials(manager.get())->action); | |
293 } | |
294 | |
200 TEST_F(PasswordFormManagerTest, TestValidForms) { | 295 TEST_F(PasswordFormManagerTest, TestValidForms) { |
201 // User submits credentials for the observed form. | 296 // User submits credentials for the observed form. |
202 PasswordForm credentials = *observed_form(); | 297 PasswordForm credentials = *observed_form(); |
203 credentials.scheme = PasswordForm::SCHEME_HTML; | 298 credentials.scheme = PasswordForm::SCHEME_HTML; |
204 credentials.username_value = saved_match()->username_value; | 299 credentials.username_value = saved_match()->username_value; |
205 credentials.password_value = saved_match()->password_value; | 300 credentials.password_value = saved_match()->password_value; |
206 | 301 |
207 // Form with both username_element and password_element. | 302 // Form with both username_element and password_element. |
208 PasswordFormManager manager1(profile(), NULL, credentials, false); | 303 PasswordFormManager manager1(profile(), NULL, credentials, false); |
209 SimulateMatchingPhase(&manager1, false); | 304 SimulateMatchingPhase(&manager1, false); |
210 EXPECT_TRUE(manager1.HasValidPasswordForm()); | 305 EXPECT_TRUE(manager1.HasValidPasswordForm()); |
211 | 306 |
212 // Form without a username_element but with a password_element. | 307 // Form without a username_element but with a password_element. |
308 // It should be true, because we should save and autofill a password | |
309 // if there isn't any username. | |
213 credentials.username_element.clear(); | 310 credentials.username_element.clear(); |
214 PasswordFormManager manager2(profile(), NULL, credentials, false); | 311 PasswordFormManager manager2(profile(), NULL, credentials, false); |
215 SimulateMatchingPhase(&manager2, false); | 312 SimulateMatchingPhase(&manager2, false); |
216 EXPECT_FALSE(manager2.HasValidPasswordForm()); | 313 EXPECT_TRUE(manager2.HasValidPasswordForm()); |
217 | 314 |
218 // Form without a password_element but with a username_element. | 315 // Form without a password_element but with a username_element. |
219 credentials.username_element = saved_match()->username_element; | 316 credentials.username_element = saved_match()->username_element; |
220 credentials.password_element.clear(); | 317 credentials.password_element.clear(); |
221 PasswordFormManager manager3(profile(), NULL, credentials, false); | 318 PasswordFormManager manager3(profile(), NULL, credentials, false); |
222 SimulateMatchingPhase(&manager3, false); | 319 SimulateMatchingPhase(&manager3, false); |
223 EXPECT_FALSE(manager3.HasValidPasswordForm()); | 320 EXPECT_FALSE(manager3.HasValidPasswordForm()); |
224 | 321 |
225 // Form with neither a password_element nor a username_element. | 322 // Form with neither a password_element nor a username_element. |
226 credentials.username_element.clear(); | 323 credentials.username_element.clear(); |
227 credentials.password_element.clear(); | 324 credentials.password_element.clear(); |
228 PasswordFormManager manager4(profile(), NULL, credentials, false); | 325 PasswordFormManager manager4(profile(), NULL, credentials, false); |
229 SimulateMatchingPhase(&manager4, false); | 326 SimulateMatchingPhase(&manager4, false); |
230 EXPECT_FALSE(manager4.HasValidPasswordForm()); | 327 EXPECT_FALSE(manager4.HasValidPasswordForm()); |
231 } | 328 } |
232 | 329 |
330 TEST_F(PasswordFormManagerTest, TestValidFormsWithNoUsername) { | |
331 // User submits credentials for the observed form. | |
332 PasswordForm credentials = *observed_form2(); | |
333 credentials.scheme = PasswordForm::SCHEME_HTML; | |
334 credentials.password_value = saved_match2()->password_value; | |
335 | |
336 // Form with a password_element. | |
337 PasswordFormManager manager1(profile2(), NULL, credentials, false); | |
338 SimulateMatchingPhase(&manager1, false); | |
339 EXPECT_TRUE(manager1.HasValidPasswordForm()); | |
340 | |
341 // Form with no a password_element | |
342 credentials.password_element.clear(); | |
343 PasswordFormManager manager2(profile2(), NULL, credentials, false); | |
344 SimulateMatchingPhase(&manager2, false); | |
345 EXPECT_FALSE(manager2.HasValidPasswordForm()); | |
346 } | |
347 | |
233 TEST_F(PasswordFormManagerTest, TestValidFormsBasic) { | 348 TEST_F(PasswordFormManagerTest, TestValidFormsBasic) { |
234 // User submits credentials for the observed form. | 349 // User submits credentials for the observed form. |
235 PasswordForm credentials = *observed_form(); | 350 PasswordForm credentials = *observed_form(); |
236 credentials.scheme = PasswordForm::SCHEME_BASIC; | 351 credentials.scheme = PasswordForm::SCHEME_BASIC; |
237 credentials.username_value = saved_match()->username_value; | 352 credentials.username_value = saved_match()->username_value; |
238 credentials.password_value = saved_match()->password_value; | 353 credentials.password_value = saved_match()->password_value; |
239 | 354 |
240 // Form with both username_element and password_element. | 355 // Form with both username_element and password_element. |
241 PasswordFormManager manager1(profile(), NULL, credentials, false); | 356 PasswordFormManager manager1(profile(), NULL, credentials, false); |
242 SimulateMatchingPhase(&manager1, false); | 357 SimulateMatchingPhase(&manager1, false); |
(...skipping 12 matching lines...) Expand all Loading... | |
255 SimulateMatchingPhase(&manager3, false); | 370 SimulateMatchingPhase(&manager3, false); |
256 EXPECT_TRUE(manager3.HasValidPasswordForm()); | 371 EXPECT_TRUE(manager3.HasValidPasswordForm()); |
257 | 372 |
258 // Form with neither a password_element nor a username_element. | 373 // Form with neither a password_element nor a username_element. |
259 credentials.username_element.clear(); | 374 credentials.username_element.clear(); |
260 credentials.password_element.clear(); | 375 credentials.password_element.clear(); |
261 PasswordFormManager manager4(profile(), NULL, credentials, false); | 376 PasswordFormManager manager4(profile(), NULL, credentials, false); |
262 SimulateMatchingPhase(&manager4, false); | 377 SimulateMatchingPhase(&manager4, false); |
263 EXPECT_TRUE(manager4.HasValidPasswordForm()); | 378 EXPECT_TRUE(manager4.HasValidPasswordForm()); |
264 } | 379 } |
380 | |
381 TEST_F(PasswordFormManagerTest, TestValidFormsBasicWithoutUsername) { | |
382 // User submits credentials for the observed form. | |
383 PasswordForm credentials = *observed_form2(); | |
384 credentials.scheme = PasswordForm::SCHEME_BASIC; | |
385 credentials.password_value = saved_match2()->password_value; | |
386 | |
387 // Form with a password_element. | |
388 PasswordFormManager manager1(profile2(), NULL, credentials, false); | |
389 SimulateMatchingPhase(&manager1, false); | |
390 EXPECT_TRUE(manager1.HasValidPasswordForm()); | |
391 | |
392 // Form with no a password_element | |
393 credentials.password_element.clear(); | |
394 PasswordFormManager manager2(profile2(), NULL, credentials, false); | |
395 SimulateMatchingPhase(&manager2, false); | |
396 EXPECT_TRUE(manager2.HasValidPasswordForm()); | |
397 } | |
tim (not reviewing)
2012/04/03 16:40:27
What happens if
- a user saves a password for a fo
Yumikiyo Osanai
2012/04/05 00:19:50
Umm... I don't understand what you want to test...
| |
398 | |
OLD | NEW |