Chromium Code Reviews| 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 #import <Foundation/Foundation.h> | |
| 6 | |
| 7 #import "ios/chrome/browser/passwords/js_password_manager.h" | |
| 8 #import "ios/web/public/test/web_js_test.h" | |
| 9 #import "ios/web/public/test/web_test_with_web_state.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 #include "testing/gtest_mac.h" | |
| 12 | |
| 13 // Unit tests for ios/chrome/browser/web/resources/password_controller.js | |
| 14 namespace { | |
| 15 | |
| 16 // Text fixture to test password controller. | |
| 17 class PasswordControllerJsTest | |
| 18 : public web::WebJsTest<web::WebTestWithWebState> { | |
| 19 public: | |
| 20 PasswordControllerJsTest() | |
| 21 : web::WebJsTest<web::WebTestWithWebState>(@[ @"password_controller" ]) {} | |
| 22 }; | |
| 23 | |
| 24 // IDs used in the Username and Password <input> elements. | |
| 25 NSString* const kEmailInputID = @"Email"; | |
| 26 NSString* const kPasswordInputID = @"Passwd"; | |
| 27 | |
| 28 // Returns an autoreleased string of an HTML form that is similar to the | |
| 29 // Google Accounts sign in form. |email| may be nil if the form does not | |
| 30 // need to be pre-filled with the username. Use |isReadOnly| flag to indicate | |
| 31 // if the email field should be read-only. | |
| 32 NSString* GAIASignInForm(NSString* formAction, | |
| 33 NSString* email, | |
| 34 BOOL isReadOnly) { | |
| 35 return [NSString | |
| 36 stringWithFormat: | |
| 37 @"<html><body>" | |
| 38 "<form novalidate method=\"post\" action=\"%@\" " | |
| 39 "id=\"gaia_loginform\">" | |
| 40 " <input name=\"GALX\" type=\"hidden\" value=\"abcdefghij\">" | |
| 41 " <input name=\"service\" type=\"hidden\" value=\"mail\">" | |
| 42 " <input id=\"%@\" name=\"Email\" type=\"email\" value=\"%@\" %@>" | |
| 43 " <input id=\"%@\" name=\"Passwd\" type=\"password\" " | |
| 44 " placeholder=\"Password\">" | |
| 45 "</form></body></html>", | |
| 46 formAction, kEmailInputID, email ? email : @"", | |
| 47 isReadOnly ? @"readonly" : @"", kPasswordInputID]; | |
| 48 } | |
| 49 | |
| 50 // Returns an autoreleased string of JSON for a parsed form. | |
| 51 NSString* GAIASignInFormData(NSString* formAction) { | |
| 52 return [NSString stringWithFormat:@"{" | |
| 53 " \"action\":\"%@\"," | |
| 54 " \"origin\":\"%@\"," | |
| 55 " \"fields\":[" | |
| 56 " {\"name\":\"%@\", \"value\":\"\"}," | |
| 57 " {\"name\":\"%@\",\"value\":\"\"}" | |
| 58 " ]" | |
| 59 "}", | |
| 60 formAction, formAction, kEmailInputID, | |
| 61 kPasswordInputID]; | |
| 62 } | |
| 63 | |
| 64 TEST_F(PasswordControllerJsTest, | |
|
Eugene But (OOO till 7-30)
2016/07/13 22:30:31
If you understand the purpose if these tests, coul
vabr (Chromium)
2016/07/14 07:36:31
Done, this was a good exercise. :)
| |
| 65 FillPasswordFormWithPrefilledUsername_SucceedsWhenUsernameMatches) { | |
| 66 NSString* const formAction = @"https://accounts.google.com/ServiceLoginAuth"; | |
| 67 NSString* const username = @"john.doe@gmail.com"; | |
| 68 NSString* const password = @"super!secret"; | |
| 69 LoadHtmlAndInject(GAIASignInForm(formAction, username, YES)); | |
| 70 EXPECT_NSEQ(@"true", EvaluateJavaScriptWithFormat( | |
| 71 @"__gCrWeb.fillPasswordForm(%@, '%@', '%@', '%@')", | |
| 72 GAIASignInFormData(formAction), username, password, | |
| 73 formAction)); | |
| 74 // Verifies that the sign-in form has been filled with username/password. | |
| 75 EvaluateJavaScriptOnElementsAndCheck(@"document.getElementById('%@').value", | |
| 76 @[ kEmailInputID, kPasswordInputID ], | |
| 77 @[ username, password ]); | |
| 78 } | |
| 79 | |
| 80 TEST_F(PasswordControllerJsTest, | |
| 81 FillPasswordFormWithPrefilledUsername_FailsWhenUsernameMismatched) { | |
| 82 NSString* const formAction = @"https://accounts.google.com/ServiceLoginAuth"; | |
| 83 NSString* const username1 = @"john.doe@gmail.com"; | |
| 84 NSString* const username2 = @"jean.dubois@gmail.com"; | |
| 85 NSString* const password = @"super!secret"; | |
| 86 LoadHtmlAndInject(GAIASignInForm(formAction, username1, YES)); | |
| 87 EXPECT_NSEQ(@"false", EvaluateJavaScriptWithFormat( | |
| 88 @"__gCrWeb.fillPasswordForm(%@, '%@', '%@', '%@')", | |
| 89 GAIASignInFormData(formAction), username2, password, | |
| 90 formAction)); | |
| 91 // Verifies that the sign-in form has not been filled. | |
| 92 EvaluateJavaScriptOnElementsAndCheck(@"document.getElementById('%@').value", | |
| 93 @[ kEmailInputID, kPasswordInputID ], | |
| 94 @[ username1, @"" ]); | |
| 95 } | |
| 96 | |
| 97 TEST_F(PasswordControllerJsTest, | |
| 98 FillPasswordFormWithPrefilledUsername_SucceedsByOverridingUsername) { | |
| 99 NSString* const formAction = @"https://accounts.google.com/ServiceLoginAuth"; | |
| 100 NSString* const username1 = @"john.doe@gmail.com"; | |
| 101 NSString* const username2 = @"jane.doe@gmail.com"; | |
| 102 NSString* const password = @"super!secret"; | |
| 103 LoadHtmlAndInject(GAIASignInForm(formAction, username1, NO)); | |
| 104 EXPECT_NSEQ(@"true", EvaluateJavaScriptWithFormat( | |
| 105 @"__gCrWeb.fillPasswordForm(%@, '%@', '%@', '%@')", | |
| 106 GAIASignInFormData(formAction), username2, password, | |
| 107 formAction)); | |
| 108 // Verifies that the sign-in form has been filled with the new username | |
| 109 // and password. | |
| 110 EvaluateJavaScriptOnElementsAndCheck(@"document.getElementById('%@').value", | |
| 111 @[ kEmailInputID, kPasswordInputID ], | |
| 112 @[ username2, password ]); | |
| 113 } | |
| 114 | |
| 115 TEST_F(PasswordControllerJsTest, | |
| 116 FillPasswordFormWithGeneratedPassword_FailsWhenFormNotFound) { | |
| 117 LoadHtmlAndInject(@"<html>" | |
| 118 " <body>" | |
| 119 " <form name=\"foo\">" | |
| 120 " <input type=\"password\" name=\"ps\">" | |
| 121 " </form>" | |
| 122 " </body" | |
| 123 "</html>"); | |
| 124 NSString* const formName = @"bar"; | |
| 125 NSString* const password = @"abc"; | |
| 126 EXPECT_NSEQ(@"false", | |
| 127 EvaluateJavaScriptWithFormat( | |
| 128 @"__gCrWeb.fillPasswordFormWithGeneratedPassword('%@', '%@')", | |
| 129 formName, password)); | |
| 130 } | |
| 131 | |
| 132 TEST_F(PasswordControllerJsTest, | |
| 133 FillPasswordFormWithGeneratedPassword_FailsWhenNoFieldsFilled) { | |
| 134 LoadHtmlAndInject(@"<html>" | |
| 135 " <body>" | |
| 136 " <form name=\"foo\">" | |
| 137 " <input type=\"text\" name=\"user\">" | |
| 138 " <input type=\"submit\" name=\"go\">" | |
| 139 " </form>" | |
| 140 " </body" | |
| 141 "</html>"); | |
| 142 NSString* const formName = @"foo"; | |
| 143 NSString* const password = @"abc"; | |
| 144 EXPECT_NSEQ(@"false", | |
| 145 EvaluateJavaScriptWithFormat( | |
| 146 @"__gCrWeb.fillPasswordFormWithGeneratedPassword('%@', '%@')", | |
| 147 formName, password)); | |
| 148 } | |
| 149 | |
| 150 TEST_F(PasswordControllerJsTest, | |
| 151 FillPasswordFormWithGeneratedPassword_SucceedsWhenFieldsFilled) { | |
| 152 LoadHtmlAndInject(@"<html>" | |
| 153 " <body>" | |
| 154 " <form name=\"foo\">" | |
| 155 " <input type=\"text\" id=\"user\" name=\"user\">" | |
| 156 " <input type=\"password\" id=\"ps1\" name=\"ps1\">" | |
| 157 " <input type=\"password\" id=\"ps2\" name=\"ps2\">" | |
| 158 " <input type=\"submit\" name=\"go\">" | |
| 159 " </form>" | |
| 160 " </body" | |
| 161 "</html>"); | |
| 162 NSString* const formName = @"foo"; | |
| 163 NSString* const password = @"abc"; | |
| 164 EXPECT_NSEQ(@"true", | |
| 165 EvaluateJavaScriptWithFormat( | |
| 166 @"__gCrWeb.fillPasswordFormWithGeneratedPassword('%@', '%@')", | |
| 167 formName, password)); | |
| 168 EXPECT_NSEQ(@"true", | |
| 169 EvaluateJavaScriptWithFormat( | |
| 170 @"document.getElementById('ps1').value == '%@'", password)); | |
| 171 EXPECT_NSEQ(@"true", | |
| 172 EvaluateJavaScriptWithFormat( | |
| 173 @"document.getElementById('ps2').value == '%@'", password)); | |
| 174 EXPECT_NSEQ(@"false", | |
| 175 EvaluateJavaScriptWithFormat( | |
| 176 @"document.getElementById('user').value == '%@'", password)); | |
| 177 } | |
| 178 | |
| 179 TEST_F(PasswordControllerJsTest, | |
| 180 FindAndPreparePasswordFormsSingleFrameSingleForm) { | |
| 181 LoadHtmlAndInject( | |
| 182 @"<html><body>" | |
| 183 "<form action='/generic_submit' method='post' name='login_form'>" | |
| 184 " Name: <input type='text' name='name'>" | |
| 185 " Password: <input type='password' name='password'>" | |
| 186 " <input type='submit' value='Submit'>" | |
| 187 "</form>" | |
| 188 "</body></html>"); | |
| 189 | |
| 190 const std::string base_url = BaseUrl(); | |
| 191 NSString* result = [NSString | |
| 192 stringWithFormat: | |
| 193 @"[{\"action\":\"/generic_submit\"," | |
| 194 "\"method\":\"post\"," | |
| 195 "\"name\":\"login_form\"," | |
| 196 "\"origin\":\"%s\"," | |
| 197 "\"fields\":[{\"element\":\"name\",\"type\":\"text\"}," | |
| 198 "{\"element\":\"password\",\"type\":\"password\"}," | |
| 199 "{\"element\":\"\",\"type\":\"submit\"}]," | |
| 200 "\"usernameElement\":\"name\"," | |
| 201 "\"usernameValue\":\"\"," | |
| 202 "\"passwords\":[{\"element\":\"password\",\"value\":\"\"}]}]", | |
| 203 base_url.c_str()]; | |
| 204 EXPECT_NSEQ(result, | |
| 205 EvaluateJavaScriptWithFormat(@"__gCrWeb.findPasswordForms()")); | |
| 206 }; | |
| 207 | |
| 208 TEST_F(PasswordControllerJsTest, | |
| 209 FindAndPreparePasswordFormsSingleFrameMultipleForms) { | |
| 210 LoadHtmlAndInject( | |
| 211 @"<html><body>" | |
| 212 "<form action='/generic_submit' method='post' id='login_form1'>" | |
| 213 " Name: <input type='text' name='name'>" | |
| 214 " Password: <input type='password' name='password'>" | |
| 215 " <input type='submit' value='Submit'>" | |
| 216 "</form>" | |
| 217 "<form action='/generic_s2' method='post' name='login_form2'>" | |
| 218 " Name: <input type='text' name='name2'>" | |
| 219 " Password: <input type='password' name='password2'>" | |
| 220 " <input type='submit' value='Submit'>" | |
| 221 "</form>" | |
| 222 "</body></html>"); | |
| 223 | |
| 224 const std::string base_url = BaseUrl(); | |
| 225 NSString* result = [NSString | |
| 226 stringWithFormat: | |
| 227 @"[{\"action\":\"/generic_submit\"," | |
| 228 "\"method\":\"post\"," | |
| 229 "\"name\":\"login_form1\"," | |
| 230 "\"origin\":\"%s\"," | |
| 231 "\"fields\":[{\"element\":\"name\",\"type\":\"text\"}," | |
| 232 "{\"element\":\"password\",\"type\":\"password\"}," | |
| 233 "{\"element\":\"\",\"type\":\"submit\"}]," | |
| 234 "\"usernameElement\":\"name\"," | |
| 235 "\"usernameValue\":\"\"," | |
| 236 "\"passwords\":[{\"element\":\"password\",\"value\":\"\"}]}," | |
| 237 "{\"action\":\"/generic_s2\"," | |
| 238 "\"method\":\"post\"," | |
| 239 "\"name\":\"login_form2\"," | |
| 240 "\"origin\":\"%s\"," | |
| 241 "\"fields\":[{\"element\":\"name2\",\"type\":\"text\"}," | |
| 242 "{\"element\":\"password2\",\"type\":\"password\"}," | |
| 243 "{\"element\":\"\",\"type\":\"submit\"}]," | |
| 244 "\"usernameElement\":\"name2\"," | |
| 245 "\"usernameValue\":\"\"," | |
| 246 "\"passwords\":[{\"element\":\"password2\",\"value\":\"\"}]}]", | |
| 247 base_url.c_str(), base_url.c_str()]; | |
| 248 EXPECT_NSEQ(result, | |
| 249 EvaluateJavaScriptWithFormat(@"__gCrWeb.findPasswordForms()")); | |
| 250 }; | |
| 251 | |
| 252 TEST_F(PasswordControllerJsTest, GetPasswordFormData) { | |
| 253 LoadHtmlAndInject( | |
| 254 @"<html><body>" | |
| 255 "<form name='np' id='np1' action='/generic_submit' method='post'>" | |
| 256 " Name: <input type='text' name='name'>" | |
| 257 " Password: <input type='password' name='password'>" | |
| 258 " <input type='submit' value='Submit'>" | |
| 259 "</form>" | |
| 260 "</body></html>"); | |
| 261 | |
| 262 const std::string base_url = BaseUrl(); | |
| 263 NSString* parameter = @"window.document.getElementsByTagName('form')[0]"; | |
| 264 NSString* result = [NSString | |
| 265 stringWithFormat: | |
| 266 @"{\"action\":\"/generic_submit\"," | |
| 267 "\"method\":\"post\"," | |
| 268 "\"name\":\"np\"," | |
| 269 "\"origin\":\"%s\"," | |
| 270 "\"fields\":[{\"element\":\"name\",\"type\":\"text\"}," | |
| 271 "{\"element\":\"password\",\"type\":\"password\"}," | |
| 272 "{\"element\":\"\",\"type\":\"submit\"}]," | |
| 273 "\"usernameElement\":\"name\"," | |
| 274 "\"usernameValue\":\"\"," | |
| 275 "\"passwords\":[{\"element\":\"password\",\"value\":\"\"}]}", | |
| 276 base_url.c_str()]; | |
| 277 EXPECT_NSEQ( | |
| 278 result, | |
| 279 EvaluateJavaScriptWithFormat( | |
| 280 @"__gCrWeb.stringify(__gCrWeb.getPasswordFormData(%@))", parameter)); | |
| 281 }; | |
| 282 | |
| 283 } // namespace | |
| OLD | NEW |